Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1 | //===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===// |
| 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 provides Objective-C code generation targetting the Apple runtime. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CGObjCRuntime.h" |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 15 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 16 | #include "CGRecordLayout.h" |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 17 | #include "CodeGenModule.h" |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 18 | #include "CodeGenFunction.h" |
John McCall | ed1ae86 | 2011-01-28 11:13:47 +0000 | [diff] [blame] | 19 | #include "CGCleanup.h" |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 21 | #include "clang/AST/Decl.h" |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 23 | #include "clang/AST/RecordLayout.h" |
Chris Lattner | f0b64d7 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 24 | #include "clang/AST/StmtObjC.h" |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 25 | #include "clang/Basic/LangOptions.h" |
Chandler Carruth | 8509824 | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/CodeGenOptions.h" |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 27 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 28 | #include "llvm/InlineAsm.h" |
| 29 | #include "llvm/IntrinsicInst.h" |
Owen Anderson | ae86c19 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 30 | #include "llvm/LLVMContext.h" |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 31 | #include "llvm/Module.h" |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/DenseSet.h" |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SetVector.h" |
| 34 | #include "llvm/ADT/SmallString.h" |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/SmallPtrSet.h" |
John McCall | 5c08ab9 | 2010-07-13 22:12:14 +0000 | [diff] [blame] | 36 | #include "llvm/Support/CallSite.h" |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 37 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetData.h" |
Torok Edwin | db71492 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 39 | #include <cstdio> |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 40 | |
| 41 | using namespace clang; |
Daniel Dunbar | 41cf9de | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 42 | using namespace CodeGen; |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 43 | |
Daniel Dunbar | 9fd114d | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 44 | // Common CGObjCRuntime functions, these don't belong here, but they |
| 45 | // don't belong in CGObjCRuntime either so we will live with it for |
| 46 | // now. |
| 47 | |
Daniel Dunbar | 0cec95f | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 48 | static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM, |
| 49 | const ObjCInterfaceDecl *OID, |
Daniel Dunbar | 96120237 | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 50 | const ObjCImplementationDecl *ID, |
Daniel Dunbar | 0cec95f | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 51 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 8c7f981 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 52 | const ObjCInterfaceDecl *Container = Ivar->getContainingInterface(); |
Daniel Dunbar | 80b4eef | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 53 | |
Daniel Dunbar | 7e5aba4 | 2010-04-02 22:29:40 +0000 | [diff] [blame] | 54 | // FIXME: We should eliminate the need to have ObjCImplementationDecl passed |
| 55 | // in here; it should never be necessary because that should be the lexical |
| 56 | // decl context for the ivar. |
Daniel Dunbar | 031d4d4 | 2010-04-02 15:43:29 +0000 | [diff] [blame] | 57 | |
Daniel Dunbar | 80b4eef | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 58 | // If we know have an implementation (and the ivar is in it) then |
| 59 | // look up in the implementation layout. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 60 | const ASTRecordLayout *RL; |
Daniel Dunbar | 80b4eef | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 61 | if (ID && ID->getClassInterface() == Container) |
| 62 | RL = &CGM.getContext().getASTObjCImplementationLayout(ID); |
| 63 | else |
| 64 | RL = &CGM.getContext().getASTObjCInterfaceLayout(Container); |
Daniel Dunbar | 8c7f981 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 65 | |
| 66 | // Compute field index. |
| 67 | // |
| 68 | // FIXME: The index here is closely tied to how ASTContext::getObjCLayout is |
| 69 | // implemented. This should be fixed to get the information from the layout |
| 70 | // directly. |
| 71 | unsigned Index = 0; |
| 72 | llvm::SmallVector<ObjCIvarDecl*, 16> Ivars; |
| 73 | CGM.getContext().ShallowCollectObjCIvars(Container, Ivars); |
| 74 | for (unsigned k = 0, e = Ivars.size(); k != e; ++k) { |
| 75 | if (Ivar == Ivars[k]) |
| 76 | break; |
| 77 | ++Index; |
| 78 | } |
| 79 | assert(Index != Ivars.size() && "Ivar is not inside container!"); |
David Chisnall | e8431a7 | 2010-11-03 16:12:44 +0000 | [diff] [blame] | 80 | assert(Index < RL->getFieldCount() && "Ivar is not inside record layout!"); |
Daniel Dunbar | 8c7f981 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 81 | |
Daniel Dunbar | 80b4eef | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 82 | return RL->getFieldOffset(Index); |
Daniel Dunbar | 0cec95f | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM, |
| 86 | const ObjCInterfaceDecl *OID, |
| 87 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 96120237 | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 88 | return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8; |
| 89 | } |
| 90 | |
| 91 | uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM, |
| 92 | const ObjCImplementationDecl *OID, |
| 93 | const ObjCIvarDecl *Ivar) { |
| 94 | return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8; |
Daniel Dunbar | 9fd114d | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF, |
| 98 | const ObjCInterfaceDecl *OID, |
| 99 | llvm::Value *BaseValue, |
| 100 | const ObjCIvarDecl *Ivar, |
| 101 | unsigned CVRQualifiers, |
| 102 | llvm::Value *Offset) { |
Daniel Dunbar | 0cec95f | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 103 | // Compute (type*) ( (char *) BaseValue + Offset) |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 104 | const llvm::Type *I8Ptr = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
Daniel Dunbar | 0cec95f | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 105 | QualType IvarTy = Ivar->getType(); |
| 106 | const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy); |
Daniel Dunbar | 9fd114d | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 107 | llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr); |
Daniel Dunbar | 9fd114d | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 108 | V = CGF.Builder.CreateGEP(V, Offset, "add.ptr"); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 109 | V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 110 | |
Daniel Dunbar | f166a52 | 2010-08-21 03:44:13 +0000 | [diff] [blame] | 111 | if (!Ivar->isBitField()) { |
| 112 | LValue LV = CGF.MakeAddrLValue(V, IvarTy); |
| 113 | LV.getQuals().addCVRQualifiers(CVRQualifiers); |
| 114 | return LV; |
| 115 | } |
Daniel Dunbar | 96120237 | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 116 | |
Daniel Dunbar | a70fab8 | 2010-09-02 23:53:31 +0000 | [diff] [blame] | 117 | // We need to compute an access strategy for this bit-field. We are given the |
| 118 | // offset to the first byte in the bit-field, the sub-byte offset is taken |
| 119 | // from the original layout. We reuse the normal bit-field access strategy by |
| 120 | // treating this as an access to a struct where the bit-field is in byte 0, |
| 121 | // and adjust the containing type size as appropriate. |
| 122 | // |
| 123 | // FIXME: Note that currently we make a very conservative estimate of the |
| 124 | // alignment of the bit-field, because (a) it is not clear what guarantees the |
| 125 | // runtime makes us, and (b) we don't have a way to specify that the struct is |
| 126 | // at an alignment plus offset. |
| 127 | // |
| 128 | // Note, there is a subtle invariant here: we can only call this routine on |
| 129 | // non-synthesized ivars but we may be called for synthesized ivars. However, |
| 130 | // a synthesized ivar can never be a bit-field, so this is safe. |
| 131 | const ASTRecordLayout &RL = |
| 132 | CGF.CGM.getContext().getASTObjCInterfaceLayout(OID); |
| 133 | uint64_t TypeSizeInBits = RL.getSize(); |
| 134 | uint64_t FieldBitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar); |
| 135 | uint64_t BitOffset = FieldBitOffset % 8; |
| 136 | uint64_t ContainingTypeAlign = 8; |
| 137 | uint64_t ContainingTypeSize = TypeSizeInBits - (FieldBitOffset - BitOffset); |
Daniel Dunbar | b76c4cd | 2010-04-05 16:20:33 +0000 | [diff] [blame] | 138 | uint64_t BitFieldSize = |
| 139 | Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue(); |
Daniel Dunbar | 9fd114d | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 140 | |
Daniel Dunbar | dc406b8 | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 141 | // Allocate a new CGBitFieldInfo object to describe this access. |
| 142 | // |
| 143 | // FIXME: This is incredibly wasteful, these should be uniqued or part of some |
| 144 | // layout object. However, this is blocked on other cleanups to the |
| 145 | // Objective-C code, so for now we just live with allocating a bunch of these |
| 146 | // objects. |
Daniel Dunbar | a70fab8 | 2010-09-02 23:53:31 +0000 | [diff] [blame] | 147 | CGBitFieldInfo *Info = new (CGF.CGM.getContext()) CGBitFieldInfo( |
| 148 | CGBitFieldInfo::MakeInfo(CGF.CGM.getTypes(), Ivar, BitOffset, BitFieldSize, |
| 149 | ContainingTypeSize, ContainingTypeAlign)); |
Daniel Dunbar | dc406b8 | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 150 | |
Daniel Dunbar | 26d2c39 | 2010-08-21 04:05:54 +0000 | [diff] [blame] | 151 | return LValue::MakeBitfield(V, *Info, |
| 152 | IvarTy.getCVRQualifiers() | CVRQualifiers); |
Daniel Dunbar | 9fd114d | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /// |
| 156 | |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 157 | namespace { |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 158 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 159 | typedef std::vector<llvm::Constant*> ConstantVector; |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 160 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 161 | // FIXME: We should find a nicer way to make the labels for metadata, string |
| 162 | // concatenation is lame. |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 163 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 164 | class ObjCCommonTypesHelper { |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 165 | protected: |
| 166 | llvm::LLVMContext &VMContext; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 167 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 168 | private: |
| 169 | llvm::Constant *getMessageSendFn() const { |
| 170 | // id objc_msgSend (id, SEL, ...) |
| 171 | std::vector<const llvm::Type*> Params; |
| 172 | Params.push_back(ObjectPtrTy); |
| 173 | Params.push_back(SelectorPtrTy); |
| 174 | return |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 175 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 176 | Params, true), |
| 177 | "objc_msgSend"); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 178 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 179 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 180 | llvm::Constant *getMessageSendStretFn() const { |
| 181 | // id objc_msgSend_stret (id, SEL, ...) |
| 182 | std::vector<const llvm::Type*> Params; |
| 183 | Params.push_back(ObjectPtrTy); |
| 184 | Params.push_back(SelectorPtrTy); |
| 185 | return |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 186 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 187 | Params, true), |
| 188 | "objc_msgSend_stret"); |
| 189 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 190 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 191 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 192 | llvm::Constant *getMessageSendFpretFn() const { |
| 193 | // FIXME: This should be long double on x86_64? |
| 194 | // [double | long double] objc_msgSend_fpret(id self, SEL op, ...) |
| 195 | std::vector<const llvm::Type*> Params; |
| 196 | Params.push_back(ObjectPtrTy); |
| 197 | Params.push_back(SelectorPtrTy); |
| 198 | return |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 199 | CGM.CreateRuntimeFunction(llvm::FunctionType::get( |
| 200 | llvm::Type::getDoubleTy(VMContext), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 201 | Params, |
| 202 | true), |
| 203 | "objc_msgSend_fpret"); |
| 204 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 205 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 206 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 207 | llvm::Constant *getMessageSendSuperFn() const { |
| 208 | // id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
| 209 | const char *SuperName = "objc_msgSendSuper"; |
| 210 | std::vector<const llvm::Type*> Params; |
| 211 | Params.push_back(SuperPtrTy); |
| 212 | Params.push_back(SelectorPtrTy); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 213 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 214 | Params, true), |
| 215 | SuperName); |
| 216 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 217 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 218 | llvm::Constant *getMessageSendSuperFn2() const { |
| 219 | // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...) |
| 220 | const char *SuperName = "objc_msgSendSuper2"; |
| 221 | std::vector<const llvm::Type*> Params; |
| 222 | Params.push_back(SuperPtrTy); |
| 223 | Params.push_back(SelectorPtrTy); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 224 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 225 | Params, true), |
| 226 | SuperName); |
| 227 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 228 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 229 | llvm::Constant *getMessageSendSuperStretFn() const { |
| 230 | // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super, |
| 231 | // SEL op, ...) |
| 232 | std::vector<const llvm::Type*> Params; |
| 233 | Params.push_back(Int8PtrTy); |
| 234 | Params.push_back(SuperPtrTy); |
| 235 | Params.push_back(SelectorPtrTy); |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 236 | return CGM.CreateRuntimeFunction( |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 237 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 238 | Params, true), |
| 239 | "objc_msgSendSuper_stret"); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 240 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 241 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 242 | llvm::Constant *getMessageSendSuperStretFn2() const { |
| 243 | // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super, |
| 244 | // SEL op, ...) |
| 245 | std::vector<const llvm::Type*> Params; |
| 246 | Params.push_back(Int8PtrTy); |
| 247 | Params.push_back(SuperPtrTy); |
| 248 | Params.push_back(SelectorPtrTy); |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 249 | return CGM.CreateRuntimeFunction( |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 250 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 251 | Params, true), |
| 252 | "objc_msgSendSuper2_stret"); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 253 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 254 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 255 | llvm::Constant *getMessageSendSuperFpretFn() const { |
| 256 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 257 | return getMessageSendSuperFn(); |
| 258 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 259 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 260 | llvm::Constant *getMessageSendSuperFpretFn2() const { |
| 261 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 262 | return getMessageSendSuperFn2(); |
| 263 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 264 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 265 | protected: |
| 266 | CodeGen::CodeGenModule &CGM; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 267 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 268 | public: |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 269 | const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 270 | const llvm::Type *Int8PtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 271 | |
Daniel Dunbar | 5d71559 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 272 | /// ObjectPtrTy - LLVM type for object handles (typeof(id)) |
| 273 | const llvm::Type *ObjectPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 274 | |
Fariborz Jahanian | 406b117 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 275 | /// PtrObjectPtrTy - LLVM type for id * |
| 276 | const llvm::Type *PtrObjectPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 277 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 278 | /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL)) |
Daniel Dunbar | 5d71559 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 279 | const llvm::Type *SelectorPtrTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 280 | /// ProtocolPtrTy - LLVM type for external protocol handles |
| 281 | /// (typeof(Protocol)) |
| 282 | const llvm::Type *ExternalProtocolPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 283 | |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 284 | // SuperCTy - clang type for struct objc_super. |
| 285 | QualType SuperCTy; |
| 286 | // SuperPtrCTy - clang type for struct objc_super *. |
| 287 | QualType SuperPtrCTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 288 | |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 289 | /// SuperTy - LLVM type for struct objc_super. |
| 290 | const llvm::StructType *SuperTy; |
Daniel Dunbar | 97ff50d | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 291 | /// SuperPtrTy - LLVM type for struct objc_super *. |
| 292 | const llvm::Type *SuperPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 293 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 294 | /// PropertyTy - LLVM type for struct objc_property (struct _prop_t |
| 295 | /// in GCC parlance). |
| 296 | const llvm::StructType *PropertyTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 297 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 298 | /// PropertyListTy - LLVM type for struct objc_property_list |
| 299 | /// (_prop_list_t in GCC parlance). |
| 300 | const llvm::StructType *PropertyListTy; |
| 301 | /// PropertyListPtrTy - LLVM type for struct objc_property_list*. |
| 302 | const llvm::Type *PropertyListPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 303 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 304 | // MethodTy - LLVM type for struct objc_method. |
| 305 | const llvm::StructType *MethodTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 306 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 307 | /// CacheTy - LLVM type for struct objc_cache. |
| 308 | const llvm::Type *CacheTy; |
| 309 | /// CachePtrTy - LLVM type for struct objc_cache *. |
| 310 | const llvm::Type *CachePtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 311 | |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 312 | llvm::Constant *getGetPropertyFn() { |
| 313 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 314 | ASTContext &Ctx = CGM.getContext(); |
| 315 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 316 | llvm::SmallVector<CanQualType,4> Params; |
| 317 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 318 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 319 | Params.push_back(IdType); |
| 320 | Params.push_back(SelType); |
| 321 | Params.push_back(Ctx.LongTy); |
| 322 | Params.push_back(Ctx.BoolTy); |
| 323 | const llvm::FunctionType *FTy = |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 324 | Types.GetFunctionType(Types.getFunctionInfo(IdType, Params, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 325 | FunctionType::ExtInfo()), |
| 326 | false); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 327 | return CGM.CreateRuntimeFunction(FTy, "objc_getProperty"); |
| 328 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 329 | |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 330 | llvm::Constant *getSetPropertyFn() { |
| 331 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 332 | ASTContext &Ctx = CGM.getContext(); |
| 333 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 334 | llvm::SmallVector<CanQualType,6> Params; |
| 335 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 336 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 337 | Params.push_back(IdType); |
| 338 | Params.push_back(SelType); |
| 339 | Params.push_back(Ctx.LongTy); |
| 340 | Params.push_back(IdType); |
| 341 | Params.push_back(Ctx.BoolTy); |
| 342 | Params.push_back(Ctx.BoolTy); |
| 343 | const llvm::FunctionType *FTy = |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 344 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 345 | FunctionType::ExtInfo()), |
| 346 | false); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 347 | return CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); |
| 348 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 349 | |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 350 | |
| 351 | llvm::Constant *getCopyStructFn() { |
| 352 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 353 | ASTContext &Ctx = CGM.getContext(); |
| 354 | // void objc_copyStruct (void *, const void *, size_t, bool, bool) |
| 355 | llvm::SmallVector<CanQualType,5> Params; |
| 356 | Params.push_back(Ctx.VoidPtrTy); |
| 357 | Params.push_back(Ctx.VoidPtrTy); |
| 358 | Params.push_back(Ctx.LongTy); |
| 359 | Params.push_back(Ctx.BoolTy); |
| 360 | Params.push_back(Ctx.BoolTy); |
| 361 | const llvm::FunctionType *FTy = |
| 362 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params, |
| 363 | FunctionType::ExtInfo()), |
| 364 | false); |
| 365 | return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct"); |
| 366 | } |
| 367 | |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 368 | llvm::Constant *getEnumerationMutationFn() { |
Daniel Dunbar | 9d82da4 | 2009-07-11 20:32:50 +0000 | [diff] [blame] | 369 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 370 | ASTContext &Ctx = CGM.getContext(); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 371 | // void objc_enumerationMutation (id) |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 372 | llvm::SmallVector<CanQualType,1> Params; |
| 373 | Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType())); |
Daniel Dunbar | 9d82da4 | 2009-07-11 20:32:50 +0000 | [diff] [blame] | 374 | const llvm::FunctionType *FTy = |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 375 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 376 | FunctionType::ExtInfo()), |
| 377 | false); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 378 | return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation"); |
| 379 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 380 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 381 | /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 382 | llvm::Constant *getGcReadWeakFn() { |
| 383 | // id objc_read_weak (id *) |
| 384 | std::vector<const llvm::Type*> Args; |
| 385 | Args.push_back(ObjectPtrTy->getPointerTo()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 386 | llvm::FunctionType *FTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 387 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 388 | return CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 391 | /// GcAssignWeakFn -- LLVM objc_assign_weak function. |
Chris Lattner | 6fdd57c | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 392 | llvm::Constant *getGcAssignWeakFn() { |
| 393 | // id objc_assign_weak (id, id *) |
| 394 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 395 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 396 | llvm::FunctionType *FTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 397 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
Chris Lattner | 6fdd57c | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 398 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 399 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 400 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 401 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 402 | llvm::Constant *getGcAssignGlobalFn() { |
| 403 | // id objc_assign_global(id, id *) |
| 404 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 405 | Args.push_back(ObjectPtrTy->getPointerTo()); |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 406 | llvm::FunctionType *FTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 407 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 408 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 409 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 410 | |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 411 | /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function. |
| 412 | llvm::Constant *getGcAssignThreadLocalFn() { |
| 413 | // id objc_assign_threadlocal(id src, id * dest) |
| 414 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 415 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 416 | llvm::FunctionType *FTy = |
| 417 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 418 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal"); |
| 419 | } |
| 420 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 421 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 422 | llvm::Constant *getGcAssignIvarFn() { |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 423 | // id objc_assign_ivar(id, id *, ptrdiff_t) |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 424 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 425 | Args.push_back(ObjectPtrTy->getPointerTo()); |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 426 | Args.push_back(LongTy); |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 427 | llvm::FunctionType *FTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 428 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 429 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 430 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 431 | |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 432 | /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function. |
| 433 | llvm::Constant *GcMemmoveCollectableFn() { |
| 434 | // void *objc_memmove_collectable(void *dst, const void *src, size_t size) |
| 435 | std::vector<const llvm::Type*> Args(1, Int8PtrTy); |
| 436 | Args.push_back(Int8PtrTy); |
| 437 | Args.push_back(LongTy); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 438 | llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 439 | return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable"); |
| 440 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 441 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 442 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 443 | llvm::Constant *getGcAssignStrongCastFn() { |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 444 | // id objc_assign_strongCast(id, id *) |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 445 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 446 | Args.push_back(ObjectPtrTy->getPointerTo()); |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 447 | llvm::FunctionType *FTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 448 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 449 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); |
| 450 | } |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 451 | |
| 452 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 453 | llvm::Constant *getExceptionThrowFn() { |
| 454 | // void objc_exception_throw(id) |
| 455 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 456 | llvm::FunctionType *FTy = |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 457 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 458 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
| 459 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 460 | |
Fariborz Jahanian | 3336de1 | 2010-05-28 17:34:43 +0000 | [diff] [blame] | 461 | /// ExceptionRethrowFn - LLVM objc_exception_rethrow function. |
| 462 | llvm::Constant *getExceptionRethrowFn() { |
| 463 | // void objc_exception_rethrow(void) |
| 464 | std::vector<const llvm::Type*> Args; |
| 465 | llvm::FunctionType *FTy = |
John McCall | 17afe45 | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 466 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false); |
Fariborz Jahanian | 3336de1 | 2010-05-28 17:34:43 +0000 | [diff] [blame] | 467 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow"); |
| 468 | } |
| 469 | |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 470 | /// SyncEnterFn - LLVM object_sync_enter function. |
Chris Lattner | dcceee7 | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 471 | llvm::Constant *getSyncEnterFn() { |
| 472 | // void objc_sync_enter (id) |
| 473 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 474 | llvm::FunctionType *FTy = |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 475 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false); |
Chris Lattner | dcceee7 | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 476 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 477 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 478 | |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 479 | /// SyncExitFn - LLVM object_sync_exit function. |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 480 | llvm::Constant *getSyncExitFn() { |
| 481 | // void objc_sync_exit (id) |
| 482 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 483 | llvm::FunctionType *FTy = |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 484 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 485 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
| 486 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 487 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 488 | llvm::Constant *getSendFn(bool IsSuper) const { |
| 489 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFn(); |
| 490 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 491 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 492 | llvm::Constant *getSendFn2(bool IsSuper) const { |
| 493 | return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn(); |
| 494 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 495 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 496 | llvm::Constant *getSendStretFn(bool IsSuper) const { |
| 497 | return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn(); |
| 498 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 499 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 500 | llvm::Constant *getSendStretFn2(bool IsSuper) const { |
| 501 | return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn(); |
| 502 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 503 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 504 | llvm::Constant *getSendFpretFn(bool IsSuper) const { |
| 505 | return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn(); |
| 506 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 507 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 508 | llvm::Constant *getSendFpretFn2(bool IsSuper) const { |
| 509 | return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn(); |
| 510 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 511 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 512 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
| 513 | ~ObjCCommonTypesHelper(){} |
| 514 | }; |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 515 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 516 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 517 | /// construction of varies types used during ObjC generation. |
| 518 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 519 | public: |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 520 | /// SymtabTy - LLVM type for struct objc_symtab. |
| 521 | const llvm::StructType *SymtabTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 522 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
| 523 | const llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 524 | /// ModuleTy - LLVM type for struct objc_module. |
| 525 | const llvm::StructType *ModuleTy; |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 526 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 527 | /// ProtocolTy - LLVM type for struct objc_protocol. |
| 528 | const llvm::StructType *ProtocolTy; |
| 529 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
| 530 | const llvm::Type *ProtocolPtrTy; |
| 531 | /// ProtocolExtensionTy - LLVM type for struct |
| 532 | /// objc_protocol_extension. |
| 533 | const llvm::StructType *ProtocolExtensionTy; |
| 534 | /// ProtocolExtensionTy - LLVM type for struct |
| 535 | /// objc_protocol_extension *. |
| 536 | const llvm::Type *ProtocolExtensionPtrTy; |
| 537 | /// MethodDescriptionTy - LLVM type for struct |
| 538 | /// objc_method_description. |
| 539 | const llvm::StructType *MethodDescriptionTy; |
| 540 | /// MethodDescriptionListTy - LLVM type for struct |
| 541 | /// objc_method_description_list. |
| 542 | const llvm::StructType *MethodDescriptionListTy; |
| 543 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 544 | /// objc_method_description_list *. |
| 545 | const llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 546 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
| 547 | const llvm::Type *ProtocolListTy; |
| 548 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
| 549 | const llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 550 | /// CategoryTy - LLVM type for struct objc_category. |
| 551 | const llvm::StructType *CategoryTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 552 | /// ClassTy - LLVM type for struct objc_class. |
| 553 | const llvm::StructType *ClassTy; |
| 554 | /// ClassPtrTy - LLVM type for struct objc_class *. |
| 555 | const llvm::Type *ClassPtrTy; |
| 556 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
| 557 | const llvm::StructType *ClassExtensionTy; |
| 558 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
| 559 | const llvm::Type *ClassExtensionPtrTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 560 | // IvarTy - LLVM type for struct objc_ivar. |
| 561 | const llvm::StructType *IvarTy; |
| 562 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
| 563 | const llvm::Type *IvarListTy; |
| 564 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
| 565 | const llvm::Type *IvarListPtrTy; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 566 | /// MethodListTy - LLVM type for struct objc_method_list. |
| 567 | const llvm::Type *MethodListTy; |
| 568 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
| 569 | const llvm::Type *MethodListPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 570 | |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 571 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
| 572 | const llvm::Type *ExceptionDataTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 573 | |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 574 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 575 | llvm::Constant *getExceptionTryEnterFn() { |
| 576 | std::vector<const llvm::Type*> Params; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 577 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 578 | return CGM.CreateRuntimeFunction( |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 579 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 580 | Params, false), |
| 581 | "objc_exception_try_enter"); |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 582 | } |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 583 | |
| 584 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 585 | llvm::Constant *getExceptionTryExitFn() { |
| 586 | std::vector<const llvm::Type*> Params; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 587 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 588 | return CGM.CreateRuntimeFunction( |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 589 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 590 | Params, false), |
| 591 | "objc_exception_try_exit"); |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 592 | } |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 593 | |
| 594 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 595 | llvm::Constant *getExceptionExtractFn() { |
| 596 | std::vector<const llvm::Type*> Params; |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 597 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 598 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 599 | Params, false), |
| 600 | "objc_exception_extract"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 601 | |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 602 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 603 | |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 604 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 605 | llvm::Constant *getExceptionMatchFn() { |
| 606 | std::vector<const llvm::Type*> Params; |
| 607 | Params.push_back(ClassPtrTy); |
| 608 | Params.push_back(ObjectPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 609 | return CGM.CreateRuntimeFunction( |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 610 | llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 611 | Params, false), |
| 612 | "objc_exception_match"); |
| 613 | |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 614 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 615 | |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 616 | /// SetJmpFn - LLVM _setjmp function. |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 617 | llvm::Constant *getSetJmpFn() { |
| 618 | std::vector<const llvm::Type*> Params; |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 619 | Params.push_back(llvm::Type::getInt32PtrTy(VMContext)); |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 620 | return |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 621 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext), |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 622 | Params, false), |
| 623 | "_setjmp"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 624 | |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 625 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 626 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 627 | public: |
| 628 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 629 | ~ObjCTypesHelper() {} |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 630 | }; |
| 631 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 632 | /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 633 | /// modern abi |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 634 | class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 635 | public: |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 636 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 637 | // MethodListnfABITy - LLVM for struct _method_list_t |
| 638 | const llvm::StructType *MethodListnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 639 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 640 | // MethodListnfABIPtrTy - LLVM for struct _method_list_t* |
| 641 | const llvm::Type *MethodListnfABIPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 642 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 643 | // ProtocolnfABITy = LLVM for struct _protocol_t |
| 644 | const llvm::StructType *ProtocolnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 645 | |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 646 | // ProtocolnfABIPtrTy = LLVM for struct _protocol_t* |
| 647 | const llvm::Type *ProtocolnfABIPtrTy; |
| 648 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 649 | // ProtocolListnfABITy - LLVM for struct _objc_protocol_list |
| 650 | const llvm::StructType *ProtocolListnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 651 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 652 | // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list* |
| 653 | const llvm::Type *ProtocolListnfABIPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 654 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 655 | // ClassnfABITy - LLVM for struct _class_t |
| 656 | const llvm::StructType *ClassnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 657 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 658 | // ClassnfABIPtrTy - LLVM for struct _class_t* |
| 659 | const llvm::Type *ClassnfABIPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 660 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 661 | // IvarnfABITy - LLVM for struct _ivar_t |
| 662 | const llvm::StructType *IvarnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 663 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 664 | // IvarListnfABITy - LLVM for struct _ivar_list_t |
| 665 | const llvm::StructType *IvarListnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 666 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 667 | // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t* |
| 668 | const llvm::Type *IvarListnfABIPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 669 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 670 | // ClassRonfABITy - LLVM for struct _class_ro_t |
| 671 | const llvm::StructType *ClassRonfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 672 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 673 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 674 | const llvm::Type *ImpnfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 675 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 676 | // CategorynfABITy - LLVM for struct _category_t |
| 677 | const llvm::StructType *CategorynfABITy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 678 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 679 | // New types for nonfragile abi messaging. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 680 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 681 | // MessageRefTy - LLVM for: |
| 682 | // struct _message_ref_t { |
| 683 | // IMP messenger; |
| 684 | // SEL name; |
| 685 | // }; |
| 686 | const llvm::StructType *MessageRefTy; |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 687 | // MessageRefCTy - clang type for struct _message_ref_t |
| 688 | QualType MessageRefCTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 689 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 690 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 691 | const llvm::Type *MessageRefPtrTy; |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 692 | // MessageRefCPtrTy - clang type for struct _message_ref_t* |
| 693 | QualType MessageRefCPtrTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 694 | |
Fariborz Jahanian | 4e87c83 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 695 | // MessengerTy - Type of the messenger (shown as IMP above) |
| 696 | const llvm::FunctionType *MessengerTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 697 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 698 | // SuperMessageRefTy - LLVM for: |
| 699 | // struct _super_message_ref_t { |
| 700 | // SUPER_IMP messenger; |
| 701 | // SEL name; |
| 702 | // }; |
| 703 | const llvm::StructType *SuperMessageRefTy; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 704 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 705 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 706 | const llvm::Type *SuperMessageRefPtrTy; |
Daniel Dunbar | 0b0dcd9 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 707 | |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 708 | llvm::Constant *getMessageSendFixupFn() { |
| 709 | // id objc_msgSend_fixup(id, struct message_ref_t*, ...) |
| 710 | std::vector<const llvm::Type*> Params; |
| 711 | Params.push_back(ObjectPtrTy); |
| 712 | Params.push_back(MessageRefPtrTy); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 713 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 714 | Params, true), |
| 715 | "objc_msgSend_fixup"); |
| 716 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 717 | |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 718 | llvm::Constant *getMessageSendFpretFixupFn() { |
| 719 | // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...) |
| 720 | std::vector<const llvm::Type*> Params; |
| 721 | Params.push_back(ObjectPtrTy); |
| 722 | Params.push_back(MessageRefPtrTy); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 723 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 724 | Params, true), |
| 725 | "objc_msgSend_fpret_fixup"); |
| 726 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 727 | |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 728 | llvm::Constant *getMessageSendStretFixupFn() { |
| 729 | // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...) |
| 730 | std::vector<const llvm::Type*> Params; |
| 731 | Params.push_back(ObjectPtrTy); |
| 732 | Params.push_back(MessageRefPtrTy); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 733 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 734 | Params, true), |
| 735 | "objc_msgSend_stret_fixup"); |
| 736 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 737 | |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 738 | llvm::Constant *getMessageSendSuper2FixupFn() { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 739 | // id objc_msgSendSuper2_fixup (struct objc_super *, |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 740 | // struct _super_message_ref_t*, ...) |
| 741 | std::vector<const llvm::Type*> Params; |
| 742 | Params.push_back(SuperPtrTy); |
| 743 | Params.push_back(SuperMessageRefPtrTy); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 744 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 745 | Params, true), |
| 746 | "objc_msgSendSuper2_fixup"); |
| 747 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 748 | |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 749 | llvm::Constant *getMessageSendSuper2StretFixupFn() { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 750 | // id objc_msgSendSuper2_stret_fixup(struct objc_super *, |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 751 | // struct _super_message_ref_t*, ...) |
| 752 | std::vector<const llvm::Type*> Params; |
| 753 | Params.push_back(SuperPtrTy); |
| 754 | Params.push_back(SuperMessageRefPtrTy); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 755 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 2dfdb3e | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 756 | Params, true), |
| 757 | "objc_msgSendSuper2_stret_fixup"); |
| 758 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 759 | |
Chris Lattner | a7c00b4 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 760 | llvm::Constant *getObjCEndCatchFn() { |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 761 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Chris Lattner | 3dd1b4b | 2009-07-01 04:13:52 +0000 | [diff] [blame] | 762 | false), |
Chris Lattner | a7c00b4 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 763 | "objc_end_catch"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 764 | |
Chris Lattner | a7c00b4 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 765 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 766 | |
Chris Lattner | a7c00b4 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 767 | llvm::Constant *getObjCBeginCatchFn() { |
| 768 | std::vector<const llvm::Type*> Params; |
| 769 | Params.push_back(Int8PtrTy); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 770 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy, |
Chris Lattner | a7c00b4 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 771 | Params, false), |
| 772 | "objc_begin_catch"); |
| 773 | } |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 774 | |
| 775 | const llvm::StructType *EHTypeTy; |
| 776 | const llvm::Type *EHTypePtrTy; |
Daniel Dunbar | 0b0dcd9 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 777 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 778 | ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); |
| 779 | ~ObjCNonFragileABITypesHelper(){} |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 780 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 781 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 782 | class CGObjCCommonMac : public CodeGen::CGObjCRuntime { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 783 | public: |
| 784 | // FIXME - accessibility |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 785 | class GC_IVAR { |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 786 | public: |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 787 | unsigned ivar_bytepos; |
| 788 | unsigned ivar_size; |
| 789 | GC_IVAR(unsigned bytepos = 0, unsigned size = 0) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 790 | : ivar_bytepos(bytepos), ivar_size(size) {} |
Daniel Dunbar | 22b0ada | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 791 | |
| 792 | // Allow sorting based on byte pos. |
| 793 | bool operator<(const GC_IVAR &b) const { |
| 794 | return ivar_bytepos < b.ivar_bytepos; |
| 795 | } |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 796 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 797 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 798 | class SKIP_SCAN { |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 799 | public: |
| 800 | unsigned skip; |
| 801 | unsigned scan; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 802 | SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0) |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 803 | : skip(_skip), scan(_scan) {} |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 804 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 805 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 806 | protected: |
| 807 | CodeGen::CodeGenModule &CGM; |
Owen Anderson | ae86c19 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 808 | llvm::LLVMContext &VMContext; |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 809 | // FIXME! May not be needing this after all. |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 810 | unsigned ObjCABI; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 811 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 812 | // gc ivar layout bitmap calculation helper caches. |
| 813 | llvm::SmallVector<GC_IVAR, 16> SkipIvars; |
| 814 | llvm::SmallVector<GC_IVAR, 16> IvarsInfo; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 815 | |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 816 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 817 | /// DefinedSymbols and FinishModule(). |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 818 | llvm::SetVector<IdentifierInfo*> LazySymbols; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 819 | |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 820 | /// DefinedSymbols - External symbols which are defined by this |
| 821 | /// module. The symbols in this list and LazySymbols are used to add |
| 822 | /// special linker symbols which ensure that Objective-C modules are |
| 823 | /// linked properly. |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 824 | llvm::SetVector<IdentifierInfo*> DefinedSymbols; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 825 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 826 | /// ClassNames - uniqued class names. |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 827 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 828 | |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 829 | /// MethodVarNames - uniqued method variable names. |
| 830 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 831 | |
Fariborz Jahanian | 9adb2e6 | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 832 | /// DefinedCategoryNames - list of category names in form Class_Category. |
| 833 | llvm::SetVector<std::string> DefinedCategoryNames; |
| 834 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 835 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 836 | /// a StringMap here because have no other unique reference. |
| 837 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 838 | |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 839 | /// MethodDefinitions - map of methods which have been defined in |
| 840 | /// this translation unit. |
| 841 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 842 | |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 843 | /// PropertyNames - uniqued method variable names. |
| 844 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 845 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 846 | /// ClassReferences - uniqued class references. |
| 847 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 848 | |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 849 | /// SelectorReferences - uniqued selector references. |
| 850 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 851 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 852 | /// Protocols - Protocols for which an objc_protocol structure has |
| 853 | /// been emitted. Forward declarations are handled by creating an |
| 854 | /// empty structure whose initializer is filled in when/if defined. |
| 855 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 856 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 857 | /// DefinedProtocols - Protocols which have actually been |
| 858 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 859 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 860 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 861 | /// DefinedClasses - List of defined classes. |
| 862 | std::vector<llvm::GlobalValue*> DefinedClasses; |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 863 | |
| 864 | /// DefinedNonLazyClasses - List of defined "non-lazy" classes. |
| 865 | std::vector<llvm::GlobalValue*> DefinedNonLazyClasses; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 866 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 867 | /// DefinedCategories - List of defined categories. |
| 868 | std::vector<llvm::GlobalValue*> DefinedCategories; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 869 | |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 870 | /// DefinedNonLazyCategories - List of defined "non-lazy" categories. |
| 871 | std::vector<llvm::GlobalValue*> DefinedNonLazyCategories; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 872 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 873 | /// GetNameForMethod - Return a name for the given method. |
| 874 | /// \param[out] NameOut - The return value. |
| 875 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 876 | const ObjCContainerDecl *CD, |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 877 | llvm::SmallVectorImpl<char> &NameOut); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 878 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 879 | /// GetMethodVarName - Return a unique constant for the given |
| 880 | /// selector's name. The return value has type char *. |
| 881 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 882 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 883 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 884 | /// GetMethodVarType - Return a unique constant for the given |
| 885 | /// selector's name. The return value has type char *. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 886 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 887 | // FIXME: This is a horrible name. |
| 888 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D); |
Daniel Dunbar | f5c1846 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 889 | llvm::Constant *GetMethodVarType(const FieldDecl *D); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 890 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 891 | /// GetPropertyName - Return a unique constant for the given |
| 892 | /// name. The return value has type char *. |
| 893 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 894 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 895 | // FIXME: This can be dropped once string functions are unified. |
| 896 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 897 | const Decl *Container); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 898 | |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 899 | /// GetClassName - Return a unique constant for the given selector's |
| 900 | /// name. The return value has type char *. |
| 901 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 902 | |
Argyrios Kyrtzidis | 13257c5 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 903 | llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD); |
| 904 | |
Fariborz Jahanian | c559f3f | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 905 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 906 | /// implementation for the __strong or __weak case. |
| 907 | /// |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 908 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
| 909 | bool ForStrongLayout); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 910 | |
Fariborz Jahanian | 1f78a9a | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 911 | llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 912 | |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 913 | void BuildAggrIvarRecordLayout(const RecordType *RT, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 914 | unsigned int BytePos, bool ForStrongLayout, |
| 915 | bool &HasUnion); |
Daniel Dunbar | 36e2a1e | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 916 | void BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 917 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 918 | const RecordDecl *RD, |
Chris Lattner | 5b36ddb | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 919 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | c559f3f | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 920 | unsigned int BytePos, bool ForStrongLayout, |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 921 | bool &HasUnion); |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 922 | |
Fariborz Jahanian | 01dff42 | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 923 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 924 | /// ivar layout bitmap. |
| 925 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 926 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 927 | |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 928 | /// EmitPropertyList - Emit the given property list. The return |
| 929 | /// value has type PropertyListPtrTy. |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 930 | llvm::Constant *EmitPropertyList(llvm::Twine Name, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 931 | const Decl *Container, |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 932 | const ObjCContainerDecl *OCD, |
| 933 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 934 | |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 935 | /// PushProtocolProperties - Push protocol's property on the input stack. |
| 936 | void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet, |
| 937 | std::vector<llvm::Constant*> &Properties, |
| 938 | const Decl *Container, |
| 939 | const ObjCProtocolDecl *PROTO, |
| 940 | const ObjCCommonTypesHelper &ObjCTypes); |
| 941 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 942 | /// GetProtocolRef - Return a reference to the internal protocol |
| 943 | /// description, creating an empty one if it has not been |
| 944 | /// defined. The return value has type ProtocolPtrTy. |
| 945 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | 6772621 | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 946 | |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 947 | /// CreateMetadataVar - Create a global variable with internal |
| 948 | /// linkage for use by the Objective-C runtime. |
| 949 | /// |
| 950 | /// This is a convenience wrapper which not only creates the |
| 951 | /// variable, but also sets the section and alignment and adds the |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 952 | /// global to the "llvm.used" list. |
Daniel Dunbar | 463cc8a | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 953 | /// |
| 954 | /// \param Name - The variable name. |
| 955 | /// \param Init - The variable initializer; this is also used to |
| 956 | /// define the type of the variable. |
| 957 | /// \param Section - The section the variable should go into, or 0. |
| 958 | /// \param Align - The alignment for the variable, or 0. |
| 959 | /// \param AddToUsed - Whether the variable should be added to |
Daniel Dunbar | 4527d30 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 960 | /// "llvm.used". |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 961 | llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name, |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 962 | llvm::Constant *Init, |
| 963 | const char *Section, |
Daniel Dunbar | 463cc8a | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 964 | unsigned Align, |
| 965 | bool AddToUsed); |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 966 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 967 | CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 968 | ReturnValueSlot Return, |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 969 | QualType ResultType, |
| 970 | llvm::Value *Sel, |
| 971 | llvm::Value *Arg0, |
| 972 | QualType Arg0Ty, |
| 973 | bool IsSuper, |
| 974 | const CallArgList &CallArgs, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 975 | const ObjCMethodDecl *OMD, |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 976 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | f5c1846 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 977 | |
Daniel Dunbar | 5e63927 | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 978 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 979 | /// level information. |
| 980 | void EmitImageInfo(); |
| 981 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 982 | public: |
Owen Anderson | ae86c19 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 983 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 984 | CGM(cgm), VMContext(cgm.getLLVMContext()) { } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 985 | |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 986 | virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 987 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 988 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 989 | const ObjCContainerDecl *CD=0); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 990 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 991 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 992 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 993 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 994 | /// declaration, emitting it if necessary. The return value has type |
| 995 | /// ProtocolPtrTy. |
| 996 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 997 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 998 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 999 | /// object for the given declaration, emitting it if needed. These |
| 1000 | /// forward references will be filled in with empty bodies if no |
| 1001 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1002 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame^] | 1003 | virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM, |
| 1004 | const CGBlockInfo &blockInfo); |
Fariborz Jahanian | c05349e | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 1005 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1006 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1007 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1008 | class CGObjCMac : public CGObjCCommonMac { |
| 1009 | private: |
| 1010 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1011 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1012 | /// EmitModuleInfo - Another marker encoding module level |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1013 | /// information. |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1014 | void EmitModuleInfo(); |
| 1015 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1016 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 1017 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1018 | llvm::Constant *EmitModuleSymbols(); |
| 1019 | |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1020 | /// FinishModule - Write out global data structures at the end of |
| 1021 | /// processing a translation unit. |
| 1022 | void FinishModule(); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1023 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1024 | /// EmitClassExtension - Generate the class extension structure used |
| 1025 | /// to store the weak ivar layout and properties. The return value |
| 1026 | /// has type ClassExtensionPtrTy. |
| 1027 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 1028 | |
| 1029 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1030 | /// for the given class. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1031 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1032 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 1033 | |
| 1034 | /// EmitSuperClassRef - Emits reference to class's main metadata class. |
| 1035 | llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1036 | |
| 1037 | /// EmitIvarList - Emit the ivar list for the given |
| 1038 | /// implementation. If ForClass is true the list of class ivars |
| 1039 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1040 | /// interface ivars will be emitted. The return value has type |
| 1041 | /// IvarListPtrTy. |
| 1042 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | b042a59 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1043 | bool ForClass); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1044 | |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1045 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 1046 | /// for the metaclass of the given interface. The return value has |
| 1047 | /// type ClassPtrTy. |
| 1048 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 1049 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1050 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1051 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1052 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1053 | llvm::Constant *Protocols, |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1054 | const ConstantVector &Methods); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1055 | |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1056 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1057 | |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1058 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1059 | |
| 1060 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | 89654ee | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1061 | /// implementation. The return value has type MethodListPtrTy. |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1062 | llvm::Constant *EmitMethodList(llvm::Twine Name, |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1063 | const char *Section, |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1064 | const ConstantVector &Methods); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1065 | |
| 1066 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1067 | /// method declarations. |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1068 | /// - TypeName: The name for the type containing the methods. |
| 1069 | /// - IsProtocol: True iff these methods are for a protocol. |
| 1070 | /// - ClassMethds: True iff these are class methods. |
| 1071 | /// - Required: When true, only "required" methods are |
| 1072 | /// listed. Similarly, when false only "optional" methods are |
| 1073 | /// listed. For classes this should always be true. |
| 1074 | /// - begin, end: The method list to output. |
| 1075 | /// |
| 1076 | /// The return value has type MethodDescriptionListPtrTy. |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1077 | llvm::Constant *EmitMethodDescList(llvm::Twine Name, |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1078 | const char *Section, |
| 1079 | const ConstantVector &Methods); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1080 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1081 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1082 | /// declaration, emitting it if necessary. The return value has type |
| 1083 | /// ProtocolPtrTy. |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1084 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1085 | |
| 1086 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1087 | /// object for the given declaration, emitting it if needed. These |
| 1088 | /// forward references will be filled in with empty bodies if no |
| 1089 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1090 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1091 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1092 | /// EmitProtocolExtension - Generate the protocol extension |
| 1093 | /// structure used to store optional instance and class methods, and |
| 1094 | /// protocol properties. The return value has type |
| 1095 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1096 | llvm::Constant * |
| 1097 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1098 | const ConstantVector &OptInstanceMethods, |
| 1099 | const ConstantVector &OptClassMethods); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1100 | |
| 1101 | /// EmitProtocolList - Generate the list of referenced |
| 1102 | /// protocols. The return value has type ProtocolListPtrTy. |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1103 | llvm::Constant *EmitProtocolList(llvm::Twine Name, |
Daniel Dunbar | dec75f8 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1104 | ObjCProtocolDecl::protocol_iterator begin, |
| 1105 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1106 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1107 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1108 | /// for the given selector. |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1109 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel, |
| 1110 | bool lval=false); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1111 | |
| 1112 | public: |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1113 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1114 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1115 | virtual llvm::Function *ModuleInitFunction(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1116 | |
Daniel Dunbar | 97db84c | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1117 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1118 | ReturnValueSlot Return, |
Daniel Dunbar | 4b8c6db | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1119 | QualType ResultType, |
| 1120 | Selector Sel, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1121 | llvm::Value *Receiver, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1122 | const CallArgList &CallArgs, |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1123 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1124 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1125 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1126 | virtual CodeGen::RValue |
Daniel Dunbar | 97db84c | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1127 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1128 | ReturnValueSlot Return, |
Daniel Dunbar | 4b8c6db | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1129 | QualType ResultType, |
| 1130 | Selector Sel, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1131 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1132 | bool isCategoryImpl, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1133 | llvm::Value *Receiver, |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1134 | bool IsClassMessage, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1135 | const CallArgList &CallArgs, |
| 1136 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1137 | |
Daniel Dunbar | cb46385 | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1138 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1139 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1140 | |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1141 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 1142 | bool lval = false); |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1143 | |
| 1144 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1145 | /// untyped one. |
| 1146 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1147 | const ObjCMethodDecl *Method); |
| 1148 | |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1149 | virtual llvm::Constant *GetEHType(QualType T); |
| 1150 | |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1151 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1152 | |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1153 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1154 | |
Daniel Dunbar | cb46385 | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1155 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1156 | const ObjCProtocolDecl *PD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1157 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1158 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 1159 | virtual llvm::Constant *GetPropertySetFunction(); |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 1160 | virtual llvm::Constant *GetGetStructFunction(); |
| 1161 | virtual llvm::Constant *GetSetStructFunction(); |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1162 | virtual llvm::Constant *EnumerationMutationFunction(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1163 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1164 | virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 1165 | const ObjCAtTryStmt &S); |
| 1166 | virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1167 | const ObjCAtSynchronizedStmt &S); |
| 1168 | void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S); |
Anders Carlsson | 1963b0c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1169 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 1170 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1171 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1172 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1173 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1174 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1175 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 1176 | llvm::Value *src, llvm::Value *dest, |
| 1177 | bool threadlocal = false); |
Fariborz Jahanian | e881b53 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 1178 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1179 | llvm::Value *src, llvm::Value *dest, |
| 1180 | llvm::Value *ivarOffset); |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1181 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 1182 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1183 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1184 | llvm::Value *dest, llvm::Value *src, |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 1185 | llvm::Value *size); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1186 | |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1187 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1188 | QualType ObjectTy, |
| 1189 | llvm::Value *BaseValue, |
| 1190 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1191 | unsigned CVRQualifiers); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1192 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 722f424 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1193 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1194 | const ObjCIvarDecl *Ivar); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1195 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1196 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1197 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1198 | private: |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1199 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1200 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 1201 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1202 | |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1203 | /// SuperClassReferences - uniqued super class references. |
| 1204 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1205 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1206 | /// MetaClassReferences - uniqued meta class references. |
| 1207 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1208 | |
| 1209 | /// EHTypeReferences - uniqued class ehtype references. |
| 1210 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1211 | |
Fariborz Jahanian | e412864 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1212 | /// NonLegacyDispatchMethods - List of methods for which we do *not* generate |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1213 | /// legacy messaging dispatch. |
Fariborz Jahanian | e412864 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1214 | llvm::DenseSet<Selector> NonLegacyDispatchMethods; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1215 | |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 1216 | /// DefinedMetaClasses - List of defined meta-classes. |
| 1217 | std::vector<llvm::GlobalValue*> DefinedMetaClasses; |
| 1218 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1219 | /// LegacyDispatchedSelector - Returns true if SEL is not in the list of |
Fariborz Jahanian | e412864 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1220 | /// NonLegacyDispatchMethods; false otherwise. |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1221 | bool LegacyDispatchedSelector(Selector Sel); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1222 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1223 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 1224 | /// processing a translation unit. |
| 1225 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1226 | |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 1227 | /// AddModuleClassList - Add the given list of class pointers to the |
| 1228 | /// module with the provided symbol and section names. |
| 1229 | void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container, |
| 1230 | const char *SymbolName, |
| 1231 | const char *SectionName); |
| 1232 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1233 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 1234 | unsigned InstanceStart, |
| 1235 | unsigned InstanceSize, |
| 1236 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1237 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1238 | llvm::Constant *IsAGV, |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1239 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | 8220825 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 1240 | llvm::Constant *ClassRoGV, |
| 1241 | bool HiddenVisibility); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1242 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1243 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1244 | |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 1245 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1246 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1247 | /// EmitMethodList - Emit the method list for the given |
| 1248 | /// implementation. The return value has type MethodListnfABITy. |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1249 | llvm::Constant *EmitMethodList(llvm::Twine Name, |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1250 | const char *Section, |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 1251 | const ConstantVector &Methods); |
| 1252 | /// EmitIvarList - Emit the ivar list for the given |
| 1253 | /// implementation. If ForClass is true the list of class ivars |
| 1254 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1255 | /// interface ivars will be emitted. The return value has type |
| 1256 | /// IvarListnfABIPtrTy. |
| 1257 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1258 | |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1259 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 3d3426f | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 1260 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 40a4bcd | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 1261 | unsigned long int offset); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1262 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1263 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1264 | /// declaration, emitting it if necessary. The return value has type |
| 1265 | /// ProtocolPtrTy. |
| 1266 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1267 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1268 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1269 | /// object for the given declaration, emitting it if needed. These |
| 1270 | /// forward references will be filled in with empty bodies if no |
| 1271 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1272 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1273 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1274 | /// EmitProtocolList - Generate the list of referenced |
| 1275 | /// protocols. The return value has type ProtocolListPtrTy. |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1276 | llvm::Constant *EmitProtocolList(llvm::Twine Name, |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1277 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 3d9296e | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1278 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1279 | |
Fariborz Jahanian | 3d9296e | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1280 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1281 | ReturnValueSlot Return, |
Fariborz Jahanian | 3d9296e | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1282 | QualType ResultType, |
| 1283 | Selector Sel, |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 1284 | llvm::Value *Receiver, |
Fariborz Jahanian | 3d9296e | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1285 | QualType Arg0Ty, |
| 1286 | bool IsSuper, |
| 1287 | const CallArgList &CallArgs); |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 1288 | |
| 1289 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1290 | /// class of the given name. |
Fariborz Jahanian | 899e7eb | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 1291 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1292 | |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1293 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1294 | /// for the given class reference. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1295 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1296 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1297 | |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1298 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1299 | /// for the given super class reference. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1300 | llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder, |
| 1301 | const ObjCInterfaceDecl *ID); |
| 1302 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1303 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 1304 | /// meta-data |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1305 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1306 | const ObjCInterfaceDecl *ID); |
| 1307 | |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1308 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 1309 | /// the given ivar. |
| 1310 | /// |
Daniel Dunbar | a106052 | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 1311 | llvm::GlobalVariable * ObjCIvarOffsetVariable( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1312 | const ObjCInterfaceDecl *ID, |
| 1313 | const ObjCIvarDecl *Ivar); |
| 1314 | |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1315 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1316 | /// for the given selector. |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1317 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel, |
| 1318 | bool lval=false); |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1319 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1320 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1321 | /// interface. The return value has type EHTypePtrTy. |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1322 | llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1323 | bool ForDefinition); |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1324 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1325 | const char *getMetaclassSymbolPrefix() const { |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1326 | return "OBJC_METACLASS_$_"; |
| 1327 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1328 | |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1329 | const char *getClassSymbolPrefix() const { |
| 1330 | return "OBJC_CLASS_$_"; |
| 1331 | } |
| 1332 | |
Daniel Dunbar | 96120237 | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 1333 | void GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | 554fd79 | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1334 | uint32_t &InstanceStart, |
| 1335 | uint32_t &InstanceSize); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1336 | |
Fariborz Jahanian | e412864 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1337 | // Shamelessly stolen from Analysis/CFRefCount.cpp |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1338 | Selector GetNullarySelector(const char* name) const { |
Fariborz Jahanian | e412864 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1339 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1340 | return CGM.getContext().Selectors.getSelector(0, &II); |
| 1341 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1342 | |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1343 | Selector GetUnarySelector(const char* name) const { |
Fariborz Jahanian | e412864 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1344 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1345 | return CGM.getContext().Selectors.getSelector(1, &II); |
| 1346 | } |
Daniel Dunbar | 554fd79 | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1347 | |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1348 | /// ImplementationIsNonLazy - Check whether the given category or |
| 1349 | /// class implementation is "non-lazy". |
Fariborz Jahanian | a6bed83 | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 1350 | bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const; |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1351 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1352 | public: |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1353 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1354 | // FIXME. All stubs for now! |
| 1355 | virtual llvm::Function *ModuleInitFunction(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1356 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1357 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1358 | ReturnValueSlot Return, |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1359 | QualType ResultType, |
| 1360 | Selector Sel, |
| 1361 | llvm::Value *Receiver, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1362 | const CallArgList &CallArgs, |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1363 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1364 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1365 | |
| 1366 | virtual CodeGen::RValue |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1367 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1368 | ReturnValueSlot Return, |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1369 | QualType ResultType, |
| 1370 | Selector Sel, |
| 1371 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1372 | bool isCategoryImpl, |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1373 | llvm::Value *Receiver, |
| 1374 | bool IsClassMessage, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1375 | const CallArgList &CallArgs, |
| 1376 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1377 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1378 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1379 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1380 | |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1381 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 1382 | bool lvalue = false) |
| 1383 | { return EmitSelector(Builder, Sel, lvalue); } |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1384 | |
| 1385 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1386 | /// untyped one. |
| 1387 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1388 | const ObjCMethodDecl *Method) |
| 1389 | { return EmitSelector(Builder, Method->getSelector()); } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1390 | |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 1391 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1392 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1393 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1394 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 1395 | const ObjCProtocolDecl *PD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1396 | |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1397 | virtual llvm::Constant *GetEHType(QualType T); |
| 1398 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1399 | virtual llvm::Constant *GetPropertyGetFunction() { |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1400 | return ObjCTypes.getGetPropertyFn(); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1401 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1402 | virtual llvm::Constant *GetPropertySetFunction() { |
| 1403 | return ObjCTypes.getSetPropertyFn(); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1404 | } |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1405 | |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 1406 | virtual llvm::Constant *GetSetStructFunction() { |
| 1407 | return ObjCTypes.getCopyStructFn(); |
| 1408 | } |
| 1409 | virtual llvm::Constant *GetGetStructFunction() { |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1410 | return ObjCTypes.getCopyStructFn(); |
| 1411 | } |
| 1412 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1413 | virtual llvm::Constant *EnumerationMutationFunction() { |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1414 | return ObjCTypes.getEnumerationMutationFn(); |
Daniel Dunbar | d73ea816 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 1415 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1416 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1417 | virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 1418 | const ObjCAtTryStmt &S); |
| 1419 | virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1420 | const ObjCAtSynchronizedStmt &S); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1421 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 1422 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1423 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1424 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1425 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1426 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1427 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 1428 | llvm::Value *src, llvm::Value *dest, |
| 1429 | bool threadlocal = false); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1430 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1431 | llvm::Value *src, llvm::Value *dest, |
| 1432 | llvm::Value *ivarOffset); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1433 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1434 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1435 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1436 | llvm::Value *dest, llvm::Value *src, |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 1437 | llvm::Value *size); |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1438 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1439 | QualType ObjectTy, |
| 1440 | llvm::Value *BaseValue, |
| 1441 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1442 | unsigned CVRQualifiers); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1443 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 722f424 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1444 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1445 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1446 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1447 | |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1448 | } // end anonymous namespace |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1449 | |
| 1450 | /* *** Helper Functions *** */ |
| 1451 | |
| 1452 | /// getConstantGEP() - Help routine to construct simple GEPs. |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1453 | static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1454 | llvm::Constant *C, |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1455 | unsigned idx0, |
| 1456 | unsigned idx1) { |
| 1457 | llvm::Value *Idxs[] = { |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1458 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0), |
| 1459 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1) |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1460 | }; |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1461 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2); |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1464 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 1465 | /// class has the __objc_exception__ attribute. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1466 | static bool hasObjCExceptionAttribute(ASTContext &Context, |
Douglas Gregor | 78bd61f | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1467 | const ObjCInterfaceDecl *OID) { |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1468 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1469 | return true; |
| 1470 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
Douglas Gregor | 78bd61f | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1471 | return hasObjCExceptionAttribute(Context, Super); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1472 | return false; |
| 1473 | } |
| 1474 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1475 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1476 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1477 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1478 | ObjCTypes(cgm) { |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1479 | ObjCABI = 1; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1480 | EmitImageInfo(); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
Daniel Dunbar | 7c6d3a7 | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1483 | /// GetClass - Return a reference to the class for the given interface |
| 1484 | /// decl. |
Daniel Dunbar | cb46385 | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1485 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1486 | const ObjCInterfaceDecl *ID) { |
| 1487 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1491 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 1492 | bool lval) { |
| 1493 | return EmitSelector(Builder, Sel, lval); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1494 | } |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1495 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1496 | *Method) { |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1497 | return EmitSelector(Builder, Method->getSelector()); |
| 1498 | } |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1499 | |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1500 | llvm::Constant *CGObjCMac::GetEHType(QualType T) { |
| 1501 | llvm_unreachable("asking for catch type for ObjC type in fragile runtime"); |
| 1502 | return 0; |
| 1503 | } |
| 1504 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1505 | /// Generate a constant CFString object. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1506 | /* |
| 1507 | struct __builtin_CFString { |
| 1508 | const int *isa; // point to __CFConstantStringClassReference |
| 1509 | int flags; |
| 1510 | const char *str; |
| 1511 | long length; |
| 1512 | }; |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1513 | */ |
| 1514 | |
Fariborz Jahanian | 63408e8 | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1515 | /// or Generate a constant NSString object. |
| 1516 | /* |
| 1517 | struct __builtin_NSString { |
| 1518 | const int *isa; // point to __NSConstantStringClassReference |
| 1519 | const char *str; |
| 1520 | unsigned int length; |
| 1521 | }; |
| 1522 | */ |
| 1523 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1524 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 1525 | const StringLiteral *SL) { |
Fariborz Jahanian | 63408e8 | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1526 | return (CGM.getLangOptions().NoConstantCFStrings == 0 ? |
| 1527 | CGM.GetAddrOfConstantCFString(SL) : |
Fariborz Jahanian | 50c925f | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 1528 | CGM.GetAddrOfConstantString(SL)); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | /// Generates a message send where the super is the receiver. This is |
| 1532 | /// a message send to self with special delivery semantics indicating |
| 1533 | /// which class's method should be called. |
Daniel Dunbar | 97db84c | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1534 | CodeGen::RValue |
| 1535 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1536 | ReturnValueSlot Return, |
Daniel Dunbar | 4b8c6db | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1537 | QualType ResultType, |
| 1538 | Selector Sel, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1539 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1540 | bool isCategoryImpl, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1541 | llvm::Value *Receiver, |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1542 | bool IsClassMessage, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1543 | const CodeGen::CallArgList &CallArgs, |
| 1544 | const ObjCMethodDecl *Method) { |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1545 | // Create and init a super structure; this is a (receiver, class) |
| 1546 | // pair we will pass to objc_msgSendSuper. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1547 | llvm::Value *ObjCSuper = |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1548 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1549 | llvm::Value *ReceiverAsObject = |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1550 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1551 | CGF.Builder.CreateStore(ReceiverAsObject, |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1552 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | f6397fe | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1553 | |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1554 | // If this is a class message the metaclass is passed as the target. |
| 1555 | llvm::Value *Target; |
| 1556 | if (IsClassMessage) { |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1557 | if (isCategoryImpl) { |
| 1558 | // Message sent to 'super' in a class method defined in a category |
| 1559 | // implementation requires an odd treatment. |
| 1560 | // If we are in a class method, we must retrieve the |
| 1561 | // _metaclass_ for the current class, pointed at by |
| 1562 | // the class's "isa" pointer. The following assumes that |
| 1563 | // isa" is the first ivar in a class (which it must be). |
| 1564 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1565 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 1566 | Target = CGF.Builder.CreateLoad(Target); |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 1567 | } else { |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1568 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 1569 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 1570 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 1571 | Target = Super; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1572 | } |
Fariborz Jahanian | da2efb0 | 2009-11-14 02:18:31 +0000 | [diff] [blame] | 1573 | } |
| 1574 | else if (isCategoryImpl) |
| 1575 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1576 | else { |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 1577 | llvm::Value *ClassPtr = EmitSuperClassRef(Class); |
| 1578 | ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1); |
| 1579 | Target = CGF.Builder.CreateLoad(ClassPtr); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1580 | } |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1581 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 1582 | // ObjCTypes types. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1583 | const llvm::Type *ClassTy = |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1584 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1585 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1586 | CGF.Builder.CreateStore(Target, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1587 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1588 | return EmitLegacyMessageSend(CGF, Return, ResultType, |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1589 | EmitSelector(CGF.Builder, Sel), |
| 1590 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1591 | true, CallArgs, Method, ObjCTypes); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1592 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1593 | |
| 1594 | /// Generate code for a message send expression. |
Daniel Dunbar | 97db84c | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1595 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1596 | ReturnValueSlot Return, |
Daniel Dunbar | 4b8c6db | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1597 | QualType ResultType, |
| 1598 | Selector Sel, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1599 | llvm::Value *Receiver, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1600 | const CallArgList &CallArgs, |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1601 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1602 | const ObjCMethodDecl *Method) { |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1603 | return EmitLegacyMessageSend(CGF, Return, ResultType, |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1604 | EmitSelector(CGF.Builder, Sel), |
| 1605 | Receiver, CGF.getContext().getObjCIdType(), |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1606 | false, CallArgs, Method, ObjCTypes); |
Daniel Dunbar | 97ff50d | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1609 | CodeGen::RValue |
| 1610 | CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1611 | ReturnValueSlot Return, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1612 | QualType ResultType, |
| 1613 | llvm::Value *Sel, |
| 1614 | llvm::Value *Arg0, |
| 1615 | QualType Arg0Ty, |
| 1616 | bool IsSuper, |
| 1617 | const CallArgList &CallArgs, |
| 1618 | const ObjCMethodDecl *Method, |
| 1619 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1620 | CallArgList ActualArgs; |
Fariborz Jahanian | 969bc68 | 2009-04-24 21:07:43 +0000 | [diff] [blame] | 1621 | if (!IsSuper) |
| 1622 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 41cf9de | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1623 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1624 | ActualArgs.push_back(std::make_pair(RValue::get(Sel), |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1625 | CGF.getContext().getObjCSelType())); |
| 1626 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1627 | |
Daniel Dunbar | bf8c24a | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1628 | CodeGenTypes &Types = CGM.getTypes(); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1629 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1630 | FunctionType::ExtInfo()); |
Daniel Dunbar | df0e62d | 2009-09-17 04:01:40 +0000 | [diff] [blame] | 1631 | const llvm::FunctionType *FTy = |
| 1632 | Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1633 | |
Anders Carlsson | 280e61f1 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 1634 | if (Method) |
| 1635 | assert(CGM.getContext().getCanonicalType(Method->getResultType()) == |
| 1636 | CGM.getContext().getCanonicalType(ResultType) && |
| 1637 | "Result type mismatch!"); |
| 1638 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1639 | llvm::Constant *Fn = NULL; |
Daniel Dunbar | 6f2e839 | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1640 | if (CGM.ReturnTypeUsesSRet(FnInfo)) { |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1641 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1642 | : ObjCTypes.getSendStretFn(IsSuper); |
Daniel Dunbar | 6f2e839 | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1643 | } else if (CGM.ReturnTypeUsesFPRet(ResultType)) { |
| 1644 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper) |
| 1645 | : ObjCTypes.getSendFpretFn(IsSuper); |
Daniel Dunbar | 3c683f5b | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1646 | } else { |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1647 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1648 | : ObjCTypes.getSendFn(IsSuper); |
Daniel Dunbar | 3c683f5b | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1649 | } |
Daniel Dunbar | 6f2e839 | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1650 | Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy)); |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1651 | return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1654 | static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) { |
| 1655 | if (FQT.isObjCGCStrong()) |
| 1656 | return Qualifiers::Strong; |
| 1657 | |
| 1658 | if (FQT.isObjCGCWeak()) |
| 1659 | return Qualifiers::Weak; |
| 1660 | |
| 1661 | if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType()) |
| 1662 | return Qualifiers::Strong; |
| 1663 | |
| 1664 | if (const PointerType *PT = FQT->getAs<PointerType>()) |
| 1665 | return GetGCAttrTypeForType(Ctx, PT->getPointeeType()); |
| 1666 | |
| 1667 | return Qualifiers::GCNone; |
| 1668 | } |
| 1669 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame^] | 1670 | llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM, |
| 1671 | const CGBlockInfo &blockInfo) { |
| 1672 | llvm::Constant *nullPtr = |
Fariborz Jahanian | afa3c0a | 2010-08-04 18:44:59 +0000 | [diff] [blame] | 1673 | llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext)); |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame^] | 1674 | |
Fariborz Jahanian | 0aa35b9 | 2010-09-13 16:09:44 +0000 | [diff] [blame] | 1675 | if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame^] | 1676 | return nullPtr; |
| 1677 | |
Fariborz Jahanian | f95e358 | 2010-08-06 16:28:55 +0000 | [diff] [blame] | 1678 | bool hasUnion = false; |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1679 | SkipIvars.clear(); |
| 1680 | IvarsInfo.clear(); |
| 1681 | unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0); |
| 1682 | unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth(); |
| 1683 | |
Fariborz Jahanian | cfddabf | 2010-09-09 00:21:45 +0000 | [diff] [blame] | 1684 | // __isa is the first field in block descriptor and must assume by runtime's |
| 1685 | // convention that it is GC'able. |
| 1686 | IvarsInfo.push_back(GC_IVAR(0, 1)); |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame^] | 1687 | |
| 1688 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); |
| 1689 | |
| 1690 | // Calculate the basic layout of the block structure. |
| 1691 | const llvm::StructLayout *layout = |
| 1692 | CGM.getTargetData().getStructLayout(blockInfo.StructureType); |
| 1693 | |
| 1694 | // Ignore the optional 'this' capture: C++ objects are not assumed |
| 1695 | // to be GC'ed. |
| 1696 | |
| 1697 | // Walk the captured variables. |
| 1698 | for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), |
| 1699 | ce = blockDecl->capture_end(); ci != ce; ++ci) { |
| 1700 | const VarDecl *variable = ci->getVariable(); |
| 1701 | QualType type = variable->getType(); |
| 1702 | |
| 1703 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 1704 | |
| 1705 | // Ignore constant captures. |
| 1706 | if (capture.isConstant()) continue; |
| 1707 | |
| 1708 | uint64_t fieldOffset = layout->getElementOffset(capture.getIndex()); |
| 1709 | |
| 1710 | // __block variables are passed by their descriptor address. |
| 1711 | if (ci->isByRef()) { |
| 1712 | IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1)); |
Fariborz Jahanian | 933c672 | 2010-09-11 01:27:29 +0000 | [diff] [blame] | 1713 | continue; |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame^] | 1714 | } |
| 1715 | |
| 1716 | assert(!type->isArrayType() && "array variable should not be caught"); |
| 1717 | if (const RecordType *record = type->getAs<RecordType>()) { |
| 1718 | BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion); |
Fariborz Jahanian | 903aba3 | 2010-08-05 21:00:25 +0000 | [diff] [blame] | 1719 | continue; |
| 1720 | } |
Fariborz Jahanian | f95e358 | 2010-08-06 16:28:55 +0000 | [diff] [blame] | 1721 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame^] | 1722 | Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type); |
| 1723 | unsigned fieldSize = CGM.getContext().getTypeSize(type); |
| 1724 | |
| 1725 | if (GCAttr == Qualifiers::Strong) |
| 1726 | IvarsInfo.push_back(GC_IVAR(fieldOffset, |
| 1727 | fieldSize / WordSizeInBits)); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1728 | else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak) |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame^] | 1729 | SkipIvars.push_back(GC_IVAR(fieldOffset, |
| 1730 | fieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
| 1733 | if (IvarsInfo.empty()) |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame^] | 1734 | return nullPtr; |
| 1735 | |
| 1736 | // Sort on byte position; captures might not be allocated in order, |
| 1737 | // and unions can do funny things. |
| 1738 | llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end()); |
| 1739 | llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end()); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1740 | |
| 1741 | std::string BitMap; |
Fariborz Jahanian | 1f78a9a | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 1742 | llvm::Constant *C = BuildIvarLayoutBitmap(BitMap); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1743 | if (CGM.getLangOptions().ObjCGCBitmapPrint) { |
| 1744 | printf("\n block variable layout for block: "); |
| 1745 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
| 1746 | for (unsigned i = 0; i < BitMap.size(); i++) |
| 1747 | if (!(s[i] & 0xf0)) |
| 1748 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 1749 | else |
| 1750 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 1751 | printf("\n"); |
| 1752 | } |
| 1753 | |
| 1754 | return C; |
Fariborz Jahanian | c05349e | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1757 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1758 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 7050c55 | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1759 | // FIXME: I don't understand why gcc generates this, or where it is |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1760 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | 7050c55 | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1761 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1762 | |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1763 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1764 | ObjCTypes.ExternalProtocolPtrTy); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1765 | } |
| 1766 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1767 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1768 | // FIXME: We shouldn't need this, the protocol decl should contain enough |
| 1769 | // information to tell us whether this was a declaration or a definition. |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1770 | DefinedProtocols.insert(PD->getIdentifier()); |
| 1771 | |
| 1772 | // If we have generated a forward reference to this protocol, emit |
| 1773 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 1774 | // emitted. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1775 | if (Protocols.count(PD->getIdentifier())) |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1776 | GetOrEmitProtocol(PD); |
| 1777 | } |
| 1778 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1779 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1780 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 1781 | return GetOrEmitProtocol(PD); |
| 1782 | return GetOrEmitProtocolRef(PD); |
| 1783 | } |
| 1784 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1785 | /* |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1786 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 1787 | struct _objc_protocol { |
| 1788 | struct _objc_protocol_extension *isa; |
| 1789 | char *protocol_name; |
| 1790 | struct _objc_protocol_list *protocol_list; |
| 1791 | struct _objc__method_prototype_list *instance_methods; |
| 1792 | struct _objc__method_prototype_list *class_methods |
| 1793 | }; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1794 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1795 | See EmitProtocolExtension(). |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1796 | */ |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1797 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
| 1798 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1799 | |
| 1800 | // Early exit if a defining object has already been generated. |
| 1801 | if (Entry && Entry->hasInitializer()) |
| 1802 | return Entry; |
| 1803 | |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1804 | // FIXME: I don't understand why gcc generates this, or where it is |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1805 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1806 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1807 | |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1808 | // Construct method lists. |
| 1809 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1810 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1811 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1812 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1813 | ObjCMethodDecl *MD = *i; |
| 1814 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1815 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1816 | OptInstanceMethods.push_back(C); |
| 1817 | } else { |
| 1818 | InstanceMethods.push_back(C); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1819 | } |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1822 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1823 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1824 | ObjCMethodDecl *MD = *i; |
| 1825 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1826 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1827 | OptClassMethods.push_back(C); |
| 1828 | } else { |
| 1829 | ClassMethods.push_back(C); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1830 | } |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1833 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1834 | Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1835 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1836 | Values[2] = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1837 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(), |
Daniel Dunbar | dec75f8 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1838 | PD->protocol_begin(), |
| 1839 | PD->protocol_end()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1840 | Values[3] = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1841 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(), |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1842 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1843 | InstanceMethods); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1844 | Values[4] = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1845 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(), |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1846 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1847 | ClassMethods); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1848 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1849 | Values); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1850 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1851 | if (Entry) { |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1852 | // Already created, fix the linkage and update the initializer. |
| 1853 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1854 | Entry->setInitializer(Init); |
| 1855 | } else { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1856 | Entry = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1857 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1858 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1859 | Init, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1860 | "\01L_OBJC_PROTOCOL_" + PD->getName()); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1861 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1862 | // FIXME: Is this necessary? Why only for protocol? |
| 1863 | Entry->setAlignment(4); |
| 1864 | } |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 1865 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1866 | |
| 1867 | return Entry; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1868 | } |
| 1869 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1870 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1871 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1872 | |
| 1873 | if (!Entry) { |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1874 | // We use the initializer as a marker of whether this is a forward |
| 1875 | // reference or not. At module finalization we add the empty |
| 1876 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1877 | Entry = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1878 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1879 | llvm::GlobalValue::ExternalLinkage, |
| 1880 | 0, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1881 | "\01L_OBJC_PROTOCOL_" + PD->getName()); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1882 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1883 | // FIXME: Is this necessary? Why only for protocol? |
| 1884 | Entry->setAlignment(4); |
| 1885 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1886 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1887 | return Entry; |
| 1888 | } |
| 1889 | |
| 1890 | /* |
| 1891 | struct _objc_protocol_extension { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1892 | uint32_t size; |
| 1893 | struct objc_method_description_list *optional_instance_methods; |
| 1894 | struct objc_method_description_list *optional_class_methods; |
| 1895 | struct objc_property_list *instance_properties; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1896 | }; |
| 1897 | */ |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1898 | llvm::Constant * |
| 1899 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1900 | const ConstantVector &OptInstanceMethods, |
| 1901 | const ConstantVector &OptClassMethods) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1902 | uint64_t Size = |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1903 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1904 | std::vector<llvm::Constant*> Values(4); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1905 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1906 | Values[1] = |
| 1907 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1908 | + PD->getName(), |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1909 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1910 | OptInstanceMethods); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1911 | Values[2] = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1912 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(), |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1913 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1914 | OptClassMethods); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1915 | Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1916 | 0, PD, ObjCTypes); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1917 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1918 | // Return null if no extension bits are used. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1919 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1920 | Values[3]->isNullValue()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1921 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1922 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1923 | llvm::Constant *Init = |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1924 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1925 | |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1926 | // No special section, but goes in llvm.used |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1927 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1928 | Init, |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1929 | 0, 0, true); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1930 | } |
| 1931 | |
| 1932 | /* |
| 1933 | struct objc_protocol_list { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1934 | struct objc_protocol_list *next; |
| 1935 | long count; |
| 1936 | Protocol *list[]; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1937 | }; |
| 1938 | */ |
Daniel Dunbar | dec75f8 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1939 | llvm::Constant * |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1940 | CGObjCMac::EmitProtocolList(llvm::Twine Name, |
Daniel Dunbar | dec75f8 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1941 | ObjCProtocolDecl::protocol_iterator begin, |
| 1942 | ObjCProtocolDecl::protocol_iterator end) { |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1943 | std::vector<llvm::Constant*> ProtocolRefs; |
| 1944 | |
Daniel Dunbar | dec75f8 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1945 | for (; begin != end; ++begin) |
| 1946 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1947 | |
| 1948 | // Just return null for empty protocol lists |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1949 | if (ProtocolRefs.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1950 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1951 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1952 | // This list is null terminated. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1953 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1954 | |
| 1955 | std::vector<llvm::Constant*> Values(3); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1956 | // This field is only used by the runtime. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1957 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1958 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1959 | ProtocolRefs.size() - 1); |
| 1960 | Values[2] = |
| 1961 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 1962 | ProtocolRefs.size()), |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1963 | ProtocolRefs); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1964 | |
Nick Lewycky | 41eaf0a | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 1965 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1966 | llvm::GlobalVariable *GV = |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1967 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | ae33384 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1968 | 4, false); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1969 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 1972 | void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet, |
| 1973 | std::vector<llvm::Constant*> &Properties, |
| 1974 | const Decl *Container, |
| 1975 | const ObjCProtocolDecl *PROTO, |
| 1976 | const ObjCCommonTypesHelper &ObjCTypes) { |
| 1977 | std::vector<llvm::Constant*> Prop(2); |
| 1978 | for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(), |
| 1979 | E = PROTO->protocol_end(); P != E; ++P) |
| 1980 | PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes); |
| 1981 | for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(), |
| 1982 | E = PROTO->prop_end(); I != E; ++I) { |
| 1983 | const ObjCPropertyDecl *PD = *I; |
| 1984 | if (!PropertySet.insert(PD->getIdentifier())) |
| 1985 | continue; |
| 1986 | Prop[0] = GetPropertyName(PD->getIdentifier()); |
| 1987 | Prop[1] = GetPropertyTypeString(PD, Container); |
| 1988 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop)); |
| 1989 | } |
| 1990 | } |
| 1991 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1992 | /* |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1993 | struct _objc_property { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1994 | const char * const name; |
| 1995 | const char * const attributes; |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1996 | }; |
| 1997 | |
| 1998 | struct _objc_property_list { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1999 | uint32_t entsize; // sizeof (struct _objc_property) |
| 2000 | uint32_t prop_count; |
| 2001 | struct _objc_property[prop_count]; |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2002 | }; |
| 2003 | */ |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2004 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2005 | const Decl *Container, |
| 2006 | const ObjCContainerDecl *OCD, |
| 2007 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2008 | std::vector<llvm::Constant*> Properties, Prop(2); |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2009 | llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2010 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(), |
| 2011 | E = OCD->prop_end(); I != E; ++I) { |
Steve Naroff | ba3dc38 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 2012 | const ObjCPropertyDecl *PD = *I; |
Fariborz Jahanian | 751c1e7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2013 | PropertySet.insert(PD->getIdentifier()); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2014 | Prop[0] = GetPropertyName(PD->getIdentifier()); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2015 | Prop[1] = GetPropertyTypeString(PD, Container); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2016 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2017 | Prop)); |
| 2018 | } |
Fariborz Jahanian | 7966aff | 2010-06-22 16:33:55 +0000 | [diff] [blame] | 2019 | if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) { |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 2020 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 2021 | P = OID->all_referenced_protocol_begin(), |
| 2022 | E = OID->all_referenced_protocol_end(); P != E; ++P) |
Fariborz Jahanian | 7966aff | 2010-06-22 16:33:55 +0000 | [diff] [blame] | 2023 | PushProtocolProperties(PropertySet, Properties, Container, (*P), |
| 2024 | ObjCTypes); |
| 2025 | } |
| 2026 | else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) { |
| 2027 | for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(), |
| 2028 | E = CD->protocol_end(); P != E; ++P) |
| 2029 | PushProtocolProperties(PropertySet, Properties, Container, (*P), |
| 2030 | ObjCTypes); |
| 2031 | } |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2032 | |
| 2033 | // Return null for empty list. |
| 2034 | if (Properties.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2035 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2036 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2037 | unsigned PropertySize = |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2038 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2039 | std::vector<llvm::Constant*> Values(3); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2040 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 2041 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2042 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2043 | Properties.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2044 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
Nick Lewycky | 41eaf0a | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 2045 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2046 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2047 | llvm::GlobalVariable *GV = |
| 2048 | CreateMetadataVar(Name, Init, |
| 2049 | (ObjCABI == 2) ? "__DATA, __objc_const" : |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2050 | "__OBJC,__property,regular,no_dead_strip", |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2051 | (ObjCABI == 2) ? 8 : 4, |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2052 | true); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2053 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | /* |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2057 | struct objc_method_description_list { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2058 | int count; |
| 2059 | struct objc_method_description list[]; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2060 | }; |
| 2061 | */ |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2062 | llvm::Constant * |
| 2063 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 2064 | std::vector<llvm::Constant*> Desc(2); |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2065 | Desc[0] = |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2066 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 2067 | ObjCTypes.SelectorPtrTy); |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2068 | Desc[1] = GetMethodVarType(MD); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2069 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2070 | Desc); |
| 2071 | } |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2072 | |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2073 | llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name, |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2074 | const char *Section, |
| 2075 | const ConstantVector &Methods) { |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2076 | // Return null for empty list. |
| 2077 | if (Methods.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2078 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2079 | |
| 2080 | std::vector<llvm::Constant*> Values(2); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2081 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2082 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2083 | Methods.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2084 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
Nick Lewycky | 41eaf0a | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 2085 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2086 | |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2087 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2088 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2089 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2092 | /* |
| 2093 | struct _objc_category { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2094 | char *category_name; |
| 2095 | char *class_name; |
| 2096 | struct _objc_method_list *instance_methods; |
| 2097 | struct _objc_method_list *class_methods; |
| 2098 | struct _objc_protocol_list *protocols; |
| 2099 | uint32_t size; // <rdar://4585769> |
| 2100 | struct _objc_property_list *instance_properties; |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2101 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2102 | */ |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2103 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2104 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2105 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2106 | // FIXME: This is poor design, the OCD should have a pointer to the category |
| 2107 | // decl. Additionally, note that Category can be null for the @implementation |
| 2108 | // w/o an @interface case. Sema should just create one for us as it does for |
| 2109 | // @implementation so everyone else can live life under a clear blue sky. |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2110 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2111 | const ObjCCategoryDecl *Category = |
Daniel Dunbar | 28e76ca | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2112 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2113 | |
| 2114 | llvm::SmallString<256> ExtName; |
| 2115 | llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_' |
| 2116 | << OCD->getName(); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2117 | |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2118 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2119 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2120 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2121 | // Instance methods should always be defined. |
| 2122 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 2123 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2124 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2125 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2126 | // Class methods should always be defined. |
| 2127 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 2128 | } |
| 2129 | |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2130 | std::vector<llvm::Constant*> Values(7); |
| 2131 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 2132 | Values[1] = GetClassName(Interface->getIdentifier()); |
Fariborz Jahanian | e55f866 | 2009-04-29 20:40:05 +0000 | [diff] [blame] | 2133 | LazySymbols.insert(Interface->getIdentifier()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2134 | Values[2] = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2135 | EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(), |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2136 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2137 | InstanceMethods); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2138 | Values[3] = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2139 | EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(), |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2140 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2141 | ClassMethods); |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2142 | if (Category) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2143 | Values[4] = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2144 | EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(), |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2145 | Category->protocol_begin(), |
| 2146 | Category->protocol_end()); |
| 2147 | } else { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2148 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2149 | } |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2150 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 28e76ca | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2151 | |
| 2152 | // If there is no category @interface then there can be no properties. |
| 2153 | if (Category) { |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2154 | Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(), |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2155 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 28e76ca | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2156 | } else { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2157 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | 28e76ca | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2158 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2159 | |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2160 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2161 | Values); |
| 2162 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2163 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2164 | CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init, |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2165 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | ae33384 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2166 | 4, true); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2167 | DefinedCategories.push_back(GV); |
Fariborz Jahanian | 9adb2e6 | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 2168 | DefinedCategoryNames.insert(ExtName.str()); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2169 | } |
| 2170 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2171 | // FIXME: Get from somewhere? |
| 2172 | enum ClassFlags { |
| 2173 | eClassFlags_Factory = 0x00001, |
| 2174 | eClassFlags_Meta = 0x00002, |
| 2175 | // <rdr://5142207> |
| 2176 | eClassFlags_HasCXXStructors = 0x02000, |
| 2177 | eClassFlags_Hidden = 0x20000, |
| 2178 | eClassFlags_ABI2_Hidden = 0x00010, |
| 2179 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 2180 | }; |
| 2181 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2182 | /* |
| 2183 | struct _objc_class { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2184 | Class isa; |
| 2185 | Class super_class; |
| 2186 | const char *name; |
| 2187 | long version; |
| 2188 | long info; |
| 2189 | long instance_size; |
| 2190 | struct _objc_ivar_list *ivars; |
| 2191 | struct _objc_method_list *methods; |
| 2192 | struct _objc_cache *cache; |
| 2193 | struct _objc_protocol_list *protocols; |
| 2194 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 2195 | const char *ivar_layout; |
| 2196 | struct _objc_class_ext *ext; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2197 | }; |
| 2198 | |
| 2199 | See EmitClassExtension(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2200 | */ |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2201 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2202 | DefinedSymbols.insert(ID->getIdentifier()); |
| 2203 | |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2204 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2205 | // FIXME: Gross |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2206 | ObjCInterfaceDecl *Interface = |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2207 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2208 | llvm::Constant *Protocols = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2209 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(), |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 2210 | Interface->all_referenced_protocol_begin(), |
| 2211 | Interface->all_referenced_protocol_end()); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2212 | unsigned Flags = eClassFlags_Factory; |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2213 | if (ID->getNumIvarInitializers()) |
| 2214 | Flags |= eClassFlags_HasCXXStructors; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2215 | unsigned Size = |
Daniel Dunbar | 12119b9 | 2009-05-03 10:46:44 +0000 | [diff] [blame] | 2216 | CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2217 | |
| 2218 | // FIXME: Set CXX-structors flag. |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 2219 | if (ID->getClassInterface()->getVisibility() == HiddenVisibility) |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2220 | Flags |= eClassFlags_Hidden; |
| 2221 | |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2222 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2223 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2224 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2225 | // Instance methods should always be defined. |
| 2226 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 2227 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2228 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2229 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2230 | // Class methods should always be defined. |
| 2231 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 2232 | } |
| 2233 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2234 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2235 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2236 | ObjCPropertyImplDecl *PID = *i; |
| 2237 | |
| 2238 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 2239 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 2240 | |
| 2241 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 2242 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 2243 | InstanceMethods.push_back(C); |
| 2244 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 2245 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 2246 | InstanceMethods.push_back(C); |
| 2247 | } |
| 2248 | } |
| 2249 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2250 | std::vector<llvm::Constant*> Values(12); |
Daniel Dunbar | ccf6183 | 2009-05-03 08:56:52 +0000 | [diff] [blame] | 2251 | Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2252 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2253 | // Record a reference to the super class. |
| 2254 | LazySymbols.insert(Super->getIdentifier()); |
| 2255 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2256 | Values[ 1] = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2257 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2258 | ObjCTypes.ClassPtrTy); |
| 2259 | } else { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2260 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2261 | } |
| 2262 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 2263 | // Version is always 0. |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2264 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2265 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 2266 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | b042a59 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2267 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2268 | Values[ 7] = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2269 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(), |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2270 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2271 | InstanceMethods); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2272 | // cache is always NULL. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2273 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2274 | Values[ 9] = Protocols; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2275 | Values[10] = BuildIvarLayout(ID, true); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2276 | Values[11] = EmitClassExtension(ID); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2277 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2278 | Values); |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 2279 | std::string Name("\01L_OBJC_CLASS_"); |
| 2280 | Name += ClassName; |
| 2281 | const char *Section = "__OBJC,__class,regular,no_dead_strip"; |
| 2282 | // Check for a forward reference. |
| 2283 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 2284 | if (GV) { |
| 2285 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2286 | "Forward metaclass reference has incorrect type."); |
| 2287 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 2288 | GV->setInitializer(Init); |
| 2289 | GV->setSection(Section); |
| 2290 | GV->setAlignment(4); |
| 2291 | CGM.AddUsedGlobal(GV); |
| 2292 | } |
| 2293 | else |
| 2294 | GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2295 | DefinedClasses.push_back(GV); |
| 2296 | } |
| 2297 | |
| 2298 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 2299 | llvm::Constant *Protocols, |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2300 | const ConstantVector &Methods) { |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2301 | unsigned Flags = eClassFlags_Meta; |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2302 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2303 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 2304 | if (ID->getClassInterface()->getVisibility() == HiddenVisibility) |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2305 | Flags |= eClassFlags_Hidden; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2306 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2307 | std::vector<llvm::Constant*> Values(12); |
| 2308 | // The isa for the metaclass is the root of the hierarchy. |
| 2309 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 2310 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 2311 | Root = Super; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2312 | Values[ 0] = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2313 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2314 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2315 | // The super class for the metaclass is emitted as the name of the |
| 2316 | // super class. The runtime fixes this up to point to the |
| 2317 | // *metaclass* for the super class. |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2318 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2319 | Values[ 1] = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2320 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2321 | ObjCTypes.ClassPtrTy); |
| 2322 | } else { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2323 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2324 | } |
| 2325 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 2326 | // Version is always 0. |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2327 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2328 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 2329 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | b042a59 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2330 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2331 | Values[ 7] = |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2332 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2333 | "__OBJC,__cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2334 | Methods); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2335 | // cache is always NULL. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2336 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2337 | Values[ 9] = Protocols; |
| 2338 | // ivar_layout for metaclass is always NULL. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2339 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2340 | // The class extension is always unused for metaclasses. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2341 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2342 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2343 | Values); |
| 2344 | |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2345 | std::string Name("\01L_OBJC_METACLASS_"); |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2346 | Name += ID->getNameAsCString(); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2347 | |
| 2348 | // Check for a forward reference. |
| 2349 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 2350 | if (GV) { |
| 2351 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2352 | "Forward metaclass reference has incorrect type."); |
| 2353 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 2354 | GV->setInitializer(Init); |
| 2355 | } else { |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2356 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2357 | llvm::GlobalValue::InternalLinkage, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2358 | Init, Name); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2359 | } |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2360 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Daniel Dunbar | ae33384 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2361 | GV->setAlignment(4); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2362 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2363 | |
| 2364 | return GV; |
| 2365 | } |
| 2366 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2367 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2368 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2369 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2370 | // FIXME: Should we look these up somewhere other than the module. Its a bit |
| 2371 | // silly since we only generate these while processing an implementation, so |
| 2372 | // exactly one pointer would work if know when we entered/exitted an |
| 2373 | // implementation block. |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2374 | |
| 2375 | // Check for an existing forward reference. |
Fariborz Jahanian | 475831b | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 2376 | // Previously, metaclass with internal linkage may have been defined. |
| 2377 | // pass 'true' as 2nd argument so it is returned. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2378 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, |
| 2379 | true)) { |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2380 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2381 | "Forward metaclass reference has incorrect type."); |
| 2382 | return GV; |
| 2383 | } else { |
| 2384 | // Generate as an external reference to keep a consistent |
| 2385 | // module. This will be patched up when we emit the metaclass. |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2386 | return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2387 | llvm::GlobalValue::ExternalLinkage, |
| 2388 | 0, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2389 | Name); |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2390 | } |
| 2391 | } |
| 2392 | |
Fariborz Jahanian | eb80c98 | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 2393 | llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) { |
| 2394 | std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString(); |
| 2395 | |
| 2396 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, |
| 2397 | true)) { |
| 2398 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2399 | "Forward class metadata reference has incorrect type."); |
| 2400 | return GV; |
| 2401 | } else { |
| 2402 | return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
| 2403 | llvm::GlobalValue::ExternalLinkage, |
| 2404 | 0, |
| 2405 | Name); |
| 2406 | } |
| 2407 | } |
| 2408 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2409 | /* |
| 2410 | struct objc_class_ext { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2411 | uint32_t size; |
| 2412 | const char *weak_ivar_layout; |
| 2413 | struct _objc_property_list *properties; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2414 | }; |
| 2415 | */ |
| 2416 | llvm::Constant * |
| 2417 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2418 | uint64_t Size = |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2419 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2420 | |
| 2421 | std::vector<llvm::Constant*> Values(3); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2422 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | 6df6986 | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 2423 | Values[1] = BuildIvarLayout(ID, false); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2424 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(), |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2425 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2426 | |
| 2427 | // Return null if no extension bits are used. |
| 2428 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2429 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2430 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2431 | llvm::Constant *Init = |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2432 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2433 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2434 | Init, "__OBJC,__class_ext,regular,no_dead_strip", |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2435 | 4, true); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2436 | } |
| 2437 | |
| 2438 | /* |
| 2439 | struct objc_ivar { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2440 | char *ivar_name; |
| 2441 | char *ivar_type; |
| 2442 | int ivar_offset; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2443 | }; |
| 2444 | |
| 2445 | struct objc_ivar_list { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2446 | int ivar_count; |
| 2447 | struct objc_ivar list[count]; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2448 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2449 | */ |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2450 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | b042a59 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2451 | bool ForClass) { |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2452 | std::vector<llvm::Constant*> Ivars, Ivar(3); |
| 2453 | |
| 2454 | // When emitting the root class GCC emits ivar entries for the |
| 2455 | // actual class structure. It is not clear if we need to follow this |
| 2456 | // behavior; for now lets try and get away with not doing it. If so, |
| 2457 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 2458 | // for the class. |
| 2459 | if (ForClass) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2460 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2461 | |
| 2462 | ObjCInterfaceDecl *OID = |
Fariborz Jahanian | b042a59 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2463 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2464 | |
Daniel Dunbar | f5c1846 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 2465 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 2466 | CGM.getContext().ShallowCollectObjCIvars(OID, OIvars); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2467 | |
Daniel Dunbar | f5c1846 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 2468 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 2469 | ObjCIvarDecl *IVD = OIvars[i]; |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 2470 | // Ignore unnamed bit-fields. |
| 2471 | if (!IVD->getDeclName()) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2472 | continue; |
Daniel Dunbar | 725dc2c | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 2473 | Ivar[0] = GetMethodVarName(IVD->getIdentifier()); |
| 2474 | Ivar[1] = GetMethodVarType(IVD); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2475 | Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, |
Daniel Dunbar | 9fd114d | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2476 | ComputeIvarBaseOffset(CGM, OID, IVD)); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2477 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2478 | } |
| 2479 | |
| 2480 | // Return null for empty list. |
| 2481 | if (Ivars.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2482 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2483 | |
| 2484 | std::vector<llvm::Constant*> Values(2); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2485 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2486 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2487 | Ivars.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2488 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
Nick Lewycky | 41eaf0a | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 2489 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2490 | |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2491 | llvm::GlobalVariable *GV; |
| 2492 | if (ForClass) |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2493 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2494 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
Daniel Dunbar | ae33384 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2495 | 4, true); |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2496 | else |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2497 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(), |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2498 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2499 | 4, true); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2500 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2501 | } |
| 2502 | |
| 2503 | /* |
| 2504 | struct objc_method { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2505 | SEL method_name; |
| 2506 | char *method_types; |
| 2507 | void *method; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2508 | }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2509 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2510 | struct objc_method_list { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2511 | struct objc_method_list *obsolete; |
| 2512 | int count; |
| 2513 | struct objc_method methods_list[count]; |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2514 | }; |
| 2515 | */ |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2516 | |
| 2517 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 2518 | /// given method if it has been defined. The result is null if the |
| 2519 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2520 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Argyrios Kyrtzidis | 13257c5 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 2521 | llvm::Function *Fn = GetMethodDefinition(MD); |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2522 | if (!Fn) |
| 2523 | return 0; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2524 | |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2525 | std::vector<llvm::Constant*> Method(3); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2526 | Method[0] = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2527 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2528 | ObjCTypes.SelectorPtrTy); |
| 2529 | Method[1] = GetMethodVarType(MD); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2530 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2531 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2534 | llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name, |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2535 | const char *Section, |
Daniel Dunbar | eb1f9a2 | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2536 | const ConstantVector &Methods) { |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2537 | // Return null for empty list. |
| 2538 | if (Methods.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2539 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2540 | |
| 2541 | std::vector<llvm::Constant*> Values(3); |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2542 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2543 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2544 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2545 | Methods.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2546 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
Nick Lewycky | 41eaf0a | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 2547 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2548 | |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2549 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2550 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2551 | ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2552 | } |
| 2553 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 2554 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2555 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 2556 | llvm::SmallString<256> Name; |
Fariborz Jahanian | 0196a1c | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 2557 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2558 | |
Daniel Dunbar | bf8c24a | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2559 | CodeGenTypes &Types = CGM.getTypes(); |
Daniel Dunbar | 7a95ca3 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2560 | const llvm::FunctionType *MethodTy = |
Daniel Dunbar | bf8c24a | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2561 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2562 | llvm::Function *Method = |
Daniel Dunbar | 7a95ca3 | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2563 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2564 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 2565 | Name.str(), |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2566 | &CGM.getModule()); |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2567 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2568 | |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2569 | return Method; |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2572 | llvm::GlobalVariable * |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2573 | CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name, |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2574 | llvm::Constant *Init, |
| 2575 | const char *Section, |
Daniel Dunbar | 463cc8a | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2576 | unsigned Align, |
| 2577 | bool AddToUsed) { |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2578 | const llvm::Type *Ty = Init->getType(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2579 | llvm::GlobalVariable *GV = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2580 | new llvm::GlobalVariable(CGM.getModule(), Ty, false, |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2581 | llvm::GlobalValue::InternalLinkage, Init, Name); |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2582 | if (Section) |
| 2583 | GV->setSection(Section); |
Daniel Dunbar | 463cc8a | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2584 | if (Align) |
| 2585 | GV->setAlignment(Align); |
| 2586 | if (AddToUsed) |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2587 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 30c6536 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2588 | return GV; |
| 2589 | } |
| 2590 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2591 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2592 | // Abuse this interface function as a place to finalize. |
| 2593 | FinishModule(); |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2594 | return NULL; |
| 2595 | } |
| 2596 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2597 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2598 | return ObjCTypes.getGetPropertyFn(); |
Daniel Dunbar | a91c3e0 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2599 | } |
| 2600 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2601 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2602 | return ObjCTypes.getSetPropertyFn(); |
Daniel Dunbar | a91c3e0 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2603 | } |
| 2604 | |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 2605 | llvm::Constant *CGObjCMac::GetGetStructFunction() { |
| 2606 | return ObjCTypes.getCopyStructFn(); |
| 2607 | } |
| 2608 | llvm::Constant *CGObjCMac::GetSetStructFunction() { |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 2609 | return ObjCTypes.getCopyStructFn(); |
| 2610 | } |
| 2611 | |
Chris Lattner | d480892 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2612 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2613 | return ObjCTypes.getEnumerationMutationFn(); |
Anders Carlsson | 3f35a26 | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2616 | void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) { |
| 2617 | return EmitTryOrSynchronizedStmt(CGF, S); |
| 2618 | } |
| 2619 | |
| 2620 | void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF, |
| 2621 | const ObjCAtSynchronizedStmt &S) { |
| 2622 | return EmitTryOrSynchronizedStmt(CGF, S); |
| 2623 | } |
| 2624 | |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2625 | namespace { |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 2626 | struct PerformFragileFinally : EHScopeStack::Cleanup { |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2627 | const Stmt &S; |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2628 | llvm::Value *SyncArgSlot; |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2629 | llvm::Value *CallTryExitVar; |
| 2630 | llvm::Value *ExceptionData; |
| 2631 | ObjCTypesHelper &ObjCTypes; |
| 2632 | PerformFragileFinally(const Stmt *S, |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2633 | llvm::Value *SyncArgSlot, |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2634 | llvm::Value *CallTryExitVar, |
| 2635 | llvm::Value *ExceptionData, |
| 2636 | ObjCTypesHelper *ObjCTypes) |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2637 | : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar), |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2638 | ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {} |
| 2639 | |
| 2640 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 2641 | // Check whether we need to call objc_exception_try_exit. |
| 2642 | // In optimized code, this branch will always be folded. |
| 2643 | llvm::BasicBlock *FinallyCallExit = |
| 2644 | CGF.createBasicBlock("finally.call_exit"); |
| 2645 | llvm::BasicBlock *FinallyNoCallExit = |
| 2646 | CGF.createBasicBlock("finally.no_call_exit"); |
| 2647 | CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar), |
| 2648 | FinallyCallExit, FinallyNoCallExit); |
| 2649 | |
| 2650 | CGF.EmitBlock(FinallyCallExit); |
| 2651 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData) |
| 2652 | ->setDoesNotThrow(); |
| 2653 | |
| 2654 | CGF.EmitBlock(FinallyNoCallExit); |
| 2655 | |
| 2656 | if (isa<ObjCAtTryStmt>(S)) { |
| 2657 | if (const ObjCAtFinallyStmt* FinallyStmt = |
John McCall | cebe0ca | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 2658 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) { |
| 2659 | // Save the current cleanup destination in case there's |
| 2660 | // control flow inside the finally statement. |
| 2661 | llvm::Value *CurCleanupDest = |
| 2662 | CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot()); |
| 2663 | |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2664 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 2665 | |
John McCall | cebe0ca | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 2666 | if (CGF.HaveInsertPoint()) { |
| 2667 | CGF.Builder.CreateStore(CurCleanupDest, |
| 2668 | CGF.getNormalCleanupDestSlot()); |
| 2669 | } else { |
| 2670 | // Currently, the end of the cleanup must always exist. |
| 2671 | CGF.EnsureInsertPoint(); |
| 2672 | } |
| 2673 | } |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2674 | } else { |
| 2675 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 2676 | // @synchronized. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2677 | llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot); |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2678 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg) |
| 2679 | ->setDoesNotThrow(); |
| 2680 | } |
| 2681 | } |
| 2682 | }; |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2683 | |
| 2684 | class FragileHazards { |
| 2685 | CodeGenFunction &CGF; |
| 2686 | llvm::SmallVector<llvm::Value*, 20> Locals; |
| 2687 | llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry; |
| 2688 | |
| 2689 | llvm::InlineAsm *ReadHazard; |
| 2690 | llvm::InlineAsm *WriteHazard; |
| 2691 | |
| 2692 | llvm::FunctionType *GetAsmFnType(); |
| 2693 | |
| 2694 | void collectLocals(); |
| 2695 | void emitReadHazard(CGBuilderTy &Builder); |
| 2696 | |
| 2697 | public: |
| 2698 | FragileHazards(CodeGenFunction &CGF); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2699 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2700 | void emitWriteHazard(); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2701 | void emitHazardsInNewBlocks(); |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2702 | }; |
| 2703 | } |
| 2704 | |
| 2705 | /// Create the fragile-ABI read and write hazards based on the current |
| 2706 | /// state of the function, which is presumed to be immediately prior |
| 2707 | /// to a @try block. These hazards are used to maintain correct |
| 2708 | /// semantics in the face of optimization and the fragile ABI's |
| 2709 | /// cavalier use of setjmp/longjmp. |
| 2710 | FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) { |
| 2711 | collectLocals(); |
| 2712 | |
| 2713 | if (Locals.empty()) return; |
| 2714 | |
| 2715 | // Collect all the blocks in the function. |
| 2716 | for (llvm::Function::iterator |
| 2717 | I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I) |
| 2718 | BlocksBeforeTry.insert(&*I); |
| 2719 | |
| 2720 | llvm::FunctionType *AsmFnTy = GetAsmFnType(); |
| 2721 | |
| 2722 | // Create a read hazard for the allocas. This inhibits dead-store |
| 2723 | // optimizations and forces the values to memory. This hazard is |
| 2724 | // inserted before any 'throwing' calls in the protected scope to |
| 2725 | // reflect the possibility that the variables might be read from the |
| 2726 | // catch block if the call throws. |
| 2727 | { |
| 2728 | std::string Constraint; |
| 2729 | for (unsigned I = 0, E = Locals.size(); I != E; ++I) { |
| 2730 | if (I) Constraint += ','; |
| 2731 | Constraint += "*m"; |
| 2732 | } |
| 2733 | |
| 2734 | ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false); |
| 2735 | } |
| 2736 | |
| 2737 | // Create a write hazard for the allocas. This inhibits folding |
| 2738 | // loads across the hazard. This hazard is inserted at the |
| 2739 | // beginning of the catch path to reflect the possibility that the |
| 2740 | // variables might have been written within the protected scope. |
| 2741 | { |
| 2742 | std::string Constraint; |
| 2743 | for (unsigned I = 0, E = Locals.size(); I != E; ++I) { |
| 2744 | if (I) Constraint += ','; |
| 2745 | Constraint += "=*m"; |
| 2746 | } |
| 2747 | |
| 2748 | WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false); |
| 2749 | } |
| 2750 | } |
| 2751 | |
| 2752 | /// Emit a write hazard at the current location. |
| 2753 | void FragileHazards::emitWriteHazard() { |
| 2754 | if (Locals.empty()) return; |
| 2755 | |
| 2756 | CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end()) |
| 2757 | ->setDoesNotThrow(); |
| 2758 | } |
| 2759 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2760 | void FragileHazards::emitReadHazard(CGBuilderTy &Builder) { |
| 2761 | assert(!Locals.empty()); |
| 2762 | Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end()) |
| 2763 | ->setDoesNotThrow(); |
| 2764 | } |
| 2765 | |
| 2766 | /// Emit read hazards in all the protected blocks, i.e. all the blocks |
| 2767 | /// which have been inserted since the beginning of the try. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2768 | void FragileHazards::emitHazardsInNewBlocks() { |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2769 | if (Locals.empty()) return; |
| 2770 | |
| 2771 | CGBuilderTy Builder(CGF.getLLVMContext()); |
| 2772 | |
| 2773 | // Iterate through all blocks, skipping those prior to the try. |
| 2774 | for (llvm::Function::iterator |
| 2775 | FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) { |
| 2776 | llvm::BasicBlock &BB = *FI; |
| 2777 | if (BlocksBeforeTry.count(&BB)) continue; |
| 2778 | |
| 2779 | // Walk through all the calls in the block. |
| 2780 | for (llvm::BasicBlock::iterator |
| 2781 | BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) { |
| 2782 | llvm::Instruction &I = *BI; |
| 2783 | |
| 2784 | // Ignore instructions that aren't non-intrinsic calls. |
| 2785 | // These are the only calls that can possibly call longjmp. |
| 2786 | if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue; |
| 2787 | if (isa<llvm::IntrinsicInst>(I)) |
| 2788 | continue; |
| 2789 | |
| 2790 | // Ignore call sites marked nounwind. This may be questionable, |
| 2791 | // since 'nounwind' doesn't necessarily mean 'does not call longjmp'. |
| 2792 | llvm::CallSite CS(&I); |
| 2793 | if (CS.doesNotThrow()) continue; |
| 2794 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2795 | // Insert a read hazard before the call. This will ensure that |
| 2796 | // any writes to the locals are performed before making the |
| 2797 | // call. If the call throws, then this is sufficient to |
| 2798 | // guarantee correctness as long as it doesn't also write to any |
| 2799 | // locals. |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2800 | Builder.SetInsertPoint(&BB, BI); |
| 2801 | emitReadHazard(Builder); |
| 2802 | } |
| 2803 | } |
| 2804 | } |
| 2805 | |
| 2806 | static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) { |
| 2807 | if (V) S.insert(V); |
| 2808 | } |
| 2809 | |
| 2810 | void FragileHazards::collectLocals() { |
| 2811 | // Compute a set of allocas to ignore. |
| 2812 | llvm::DenseSet<llvm::Value*> AllocasToIgnore; |
| 2813 | addIfPresent(AllocasToIgnore, CGF.ReturnValue); |
| 2814 | addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest); |
| 2815 | addIfPresent(AllocasToIgnore, CGF.EHCleanupDest); |
| 2816 | |
| 2817 | // Collect all the allocas currently in the function. This is |
| 2818 | // probably way too aggressive. |
| 2819 | llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock(); |
| 2820 | for (llvm::BasicBlock::iterator |
| 2821 | I = Entry.begin(), E = Entry.end(); I != E; ++I) |
| 2822 | if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I)) |
| 2823 | Locals.push_back(&*I); |
| 2824 | } |
| 2825 | |
| 2826 | llvm::FunctionType *FragileHazards::GetAsmFnType() { |
| 2827 | std::vector<const llvm::Type *> Tys(Locals.size()); |
| 2828 | for (unsigned I = 0, E = Locals.size(); I != E; ++I) |
| 2829 | Tys[I] = Locals[I]->getType(); |
| 2830 | return llvm::FunctionType::get(CGF.Builder.getVoidTy(), Tys, false); |
John McCall | 65bea08 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2831 | } |
| 2832 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2833 | /* |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2834 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2835 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 2836 | -- |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2837 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2838 | A catch buffer is a setjmp buffer plus: |
| 2839 | - a pointer to the exception that was caught |
| 2840 | - a pointer to the previous exception data buffer |
| 2841 | - two pointers of reserved storage |
| 2842 | Therefore catch buffers form a stack, with a pointer to the top |
| 2843 | of the stack kept in thread-local storage. |
| 2844 | |
| 2845 | objc_exception_try_enter pushes a catch buffer onto the EH stack. |
| 2846 | objc_exception_try_exit pops the given catch buffer, which is |
| 2847 | required to be the top of the EH stack. |
| 2848 | objc_exception_throw pops the top of the EH stack, writes the |
| 2849 | thrown exception into the appropriate field, and longjmps |
| 2850 | to the setjmp buffer. It crashes the process (with a printf |
| 2851 | and an abort()) if there are no catch buffers on the stack. |
| 2852 | objc_exception_extract just reads the exception pointer out of the |
| 2853 | catch buffer. |
| 2854 | |
| 2855 | There's no reason an implementation couldn't use a light-weight |
| 2856 | setjmp here --- something like __builtin_setjmp, but API-compatible |
| 2857 | with the heavyweight setjmp. This will be more important if we ever |
| 2858 | want to implement correct ObjC/C++ exception interactions for the |
| 2859 | fragile ABI. |
| 2860 | |
| 2861 | Note that for this use of setjmp/longjmp to be correct, we may need |
| 2862 | to mark some local variables volatile: if a non-volatile local |
| 2863 | variable is modified between the setjmp and the longjmp, it has |
| 2864 | indeterminate value. For the purposes of LLVM IR, it may be |
| 2865 | sufficient to make loads and stores within the @try (to variables |
| 2866 | declared outside the @try) volatile. This is necessary for |
| 2867 | optimized correctness, but is not currently being done; this is |
| 2868 | being tracked as rdar://problem/8160285 |
| 2869 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2870 | The basic framework for a @try-catch-finally is as follows: |
| 2871 | { |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2872 | objc_exception_data d; |
| 2873 | id _rethrow = null; |
Anders Carlsson | da0e456 | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2874 | bool _call_try_exit = true; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2875 | |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2876 | objc_exception_try_enter(&d); |
| 2877 | if (!setjmp(d.jmp_buf)) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2878 | ... try body ... |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2879 | } else { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2880 | // exception path |
| 2881 | id _caught = objc_exception_extract(&d); |
| 2882 | |
| 2883 | // enter new try scope for handlers |
| 2884 | if (!setjmp(d.jmp_buf)) { |
| 2885 | ... match exception and execute catch blocks ... |
| 2886 | |
| 2887 | // fell off end, rethrow. |
| 2888 | _rethrow = _caught; |
| 2889 | ... jump-through-finally to finally_rethrow ... |
| 2890 | } else { |
| 2891 | // exception in catch block |
| 2892 | _rethrow = objc_exception_extract(&d); |
| 2893 | _call_try_exit = false; |
| 2894 | ... jump-through-finally to finally_rethrow ... |
| 2895 | } |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2896 | } |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2897 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2898 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2899 | finally: |
Anders Carlsson | da0e456 | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2900 | if (_call_try_exit) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2901 | objc_exception_try_exit(&d); |
Anders Carlsson | da0e456 | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2902 | |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2903 | ... finally block .... |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2904 | ... dispatch to finally destination ... |
| 2905 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2906 | finally_rethrow: |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2907 | objc_exception_throw(_rethrow); |
| 2908 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2909 | finally_end: |
| 2910 | } |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2911 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2912 | This framework differs slightly from the one gcc uses, in that gcc |
| 2913 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 2914 | and if the object should be rethrown. This breaks in the face of |
| 2915 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2916 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2917 | We specialize this framework for a few particular circumstances: |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2918 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2919 | - If there are no catch blocks, then we avoid emitting the second |
| 2920 | exception handling context. |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2921 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2922 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 2923 | e)) we avoid emitting the code to rethrow an uncaught exception. |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2924 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2925 | - FIXME: If there is no @finally block we can do a few more |
| 2926 | simplifications. |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2927 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2928 | Rethrows and Jumps-Through-Finally |
| 2929 | -- |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2930 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2931 | '@throw;' is supported by pushing the currently-caught exception |
| 2932 | onto ObjCEHStack while the @catch blocks are emitted. |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2933 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2934 | Branches through the @finally block are handled with an ordinary |
| 2935 | normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC |
| 2936 | exceptions are not compatible with C++ exceptions, and this is |
| 2937 | hardly the only place where this will go wrong. |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2938 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2939 | @synchronized(expr) { stmt; } is emitted as if it were: |
| 2940 | id synch_value = expr; |
| 2941 | objc_sync_enter(synch_value); |
| 2942 | @try { stmt; } @finally { objc_sync_exit(synch_value); } |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2943 | */ |
| 2944 | |
Fariborz Jahanian | c2ad6dc | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2945 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 2946 | const Stmt &S) { |
| 2947 | bool isTry = isa<ObjCAtTryStmt>(S); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2948 | |
| 2949 | // A destination for the fall-through edges of the catch handlers to |
| 2950 | // jump to. |
| 2951 | CodeGenFunction::JumpDest FinallyEnd = |
| 2952 | CGF.getJumpDestInCurrentScope("finally.end"); |
| 2953 | |
| 2954 | // A destination for the rethrow edge of the catch handlers to jump |
| 2955 | // to. |
| 2956 | CodeGenFunction::JumpDest FinallyRethrow = |
| 2957 | CGF.getJumpDestInCurrentScope("finally.rethrow"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2958 | |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2959 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 2960 | // evaluation of the expression must occur before we enter the |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2961 | // @synchronized. We can't avoid a temp here because we need the |
| 2962 | // value to be preserved. If the backend ever does liveness |
| 2963 | // correctly after setjmp, this will be unnecessary. |
| 2964 | llvm::Value *SyncArgSlot = 0; |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2965 | if (!isTry) { |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2966 | llvm::Value *SyncArg = |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2967 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 2968 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2969 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg) |
| 2970 | ->setDoesNotThrow(); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2971 | |
| 2972 | SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg"); |
| 2973 | CGF.Builder.CreateStore(SyncArg, SyncArgSlot); |
Daniel Dunbar | 94ceb61 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2974 | } |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2975 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2976 | // Allocate memory for the setjmp buffer. This needs to be kept |
| 2977 | // live throughout the try and catch blocks. |
| 2978 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 2979 | "exceptiondata.ptr"); |
| 2980 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2981 | // Create the fragile hazards. Note that this will not capture any |
| 2982 | // of the allocas required for exception processing, but will |
| 2983 | // capture the current basic block (which extends all the way to the |
| 2984 | // setjmp call) as "before the @try". |
| 2985 | FragileHazards Hazards(CGF); |
| 2986 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2987 | // Create a flag indicating whether the cleanup needs to call |
| 2988 | // objc_exception_try_exit. This is true except when |
| 2989 | // - no catches match and we're branching through the cleanup |
| 2990 | // just to rethrow the exception, or |
| 2991 | // - a catch matched and we're falling out of the catch handler. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2992 | // The setjmp-safety rule here is that we should always store to this |
| 2993 | // variable in a place that dominates the branch through the cleanup |
| 2994 | // without passing through any setjmps. |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2995 | llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), |
Anders Carlsson | da0e456 | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2996 | "_call_try_exit"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2997 | |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 2998 | // A slot containing the exception to rethrow. Only needed when we |
| 2999 | // have both a @catch and a @finally. |
| 3000 | llvm::Value *PropagatingExnVar = 0; |
| 3001 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3002 | // Push a normal cleanup to leave the try scope. |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 3003 | CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S, |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3004 | SyncArgSlot, |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 3005 | CallTryExitVar, |
| 3006 | ExceptionData, |
| 3007 | &ObjCTypes); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3008 | |
| 3009 | // Enter a try block: |
| 3010 | // - Call objc_exception_try_enter to push ExceptionData on top of |
| 3011 | // the EH stack. |
| 3012 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData) |
| 3013 | ->setDoesNotThrow(); |
| 3014 | |
| 3015 | // - Call setjmp on the exception data buffer. |
| 3016 | llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0); |
| 3017 | llvm::Value *GEPIndexes[] = { Zero, Zero, Zero }; |
| 3018 | llvm::Value *SetJmpBuffer = |
| 3019 | CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer"); |
| 3020 | llvm::CallInst *SetJmpResult = |
| 3021 | CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result"); |
| 3022 | SetJmpResult->setDoesNotThrow(); |
| 3023 | |
| 3024 | // If setjmp returned 0, enter the protected block; otherwise, |
| 3025 | // branch to the handler. |
Daniel Dunbar | 75283ff | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 3026 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 3027 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3028 | llvm::Value *DidCatch = |
John McCall | cebe0ca | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 3029 | CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception"); |
| 3030 | CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3031 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3032 | // Emit the protected block. |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3033 | CGF.EmitBlock(TryBlock); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3034 | CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3035 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3036 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3037 | |
| 3038 | CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3039 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3040 | // Emit the exception handler block. |
Daniel Dunbar | 7f08678 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 3041 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | b22ff59 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3042 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3043 | // Don't optimize loads of the in-scope locals across this point. |
| 3044 | Hazards.emitWriteHazard(); |
| 3045 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3046 | // For a @synchronized (or a @try with no catches), just branch |
| 3047 | // through the cleanup to the rethrow block. |
| 3048 | if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) { |
| 3049 | // Tell the cleanup not to re-pop the exit. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3050 | CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar); |
Anders Carlsson | bfee7e9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3051 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3052 | |
| 3053 | // Otherwise, we have to match against the caught exceptions. |
| 3054 | } else { |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3055 | // Retrieve the exception object. We may emit multiple blocks but |
| 3056 | // nothing can cross this so the value is already in SSA form. |
| 3057 | llvm::CallInst *Caught = |
| 3058 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 3059 | ExceptionData, "caught"); |
| 3060 | Caught->setDoesNotThrow(); |
| 3061 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3062 | // Push the exception to rethrow onto the EH value stack for the |
| 3063 | // benefit of any @throws in the handlers. |
| 3064 | CGF.ObjCEHValueStack.push_back(Caught); |
| 3065 | |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 3066 | const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3067 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3068 | bool HasFinally = (AtTryStmt->getFinallyStmt() != 0); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3069 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3070 | llvm::BasicBlock *CatchBlock = 0; |
| 3071 | llvm::BasicBlock *CatchHandler = 0; |
| 3072 | if (HasFinally) { |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3073 | // Save the currently-propagating exception before |
| 3074 | // objc_exception_try_enter clears the exception slot. |
| 3075 | PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(), |
| 3076 | "propagating_exception"); |
| 3077 | CGF.Builder.CreateStore(Caught, PropagatingExnVar); |
| 3078 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3079 | // Enter a new exception try block (in case a @catch block |
| 3080 | // throws an exception). |
| 3081 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData) |
| 3082 | ->setDoesNotThrow(); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3083 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3084 | llvm::CallInst *SetJmpResult = |
| 3085 | CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, |
| 3086 | "setjmp.result"); |
| 3087 | SetJmpResult->setDoesNotThrow(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3088 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3089 | llvm::Value *Threw = |
| 3090 | CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception"); |
| 3091 | |
| 3092 | CatchBlock = CGF.createBasicBlock("catch"); |
| 3093 | CatchHandler = CGF.createBasicBlock("catch_for_catch"); |
| 3094 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
| 3095 | |
| 3096 | CGF.EmitBlock(CatchBlock); |
| 3097 | } |
| 3098 | |
| 3099 | CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3100 | |
Daniel Dunbar | b22ff59 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3101 | // Handle catch list. As a special case we check if everything is |
| 3102 | // matched and avoid generating code for falling off the end if |
| 3103 | // so. |
| 3104 | bool AllMatched = false; |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 3105 | for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) { |
| 3106 | const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3107 | |
Douglas Gregor | 46a572b | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 3108 | const VarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3109 | const ObjCObjectPointerType *OPT = 0; |
Daniel Dunbar | 523208f | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 3110 | |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3111 | // catch(...) always matches. |
Daniel Dunbar | b22ff59 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3112 | if (!CatchParam) { |
| 3113 | AllMatched = true; |
| 3114 | } else { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3115 | OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3116 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3117 | // catch(id e) always matches under this ABI, since only |
| 3118 | // ObjC exceptions end up here in the first place. |
Daniel Dunbar | 86919f4 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 3119 | // FIXME: For the time being we also match id<X>; this should |
| 3120 | // be rejected by Sema instead. |
Eli Friedman | 55179ca | 2009-07-11 00:57:02 +0000 | [diff] [blame] | 3121 | if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType())) |
Daniel Dunbar | b22ff59 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3122 | AllMatched = true; |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3123 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3124 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3125 | // If this is a catch-all, we don't need to test anything. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3126 | if (AllMatched) { |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3127 | CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF); |
| 3128 | |
Anders Carlsson | 9396a89 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3129 | if (CatchParam) { |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 3130 | CGF.EmitAutoVarDecl(*CatchParam); |
Daniel Dunbar | 5c7e393 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 3131 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3132 | |
| 3133 | // These types work out because ConvertType(id) == i8*. |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 3134 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | 9396a89 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3135 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3136 | |
Anders Carlsson | 9396a89 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3137 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3138 | |
| 3139 | // The scope of the catch variable ends right here. |
| 3140 | CatchVarCleanups.ForceCleanup(); |
| 3141 | |
Anders Carlsson | bfee7e9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3142 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3143 | break; |
| 3144 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3145 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3146 | assert(OPT && "Unexpected non-object pointer type in @catch"); |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3147 | const ObjCObjectType *ObjTy = OPT->getObjectType(); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3148 | |
| 3149 | // FIXME: @catch (Class c) ? |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3150 | ObjCInterfaceDecl *IDecl = ObjTy->getInterface(); |
| 3151 | assert(IDecl && "Catch parameter must have Objective-C type!"); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3152 | |
| 3153 | // Check if the @catch block matches the exception object. |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3154 | llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3155 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3156 | llvm::CallInst *Match = |
Chris Lattner | c6406db | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 3157 | CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(), |
| 3158 | Class, Caught, "match"); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3159 | Match->setDoesNotThrow(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3160 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3161 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match"); |
| 3162 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3163 | |
| 3164 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | 7f08678 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 3165 | MatchedBlock, NextCatchBlock); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3166 | |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3167 | // Emit the @catch block. |
| 3168 | CGF.EmitBlock(MatchedBlock); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3169 | |
| 3170 | // Collect any cleanups for the catch variable. The scope lasts until |
| 3171 | // the end of the catch body. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3172 | CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3173 | |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 3174 | CGF.EmitAutoVarDecl(*CatchParam); |
Daniel Dunbar | 5c7e393 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 3175 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3176 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3177 | // Initialize the catch variable. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3178 | llvm::Value *Tmp = |
| 3179 | CGF.Builder.CreateBitCast(Caught, |
| 3180 | CGF.ConvertType(CatchParam->getType()), |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3181 | "tmp"); |
Steve Naroff | 371b8fb | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 3182 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3183 | |
Anders Carlsson | 9396a89 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3184 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3185 | |
| 3186 | // We're done with the catch variable. |
| 3187 | CatchVarCleanups.ForceCleanup(); |
| 3188 | |
Anders Carlsson | bfee7e9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3189 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3190 | |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3191 | CGF.EmitBlock(NextCatchBlock); |
| 3192 | } |
| 3193 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3194 | CGF.ObjCEHValueStack.pop_back(); |
| 3195 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3196 | // If nothing wanted anything to do with the caught exception, |
| 3197 | // kill the extract call. |
| 3198 | if (Caught->use_empty()) |
| 3199 | Caught->eraseFromParent(); |
| 3200 | |
| 3201 | if (!AllMatched) |
| 3202 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 3203 | |
| 3204 | if (HasFinally) { |
| 3205 | // Emit the exception handler for the @catch blocks. |
| 3206 | CGF.EmitBlock(CatchHandler); |
| 3207 | |
| 3208 | // In theory we might now need a write hazard, but actually it's |
| 3209 | // unnecessary because there's no local-accessing code between |
| 3210 | // the try's write hazard and here. |
| 3211 | //Hazards.emitWriteHazard(); |
| 3212 | |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3213 | // Extract the new exception and save it to the |
| 3214 | // propagating-exception slot. |
| 3215 | assert(PropagatingExnVar); |
| 3216 | llvm::CallInst *NewCaught = |
| 3217 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 3218 | ExceptionData, "caught"); |
| 3219 | NewCaught->setDoesNotThrow(); |
| 3220 | CGF.Builder.CreateStore(NewCaught, PropagatingExnVar); |
| 3221 | |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3222 | // Don't pop the catch handler; the throw already did. |
| 3223 | CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar); |
Anders Carlsson | bfee7e9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3224 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | b22ff59 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3225 | } |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3226 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3227 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3228 | // Insert read hazards as required in the new blocks. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3229 | Hazards.emitHazardsInNewBlocks(); |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3230 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3231 | // Pop the cleanup. |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3232 | CGF.Builder.restoreIP(TryFallthroughIP); |
| 3233 | if (CGF.HaveInsertPoint()) |
| 3234 | CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3235 | CGF.PopCleanupBlock(); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3236 | CGF.EmitBlock(FinallyEnd.getBlock(), true); |
Anders Carlsson | bfee7e9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3237 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3238 | // Emit the rethrow block. |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3239 | CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP(); |
John McCall | ad5d61e | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 3240 | CGF.EmitBlock(FinallyRethrow.getBlock(), true); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3241 | if (CGF.HaveInsertPoint()) { |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3242 | // If we have a propagating-exception variable, check it. |
| 3243 | llvm::Value *PropagatingExn; |
| 3244 | if (PropagatingExnVar) { |
| 3245 | PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar); |
John McCall | 2dd7d44 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3246 | |
John McCall | 9916e3f | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3247 | // Otherwise, just look in the buffer for the exception to throw. |
| 3248 | } else { |
| 3249 | llvm::CallInst *Caught = |
| 3250 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 3251 | ExceptionData); |
| 3252 | Caught->setDoesNotThrow(); |
| 3253 | PropagatingExn = Caught; |
| 3254 | } |
| 3255 | |
| 3256 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn) |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3257 | ->setDoesNotThrow(); |
| 3258 | CGF.Builder.CreateUnreachable(); |
Fariborz Jahanian | e2caaaa | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 3259 | } |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3260 | |
John McCall | 42227ed | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3261 | CGF.Builder.restoreIP(SavedIP); |
Anders Carlsson | 1963b0c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 3262 | } |
| 3263 | |
| 3264 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2efd538 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3265 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | e005aa1 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3266 | llvm::Value *ExceptionAsObject; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3267 | |
Anders Carlsson | e005aa1 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3268 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 3269 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3270 | ExceptionAsObject = |
Anders Carlsson | e005aa1 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3271 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 3272 | } else { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3273 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | d3dcb4f8 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3274 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | bf8a1be | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 3275 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | e005aa1 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3276 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3277 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3278 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject) |
| 3279 | ->setDoesNotReturn(); |
Anders Carlsson | 4f1c7c3 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3280 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 5c7e393 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 3281 | |
| 3282 | // Clear the insertion point to indicate we are in unreachable code. |
| 3283 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 1963b0c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 3284 | } |
| 3285 | |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3286 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | f5125d1 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 3287 | /// object: objc_read_weak (id *src) |
| 3288 | /// |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3289 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3290 | llvm::Value *AddrWeakObj) { |
Eli Friedman | a374b68 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 3291 | const llvm::Type* DestTy = |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3292 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
| 3293 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, |
| 3294 | ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 3295 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3296 | AddrWeakObj, "weakread"); |
Eli Friedman | a374b68 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 3297 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | f5125d1 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 3298 | return read_weak; |
| 3299 | } |
| 3300 | |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3301 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 3302 | /// objc_assign_weak (id src, id *dst) |
| 3303 | /// |
| 3304 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3305 | llvm::Value *src, llvm::Value *dst) { |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3306 | const llvm::Type * SrcTy = src->getType(); |
| 3307 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3308 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3309 | assert(Size <= 8 && "does not support size > 8"); |
| 3310 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3311 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3312 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 3313 | } |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 3314 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 3315 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 6fdd57c | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 3316 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3317 | src, dst, "weakassign"); |
| 3318 | return; |
| 3319 | } |
| 3320 | |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3321 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 3322 | /// objc_assign_global (id src, id *dst) |
| 3323 | /// |
| 3324 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 3325 | llvm::Value *src, llvm::Value *dst, |
| 3326 | bool threadlocal) { |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3327 | const llvm::Type * SrcTy = src->getType(); |
| 3328 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3329 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3330 | assert(Size <= 8 && "does not support size > 8"); |
| 3331 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3332 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3333 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 3334 | } |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 3335 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 3336 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 3337 | if (!threadlocal) |
| 3338 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
| 3339 | src, dst, "globalassign"); |
| 3340 | else |
| 3341 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(), |
| 3342 | src, dst, "threadlocalassign"); |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3343 | return; |
| 3344 | } |
| 3345 | |
Fariborz Jahanian | e881b53 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3346 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 3347 | /// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset) |
Fariborz Jahanian | e881b53 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3348 | /// |
| 3349 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 3350 | llvm::Value *src, llvm::Value *dst, |
| 3351 | llvm::Value *ivarOffset) { |
| 3352 | assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL"); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3353 | const llvm::Type * SrcTy = src->getType(); |
| 3354 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3355 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3356 | assert(Size <= 8 && "does not support size > 8"); |
| 3357 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3358 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3359 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 3360 | } |
Fariborz Jahanian | e881b53 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3361 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 3362 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 3363 | CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(), |
| 3364 | src, dst, ivarOffset); |
Fariborz Jahanian | e881b53 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3365 | return; |
| 3366 | } |
| 3367 | |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3368 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 3369 | /// objc_assign_strongCast (id src, id *dst) |
| 3370 | /// |
| 3371 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3372 | llvm::Value *src, llvm::Value *dst) { |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3373 | const llvm::Type * SrcTy = src->getType(); |
| 3374 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3375 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3376 | assert(Size <= 8 && "does not support size > 8"); |
| 3377 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3378 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3379 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 3380 | } |
Fariborz Jahanian | 50a1270 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 3381 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 3382 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 3383 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3384 | src, dst, "weakassign"); |
| 3385 | return; |
| 3386 | } |
| 3387 | |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3388 | void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3389 | llvm::Value *DestPtr, |
| 3390 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 3391 | llvm::Value *size) { |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3392 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 3393 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3394 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 3395 | DestPtr, SrcPtr, size); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3396 | return; |
| 3397 | } |
| 3398 | |
Fariborz Jahanian | 9f84b78 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 3399 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 3400 | /// |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 3401 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 3402 | QualType ObjectTy, |
| 3403 | llvm::Value *BaseValue, |
| 3404 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 3405 | unsigned CVRQualifiers) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3406 | const ObjCInterfaceDecl *ID = |
| 3407 | ObjectTy->getAs<ObjCObjectType>()->getInterface(); |
Daniel Dunbar | 9fd114d | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 3408 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 3409 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 9f84b78 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 3410 | } |
| 3411 | |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 3412 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 722f424 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 3413 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 3414 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 9fd114d | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 3415 | uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3416 | return llvm::ConstantInt::get( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3417 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 3418 | Offset); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 3419 | } |
| 3420 | |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3421 | /* *** Private Interface *** */ |
| 3422 | |
| 3423 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 3424 | /// level information. |
| 3425 | /// |
| 3426 | /// See: <rdr://4810609&4810587&4810587> |
| 3427 | /// struct IMAGE_INFO { |
| 3428 | /// unsigned version; |
| 3429 | /// unsigned flags; |
| 3430 | /// }; |
| 3431 | enum ImageInfoFlags { |
Daniel Dunbar | 5e63927 | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 3432 | eImageInfo_FixAndContinue = (1 << 0), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3433 | eImageInfo_GarbageCollected = (1 << 1), |
| 3434 | eImageInfo_GCOnly = (1 << 2), |
Daniel Dunbar | 75e909f | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 3435 | eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set. |
| 3436 | |
Daniel Dunbar | 5e63927 | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 3437 | // A flag indicating that the module has no instances of a @synthesize of a |
| 3438 | // superclass variable. <rdar://problem/6803242> |
Daniel Dunbar | 75e909f | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 3439 | eImageInfo_CorrectedSynthesize = (1 << 4) |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3440 | }; |
| 3441 | |
Daniel Dunbar | 5e63927 | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 3442 | void CGObjCCommonMac::EmitImageInfo() { |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3443 | unsigned version = 0; // Version is unused? |
| 3444 | unsigned flags = 0; |
| 3445 | |
| 3446 | // FIXME: Fix and continue? |
| 3447 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 3448 | flags |= eImageInfo_GarbageCollected; |
| 3449 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 3450 | flags |= eImageInfo_GCOnly; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3451 | |
Daniel Dunbar | 75e909f | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 3452 | // We never allow @synthesize of a superclass property. |
| 3453 | flags |= eImageInfo_CorrectedSynthesize; |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3454 | |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3455 | const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext); |
| 3456 | |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3457 | // Emitted as int[2]; |
| 3458 | llvm::Constant *values[2] = { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3459 | llvm::ConstantInt::get(Int32Ty, version), |
| 3460 | llvm::ConstantInt::get(Int32Ty, flags) |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3461 | }; |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3462 | llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2); |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3463 | |
| 3464 | const char *Section; |
| 3465 | if (ObjCABI == 1) |
| 3466 | Section = "__OBJC, __image_info,regular"; |
| 3467 | else |
| 3468 | Section = "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3469 | llvm::GlobalVariable *GV = |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3470 | CreateMetadataVar("\01L_OBJC_IMAGE_INFO", |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 3471 | llvm::ConstantArray::get(AT, values, 2), |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3472 | Section, |
| 3473 | 0, |
| 3474 | true); |
| 3475 | GV->setConstant(true); |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3476 | } |
| 3477 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3478 | |
| 3479 | // struct objc_module { |
| 3480 | // unsigned long version; |
| 3481 | // unsigned long size; |
| 3482 | // const char *name; |
| 3483 | // Symtab symtab; |
| 3484 | // }; |
| 3485 | |
| 3486 | // FIXME: Get from somewhere |
| 3487 | static const int ModuleVersion = 7; |
| 3488 | |
| 3489 | void CGObjCMac::EmitModuleInfo() { |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3490 | uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3491 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3492 | std::vector<llvm::Constant*> Values(4); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3493 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion); |
| 3494 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 3495 | // This used to be the filename, now it is unused. <rdr://4327263> |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3496 | Values[2] = GetClassName(&CGM.getContext().Idents.get("")); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3497 | Values[3] = EmitModuleSymbols(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3498 | CreateMetadataVar("\01L_OBJC_MODULES", |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3499 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3500 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | ae33384 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 3501 | 4, true); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
| 3504 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3505 | unsigned NumClasses = DefinedClasses.size(); |
| 3506 | unsigned NumCategories = DefinedCategories.size(); |
| 3507 | |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3508 | // Return null if no symbols were defined. |
| 3509 | if (!NumClasses && !NumCategories) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3510 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3511 | |
| 3512 | std::vector<llvm::Constant*> Values(5); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3513 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3514 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3515 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 3516 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3517 | |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3518 | // The runtime expects exactly the list of defined classes followed |
| 3519 | // by the list of defined categories, in a single array. |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3520 | std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3521 | for (unsigned i=0; i<NumClasses; i++) |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3522 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3523 | ObjCTypes.Int8PtrTy); |
| 3524 | for (unsigned i=0; i<NumCategories; i++) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3525 | Symbols[NumClasses + i] = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3526 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3527 | ObjCTypes.Int8PtrTy); |
| 3528 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3529 | Values[4] = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3530 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3531 | NumClasses + NumCategories), |
| 3532 | Symbols); |
| 3533 | |
Nick Lewycky | 41eaf0a | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 3534 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3535 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3536 | llvm::GlobalVariable *GV = |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3537 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 3538 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3539 | 4, true); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3540 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3541 | } |
| 3542 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3543 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3544 | const ObjCInterfaceDecl *ID) { |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3545 | LazySymbols.insert(ID->getIdentifier()); |
| 3546 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3547 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3548 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3549 | if (!Entry) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3550 | llvm::Constant *Casted = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3551 | llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()), |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3552 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3553 | Entry = |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3554 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 3555 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3556 | 4, true); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3557 | } |
| 3558 | |
Daniel Dunbar | c76493a | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 3559 | return Builder.CreateLoad(Entry, "tmp"); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3560 | } |
| 3561 | |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 3562 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel, |
| 3563 | bool lvalue) { |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3564 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3565 | |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3566 | if (!Entry) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3567 | llvm::Constant *Casted = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3568 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3569 | ObjCTypes.SelectorPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3570 | Entry = |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3571 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 3572 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3573 | 4, true); |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3574 | } |
| 3575 | |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 3576 | if (lvalue) |
| 3577 | return Entry; |
Daniel Dunbar | c76493a | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 3578 | return Builder.CreateLoad(Entry, "tmp"); |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3579 | } |
| 3580 | |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3581 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3582 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3583 | |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3584 | if (!Entry) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3585 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3586 | llvm::ConstantArray::get(VMContext, |
| 3587 | Ident->getNameStart()), |
Daniel Dunbar | cda4307 | 2010-07-29 22:57:21 +0000 | [diff] [blame] | 3588 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | 3241fae | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3589 | 1, true); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3590 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3591 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3592 | } |
| 3593 | |
Argyrios Kyrtzidis | 13257c5 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 3594 | llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) { |
| 3595 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator |
| 3596 | I = MethodDefinitions.find(MD); |
| 3597 | if (I != MethodDefinitions.end()) |
| 3598 | return I->second; |
| 3599 | |
| 3600 | if (MD->hasBody() && MD->getPCHLevel() > 0) { |
| 3601 | // MD isn't emitted yet because it comes from PCH. |
| 3602 | CGM.EmitTopLevelDecl(const_cast<ObjCMethodDecl*>(MD)); |
| 3603 | assert(MethodDefinitions[MD] && "EmitTopLevelDecl didn't emit the method!"); |
| 3604 | return MethodDefinitions[MD]; |
| 3605 | } |
| 3606 | |
| 3607 | return NULL; |
| 3608 | } |
| 3609 | |
Fariborz Jahanian | 01dff42 | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 3610 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 3611 | /// ivar layout bitmap. |
| 3612 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3613 | const ObjCCommonTypesHelper &ObjCTypes) { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3614 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 01dff42 | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 3615 | } |
| 3616 | |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3617 | void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3618 | unsigned int BytePos, |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3619 | bool ForStrongLayout, |
| 3620 | bool &HasUnion) { |
| 3621 | const RecordDecl *RD = RT->getDecl(); |
| 3622 | // FIXME - Use iterator. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3623 | llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end()); |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3624 | const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3625 | const llvm::StructLayout *RecLayout = |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3626 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3627 | |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3628 | BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos, |
| 3629 | ForStrongLayout, HasUnion); |
| 3630 | } |
| 3631 | |
Daniel Dunbar | 36e2a1e | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 3632 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3633 | const llvm::StructLayout *Layout, |
| 3634 | const RecordDecl *RD, |
Chris Lattner | 5b36ddb | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3635 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3636 | unsigned int BytePos, bool ForStrongLayout, |
| 3637 | bool &HasUnion) { |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3638 | bool IsUnion = (RD && RD->isUnion()); |
| 3639 | uint64_t MaxUnionIvarSize = 0; |
| 3640 | uint64_t MaxSkippedUnionIvarSize = 0; |
| 3641 | FieldDecl *MaxField = 0; |
| 3642 | FieldDecl *MaxSkippedField = 0; |
Argyrios Kyrtzidis | 2fdb5b5 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3643 | FieldDecl *LastFieldBitfieldOrUnnamed = 0; |
Daniel Dunbar | 5b74391 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3644 | uint64_t MaxFieldOffset = 0; |
| 3645 | uint64_t MaxSkippedFieldOffset = 0; |
Argyrios Kyrtzidis | 2fdb5b5 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3646 | uint64_t LastBitfieldOrUnnamedOffset = 0; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3647 | |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3648 | if (RecFields.empty()) |
| 3649 | return; |
Chris Lattner | 5b36ddb | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3650 | unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0); |
| 3651 | unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth(); |
| 3652 | |
Chris Lattner | 5b36ddb | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3653 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3654 | FieldDecl *Field = RecFields[i]; |
Daniel Dunbar | 62b1070 | 2009-05-03 23:35:23 +0000 | [diff] [blame] | 3655 | uint64_t FieldOffset; |
Anders Carlsson | e2c6baf | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 3656 | if (RD) { |
Daniel Dunbar | d51c1a6 | 2010-04-14 17:02:21 +0000 | [diff] [blame] | 3657 | // Note that 'i' here is actually the field index inside RD of Field, |
| 3658 | // although this dependency is hidden. |
| 3659 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
| 3660 | FieldOffset = RL.getFieldOffset(i) / 8; |
Anders Carlsson | e2c6baf | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 3661 | } else |
Daniel Dunbar | 62b1070 | 2009-05-03 23:35:23 +0000 | [diff] [blame] | 3662 | FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)); |
Daniel Dunbar | 94f46dc | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3663 | |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3664 | // Skip over unnamed or bitfields |
Fariborz Jahanian | f5fec02 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3665 | if (!Field->getIdentifier() || Field->isBitField()) { |
Argyrios Kyrtzidis | 2fdb5b5 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3666 | LastFieldBitfieldOrUnnamed = Field; |
| 3667 | LastBitfieldOrUnnamedOffset = FieldOffset; |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3668 | continue; |
Fariborz Jahanian | f5fec02 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3669 | } |
Daniel Dunbar | 94f46dc | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3670 | |
Argyrios Kyrtzidis | 2fdb5b5 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3671 | LastFieldBitfieldOrUnnamed = 0; |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3672 | QualType FQT = Field->getType(); |
Fariborz Jahanian | f909f92 | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3673 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3674 | if (FQT->isUnionType()) |
| 3675 | HasUnion = true; |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3676 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3677 | BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(), |
Daniel Dunbar | 94f46dc | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3678 | BytePos + FieldOffset, |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3679 | ForStrongLayout, HasUnion); |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3680 | continue; |
| 3681 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3682 | |
Chris Lattner | 5b36ddb | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3683 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3684 | const ConstantArrayType *CArray = |
Daniel Dunbar | 7abf83c | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3685 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | f5fec02 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3686 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
Daniel Dunbar | 7abf83c | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3687 | assert(CArray && "only array with known element size is supported"); |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3688 | FQT = CArray->getElementType(); |
Fariborz Jahanian | f909f92 | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3689 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 3690 | const ConstantArrayType *CArray = |
Daniel Dunbar | 7abf83c | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3691 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | f5fec02 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3692 | ElCount *= CArray->getSize().getZExtValue(); |
Fariborz Jahanian | f909f92 | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3693 | FQT = CArray->getElementType(); |
| 3694 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3695 | |
| 3696 | assert(!FQT->isUnionType() && |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3697 | "layout for array of unions not supported"); |
Fariborz Jahanian | 9a7d57d | 2011-01-03 19:23:18 +0000 | [diff] [blame] | 3698 | if (FQT->isRecordType() && ElCount) { |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3699 | int OldIndex = IvarsInfo.size() - 1; |
| 3700 | int OldSkIndex = SkipIvars.size() -1; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3701 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3702 | const RecordType *RT = FQT->getAs<RecordType>(); |
Daniel Dunbar | 94f46dc | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3703 | BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset, |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3704 | ForStrongLayout, HasUnion); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3705 | |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3706 | // Replicate layout information for each array element. Note that |
| 3707 | // one element is already done. |
| 3708 | uint64_t ElIx = 1; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3709 | for (int FirstIndex = IvarsInfo.size() - 1, |
| 3710 | FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3711 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3712 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 3713 | IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx, |
| 3714 | IvarsInfo[i].ivar_size)); |
| 3715 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) |
| 3716 | SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx, |
| 3717 | SkipIvars[i].ivar_size)); |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3718 | } |
| 3719 | continue; |
| 3720 | } |
Fariborz Jahanian | 524bb20 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3721 | } |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3722 | // At this point, we are done with Record/Union and array there of. |
| 3723 | // For other arrays we are down to its element type. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3724 | Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT); |
Daniel Dunbar | 7abf83c | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3725 | |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3726 | unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3727 | if ((ForStrongLayout && GCAttr == Qualifiers::Strong) |
| 3728 | || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) { |
Daniel Dunbar | 22007d3 | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3729 | if (IsUnion) { |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3730 | uint64_t UnionIvarSize = FieldSize / WordSizeInBits; |
Daniel Dunbar | 22007d3 | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3731 | if (UnionIvarSize > MaxUnionIvarSize) { |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3732 | MaxUnionIvarSize = UnionIvarSize; |
| 3733 | MaxField = Field; |
Daniel Dunbar | 5b74391 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3734 | MaxFieldOffset = FieldOffset; |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3735 | } |
Daniel Dunbar | 22007d3 | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3736 | } else { |
Daniel Dunbar | 94f46dc | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3737 | IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3738 | FieldSize / WordSizeInBits)); |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3739 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3740 | } else if ((ForStrongLayout && |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3741 | (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)) |
| 3742 | || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) { |
Daniel Dunbar | 22007d3 | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3743 | if (IsUnion) { |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3744 | // FIXME: Why the asymmetry? We divide by word size in bits on other |
| 3745 | // side. |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3746 | uint64_t UnionIvarSize = FieldSize; |
Daniel Dunbar | 22007d3 | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3747 | if (UnionIvarSize > MaxSkippedUnionIvarSize) { |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3748 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 3749 | MaxSkippedField = Field; |
Daniel Dunbar | 5b74391 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3750 | MaxSkippedFieldOffset = FieldOffset; |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3751 | } |
Daniel Dunbar | 22007d3 | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3752 | } else { |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3753 | // FIXME: Why the asymmetry, we divide by byte size in bits here? |
Daniel Dunbar | 94f46dc | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3754 | SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3755 | FieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 3b0f886 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3756 | } |
| 3757 | } |
| 3758 | } |
Daniel Dunbar | 15bd888 | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3759 | |
Argyrios Kyrtzidis | 2fdb5b5 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3760 | if (LastFieldBitfieldOrUnnamed) { |
| 3761 | if (LastFieldBitfieldOrUnnamed->isBitField()) { |
| 3762 | // Last field was a bitfield. Must update skip info. |
| 3763 | Expr *BitWidth = LastFieldBitfieldOrUnnamed->getBitWidth(); |
| 3764 | uint64_t BitFieldSize = |
| 3765 | BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue(); |
| 3766 | GC_IVAR skivar; |
| 3767 | skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset; |
| 3768 | skivar.ivar_size = (BitFieldSize / ByteSizeInBits) |
| 3769 | + ((BitFieldSize % ByteSizeInBits) != 0); |
| 3770 | SkipIvars.push_back(skivar); |
| 3771 | } else { |
| 3772 | assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed"); |
| 3773 | // Last field was unnamed. Must update skip info. |
| 3774 | unsigned FieldSize |
| 3775 | = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType()); |
| 3776 | SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset, |
| 3777 | FieldSize / ByteSizeInBits)); |
| 3778 | } |
Fariborz Jahanian | f5fec02 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3779 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3780 | |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3781 | if (MaxField) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3782 | IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset, |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3783 | MaxUnionIvarSize)); |
| 3784 | if (MaxSkippedField) |
Daniel Dunbar | 5b74391 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3785 | SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset, |
Daniel Dunbar | 7b89ace | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3786 | MaxSkippedUnionIvarSize)); |
Fariborz Jahanian | c559f3f | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3787 | } |
| 3788 | |
Fariborz Jahanian | 1f78a9a | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 3789 | /// BuildIvarLayoutBitmap - This routine is the horsework for doing all |
| 3790 | /// the computations and returning the layout bitmap (for ivar or blocks) in |
| 3791 | /// the given argument BitMap string container. Routine reads |
| 3792 | /// two containers, IvarsInfo and SkipIvars which are assumed to be |
| 3793 | /// filled already by the caller. |
| 3794 | llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3795 | unsigned int WordsToScan, WordsToSkip; |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 3796 | const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3797 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3798 | // Build the string of skip/scan nibbles |
Fariborz Jahanian | ce567657 | 2009-04-24 17:15:27 +0000 | [diff] [blame] | 3799 | llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3800 | unsigned int WordSize = |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3801 | CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy); |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3802 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 3803 | WordsToSkip = 0; |
| 3804 | WordsToScan = IvarsInfo[0].ivar_size; |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3805 | } else { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3806 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 3807 | WordsToScan = IvarsInfo[0].ivar_size; |
| 3808 | } |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3809 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3810 | unsigned int TailPrevGCObjC = |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3811 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3812 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3813 | // consecutive 'scanned' object pointers. |
| 3814 | WordsToScan += IvarsInfo[i].ivar_size; |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3815 | } else { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3816 | // Skip over 'gc'able object pointer which lay over each other. |
| 3817 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 3818 | continue; |
| 3819 | // Must skip over 1 or more words. We save current skip/scan values |
| 3820 | // and start a new pair. |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3821 | SKIP_SCAN SkScan; |
| 3822 | SkScan.skip = WordsToSkip; |
| 3823 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3824 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3825 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3826 | // Skip the hole. |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3827 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 3828 | SkScan.scan = 0; |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3829 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3830 | WordsToSkip = 0; |
| 3831 | WordsToScan = IvarsInfo[i].ivar_size; |
| 3832 | } |
| 3833 | } |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3834 | if (WordsToScan > 0) { |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3835 | SKIP_SCAN SkScan; |
| 3836 | SkScan.skip = WordsToSkip; |
| 3837 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3838 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3839 | } |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3840 | |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3841 | if (!SkipIvars.empty()) { |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3842 | unsigned int LastIndex = SkipIvars.size()-1; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3843 | int LastByteSkipped = |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3844 | SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size; |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3845 | LastIndex = IvarsInfo.size()-1; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3846 | int LastByteScanned = |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3847 | IvarsInfo[LastIndex].ivar_bytepos + |
| 3848 | IvarsInfo[LastIndex].ivar_size * WordSize; |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3849 | // Compute number of bytes to skip at the tail end of the last ivar scanned. |
Benjamin Kramer | d20ef75 | 2009-12-25 15:43:36 +0000 | [diff] [blame] | 3850 | if (LastByteSkipped > LastByteScanned) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3851 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | 1bf7288 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3852 | SKIP_SCAN SkScan; |
| 3853 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
| 3854 | SkScan.scan = 0; |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3855 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3856 | } |
| 3857 | } |
| 3858 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 3859 | // as 0xMN. |
Fariborz Jahanian | a123b64 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3860 | int SkipScan = SkipScanIvars.size()-1; |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3861 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3862 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 3863 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 3864 | // 0xM0 followed by 0x0N detected. |
| 3865 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 3866 | for (int j = i+1; j < SkipScan; j++) |
| 3867 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 3868 | --SkipScan; |
| 3869 | } |
| 3870 | } |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3871 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3872 | // Generate the string. |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3873 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3874 | unsigned char byte; |
| 3875 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 3876 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 3877 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 3878 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3879 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3880 | // first skip big. |
| 3881 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 3882 | BitMap += (unsigned char)(0xf0); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3883 | |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3884 | // next (skip small, scan) |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3885 | if (skip_small) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3886 | byte = skip_small << 4; |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3887 | if (scan_big > 0) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3888 | byte |= 0xf; |
| 3889 | --scan_big; |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3890 | } else if (scan_small) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3891 | byte |= scan_small; |
| 3892 | scan_small = 0; |
| 3893 | } |
| 3894 | BitMap += byte; |
| 3895 | } |
| 3896 | // next scan big |
| 3897 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 3898 | BitMap += (unsigned char)(0x0f); |
| 3899 | // last scan small |
Daniel Dunbar | d22aa4a | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3900 | if (scan_small) { |
Fariborz Jahanian | cbaf73c | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3901 | byte = scan_small; |
| 3902 | BitMap += byte; |
| 3903 | } |
| 3904 | } |
| 3905 | // null terminate string. |
Fariborz Jahanian | f909f92 | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3906 | unsigned char zero = 0; |
| 3907 | BitMap += zero; |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3908 | |
| 3909 | llvm::GlobalVariable * Entry = |
| 3910 | CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 3911 | llvm::ConstantArray::get(VMContext, BitMap.c_str()), |
| 3912 | "__TEXT,__cstring,cstring_literals", |
| 3913 | 1, true); |
| 3914 | return getConstantGEP(VMContext, Entry, 0, 0); |
| 3915 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3916 | |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3917 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 3918 | /// implementation for the __strong or __weak case. |
| 3919 | /// The layout map displays which words in ivar list must be skipped |
| 3920 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 3921 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
| 3922 | /// of words to skip and right nibble is count of words to scan. So, each |
| 3923 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
| 3924 | /// represented by a 0x00 byte which also ends the string. |
| 3925 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 3926 | /// - id, Class |
| 3927 | /// - object * |
| 3928 | /// - __strong anything |
| 3929 | /// |
| 3930 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 3931 | /// - __weak anything |
| 3932 | /// |
| 3933 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
| 3934 | const ObjCImplementationDecl *OMD, |
| 3935 | bool ForStrongLayout) { |
| 3936 | bool hasUnion = false; |
| 3937 | |
| 3938 | const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
| 3939 | if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) |
| 3940 | return llvm::Constant::getNullValue(PtrTy); |
| 3941 | |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 3942 | llvm::SmallVector<ObjCIvarDecl*, 32> Ivars; |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3943 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 3944 | CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3945 | |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 3946 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3947 | for (unsigned k = 0, e = Ivars.size(); k != e; ++k) |
| 3948 | RecFields.push_back(cast<FieldDecl>(Ivars[k])); |
| 3949 | |
| 3950 | if (RecFields.empty()) |
| 3951 | return llvm::Constant::getNullValue(PtrTy); |
| 3952 | |
| 3953 | SkipIvars.clear(); |
| 3954 | IvarsInfo.clear(); |
| 3955 | |
| 3956 | BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion); |
| 3957 | if (IvarsInfo.empty()) |
| 3958 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | 1f78a9a | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 3959 | // Sort on byte position in case we encounterred a union nested in |
| 3960 | // the ivar list. |
| 3961 | if (hasUnion && !IvarsInfo.empty()) |
| 3962 | std::sort(IvarsInfo.begin(), IvarsInfo.end()); |
| 3963 | if (hasUnion && !SkipIvars.empty()) |
| 3964 | std::sort(SkipIvars.begin(), SkipIvars.end()); |
| 3965 | |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3966 | std::string BitMap; |
Fariborz Jahanian | 1f78a9a | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 3967 | llvm::Constant *C = BuildIvarLayoutBitmap(BitMap); |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3968 | |
| 3969 | if (CGM.getLangOptions().ObjCGCBitmapPrint) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3970 | printf("\n%s ivar layout for class '%s': ", |
Fariborz Jahanian | 80c9ce2 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3971 | ForStrongLayout ? "strong" : "weak", |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3972 | OMD->getClassInterface()->getName().data()); |
Fariborz Jahanian | 80c9ce2 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3973 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
| 3974 | for (unsigned i = 0; i < BitMap.size(); i++) |
| 3975 | if (!(s[i] & 0xf0)) |
| 3976 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3977 | else |
| 3978 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3979 | printf("\n"); |
| 3980 | } |
Fariborz Jahanian | 9659f6b | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3981 | return C; |
Fariborz Jahanian | c559f3f | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3982 | } |
| 3983 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3984 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3985 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 3986 | |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3987 | // FIXME: Avoid std::string copying. |
| 3988 | if (!Entry) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3989 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3990 | llvm::ConstantArray::get(VMContext, Sel.getAsString()), |
Daniel Dunbar | cda4307 | 2010-07-29 22:57:21 +0000 | [diff] [blame] | 3991 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | 3241fae | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3992 | 1, true); |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3993 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3994 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3995 | } |
| 3996 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3997 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3998 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3999 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 4000 | } |
| 4001 | |
Daniel Dunbar | f5c1846 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4002 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { |
Devang Patel | 4b6e4bb | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4003 | std::string TypeStr; |
| 4004 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 4005 | |
| 4006 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4007 | |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4008 | if (!Entry) |
| 4009 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 4010 | llvm::ConstantArray::get(VMContext, TypeStr), |
Daniel Dunbar | cda4307 | 2010-07-29 22:57:21 +0000 | [diff] [blame] | 4011 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | 3241fae | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4012 | 1, true); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4013 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4014 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | cb515c8 | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4015 | } |
| 4016 | |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4017 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) { |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4018 | std::string TypeStr; |
Daniel Dunbar | 3c76cb5 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 4019 | CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D), |
| 4020 | TypeStr); |
Devang Patel | 4b6e4bb | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4021 | |
| 4022 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 4023 | |
Daniel Dunbar | 3241fae | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4024 | if (!Entry) |
| 4025 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 4026 | llvm::ConstantArray::get(VMContext, TypeStr), |
Daniel Dunbar | cda4307 | 2010-07-29 22:57:21 +0000 | [diff] [blame] | 4027 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | 3241fae | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4028 | 1, true); |
Devang Patel | 4b6e4bb | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4029 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4030 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4031 | } |
| 4032 | |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4033 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4034 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4035 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4036 | |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4037 | if (!Entry) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4038 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4039 | llvm::ConstantArray::get(VMContext, |
| 4040 | Ident->getNameStart()), |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4041 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | 3241fae | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4042 | 1, true); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4043 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4044 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4045 | } |
| 4046 | |
| 4047 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4048 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | c2d4b62 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4049 | llvm::Constant * |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4050 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 4051 | const Decl *Container) { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4052 | std::string TypeStr; |
| 4053 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | 80a840b | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4054 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 4055 | } |
| 4056 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4057 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
Fariborz Jahanian | 0b1ccdc | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4058 | const ObjCContainerDecl *CD, |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4059 | llvm::SmallVectorImpl<char> &Name) { |
| 4060 | llvm::raw_svector_ostream OS(Name); |
Fariborz Jahanian | 0196a1c | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 4061 | assert (CD && "Missing container decl in GetNameForMethod"); |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4062 | OS << '\01' << (D->isInstanceMethod() ? '-' : '+') |
| 4063 | << '[' << CD->getName(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4064 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4065 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) |
Benjamin Kramer | b11416d | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 4066 | OS << '(' << CID << ')'; |
Daniel Dunbar | d238681 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4067 | OS << ' ' << D->getSelector().getAsString() << ']'; |
Daniel Dunbar | a94ecd2 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 4068 | } |
| 4069 | |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4070 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4071 | EmitModuleInfo(); |
| 4072 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4073 | // Emit the dummy bodies for any protocols which were referenced but |
| 4074 | // never defined. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4075 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4076 | I = Protocols.begin(), e = Protocols.end(); I != e; ++I) { |
| 4077 | if (I->second->hasInitializer()) |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4078 | continue; |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4079 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4080 | std::vector<llvm::Constant*> Values(5); |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4081 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4082 | Values[1] = GetClassName(I->first); |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4083 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4084 | Values[3] = Values[4] = |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4085 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4086 | I->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4087 | I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4088 | Values)); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4089 | CGM.AddUsedGlobal(I->second); |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4090 | } |
| 4091 | |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4092 | // Add assembler directives to add lazy undefined symbol references |
| 4093 | // for classes which are referenced but not defined. This is |
| 4094 | // important for correct linker interaction. |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4095 | // |
| 4096 | // FIXME: It would be nice if we had an LLVM construct for this. |
| 4097 | if (!LazySymbols.empty() || !DefinedSymbols.empty()) { |
| 4098 | llvm::SmallString<256> Asm; |
| 4099 | Asm += CGM.getModule().getModuleInlineAsm(); |
| 4100 | if (!Asm.empty() && Asm.back() != '\n') |
| 4101 | Asm += '\n'; |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4102 | |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4103 | llvm::raw_svector_ostream OS(Asm); |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4104 | for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(), |
| 4105 | e = DefinedSymbols.end(); I != e; ++I) |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 4106 | OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n" |
| 4107 | << "\t.globl .objc_class_name_" << (*I)->getName() << "\n"; |
Chris Lattner | a299f2c | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4108 | for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(), |
Fariborz Jahanian | 9adb2e6 | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 4109 | e = LazySymbols.end(); I != e; ++I) { |
Chris Lattner | a299f2c | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4110 | OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n"; |
Fariborz Jahanian | 9adb2e6 | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 4111 | } |
| 4112 | |
| 4113 | for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) { |
| 4114 | OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n" |
| 4115 | << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n"; |
| 4116 | } |
Chris Lattner | a299f2c | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4117 | |
Daniel Dunbar | d027a92 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4118 | CGM.getModule().setModuleInlineAsm(OS.str()); |
Daniel Dunbar | c61d0e9 | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4119 | } |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4120 | } |
| 4121 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4122 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4123 | : CGObjCCommonMac(cgm), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4124 | ObjCTypes(cgm) { |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4125 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4126 | ObjCABI = 2; |
| 4127 | } |
| 4128 | |
Daniel Dunbar | 3ad5348 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4129 | /* *** */ |
| 4130 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4131 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4132 | : VMContext(cgm.getLLVMContext()), CGM(cgm) { |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4133 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 4134 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4135 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4136 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4137 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4138 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4139 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 4140 | Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4141 | |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4142 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4143 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4144 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4145 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 4146 | // FIXME: It would be nice to unify this with the opaque type, so that the IR |
| 4147 | // comes out a bit cleaner. |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4148 | const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4149 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4150 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4151 | // I'm not sure I like this. The implicit coordination is a bit |
| 4152 | // gross. We should solve this in a reasonable fashion because this |
| 4153 | // is a pretty common task (match some runtime data structure with |
| 4154 | // an LLVM data structure). |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4155 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4156 | // FIXME: This is leaked. |
| 4157 | // FIXME: Merge with rewriter code? |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4158 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4159 | // struct _objc_super { |
| 4160 | // id self; |
| 4161 | // Class cls; |
| 4162 | // } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4163 | RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct, |
Daniel Dunbar | 0c00537 | 2010-04-29 16:29:11 +0000 | [diff] [blame] | 4164 | Ctx.getTranslationUnitDecl(), |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4165 | SourceLocation(), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4166 | &Ctx.Idents.get("_objc_super")); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4167 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 4168 | Ctx.getObjCIdType(), 0, 0, false)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4169 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 4170 | Ctx.getObjCClassType(), 0, 0, false)); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4171 | RD->completeDefinition(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4172 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4173 | SuperCTy = Ctx.getTagDeclType(RD); |
| 4174 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4175 | |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4176 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4177 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 4178 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4179 | // struct _prop_t { |
| 4180 | // char *name; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4181 | // char *attributes; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4182 | // } |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4183 | PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4184 | CGM.getModule().addTypeName("struct._prop_t", |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4185 | PropertyTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4186 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4187 | // struct _prop_list_t { |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4188 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4189 | // uint32_t count_of_properties; |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4190 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4191 | // } |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4192 | PropertyListTy = llvm::StructType::get(VMContext, IntTy, |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4193 | IntTy, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4194 | llvm::ArrayType::get(PropertyTy, 0), |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4195 | NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4196 | CGM.getModule().addTypeName("struct._prop_list_t", |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4197 | PropertyListTy); |
| 4198 | // struct _prop_list_t * |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4199 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4200 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4201 | // struct _objc_method { |
| 4202 | // SEL _cmd; |
| 4203 | // char *method_type; |
| 4204 | // char *_imp; |
| 4205 | // } |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4206 | MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy, |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4207 | Int8PtrTy, |
| 4208 | Int8PtrTy, |
| 4209 | NULL); |
| 4210 | CGM.getModule().addTypeName("struct._objc_method", MethodTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4211 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4212 | // struct _objc_cache * |
Owen Anderson | c36edfe | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 4213 | CacheTy = llvm::OpaqueType::get(VMContext); |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4214 | CGM.getModule().addTypeName("struct._objc_cache", CacheTy); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4215 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4216 | } |
Daniel Dunbar | 7bd00bd | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4217 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4218 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4219 | : ObjCCommonTypesHelper(cgm) { |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4220 | // struct _objc_method_description { |
| 4221 | // SEL name; |
| 4222 | // char *types; |
| 4223 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4224 | MethodDescriptionTy = |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4225 | llvm::StructType::get(VMContext, SelectorPtrTy, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4226 | Int8PtrTy, |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4227 | NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4228 | CGM.getModule().addTypeName("struct._objc_method_description", |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4229 | MethodDescriptionTy); |
| 4230 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4231 | // struct _objc_method_description_list { |
| 4232 | // int count; |
| 4233 | // struct _objc_method_description[1]; |
| 4234 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4235 | MethodDescriptionListTy = |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4236 | llvm::StructType::get(VMContext, IntTy, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4237 | llvm::ArrayType::get(MethodDescriptionTy, 0), |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4238 | NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4239 | CGM.getModule().addTypeName("struct._objc_method_description_list", |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4240 | MethodDescriptionListTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4241 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4242 | // struct _objc_method_description_list * |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4243 | MethodDescriptionListPtrTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4244 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4245 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4246 | // Protocol description structures |
| 4247 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4248 | // struct _objc_protocol_extension { |
| 4249 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 4250 | // struct _objc_method_description_list *optional_instance_methods; |
| 4251 | // struct _objc_method_description_list *optional_class_methods; |
| 4252 | // struct _objc_property_list *instance_properties; |
| 4253 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4254 | ProtocolExtensionTy = |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4255 | llvm::StructType::get(VMContext, IntTy, |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4256 | MethodDescriptionListPtrTy, |
| 4257 | MethodDescriptionListPtrTy, |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4258 | PropertyListPtrTy, |
| 4259 | NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4260 | CGM.getModule().addTypeName("struct._objc_protocol_extension", |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4261 | ProtocolExtensionTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4262 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4263 | // struct _objc_protocol_extension * |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4264 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4265 | |
Daniel Dunbar | c475d42 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4266 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4267 | |
Owen Anderson | c36edfe | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 4268 | llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext); |
| 4269 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4270 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4271 | const llvm::Type *T = |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4272 | llvm::StructType::get(VMContext, |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4273 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4274 | LongTy, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4275 | llvm::ArrayType::get(ProtocolTyHolder, 0), |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4276 | NULL); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4277 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T); |
| 4278 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4279 | // struct _objc_protocol { |
| 4280 | // struct _objc_protocol_extension *isa; |
| 4281 | // char *protocol_name; |
| 4282 | // struct _objc_protocol **_objc_protocol_list; |
| 4283 | // struct _objc_method_description_list *instance_methods; |
| 4284 | // struct _objc_method_description_list *class_methods; |
| 4285 | // } |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4286 | T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4287 | Int8PtrTy, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4288 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4289 | MethodDescriptionListPtrTy, |
| 4290 | MethodDescriptionListPtrTy, |
| 4291 | NULL); |
| 4292 | cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T); |
| 4293 | |
| 4294 | ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4295 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4296 | ProtocolListTy); |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4297 | // struct _objc_protocol_list * |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4298 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4299 | |
| 4300 | ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get()); |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4301 | CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4302 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4303 | |
| 4304 | // Class description structures |
| 4305 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4306 | // struct _objc_ivar { |
| 4307 | // char *ivar_name; |
| 4308 | // char *ivar_type; |
| 4309 | // int ivar_offset; |
| 4310 | // } |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4311 | IvarTy = llvm::StructType::get(VMContext, Int8PtrTy, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4312 | Int8PtrTy, |
| 4313 | IntTy, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4314 | NULL); |
| 4315 | CGM.getModule().addTypeName("struct._objc_ivar", IvarTy); |
| 4316 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4317 | // struct _objc_ivar_list * |
Owen Anderson | c36edfe | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 4318 | IvarListTy = llvm::OpaqueType::get(VMContext); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4319 | CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4320 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4321 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4322 | // struct _objc_method_list * |
Owen Anderson | c36edfe | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 4323 | MethodListTy = llvm::OpaqueType::get(VMContext); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4324 | CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4325 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4326 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4327 | // struct _objc_class_extension * |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4328 | ClassExtensionTy = |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4329 | llvm::StructType::get(VMContext, IntTy, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4330 | Int8PtrTy, |
| 4331 | PropertyListPtrTy, |
| 4332 | NULL); |
| 4333 | CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4334 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4335 | |
Owen Anderson | c36edfe | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 4336 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4337 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4338 | // struct _objc_class { |
| 4339 | // Class isa; |
| 4340 | // Class super_class; |
| 4341 | // char *name; |
| 4342 | // long version; |
| 4343 | // long info; |
| 4344 | // long instance_size; |
| 4345 | // struct _objc_ivar_list *ivars; |
| 4346 | // struct _objc_method_list *methods; |
| 4347 | // struct _objc_cache *cache; |
| 4348 | // struct _objc_protocol_list *protocols; |
| 4349 | // char *ivar_layout; |
| 4350 | // struct _objc_class_ext *ext; |
| 4351 | // }; |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4352 | T = llvm::StructType::get(VMContext, |
| 4353 | llvm::PointerType::getUnqual(ClassTyHolder), |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4354 | llvm::PointerType::getUnqual(ClassTyHolder), |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4355 | Int8PtrTy, |
| 4356 | LongTy, |
| 4357 | LongTy, |
| 4358 | LongTy, |
| 4359 | IvarListPtrTy, |
| 4360 | MethodListPtrTy, |
| 4361 | CachePtrTy, |
| 4362 | ProtocolListPtrTy, |
| 4363 | Int8PtrTy, |
| 4364 | ClassExtensionPtrTy, |
| 4365 | NULL); |
| 4366 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4367 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4368 | ClassTy = cast<llvm::StructType>(ClassTyHolder.get()); |
| 4369 | CGM.getModule().addTypeName("struct._objc_class", ClassTy); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4370 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4371 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4372 | // struct _objc_category { |
| 4373 | // char *category_name; |
| 4374 | // char *class_name; |
| 4375 | // struct _objc_method_list *instance_method; |
| 4376 | // struct _objc_method_list *class_method; |
| 4377 | // uint32_t size; // sizeof(struct _objc_category) |
| 4378 | // struct _objc_property_list *instance_properties;// category's @property |
| 4379 | // } |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4380 | CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy, |
Daniel Dunbar | 938a77f | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 4381 | Int8PtrTy, |
| 4382 | MethodListPtrTy, |
| 4383 | MethodListPtrTy, |
| 4384 | ProtocolListPtrTy, |
| 4385 | IntTy, |
| 4386 | PropertyListPtrTy, |
| 4387 | NULL); |
| 4388 | CGM.getModule().addTypeName("struct._objc_category", CategoryTy); |
| 4389 | |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4390 | // Global metadata structures |
| 4391 | |
Fariborz Jahanian | 4b4c826 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4392 | // struct _objc_symtab { |
| 4393 | // long sel_ref_cnt; |
| 4394 | // SEL *refs; |
| 4395 | // short cls_def_cnt; |
| 4396 | // short cat_def_cnt; |
| 4397 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 4398 | // } |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4399 | SymtabTy = llvm::StructType::get(VMContext, LongTy, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4400 | SelectorPtrTy, |
| 4401 | ShortTy, |
| 4402 | ShortTy, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4403 | llvm::ArrayType::get(Int8PtrTy, 0), |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4404 | NULL); |
| 4405 | CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4406 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4407 | |
Fariborz Jahanian | eee54df | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 4408 | // struct _objc_module { |
| 4409 | // long version; |
| 4410 | // long size; // sizeof(struct _objc_module) |
| 4411 | // char *name; |
| 4412 | // struct _objc_symtab* symtab; |
| 4413 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4414 | ModuleTy = |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4415 | llvm::StructType::get(VMContext, LongTy, |
Daniel Dunbar | 22d82ed | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4416 | LongTy, |
| 4417 | Int8PtrTy, |
| 4418 | SymtabPtrTy, |
| 4419 | NULL); |
| 4420 | CGM.getModule().addTypeName("struct._objc_module", ModuleTy); |
Daniel Dunbar | 97ff50d | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 4421 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4422 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 4423 | // FIXME: This is the size of the setjmp buffer and should be target |
| 4424 | // specific. 18 is what's used on 32-bit X86. |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 4425 | uint64_t SetJmpBufferSize = 18; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4426 | |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 4427 | // Exceptions |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4428 | const llvm::Type *StackPtrTy = llvm::ArrayType::get( |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 4429 | llvm::Type::getInt8PtrTy(VMContext), 4); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4430 | |
| 4431 | ExceptionDataTy = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 4432 | llvm::StructType::get(VMContext, |
| 4433 | llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext), |
| 4434 | SetJmpBufferSize), |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 4435 | StackPtrTy, NULL); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4436 | CGM.getModule().addTypeName("struct._objc_exception_data", |
Anders Carlsson | 9ff2248 | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 4437 | ExceptionDataTy); |
| 4438 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4439 | } |
| 4440 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4441 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4442 | : ObjCCommonTypesHelper(cgm) { |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4443 | // struct _method_list_t { |
| 4444 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 4445 | // uint32_t method_count; |
| 4446 | // struct _objc_method method_list[method_count]; |
| 4447 | // } |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4448 | MethodListnfABITy = llvm::StructType::get(VMContext, IntTy, |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4449 | IntTy, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4450 | llvm::ArrayType::get(MethodTy, 0), |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4451 | NULL); |
| 4452 | CGM.getModule().addTypeName("struct.__method_list_t", |
| 4453 | MethodListnfABITy); |
| 4454 | // struct method_list_t * |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4455 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4456 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4457 | // struct _protocol_t { |
| 4458 | // id isa; // NULL |
| 4459 | // const char * const protocol_name; |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4460 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4461 | // const struct method_list_t * const instance_methods; |
| 4462 | // const struct method_list_t * const class_methods; |
| 4463 | // const struct method_list_t *optionalInstanceMethods; |
| 4464 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4465 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4466 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 4467 | // const uint32_t flags; // = 0 |
| 4468 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4469 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4470 | // Holder for struct _protocol_list_t * |
Owen Anderson | c36edfe | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 4471 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4472 | |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4473 | ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy, |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4474 | Int8PtrTy, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4475 | llvm::PointerType::getUnqual( |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4476 | ProtocolListTyHolder), |
| 4477 | MethodListnfABIPtrTy, |
| 4478 | MethodListnfABIPtrTy, |
| 4479 | MethodListnfABIPtrTy, |
| 4480 | MethodListnfABIPtrTy, |
| 4481 | PropertyListPtrTy, |
| 4482 | IntTy, |
| 4483 | IntTy, |
| 4484 | NULL); |
| 4485 | CGM.getModule().addTypeName("struct._protocol_t", |
| 4486 | ProtocolnfABITy); |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4487 | |
| 4488 | // struct _protocol_t* |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4489 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4490 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4491 | // struct _protocol_list_t { |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4492 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4493 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4494 | // } |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4495 | ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4496 | llvm::ArrayType::get( |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4497 | ProtocolnfABIPtrTy, 0), |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4498 | NULL); |
| 4499 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 4500 | ProtocolListnfABITy); |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4501 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4502 | ProtocolListnfABITy); |
| 4503 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4504 | // struct _objc_protocol_list* |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4505 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4506 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4507 | // struct _ivar_t { |
| 4508 | // unsigned long int *offset; // pointer to ivar offset location |
| 4509 | // char *name; |
| 4510 | // char *type; |
| 4511 | // uint32_t alignment; |
| 4512 | // uint32_t size; |
| 4513 | // } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4514 | IvarnfABITy = llvm::StructType::get(VMContext, |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4515 | llvm::PointerType::getUnqual(LongTy), |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4516 | Int8PtrTy, |
| 4517 | Int8PtrTy, |
| 4518 | IntTy, |
| 4519 | IntTy, |
| 4520 | NULL); |
| 4521 | CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4522 | |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4523 | // struct _ivar_list_t { |
| 4524 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 4525 | // uint32 count; |
| 4526 | // struct _iver_t list[count]; |
| 4527 | // } |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4528 | IvarListnfABITy = llvm::StructType::get(VMContext, IntTy, |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4529 | IntTy, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4530 | llvm::ArrayType::get( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4531 | IvarnfABITy, 0), |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4532 | NULL); |
| 4533 | CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4534 | |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4535 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4536 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4537 | // struct _class_ro_t { |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4538 | // uint32_t const flags; |
| 4539 | // uint32_t const instanceStart; |
| 4540 | // uint32_t const instanceSize; |
| 4541 | // uint32_t const reserved; // only when building for 64bit targets |
| 4542 | // const uint8_t * const ivarLayout; |
| 4543 | // const char *const name; |
| 4544 | // const struct _method_list_t * const baseMethods; |
| 4545 | // const struct _objc_protocol_list *const baseProtocols; |
| 4546 | // const struct _ivar_list_t *const ivars; |
| 4547 | // const uint8_t * const weakIvarLayout; |
| 4548 | // const struct _prop_list_t * const properties; |
| 4549 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4550 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4551 | // FIXME. Add 'reserved' field in 64bit abi mode! |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4552 | ClassRonfABITy = llvm::StructType::get(VMContext, IntTy, |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4553 | IntTy, |
| 4554 | IntTy, |
| 4555 | Int8PtrTy, |
| 4556 | Int8PtrTy, |
| 4557 | MethodListnfABIPtrTy, |
| 4558 | ProtocolListnfABIPtrTy, |
| 4559 | IvarListnfABIPtrTy, |
| 4560 | Int8PtrTy, |
| 4561 | PropertyListPtrTy, |
| 4562 | NULL); |
| 4563 | CGM.getModule().addTypeName("struct._class_ro_t", |
| 4564 | ClassRonfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4565 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4566 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 4567 | std::vector<const llvm::Type*> Params; |
| 4568 | Params.push_back(ObjectPtrTy); |
| 4569 | Params.push_back(SelectorPtrTy); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4570 | ImpnfABITy = llvm::PointerType::getUnqual( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4571 | llvm::FunctionType::get(ObjectPtrTy, Params, false)); |
| 4572 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4573 | // struct _class_t { |
| 4574 | // struct _class_t *isa; |
| 4575 | // struct _class_t * const superclass; |
| 4576 | // void *cache; |
| 4577 | // IMP *vtable; |
| 4578 | // struct class_ro_t *ro; |
| 4579 | // } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4580 | |
Owen Anderson | c36edfe | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 4581 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext); |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4582 | ClassnfABITy = |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4583 | llvm::StructType::get(VMContext, |
| 4584 | llvm::PointerType::getUnqual(ClassTyHolder), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4585 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 4586 | CachePtrTy, |
| 4587 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 4588 | llvm::PointerType::getUnqual(ClassRonfABITy), |
| 4589 | NULL); |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4590 | CGM.getModule().addTypeName("struct._class_t", ClassnfABITy); |
| 4591 | |
| 4592 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4593 | ClassnfABITy); |
| 4594 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4595 | // LLVM for struct _class_t * |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4596 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4597 | |
Fariborz Jahanian | 0232c05 | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4598 | // struct _category_t { |
| 4599 | // const char * const name; |
| 4600 | // struct _class_t *const cls; |
| 4601 | // const struct _method_list_t * const instance_methods; |
| 4602 | // const struct _method_list_t * const class_methods; |
| 4603 | // const struct _protocol_list_t * const protocols; |
| 4604 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 5a63e4c | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 4605 | // } |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4606 | CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy, |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4607 | ClassnfABIPtrTy, |
Fariborz Jahanian | 5a63e4c | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 4608 | MethodListnfABIPtrTy, |
| 4609 | MethodListnfABIPtrTy, |
| 4610 | ProtocolListnfABIPtrTy, |
| 4611 | PropertyListPtrTy, |
| 4612 | NULL); |
| 4613 | CGM.getModule().addTypeName("struct._category_t", CategorynfABITy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4614 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4615 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4616 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 4617 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4618 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4619 | // MessageRefTy - LLVM for: |
| 4620 | // struct _message_ref_t { |
| 4621 | // IMP messenger; |
| 4622 | // SEL name; |
| 4623 | // }; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4624 | |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4625 | // First the clang type for struct _message_ref_t |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4626 | RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct, |
Daniel Dunbar | 0c00537 | 2010-04-29 16:29:11 +0000 | [diff] [blame] | 4627 | Ctx.getTranslationUnitDecl(), |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4628 | SourceLocation(), |
| 4629 | &Ctx.Idents.get("_message_ref_t")); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4630 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 4631 | Ctx.VoidPtrTy, 0, 0, false)); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4632 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 4633 | Ctx.getObjCSelType(), 0, 0, false)); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4634 | RD->completeDefinition(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4635 | |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4636 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 4637 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 4638 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4639 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4640 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4641 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4642 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4643 | // SuperMessageRefTy - LLVM for: |
| 4644 | // struct _super_message_ref_t { |
| 4645 | // SUPER_IMP messenger; |
| 4646 | // SEL name; |
| 4647 | // }; |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4648 | SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy, |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4649 | SelectorPtrTy, |
| 4650 | NULL); |
| 4651 | CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4652 | |
Fariborz Jahanian | 82c72e1 | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4653 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4654 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
| 4655 | |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 4656 | |
| 4657 | // struct objc_typeinfo { |
| 4658 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 4659 | // const char* name; // c++ typeinfo string |
| 4660 | // Class cls; |
| 4661 | // }; |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4662 | EHTypeTy = llvm::StructType::get(VMContext, |
| 4663 | llvm::PointerType::getUnqual(Int8PtrTy), |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 4664 | Int8PtrTy, |
| 4665 | ClassnfABIPtrTy, |
| 4666 | NULL); |
Daniel Dunbar | 76b7acc | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 4667 | CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4668 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4669 | } |
| 4670 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4671 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4672 | FinishNonFragileABIModule(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4673 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4674 | return NULL; |
| 4675 | } |
| 4676 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4677 | void CGObjCNonFragileABIMac::AddModuleClassList(const |
| 4678 | std::vector<llvm::GlobalValue*> |
| 4679 | &Container, |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4680 | const char *SymbolName, |
| 4681 | const char *SectionName) { |
| 4682 | unsigned NumClasses = Container.size(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4683 | |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4684 | if (!NumClasses) |
| 4685 | return; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4686 | |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4687 | std::vector<llvm::Constant*> Symbols(NumClasses); |
| 4688 | for (unsigned i=0; i<NumClasses; i++) |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4689 | Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i], |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4690 | ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4691 | llvm::Constant* Init = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4692 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4693 | NumClasses), |
| 4694 | Symbols); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4695 | |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4696 | llvm::GlobalVariable *GV = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4697 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4698 | llvm::GlobalValue::InternalLinkage, |
| 4699 | Init, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4700 | SymbolName); |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 4701 | GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType())); |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4702 | GV->setSection(SectionName); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4703 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4704 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4705 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4706 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 4707 | // nonfragile abi has no module definition. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4708 | |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4709 | // Build list of all implemented class addresses in array |
Fariborz Jahanian | 279abd3 | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4710 | // L_OBJC_LABEL_CLASS_$. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4711 | AddModuleClassList(DefinedClasses, |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4712 | "\01L_OBJC_LABEL_CLASS_$", |
| 4713 | "__DATA, __objc_classlist, regular, no_dead_strip"); |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4714 | |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4715 | for (unsigned i = 0; i < DefinedClasses.size(); i++) { |
| 4716 | llvm::GlobalValue *IMPLGV = DefinedClasses[i]; |
| 4717 | if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage) |
| 4718 | continue; |
| 4719 | IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4720 | } |
| 4721 | |
Fariborz Jahanian | a6227fd | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 4722 | for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) { |
| 4723 | llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i]; |
| 4724 | if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage) |
| 4725 | continue; |
| 4726 | IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 4727 | } |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4728 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4729 | AddModuleClassList(DefinedNonLazyClasses, |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4730 | "\01L_OBJC_LABEL_NONLAZY_CLASS_$", |
| 4731 | "__DATA, __objc_nlclslist, regular, no_dead_strip"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4732 | |
Fariborz Jahanian | 279abd3 | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4733 | // Build list of all implemented category addresses in array |
| 4734 | // L_OBJC_LABEL_CATEGORY_$. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4735 | AddModuleClassList(DefinedCategories, |
Daniel Dunbar | 19573e7 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4736 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 4737 | "__DATA, __objc_catlist, regular, no_dead_strip"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4738 | AddModuleClassList(DefinedNonLazyCategories, |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4739 | "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$", |
| 4740 | "__DATA, __objc_nlcatlist, regular, no_dead_strip"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4741 | |
Daniel Dunbar | 5e63927 | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 4742 | EmitImageInfo(); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4743 | } |
| 4744 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4745 | /// LegacyDispatchedSelector - Returns true if SEL is not in the list of |
Fariborz Jahanian | e412864 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4746 | /// NonLegacyDispatchMethods; false otherwise. What this means is that |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4747 | /// except for the 19 selectors in the list, we generate 32bit-style |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4748 | /// message dispatch call for all the rest. |
| 4749 | /// |
| 4750 | bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) { |
Daniel Dunbar | fca18c1b4 | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4751 | switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) { |
| 4752 | default: |
| 4753 | assert(0 && "Invalid dispatch method!"); |
| 4754 | case CodeGenOptions::Legacy: |
Daniel Dunbar | ca5e3eb | 2010-02-01 21:07:33 +0000 | [diff] [blame] | 4755 | return true; |
Daniel Dunbar | fca18c1b4 | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4756 | case CodeGenOptions::NonLegacy: |
Fariborz Jahanian | dfb3983 | 2010-04-19 17:53:30 +0000 | [diff] [blame] | 4757 | return false; |
Daniel Dunbar | fca18c1b4 | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4758 | case CodeGenOptions::Mixed: |
| 4759 | break; |
| 4760 | } |
| 4761 | |
| 4762 | // If so, see whether this selector is in the white-list of things which must |
| 4763 | // use the new dispatch convention. We lazily build a dense set for this. |
Fariborz Jahanian | e412864 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4764 | if (NonLegacyDispatchMethods.empty()) { |
| 4765 | NonLegacyDispatchMethods.insert(GetNullarySelector("alloc")); |
| 4766 | NonLegacyDispatchMethods.insert(GetNullarySelector("class")); |
| 4767 | NonLegacyDispatchMethods.insert(GetNullarySelector("self")); |
| 4768 | NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped")); |
| 4769 | NonLegacyDispatchMethods.insert(GetNullarySelector("length")); |
| 4770 | NonLegacyDispatchMethods.insert(GetNullarySelector("count")); |
| 4771 | NonLegacyDispatchMethods.insert(GetNullarySelector("retain")); |
| 4772 | NonLegacyDispatchMethods.insert(GetNullarySelector("release")); |
| 4773 | NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease")); |
| 4774 | NonLegacyDispatchMethods.insert(GetNullarySelector("hash")); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4775 | |
Fariborz Jahanian | e412864 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4776 | NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone")); |
| 4777 | NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass")); |
| 4778 | NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector")); |
| 4779 | NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey")); |
| 4780 | NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex")); |
| 4781 | NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString")); |
| 4782 | NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual")); |
| 4783 | NonLegacyDispatchMethods.insert(GetUnarySelector("addObject")); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4784 | // "countByEnumeratingWithState:objects:count" |
Fariborz Jahanian | 1880136 | 2009-05-13 16:19:02 +0000 | [diff] [blame] | 4785 | IdentifierInfo *KeyIdents[] = { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4786 | &CGM.getContext().Idents.get("countByEnumeratingWithState"), |
| 4787 | &CGM.getContext().Idents.get("objects"), |
| 4788 | &CGM.getContext().Idents.get("count") |
Fariborz Jahanian | 1880136 | 2009-05-13 16:19:02 +0000 | [diff] [blame] | 4789 | }; |
| 4790 | NonLegacyDispatchMethods.insert( |
| 4791 | CGM.getContext().Selectors.getSelector(3, KeyIdents)); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4792 | } |
Daniel Dunbar | fca18c1b4 | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4793 | |
Fariborz Jahanian | e412864 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4794 | return (NonLegacyDispatchMethods.count(Sel) == 0); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4795 | } |
| 4796 | |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4797 | // Metadata flags |
| 4798 | enum MetaDataDlags { |
| 4799 | CLS = 0x0, |
| 4800 | CLS_META = 0x1, |
| 4801 | CLS_ROOT = 0x2, |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4802 | OBJC2_CLS_HIDDEN = 0x10, |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4803 | CLS_EXCEPTION = 0x20 |
| 4804 | }; |
| 4805 | /// BuildClassRoTInitializer - generate meta-data for: |
| 4806 | /// struct _class_ro_t { |
| 4807 | /// uint32_t const flags; |
| 4808 | /// uint32_t const instanceStart; |
| 4809 | /// uint32_t const instanceSize; |
| 4810 | /// uint32_t const reserved; // only when building for 64bit targets |
| 4811 | /// const uint8_t * const ivarLayout; |
| 4812 | /// const char *const name; |
| 4813 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4814 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4815 | /// const struct _ivar_list_t *const ivars; |
| 4816 | /// const uint8_t * const weakIvarLayout; |
| 4817 | /// const struct _prop_list_t * const properties; |
| 4818 | /// } |
| 4819 | /// |
| 4820 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4821 | unsigned flags, |
| 4822 | unsigned InstanceStart, |
| 4823 | unsigned InstanceSize, |
| 4824 | const ObjCImplementationDecl *ID) { |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4825 | std::string ClassName = ID->getNameAsString(); |
| 4826 | std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets! |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4827 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 4828 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 4829 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4830 | // FIXME. For 64bit targets add 0 here. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4831 | Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4832 | : BuildIvarLayout(ID, true); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4833 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4834 | // const struct _method_list_t * const baseMethods; |
| 4835 | std::vector<llvm::Constant*> Methods; |
| 4836 | std::string MethodListName("\01l_OBJC_$_"); |
| 4837 | if (flags & CLS_META) { |
| 4838 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4839 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4840 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4841 | // Class methods should always be defined. |
| 4842 | Methods.push_back(GetMethodConstant(*i)); |
| 4843 | } |
| 4844 | } else { |
| 4845 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4846 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4847 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4848 | // Instance methods should always be defined. |
| 4849 | Methods.push_back(GetMethodConstant(*i)); |
| 4850 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4851 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4852 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
Fariborz Jahanian | d27a820 | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4853 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4854 | |
Fariborz Jahanian | d27a820 | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4855 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 4856 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4857 | |
Fariborz Jahanian | d27a820 | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4858 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 4859 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4860 | Methods.push_back(C); |
| 4861 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 4862 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4863 | Methods.push_back(C); |
| 4864 | } |
| 4865 | } |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4866 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4867 | Values[ 5] = EmitMethodList(MethodListName, |
| 4868 | "__DATA, __objc_const", Methods); |
| 4869 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4870 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4871 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4872 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4873 | + OID->getName(), |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 4874 | OID->all_referenced_protocol_begin(), |
| 4875 | OID->all_referenced_protocol_end()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4876 | |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4877 | if (flags & CLS_META) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4878 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4879 | else |
| 4880 | Values[ 7] = EmitIvarList(ID); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4881 | Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4882 | : BuildIvarLayout(ID, false); |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4883 | if (flags & CLS_META) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4884 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4885 | else |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4886 | Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(), |
| 4887 | ID, ID->getClassInterface(), ObjCTypes); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4888 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4889 | Values); |
| 4890 | llvm::GlobalVariable *CLASS_RO_GV = |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4891 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false, |
| 4892 | llvm::GlobalValue::InternalLinkage, |
| 4893 | Init, |
| 4894 | (flags & CLS_META) ? |
| 4895 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 4896 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4897 | CLASS_RO_GV->setAlignment( |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 4898 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 40a4bcd | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4899 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4900 | return CLASS_RO_GV; |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4901 | |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4902 | } |
| 4903 | |
| 4904 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 4905 | /// for the given ClassName for: |
| 4906 | /// struct _class_t { |
| 4907 | /// struct _class_t *isa; |
| 4908 | /// struct _class_t * const superclass; |
| 4909 | /// void *cache; |
| 4910 | /// IMP *vtable; |
| 4911 | /// struct class_ro_t *ro; |
| 4912 | /// } |
| 4913 | /// |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4914 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4915 | std::string &ClassName, |
| 4916 | llvm::Constant *IsAGV, |
| 4917 | llvm::Constant *SuperClassGV, |
| 4918 | llvm::Constant *ClassRoGV, |
| 4919 | bool HiddenVisibility) { |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4920 | std::vector<llvm::Constant*> Values(5); |
| 4921 | Values[0] = IsAGV; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4922 | Values[1] = SuperClassGV; |
| 4923 | if (!Values[1]) |
| 4924 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4925 | Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar |
| 4926 | Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar |
| 4927 | Values[4] = ClassRoGV; // &CLASS_RO_GV |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4928 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4929 | Values); |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4930 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 4931 | GV->setInitializer(Init); |
Fariborz Jahanian | 0408723 | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 4932 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4933 | GV->setAlignment( |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 4934 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | 8220825 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4935 | if (HiddenVisibility) |
| 4936 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4937 | return GV; |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4938 | } |
| 4939 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4940 | bool |
Fariborz Jahanian | a6bed83 | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 4941 | CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4942 | return OD->getClassMethod(GetNullarySelector("load")) != 0; |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4943 | } |
| 4944 | |
Daniel Dunbar | 96120237 | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4945 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | 554fd79 | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4946 | uint32_t &InstanceStart, |
| 4947 | uint32_t &InstanceSize) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4948 | const ASTRecordLayout &RL = |
Daniel Dunbar | 9252ee1 | 2009-05-04 21:26:30 +0000 | [diff] [blame] | 4949 | CGM.getContext().getASTObjCImplementationLayout(OID); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4950 | |
Daniel Dunbar | 9b042e0 | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 4951 | // InstanceSize is really instance end. |
Anders Carlsson | 27b5013 | 2009-07-18 21:26:44 +0000 | [diff] [blame] | 4952 | InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8; |
Daniel Dunbar | 9b042e0 | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 4953 | |
| 4954 | // If there are no fields, the start is the same as the end. |
| 4955 | if (!RL.getFieldCount()) |
| 4956 | InstanceStart = InstanceSize; |
| 4957 | else |
| 4958 | InstanceStart = RL.getFieldOffset(0) / 8; |
Daniel Dunbar | 554fd79 | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4959 | } |
| 4960 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4961 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 4962 | std::string ClassName = ID->getNameAsString(); |
| 4963 | if (!ObjCEmptyCacheVar) { |
| 4964 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4965 | CGM.getModule(), |
| 4966 | ObjCTypes.CacheTy, |
| 4967 | false, |
| 4968 | llvm::GlobalValue::ExternalLinkage, |
| 4969 | 0, |
| 4970 | "_objc_empty_cache"); |
| 4971 | |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4972 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4973 | CGM.getModule(), |
| 4974 | ObjCTypes.ImpnfABITy, |
| 4975 | false, |
| 4976 | llvm::GlobalValue::ExternalLinkage, |
| 4977 | 0, |
| 4978 | "_objc_empty_vtable"); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4979 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4980 | assert(ID->getClassInterface() && |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4981 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Daniel Dunbar | e3f5cfc | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 4982 | // FIXME: Is this correct (that meta class size is never computed)? |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4983 | uint32_t InstanceStart = |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4984 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4985 | uint32_t InstanceSize = InstanceStart; |
| 4986 | uint32_t flags = CLS_META; |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4987 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 4988 | std::string ObjCClassName(getClassSymbolPrefix()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4989 | |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4990 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4991 | |
| 4992 | bool classIsHidden = |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 4993 | ID->getClassInterface()->getVisibility() == HiddenVisibility; |
Fariborz Jahanian | 8220825 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4994 | if (classIsHidden) |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4995 | flags |= OBJC2_CLS_HIDDEN; |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 4996 | if (ID->getNumIvarInitializers()) |
| 4997 | flags |= eClassFlags_ABI2_HasCXXStructors; |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4998 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4999 | // class is root |
| 5000 | flags |= CLS_ROOT; |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5001 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Fariborz Jahanian | 899e7eb | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5002 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5003 | } else { |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5004 | // Has a root. Current class is not a root. |
Fariborz Jahanian | 03b300b | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5005 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 5006 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 5007 | Root = Super; |
Fariborz Jahanian | 899e7eb | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5008 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); |
Fariborz Jahanian | a6227fd | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 5009 | if (Root->hasAttr<WeakImportAttr>()) |
| 5010 | IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | 03b300b | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5011 | // work on super class metadata symbol. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5012 | std::string SuperClassName = |
Fariborz Jahanian | a6227fd | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 5013 | ObjCMetaClassName + |
| 5014 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 899e7eb | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5015 | SuperClassGV = GetClassGlobal(SuperClassName); |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5016 | if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>()) |
| 5017 | SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | 9e3ad52 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5018 | } |
| 5019 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 5020 | InstanceStart, |
| 5021 | InstanceSize,ID); |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5022 | std::string TClassName = ObjCMetaClassName + ClassName; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5023 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | 8220825 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5024 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 5025 | classIsHidden); |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5026 | DefinedMetaClasses.push_back(MetaTClass); |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5027 | |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5028 | // Metadata for the class |
| 5029 | flags = CLS; |
Fariborz Jahanian | 8220825 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5030 | if (classIsHidden) |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5031 | flags |= OBJC2_CLS_HIDDEN; |
Fariborz Jahanian | 0dec1e0 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 5032 | if (ID->getNumIvarInitializers()) |
| 5033 | flags |= eClassFlags_ABI2_HasCXXStructors; |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5034 | |
Douglas Gregor | 78bd61f | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 5035 | if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface())) |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5036 | flags |= CLS_EXCEPTION; |
| 5037 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5038 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5039 | flags |= CLS_ROOT; |
| 5040 | SuperClassGV = 0; |
Chris Lattner | b433b27 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 5041 | } else { |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5042 | // Has a root. Current class is not a root. |
Fariborz Jahanian | 03b300b | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5043 | std::string RootClassName = |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5044 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5045 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Fariborz Jahanian | 6726055 | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5046 | if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>()) |
| 5047 | SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5048 | } |
Daniel Dunbar | 96120237 | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 5049 | GetClassSizeInfo(ID, InstanceStart, InstanceSize); |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5050 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | a887e63 | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 5051 | InstanceStart, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5052 | InstanceSize, |
Fariborz Jahanian | a887e63 | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 5053 | ID); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5054 | |
Fariborz Jahanian | 4723fb7 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5055 | TClassName = ObjCClassName + ClassName; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5056 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | 8220825 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5057 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 5058 | classIsHidden); |
Fariborz Jahanian | 279abd3 | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 5059 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5060 | |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5061 | // Determine if this class is also "non-lazy". |
| 5062 | if (ImplementationIsNonLazy(ID)) |
| 5063 | DefinedNonLazyClasses.push_back(ClassMD); |
| 5064 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5065 | // Force the definition of the EHType if necessary. |
| 5066 | if (flags & CLS_EXCEPTION) |
| 5067 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5068 | } |
| 5069 | |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5070 | /// GenerateProtocolRef - This routine is called to generate code for |
| 5071 | /// a protocol reference expression; as in: |
| 5072 | /// @code |
| 5073 | /// @protocol(Proto1); |
| 5074 | /// @endcode |
| 5075 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 5076 | /// which will hold address of the protocol meta-data. |
| 5077 | /// |
| 5078 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5079 | const ObjCProtocolDecl *PD) { |
| 5080 | |
Fariborz Jahanian | 464423d | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 5081 | // This routine is called for @protocol only. So, we must build definition |
| 5082 | // of protocol's meta-data (not a reference to it!) |
| 5083 | // |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5084 | llvm::Constant *Init = |
| 5085 | llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
| 5086 | ObjCTypes.ExternalProtocolPtrTy); |
| 5087 | |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5088 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
Daniel Dunbar | 56df977 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5089 | ProtocolName += PD->getName(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5090 | |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5091 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 5092 | if (PTGV) |
Daniel Dunbar | c76493a | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 5093 | return Builder.CreateLoad(PTGV, "tmp"); |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5094 | PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5095 | CGM.getModule(), |
| 5096 | Init->getType(), false, |
| 5097 | llvm::GlobalValue::WeakAnyLinkage, |
| 5098 | Init, |
| 5099 | ProtocolName); |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5100 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 5101 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5102 | CGM.AddUsedGlobal(PTGV); |
Daniel Dunbar | c76493a | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 5103 | return Builder.CreateLoad(PTGV, "tmp"); |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5104 | } |
| 5105 | |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5106 | /// GenerateCategory - Build metadata for a category implementation. |
| 5107 | /// struct _category_t { |
| 5108 | /// const char * const name; |
| 5109 | /// struct _class_t *const cls; |
| 5110 | /// const struct _method_list_t * const instance_methods; |
| 5111 | /// const struct _method_list_t * const class_methods; |
| 5112 | /// const struct _protocol_list_t * const protocols; |
| 5113 | /// const struct _prop_list_t * const properties; |
| 5114 | /// } |
| 5115 | /// |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5116 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5117 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5118 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5119 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
| 5120 | "_$_" + OCD->getNameAsString()); |
| 5121 | std::string ExtClassName(getClassSymbolPrefix() + |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5122 | Interface->getNameAsString()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5123 | |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5124 | std::vector<llvm::Constant*> Values(6); |
| 5125 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 5126 | // meta-class entry symbol |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5127 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Fariborz Jahanian | 3ad8dcf | 2009-11-17 22:02:21 +0000 | [diff] [blame] | 5128 | if (Interface->hasAttr<WeakImportAttr>()) |
| 5129 | ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
| 5130 | |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5131 | Values[1] = ClassGV; |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5132 | std::vector<llvm::Constant*> Methods; |
| 5133 | std::string MethodListName(Prefix); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5134 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5135 | "_$_" + OCD->getNameAsString(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5136 | |
| 5137 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5138 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5139 | // Instance methods should always be defined. |
| 5140 | Methods.push_back(GetMethodConstant(*i)); |
| 5141 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5142 | |
| 5143 | Values[2] = EmitMethodList(MethodListName, |
| 5144 | "__DATA, __objc_const", |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5145 | Methods); |
| 5146 | |
| 5147 | MethodListName = Prefix; |
| 5148 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 5149 | OCD->getNameAsString(); |
| 5150 | Methods.clear(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5151 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5152 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5153 | // Class methods should always be defined. |
| 5154 | Methods.push_back(GetMethodConstant(*i)); |
| 5155 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5156 | |
| 5157 | Values[3] = EmitMethodList(MethodListName, |
| 5158 | "__DATA, __objc_const", |
Fariborz Jahanian | 2612e14 | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5159 | Methods); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5160 | const ObjCCategoryDecl *Category = |
Fariborz Jahanian | 066347e | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 5161 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | d8fc105 | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5162 | if (Category) { |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5163 | llvm::SmallString<256> ExtName; |
| 5164 | llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_" |
| 5165 | << OCD->getName(); |
Fariborz Jahanian | d8fc105 | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5166 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5167 | + Interface->getName() + "_$_" |
| 5168 | + Category->getName(), |
Fariborz Jahanian | d8fc105 | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5169 | Category->protocol_begin(), |
| 5170 | Category->protocol_end()); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5171 | Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(), |
| 5172 | OCD, Category, ObjCTypes); |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5173 | } else { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5174 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 5175 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Fariborz Jahanian | d8fc105 | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5176 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5177 | |
| 5178 | llvm::Constant *Init = |
| 5179 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5180 | Values); |
| 5181 | llvm::GlobalVariable *GCATV |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5182 | = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5183 | false, |
| 5184 | llvm::GlobalValue::InternalLinkage, |
| 5185 | Init, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5186 | ExtCatName); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5187 | GCATV->setAlignment( |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5188 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 40a4bcd | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5189 | GCATV->setSection("__DATA, __objc_const"); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5190 | CGM.AddUsedGlobal(GCATV); |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5191 | DefinedCategories.push_back(GCATV); |
Daniel Dunbar | 9a017d7 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5192 | |
| 5193 | // Determine if this category is also "non-lazy". |
| 5194 | if (ImplementationIsNonLazy(OCD)) |
| 5195 | DefinedNonLazyCategories.push_back(GCATV); |
Fariborz Jahanian | 0c8d060 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5196 | } |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5197 | |
| 5198 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 5199 | /// given method if it has been defined. The result is null if the |
| 5200 | /// method has not been defined. The return value has type MethodPtrTy. |
| 5201 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5202 | const ObjCMethodDecl *MD) { |
Argyrios Kyrtzidis | 13257c5 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 5203 | llvm::Function *Fn = GetMethodDefinition(MD); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5204 | if (!Fn) |
| 5205 | return 0; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5206 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5207 | std::vector<llvm::Constant*> Method(3); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5208 | Method[0] = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 5209 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5210 | ObjCTypes.SelectorPtrTy); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5211 | Method[1] = GetMethodVarType(MD); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 5212 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5213 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5214 | } |
| 5215 | |
| 5216 | /// EmitMethodList - Build meta-data for method declarations |
| 5217 | /// struct _method_list_t { |
| 5218 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 5219 | /// uint32_t method_count; |
| 5220 | /// struct _objc_method method_list[method_count]; |
| 5221 | /// } |
| 5222 | /// |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5223 | llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name, |
| 5224 | const char *Section, |
| 5225 | const ConstantVector &Methods) { |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5226 | // Return null for empty list. |
| 5227 | if (Methods.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5228 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5229 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5230 | std::vector<llvm::Constant*> Values(3); |
| 5231 | // sizeof(struct _objc_method) |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5232 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5233 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5234 | // method_count |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5235 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5236 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5237 | Methods.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 5238 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
Nick Lewycky | 41eaf0a | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 5239 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5240 | |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5241 | llvm::GlobalVariable *GV = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5242 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5243 | llvm::GlobalValue::InternalLinkage, |
| 5244 | Init, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5245 | Name); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5246 | GV->setAlignment( |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5247 | CGM.getTargetData().getABITypeAlignment(Init->getType())); |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5248 | GV->setSection(Section); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5249 | CGM.AddUsedGlobal(GV); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 5250 | return llvm::ConstantExpr::getBitCast(GV, |
Fariborz Jahanian | 99113fd | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5251 | ObjCTypes.MethodListnfABIPtrTy); |
| 5252 | } |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5253 | |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5254 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 5255 | /// the given ivar. |
Daniel Dunbar | 8c7f981 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 5256 | llvm::GlobalVariable * |
| 5257 | CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID, |
| 5258 | const ObjCIvarDecl *Ivar) { |
| 5259 | const ObjCInterfaceDecl *Container = Ivar->getContainingInterface(); |
Daniel Dunbar | 3f86b9c | 2009-05-05 00:36:57 +0000 | [diff] [blame] | 5260 | std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() + |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 5261 | '.' + Ivar->getNameAsString(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5262 | llvm::GlobalVariable *IvarOffsetGV = |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5263 | CGM.getModule().getGlobalVariable(Name); |
| 5264 | if (!IvarOffsetGV) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5265 | IvarOffsetGV = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5266 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy, |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5267 | false, |
| 5268 | llvm::GlobalValue::ExternalLinkage, |
| 5269 | 0, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5270 | Name); |
Fariborz Jahanian | 4e7ae06 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5271 | return IvarOffsetGV; |
| 5272 | } |
| 5273 | |
Daniel Dunbar | 8c7f981 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 5274 | llvm::Constant * |
| 5275 | CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
| 5276 | const ObjCIvarDecl *Ivar, |
| 5277 | unsigned long int Offset) { |
Daniel Dunbar | bf90b33 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5278 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5279 | IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy, |
Daniel Dunbar | bf90b33 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5280 | Offset)); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5281 | IvarOffsetGV->setAlignment( |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5282 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy)); |
Daniel Dunbar | bf90b33 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5283 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5284 | // FIXME: This matches gcc, but shouldn't the visibility be set on the use as |
| 5285 | // well (i.e., in ObjCIvarOffsetVariable). |
Daniel Dunbar | bf90b33 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5286 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 5287 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 5288 | ID->getVisibility() == HiddenVisibility) |
Fariborz Jahanian | 3d3426f | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 5289 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | f5f359f | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 5290 | else |
Fariborz Jahanian | bc3c77b | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 5291 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Fariborz Jahanian | 40a4bcd | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5292 | IvarOffsetGV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5293 | return IvarOffsetGV; |
Fariborz Jahanian | 40a4bcd | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5294 | } |
| 5295 | |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5296 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5297 | /// implementation. The return value has type |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5298 | /// IvarListnfABIPtrTy. |
| 5299 | /// struct _ivar_t { |
| 5300 | /// unsigned long int *offset; // pointer to ivar offset location |
| 5301 | /// char *name; |
| 5302 | /// char *type; |
| 5303 | /// uint32_t alignment; |
| 5304 | /// uint32_t size; |
| 5305 | /// } |
| 5306 | /// struct _ivar_list_t { |
| 5307 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 5308 | /// uint32 count; |
| 5309 | /// struct _iver_t list[count]; |
| 5310 | /// } |
| 5311 | /// |
Daniel Dunbar | f5c1846 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 5312 | |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5313 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5314 | const ObjCImplementationDecl *ID) { |
| 5315 | |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5316 | std::vector<llvm::Constant*> Ivars, Ivar(5); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5317 | |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5318 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 5319 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5320 | |
Fariborz Jahanian | 40a4bcd | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5321 | // FIXME. Consolidate this with similar code in GenerateClass. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5322 | |
Daniel Dunbar | ae03226 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 5323 | // Collect declared and synthesized ivars in a small vector. |
Fariborz Jahanian | 63a224a | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 5324 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 5325 | CGM.getContext().ShallowCollectObjCIvars(OID, OIvars); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5326 | |
Daniel Dunbar | f5c1846 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 5327 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 5328 | ObjCIvarDecl *IVD = OIvars[i]; |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 5329 | // Ignore unnamed bit-fields. |
| 5330 | if (!IVD->getDeclName()) |
| 5331 | continue; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5332 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD, |
Daniel Dunbar | 96120237 | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 5333 | ComputeIvarBaseOffset(CGM, ID, IVD)); |
Daniel Dunbar | 725dc2c | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 5334 | Ivar[1] = GetMethodVarName(IVD->getIdentifier()); |
| 5335 | Ivar[2] = GetMethodVarType(IVD); |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5336 | const llvm::Type *FieldTy = |
Daniel Dunbar | 725dc2c | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 5337 | CGM.getTypes().ConvertTypeForMem(IVD->getType()); |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5338 | unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy); |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5339 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5340 | IVD->getType().getTypePtr()) >> 3; |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5341 | Align = llvm::Log2_32(Align); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5342 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Daniel Dunbar | ae03226 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 5343 | // NOTE. Size of a bitfield does not match gcc's, because of the |
| 5344 | // way bitfields are treated special in each. But I am told that |
| 5345 | // 'size' for bitfield ivars is ignored by the runtime so it does |
| 5346 | // not matter. If it matters, there is enough info to get the |
| 5347 | // bitfield right! |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5348 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5349 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5350 | } |
| 5351 | // Return null for empty list. |
| 5352 | if (Ivars.empty()) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5353 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5354 | std::vector<llvm::Constant*> Values(3); |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5355 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5356 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 5357 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5358 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5359 | Ivars.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 5360 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
Nick Lewycky | 41eaf0a | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 5361 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5362 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 5363 | llvm::GlobalVariable *GV = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5364 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | 7415caa | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5365 | llvm::GlobalValue::InternalLinkage, |
| 5366 | Init, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5367 | Prefix + OID->getName()); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5368 | GV->setAlignment( |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5369 | CGM.getTargetData().getABITypeAlignment(Init->getType())); |
Fariborz Jahanian | 40a4bcd | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5370 | GV->setSection("__DATA, __objc_const"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5371 | |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5372 | CGM.AddUsedGlobal(GV); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 5373 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5374 | } |
| 5375 | |
| 5376 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5377 | const ObjCProtocolDecl *PD) { |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5378 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5379 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5380 | if (!Entry) { |
| 5381 | // We use the initializer as a marker of whether this is a forward |
| 5382 | // reference or not. At module finalization we add the empty |
| 5383 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5384 | Entry = |
| 5385 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false, |
| 5386 | llvm::GlobalValue::ExternalLinkage, |
| 5387 | 0, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5388 | "\01l_OBJC_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5389 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5390 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5391 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5392 | return Entry; |
| 5393 | } |
| 5394 | |
| 5395 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 5396 | /// @code |
| 5397 | /// struct _protocol_t { |
| 5398 | /// id isa; // NULL |
| 5399 | /// const char * const protocol_name; |
| 5400 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 5401 | /// const struct method_list_t * const instance_methods; |
| 5402 | /// const struct method_list_t * const class_methods; |
| 5403 | /// const struct method_list_t *optionalInstanceMethods; |
| 5404 | /// const struct method_list_t *optionalClassMethods; |
| 5405 | /// const struct _prop_list_t * properties; |
| 5406 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 5407 | /// const uint32_t flags; // = 0 |
| 5408 | /// } |
| 5409 | /// @endcode |
| 5410 | /// |
| 5411 | |
| 5412 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5413 | const ObjCProtocolDecl *PD) { |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5414 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5415 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5416 | // Early exit if a defining object has already been generated. |
| 5417 | if (Entry && Entry->hasInitializer()) |
| 5418 | return Entry; |
| 5419 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5420 | // Construct method lists. |
| 5421 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 5422 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5423 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5424 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5425 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5426 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5427 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 5428 | OptInstanceMethods.push_back(C); |
| 5429 | } else { |
| 5430 | InstanceMethods.push_back(C); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5431 | } |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5432 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5433 | |
| 5434 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5435 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5436 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5437 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5438 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 5439 | OptClassMethods.push_back(C); |
| 5440 | } else { |
| 5441 | ClassMethods.push_back(C); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5442 | } |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5443 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5444 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5445 | std::vector<llvm::Constant*> Values(10); |
| 5446 | // isa is NULL |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5447 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5448 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5449 | Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(), |
| 5450 | PD->protocol_begin(), |
| 5451 | PD->protocol_end()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5452 | |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5453 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5454 | + PD->getName(), |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5455 | "__DATA, __objc_const", |
| 5456 | InstanceMethods); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5457 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5458 | + PD->getName(), |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5459 | "__DATA, __objc_const", |
| 5460 | ClassMethods); |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5461 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5462 | + PD->getName(), |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5463 | "__DATA, __objc_const", |
| 5464 | OptInstanceMethods); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5465 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5466 | + PD->getName(), |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5467 | "__DATA, __objc_const", |
| 5468 | OptClassMethods); |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5469 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(), |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5470 | 0, PD, ObjCTypes); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5471 | uint32_t Size = |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5472 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5473 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5474 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5475 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5476 | Values); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5477 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5478 | if (Entry) { |
| 5479 | // Already created, fix the linkage and update the initializer. |
Mike Stump | a6ca334 | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 5480 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5481 | Entry->setInitializer(Init); |
| 5482 | } else { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5483 | Entry = |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5484 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, |
| 5485 | false, llvm::GlobalValue::WeakAnyLinkage, Init, |
| 5486 | "\01l_OBJC_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5487 | Entry->setAlignment( |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5488 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5489 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5490 | } |
Fariborz Jahanian | 61cd4b5 | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5491 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5492 | CGM.AddUsedGlobal(Entry); |
| 5493 | |
Fariborz Jahanian | 61cd4b5 | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5494 | // Use this protocol meta-data to build protocol list table in section |
| 5495 | // __DATA, __objc_protolist |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5496 | llvm::GlobalVariable *PTGV = |
| 5497 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy, |
| 5498 | false, llvm::GlobalValue::WeakAnyLinkage, Entry, |
| 5499 | "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5500 | PTGV->setAlignment( |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5501 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | b25452a | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 5502 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Fariborz Jahanian | 61cd4b5 | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5503 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5504 | CGM.AddUsedGlobal(PTGV); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5505 | return Entry; |
| 5506 | } |
| 5507 | |
| 5508 | /// EmitProtocolList - Generate protocol list meta-data: |
| 5509 | /// @code |
| 5510 | /// struct _protocol_list_t { |
| 5511 | /// long protocol_count; // Note, this is 32/64 bit |
| 5512 | /// struct _protocol_t[protocol_count]; |
| 5513 | /// } |
| 5514 | /// @endcode |
| 5515 | /// |
| 5516 | llvm::Constant * |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5517 | CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name, |
| 5518 | ObjCProtocolDecl::protocol_iterator begin, |
| 5519 | ObjCProtocolDecl::protocol_iterator end) { |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5520 | std::vector<llvm::Constant*> ProtocolRefs; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5521 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5522 | // Just return null for empty protocol lists |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5523 | if (begin == end) |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5524 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5525 | |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5526 | // FIXME: We shouldn't need to do this lookup here, should we? |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5527 | llvm::SmallString<256> TmpName; |
| 5528 | Name.toVector(TmpName); |
| 5529 | llvm::GlobalVariable *GV = |
| 5530 | CGM.getModule().getGlobalVariable(TmpName.str(), true); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5531 | if (GV) |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5532 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5533 | |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5534 | for (; begin != end; ++begin) |
| 5535 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 5536 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5537 | // This list is null terminated. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5538 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5539 | ObjCTypes.ProtocolnfABIPtrTy)); |
| 5540 | |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5541 | std::vector<llvm::Constant*> Values(2); |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5542 | Values[0] = |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5543 | llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5544 | Values[1] = |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 5545 | llvm::ConstantArray::get( |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5546 | llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5547 | ProtocolRefs.size()), |
| 5548 | ProtocolRefs); |
| 5549 | |
Nick Lewycky | 41eaf0a | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 5550 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5551 | GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5552 | llvm::GlobalValue::InternalLinkage, |
| 5553 | Init, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5554 | Name); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5555 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | c22f236 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5556 | GV->setAlignment( |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5557 | CGM.getTargetData().getABITypeAlignment(Init->getType())); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5558 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5559 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | 8de90f0 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5560 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | 56b3b77 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5561 | } |
| 5562 | |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5563 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 5564 | /// struct _objc_method { |
| 5565 | /// SEL _cmd; |
| 5566 | /// char *method_type; |
| 5567 | /// char *_imp; |
| 5568 | /// } |
| 5569 | |
| 5570 | llvm::Constant * |
| 5571 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 5572 | std::vector<llvm::Constant*> Desc(3); |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5573 | Desc[0] = |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5574 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 5575 | ObjCTypes.SelectorPtrTy); |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5576 | Desc[1] = GetMethodVarType(MD); |
Fariborz Jahanian | 097feda | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5577 | // Protocol methods have no implementation. So, this entry is always NULL. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5578 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5579 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
Fariborz Jahanian | d9c28b8 | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5580 | } |
Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5581 | |
| 5582 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 5583 | /// This code gen. amounts to generating code for: |
| 5584 | /// @code |
| 5585 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 5586 | /// @encode |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5587 | /// |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 5588 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 5589 | CodeGen::CodeGenFunction &CGF, |
| 5590 | QualType ObjectTy, |
| 5591 | llvm::Value *BaseValue, |
| 5592 | const ObjCIvarDecl *Ivar, |
| 5593 | unsigned CVRQualifiers) { |
| 5594 | ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface(); |
Daniel Dunbar | 9fd114d | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 5595 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 5596 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | c88a70d | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5597 | } |
| 5598 | |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 5599 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5600 | CodeGen::CodeGenFunction &CGF, |
| 5601 | const ObjCInterfaceDecl *Interface, |
| 5602 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | c76493a | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 5603 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar"); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 5604 | } |
| 5605 | |
Fariborz Jahanian | 3d9296e | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5606 | CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5607 | CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 5608 | ReturnValueSlot Return, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5609 | QualType ResultType, |
| 5610 | Selector Sel, |
| 5611 | llvm::Value *Receiver, |
| 5612 | QualType Arg0Ty, |
| 5613 | bool IsSuper, |
| 5614 | const CallArgList &CallArgs) { |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5615 | // FIXME. Even though IsSuper is passes. This function doese not handle calls |
| 5616 | // to 'super' receivers. |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5617 | CodeGenTypes &Types = CGM.getTypes(); |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5618 | llvm::Value *Arg0 = Receiver; |
| 5619 | if (!IsSuper) |
| 5620 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5621 | |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5622 | // Find the message function name. |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5623 | // FIXME. This is too much work to get the ABI-specific result type needed to |
| 5624 | // find the message name. |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 5625 | const CGFunctionInfo &FnInfo |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 5626 | = Types.getFunctionInfo(ResultType, CallArgList(), |
| 5627 | FunctionType::ExtInfo()); |
Fariborz Jahanian | e29b4f0 | 2009-04-30 23:08:58 +0000 | [diff] [blame] | 5628 | llvm::Constant *Fn = 0; |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5629 | std::string Name("\01l_"); |
Daniel Dunbar | 6f2e839 | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 5630 | if (CGM.ReturnTypeUsesSRet(FnInfo)) { |
Chris Lattner | 396639d | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 5631 | if (IsSuper) { |
| 5632 | Fn = ObjCTypes.getMessageSendSuper2StretFixupFn(); |
| 5633 | Name += "objc_msgSendSuper2_stret_fixup"; |
| 5634 | } else { |
| 5635 | Fn = ObjCTypes.getMessageSendStretFixupFn(); |
| 5636 | Name += "objc_msgSend_stret_fixup"; |
| 5637 | } |
Daniel Dunbar | 6f2e839 | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 5638 | } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) { |
| 5639 | Fn = ObjCTypes.getMessageSendFpretFixupFn(); |
| 5640 | Name += "objc_msgSend_fpret_fixup"; |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5641 | } else { |
Chris Lattner | 396639d | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 5642 | if (IsSuper) { |
| 5643 | Fn = ObjCTypes.getMessageSendSuper2FixupFn(); |
| 5644 | Name += "objc_msgSendSuper2_fixup"; |
| 5645 | } else { |
| 5646 | Fn = ObjCTypes.getMessageSendFixupFn(); |
| 5647 | Name += "objc_msgSend_fixup"; |
| 5648 | } |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5649 | } |
Fariborz Jahanian | e29b4f0 | 2009-04-30 23:08:58 +0000 | [diff] [blame] | 5650 | assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend"); |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5651 | Name += '_'; |
| 5652 | std::string SelName(Sel.getAsString()); |
| 5653 | // Replace all ':' in selector name with '_' ouch! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5654 | for (unsigned i = 0; i < SelName.size(); i++) |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5655 | if (SelName[i] == ':') |
| 5656 | SelName[i] = '_'; |
| 5657 | Name += SelName; |
| 5658 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5659 | if (!GV) { |
Daniel Dunbar | e60aa05 | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5660 | // Build message ref table entry. |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5661 | std::vector<llvm::Constant*> Values(2); |
| 5662 | Values[0] = Fn; |
| 5663 | Values[1] = GetMethodVarName(Sel); |
Nick Lewycky | 41eaf0a | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 5664 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5665 | GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Mike Stump | a6ca334 | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 5666 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5667 | Init, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5668 | Name); |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5669 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 24645c9 | 2009-04-15 19:04:46 +0000 | [diff] [blame] | 5670 | GV->setAlignment(16); |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5671 | GV->setSection("__DATA, __objc_msgrefs, coalesced"); |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5672 | } |
| 5673 | llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5674 | |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5675 | CallArgList ActualArgs; |
| 5676 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5677 | ActualArgs.push_back(std::make_pair(RValue::get(Arg1), |
Fariborz Jahanian | e4dc35d | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5678 | ObjCTypes.MessageRefCPtrTy)); |
| 5679 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 5680 | const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 5681 | FunctionType::ExtInfo()); |
Fariborz Jahanian | 4e87c83 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5682 | llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0); |
| 5683 | Callee = CGF.Builder.CreateLoad(Callee); |
Fariborz Jahanian | 35afdfc | 2009-02-14 21:25:36 +0000 | [diff] [blame] | 5684 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true); |
Fariborz Jahanian | 4e87c83 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5685 | Callee = CGF.Builder.CreateBitCast(Callee, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5686 | llvm::PointerType::getUnqual(FTy)); |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 5687 | return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs); |
Fariborz Jahanian | 3d9296e | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5688 | } |
| 5689 | |
| 5690 | /// Generate code for a message send expression in the nonfragile abi. |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5691 | CodeGen::RValue |
| 5692 | CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 5693 | ReturnValueSlot Return, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5694 | QualType ResultType, |
| 5695 | Selector Sel, |
| 5696 | llvm::Value *Receiver, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5697 | const CallArgList &CallArgs, |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 5698 | const ObjCInterfaceDecl *Class, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5699 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5700 | return LegacyDispatchedSelector(Sel) |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 5701 | ? EmitLegacyMessageSend(CGF, Return, ResultType, |
| 5702 | EmitSelector(CGF.Builder, Sel), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5703 | Receiver, CGF.getContext().getObjCIdType(), |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5704 | false, CallArgs, Method, ObjCTypes) |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 5705 | : EmitMessageSend(CGF, Return, ResultType, Sel, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5706 | Receiver, CGF.getContext().getObjCIdType(), |
| 5707 | false, CallArgs); |
Fariborz Jahanian | 3d9296e | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5708 | } |
| 5709 | |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5710 | llvm::GlobalVariable * |
Fariborz Jahanian | 899e7eb | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5711 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5712 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5713 | |
Daniel Dunbar | a646834 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 5714 | if (!GV) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5715 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5716 | false, llvm::GlobalValue::ExternalLinkage, |
| 5717 | 0, Name); |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5718 | } |
| 5719 | |
| 5720 | return GV; |
| 5721 | } |
| 5722 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5723 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
| 5724 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5725 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5726 | |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5727 | if (!Entry) { |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5728 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | c6928bb | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5729 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5730 | Entry = |
| 5731 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5732 | false, llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5733 | ClassGV, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5734 | "\01L_OBJC_CLASSLIST_REFERENCES_$_"); |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5735 | Entry->setAlignment( |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5736 | CGM.getTargetData().getABITypeAlignment( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5737 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5738 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5739 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5740 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5741 | |
Daniel Dunbar | c76493a | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 5742 | return Builder.CreateLoad(Entry, "tmp"); |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5743 | } |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5744 | |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5745 | llvm::Value * |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5746 | CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5747 | const ObjCInterfaceDecl *ID) { |
| 5748 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5749 | |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5750 | if (!Entry) { |
| 5751 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5752 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5753 | Entry = |
| 5754 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5755 | false, llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5756 | ClassGV, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5757 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5758 | Entry->setAlignment( |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5759 | CGM.getTargetData().getABITypeAlignment( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5760 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5761 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5762 | CGM.AddUsedGlobal(Entry); |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5763 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5764 | |
Daniel Dunbar | c76493a | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 5765 | return Builder.CreateLoad(Entry, "tmp"); |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5766 | } |
| 5767 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5768 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 5769 | /// meta-data |
| 5770 | /// |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5771 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 5772 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5773 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 5774 | if (Entry) |
Daniel Dunbar | c76493a | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 5775 | return Builder.CreateLoad(Entry, "tmp"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5776 | |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5777 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | 899e7eb | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5778 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5779 | Entry = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5780 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false, |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5781 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5782 | MetaClassGV, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5783 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5784 | Entry->setAlignment( |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5785 | CGM.getTargetData().getABITypeAlignment( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5786 | ObjCTypes.ClassnfABIPtrTy)); |
| 5787 | |
Daniel Dunbar | e60aa05 | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5788 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5789 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5790 | |
Daniel Dunbar | c76493a | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 5791 | return Builder.CreateLoad(Entry, "tmp"); |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5792 | } |
| 5793 | |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5794 | /// GetClass - Return a reference to the class for the given interface |
| 5795 | /// decl. |
| 5796 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 5797 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 95ace55 | 2009-11-17 22:42:00 +0000 | [diff] [blame] | 5798 | if (ID->hasAttr<WeakImportAttr>()) { |
| 5799 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5800 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
| 5801 | ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
| 5802 | } |
| 5803 | |
Fariborz Jahanian | 33f66e6 | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5804 | return EmitClassRef(Builder, ID); |
| 5805 | } |
| 5806 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5807 | /// Generates a message send where the super is the receiver. This is |
| 5808 | /// a message send to self with special delivery semantics indicating |
| 5809 | /// which class's method should be called. |
| 5810 | CodeGen::RValue |
| 5811 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 5812 | ReturnValueSlot Return, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5813 | QualType ResultType, |
| 5814 | Selector Sel, |
| 5815 | const ObjCInterfaceDecl *Class, |
| 5816 | bool isCategoryImpl, |
| 5817 | llvm::Value *Receiver, |
| 5818 | bool IsClassMessage, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5819 | const CodeGen::CallArgList &CallArgs, |
| 5820 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5821 | // ... |
| 5822 | // Create and init a super structure; this is a (receiver, class) |
| 5823 | // pair we will pass to objc_msgSendSuper. |
| 5824 | llvm::Value *ObjCSuper = |
| 5825 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5826 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5827 | llvm::Value *ReceiverAsObject = |
| 5828 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 5829 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 5830 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5831 | |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5832 | // If this is a class message the metaclass is passed as the target. |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5833 | llvm::Value *Target; |
| 5834 | if (IsClassMessage) { |
| 5835 | if (isCategoryImpl) { |
| 5836 | // Message sent to "super' in a class method defined in |
| 5837 | // a category implementation. |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5838 | Target = EmitClassRef(CGF.Builder, Class); |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5839 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 5840 | Target = CGF.Builder.CreateLoad(Target); |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5841 | } else |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5842 | Target = EmitMetaClassRef(CGF.Builder, Class); |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5843 | } else |
Daniel Dunbar | 508a7dd | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5844 | Target = EmitSuperClassRef(CGF.Builder, Class); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5845 | |
Mike Stump | 18bb928 | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5846 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 5847 | // ObjCTypes types. |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5848 | const llvm::Type *ClassTy = |
| 5849 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 5850 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 5851 | CGF.Builder.CreateStore(Target, |
| 5852 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5853 | |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5854 | return (LegacyDispatchedSelector(Sel)) |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 5855 | ? EmitLegacyMessageSend(CGF, Return, ResultType, |
| 5856 | EmitSelector(CGF.Builder, Sel), |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5857 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5858 | true, CallArgs, Method, ObjCTypes) |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 5859 | : EmitMessageSend(CGF, Return, ResultType, Sel, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5860 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 5861 | true, CallArgs); |
Fariborz Jahanian | 6b7cd6e | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5862 | } |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5863 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5864 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 5865 | Selector Sel, bool lval) { |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5866 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5867 | |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5868 | if (!Entry) { |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5869 | llvm::Constant *Casted = |
| 5870 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 5871 | ObjCTypes.SelectorPtrTy); |
| 5872 | Entry = |
| 5873 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false, |
| 5874 | llvm::GlobalValue::InternalLinkage, |
| 5875 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_"); |
Fariborz Jahanian | 5d5ed2d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5876 | Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip"); |
Chris Lattner | f56501c | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5877 | CGM.AddUsedGlobal(Entry); |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5878 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5879 | |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 5880 | if (lval) |
| 5881 | return Entry; |
Daniel Dunbar | c76493a | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 5882 | return Builder.CreateLoad(Entry, "tmp"); |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5883 | } |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5884 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 5885 | /// objc_assign_ivar (id src, id *dst, ptrdiff_t) |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5886 | /// |
| 5887 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5888 | llvm::Value *src, |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 5889 | llvm::Value *dst, |
| 5890 | llvm::Value *ivarOffset) { |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5891 | const llvm::Type * SrcTy = src->getType(); |
| 5892 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5893 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5894 | assert(Size <= 8 && "does not support size > 8"); |
| 5895 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5896 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5897 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5898 | } |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5899 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5900 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 5901 | CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(), |
| 5902 | src, dst, ivarOffset); |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5903 | return; |
| 5904 | } |
| 5905 | |
| 5906 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 5907 | /// objc_assign_strongCast (id src, id *dst) |
| 5908 | /// |
| 5909 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5910 | CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5911 | llvm::Value *src, llvm::Value *dst) { |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5912 | const llvm::Type * SrcTy = src->getType(); |
| 5913 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5914 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5915 | assert(Size <= 8 && "does not support size > 8"); |
| 5916 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5917 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5918 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5919 | } |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5920 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5921 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 0a696a42 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5922 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5923 | src, dst, "weakassign"); |
| 5924 | return; |
| 5925 | } |
| 5926 | |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 5927 | void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5928 | CodeGen::CodeGenFunction &CGF, |
| 5929 | llvm::Value *DestPtr, |
| 5930 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 5931 | llvm::Value *Size) { |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 5932 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 5933 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 5934 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 5935 | DestPtr, SrcPtr, Size); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 5936 | return; |
| 5937 | } |
| 5938 | |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5939 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 5940 | /// object: objc_read_weak (id *src) |
| 5941 | /// |
| 5942 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5943 | CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5944 | llvm::Value *AddrWeakObj) { |
Eli Friedman | a374b68 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5945 | const llvm::Type* DestTy = |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5946 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
| 5947 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | ce8754e | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 5948 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5949 | AddrWeakObj, "weakread"); |
Eli Friedman | a374b68 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5950 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5951 | return read_weak; |
| 5952 | } |
| 5953 | |
| 5954 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 5955 | /// objc_assign_weak (id src, id *dst) |
| 5956 | /// |
| 5957 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5958 | llvm::Value *src, llvm::Value *dst) { |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5959 | const llvm::Type * SrcTy = src->getType(); |
| 5960 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5961 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5962 | assert(Size <= 8 && "does not support size > 8"); |
| 5963 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5964 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5965 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5966 | } |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5967 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5968 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 6fdd57c | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 5969 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5970 | src, dst, "weakassign"); |
| 5971 | return; |
| 5972 | } |
| 5973 | |
| 5974 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 5975 | /// objc_assign_global (id src, id *dst) |
| 5976 | /// |
| 5977 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 5978 | llvm::Value *src, llvm::Value *dst, |
| 5979 | bool threadlocal) { |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5980 | const llvm::Type * SrcTy = src->getType(); |
| 5981 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | c76fe8b | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5982 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | aedcfa4 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5983 | assert(Size <= 8 && "does not support size > 8"); |
| 5984 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5985 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 1b074a3 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5986 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5987 | } |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5988 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5989 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 5990 | if (!threadlocal) |
| 5991 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
| 5992 | src, dst, "globalassign"); |
| 5993 | else |
| 5994 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(), |
| 5995 | src, dst, "threadlocalassign"); |
Fariborz Jahanian | 0629295 | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5996 | return; |
| 5997 | } |
Fariborz Jahanian | 74b7722 | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5998 | |
John McCall | c20acd3 | 2010-07-21 00:41:47 +0000 | [diff] [blame] | 5999 | namespace { |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 6000 | struct CallSyncExit : EHScopeStack::Cleanup { |
John McCall | c20acd3 | 2010-07-21 00:41:47 +0000 | [diff] [blame] | 6001 | llvm::Value *SyncExitFn; |
| 6002 | llvm::Value *SyncArg; |
| 6003 | CallSyncExit(llvm::Value *SyncExitFn, llvm::Value *SyncArg) |
| 6004 | : SyncExitFn(SyncExitFn), SyncArg(SyncArg) {} |
| 6005 | |
| 6006 | void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) { |
| 6007 | CGF.Builder.CreateCall(SyncExitFn, SyncArg)->setDoesNotThrow(); |
| 6008 | } |
| 6009 | }; |
| 6010 | } |
| 6011 | |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6012 | void |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6013 | CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 6014 | const ObjCAtSynchronizedStmt &S) { |
| 6015 | // Evaluate the lock operand. This should dominate the cleanup. |
| 6016 | llvm::Value *SyncArg = CGF.EmitScalarExpr(S.getSynchExpr()); |
Daniel Dunbar | 0b0dcd9 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 6017 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6018 | // Acquire the lock. |
| 6019 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
| 6020 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg) |
| 6021 | ->setDoesNotThrow(); |
| 6022 | |
| 6023 | // Register an all-paths cleanup to release the lock. |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 6024 | CGF.EHStack.pushCleanup<CallSyncExit>(NormalAndEHCleanup, |
| 6025 | ObjCTypes.getSyncExitFn(), |
| 6026 | SyncArg); |
Daniel Dunbar | 0b0dcd9 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 6027 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6028 | // Emit the body of the statement. |
| 6029 | CGF.EmitStmt(S.getSynchBody()); |
Daniel Dunbar | 0b0dcd9 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 6030 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6031 | // Pop the lock-release cleanup. |
| 6032 | CGF.PopCleanupBlock(); |
| 6033 | } |
Daniel Dunbar | 0b0dcd9 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 6034 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6035 | namespace { |
| 6036 | struct CatchHandler { |
| 6037 | const VarDecl *Variable; |
| 6038 | const Stmt *Body; |
| 6039 | llvm::BasicBlock *Block; |
| 6040 | llvm::Value *TypeInfo; |
| 6041 | }; |
John McCall | 5c08ab9 | 2010-07-13 22:12:14 +0000 | [diff] [blame] | 6042 | |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 6043 | struct CallObjCEndCatch : EHScopeStack::Cleanup { |
John McCall | 5c08ab9 | 2010-07-13 22:12:14 +0000 | [diff] [blame] | 6044 | CallObjCEndCatch(bool MightThrow, llvm::Value *Fn) : |
| 6045 | MightThrow(MightThrow), Fn(Fn) {} |
| 6046 | bool MightThrow; |
| 6047 | llvm::Value *Fn; |
| 6048 | |
| 6049 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 6050 | if (!MightThrow) { |
| 6051 | CGF.Builder.CreateCall(Fn)->setDoesNotThrow(); |
| 6052 | return; |
| 6053 | } |
| 6054 | |
| 6055 | CGF.EmitCallOrInvoke(Fn, 0, 0); |
| 6056 | } |
| 6057 | }; |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6058 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6059 | |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6060 | llvm::Constant * |
| 6061 | CGObjCNonFragileABIMac::GetEHType(QualType T) { |
| 6062 | // There's a particular fixed type info for 'id'. |
| 6063 | if (T->isObjCIdType() || |
| 6064 | T->isObjCQualifiedIdType()) { |
| 6065 | llvm::Constant *IDEHType = |
| 6066 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 6067 | if (!IDEHType) |
| 6068 | IDEHType = |
| 6069 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, |
| 6070 | false, |
| 6071 | llvm::GlobalValue::ExternalLinkage, |
| 6072 | 0, "OBJC_EHTYPE_id"); |
| 6073 | return IDEHType; |
| 6074 | } |
| 6075 | |
| 6076 | // All other types should be Objective-C interface pointer types. |
| 6077 | const ObjCObjectPointerType *PT = |
| 6078 | T->getAs<ObjCObjectPointerType>(); |
| 6079 | assert(PT && "Invalid @catch type."); |
| 6080 | const ObjCInterfaceType *IT = PT->getInterfaceType(); |
| 6081 | assert(IT && "Invalid @catch type."); |
| 6082 | return GetInterfaceEHType(IT->getDecl(), false); |
| 6083 | } |
| 6084 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6085 | void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 6086 | const ObjCAtTryStmt &S) { |
| 6087 | // Jump destination for falling out of catch bodies. |
| 6088 | CodeGenFunction::JumpDest Cont; |
| 6089 | if (S.getNumCatchStmts()) |
| 6090 | Cont = CGF.getJumpDestInCurrentScope("eh.cont"); |
Daniel Dunbar | 76b7acc | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 6091 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6092 | CodeGenFunction::FinallyInfo FinallyInfo; |
| 6093 | if (const ObjCAtFinallyStmt *Finally = S.getFinallyStmt()) |
| 6094 | FinallyInfo = CGF.EnterFinallyBlock(Finally->getFinallyBody(), |
| 6095 | ObjCTypes.getObjCBeginCatchFn(), |
| 6096 | ObjCTypes.getObjCEndCatchFn(), |
| 6097 | ObjCTypes.getExceptionRethrowFn()); |
Daniel Dunbar | 76b7acc | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 6098 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6099 | llvm::SmallVector<CatchHandler, 8> Handlers; |
Daniel Dunbar | 76b7acc | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 6100 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6101 | // Enter the catch, if there is one. |
| 6102 | if (S.getNumCatchStmts()) { |
| 6103 | for (unsigned I = 0, N = S.getNumCatchStmts(); I != N; ++I) { |
| 6104 | const ObjCAtCatchStmt *CatchStmt = S.getCatchStmt(I); |
Douglas Gregor | 46a572b | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 6105 | const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl(); |
Daniel Dunbar | 76b7acc | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 6106 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6107 | Handlers.push_back(CatchHandler()); |
| 6108 | CatchHandler &Handler = Handlers.back(); |
| 6109 | Handler.Variable = CatchDecl; |
| 6110 | Handler.Body = CatchStmt->getCatchBody(); |
| 6111 | Handler.Block = CGF.createBasicBlock("catch"); |
| 6112 | |
| 6113 | // @catch(...) always matches. |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 6114 | if (!CatchDecl) { |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6115 | Handler.TypeInfo = 0; // catch-all |
| 6116 | // Don't consider any other catches. |
Douglas Gregor | 96c7949 | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 6117 | break; |
| 6118 | } |
Daniel Dunbar | 76b7acc | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 6119 | |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6120 | Handler.TypeInfo = GetEHType(CatchDecl->getType()); |
Daniel Dunbar | 76b7acc | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 6121 | } |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6122 | |
| 6123 | EHCatchScope *Catch = CGF.EHStack.pushCatch(Handlers.size()); |
| 6124 | for (unsigned I = 0, E = Handlers.size(); I != E; ++I) |
| 6125 | Catch->setHandler(I, Handlers[I].TypeInfo, Handlers[I].Block); |
Daniel Dunbar | 76b7acc | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 6126 | } |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6127 | |
| 6128 | // Emit the try body. |
| 6129 | CGF.EmitStmt(S.getTryBody()); |
Daniel Dunbar | 76b7acc | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 6130 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6131 | // Leave the try. |
| 6132 | if (S.getNumCatchStmts()) |
| 6133 | CGF.EHStack.popCatch(); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6134 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6135 | // Remember where we were. |
| 6136 | CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP(); |
Daniel Dunbar | 76b7acc | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 6137 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6138 | // Emit the handlers. |
| 6139 | for (unsigned I = 0, E = Handlers.size(); I != E; ++I) { |
| 6140 | CatchHandler &Handler = Handlers[I]; |
Daniel Dunbar | 76b7acc | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 6141 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6142 | CGF.EmitBlock(Handler.Block); |
| 6143 | llvm::Value *RawExn = CGF.Builder.CreateLoad(CGF.getExceptionSlot()); |
Daniel Dunbar | 76b7acc | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 6144 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6145 | // Enter the catch. |
| 6146 | llvm::CallInst *Exn = |
| 6147 | CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), RawExn, |
| 6148 | "exn.adjusted"); |
| 6149 | Exn->setDoesNotThrow(); |
Daniel Dunbar | 76b7acc | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 6150 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6151 | // Add a cleanup to leave the catch. |
John McCall | 5c08ab9 | 2010-07-13 22:12:14 +0000 | [diff] [blame] | 6152 | bool EndCatchMightThrow = (Handler.Variable == 0); |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 6153 | CGF.EHStack.pushCleanup<CallObjCEndCatch>(NormalAndEHCleanup, |
| 6154 | EndCatchMightThrow, |
| 6155 | ObjCTypes.getObjCEndCatchFn()); |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6156 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6157 | // Bind the catch parameter if it exists. |
| 6158 | if (const VarDecl *CatchParam = Handler.Variable) { |
| 6159 | const llvm::Type *CatchType = CGF.ConvertType(CatchParam->getType()); |
| 6160 | llvm::Value *CastExn = CGF.Builder.CreateBitCast(Exn, CatchType); |
Daniel Dunbar | 76b7acc | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 6161 | |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 6162 | CGF.EmitAutoVarDecl(*CatchParam); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6163 | CGF.Builder.CreateStore(CastExn, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 76b7acc | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 6164 | } |
Daniel Dunbar | 76b7acc | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 6165 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6166 | CGF.ObjCEHValueStack.push_back(Exn); |
| 6167 | CGF.EmitStmt(Handler.Body); |
| 6168 | CGF.ObjCEHValueStack.pop_back(); |
Daniel Dunbar | 0b0dcd9 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 6169 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6170 | // Leave the earlier cleanup. |
| 6171 | CGF.PopCleanupBlock(); |
Daniel Dunbar | 0b0dcd9 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 6172 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6173 | CGF.EmitBranchThroughCleanup(Cont); |
| 6174 | } |
Daniel Dunbar | 0b0dcd9 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 6175 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6176 | // Go back to the try-statement fallthrough. |
| 6177 | CGF.Builder.restoreIP(SavedIP); |
Daniel Dunbar | 0b0dcd9 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 6178 | |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6179 | // Pop out of the normal cleanup on the finally. |
| 6180 | if (S.getFinallyStmt()) |
| 6181 | CGF.ExitFinallyBlock(FinallyInfo); |
Daniel Dunbar | 0b0dcd9 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 6182 | |
John McCall | ad5d61e | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 6183 | if (Cont.isValid()) |
| 6184 | CGF.EmitBlock(Cont.getBlock()); |
Daniel Dunbar | 0b0dcd9 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 6185 | } |
| 6186 | |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6187 | /// EmitThrowStmt - Generate code for a throw statement. |
| 6188 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 6189 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6190 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
John McCall | 17afe45 | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6191 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
John McCall | d509182 | 2010-10-16 16:34:08 +0000 | [diff] [blame] | 6192 | Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, |
| 6193 | "tmp"); |
John McCall | 17afe45 | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6194 | llvm::Value *Args[] = { Exception }; |
| 6195 | CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), |
| 6196 | Args, Args+1) |
| 6197 | .setDoesNotReturn(); |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6198 | } else { |
John McCall | 17afe45 | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6199 | CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn(), 0, 0) |
| 6200 | .setDoesNotReturn(); |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6201 | } |
| 6202 | |
John McCall | 17afe45 | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6203 | CGF.Builder.CreateUnreachable(); |
Anders Carlsson | 9ab53d1 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6204 | CGF.Builder.ClearInsertionPoint(); |
| 6205 | } |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6206 | |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6207 | llvm::Constant * |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6208 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6209 | bool ForDefinition) { |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6210 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6211 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6212 | // If we don't need a definition, return the entry if found or check |
| 6213 | // if we use an external reference. |
| 6214 | if (!ForDefinition) { |
| 6215 | if (Entry) |
| 6216 | return Entry; |
Daniel Dunbar | d7beeea | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 6217 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6218 | // If this type (or a super class) has the __objc_exception__ |
| 6219 | // attribute, emit an external reference. |
Douglas Gregor | 78bd61f | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 6220 | if (hasObjCExceptionAttribute(CGM.getContext(), ID)) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6221 | return Entry = |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6222 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6223 | llvm::GlobalValue::ExternalLinkage, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6224 | 0, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6225 | ("OBJC_EHTYPE_$_" + |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 6226 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6227 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6228 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6229 | // Otherwise we need to either make a new entry or fill in the |
| 6230 | // initializer. |
| 6231 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 6232 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6233 | std::string VTableName = "objc_ehtype_vtable"; |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6234 | llvm::GlobalVariable *VTableGV = |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6235 | CGM.getModule().getGlobalVariable(VTableName); |
| 6236 | if (!VTableGV) |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6237 | VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy, |
| 6238 | false, |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6239 | llvm::GlobalValue::ExternalLinkage, |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6240 | 0, VTableName); |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6241 | |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 6242 | llvm::Value *VTableIdx = |
| 6243 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2); |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6244 | |
| 6245 | std::vector<llvm::Constant*> Values(3); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 6246 | Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1); |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6247 | Values[1] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 899e7eb | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 6248 | Values[2] = GetClassGlobal(ClassName); |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 6249 | llvm::Constant *Init = |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 6250 | llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6251 | |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6252 | if (Entry) { |
| 6253 | Entry->setInitializer(Init); |
| 6254 | } else { |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6255 | Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6256 | llvm::GlobalValue::WeakAnyLinkage, |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6257 | Init, |
Daniel Dunbar | 349e6fb | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6258 | ("OBJC_EHTYPE_$_" + |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 6259 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6260 | } |
| 6261 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 6262 | if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility) |
Daniel Dunbar | 15894b7 | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 6263 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 710cb20 | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 6264 | Entry->setAlignment(CGM.getTargetData().getABITypeAlignment( |
| 6265 | ObjCTypes.EHTypeTy)); |
Daniel Dunbar | 8f28d01 | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6266 | |
| 6267 | if (ForDefinition) { |
| 6268 | Entry->setSection("__DATA,__objc_const"); |
| 6269 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 6270 | } else { |
| 6271 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 6272 | } |
Daniel Dunbar | b1559a4 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6273 | |
| 6274 | return Entry; |
| 6275 | } |
Daniel Dunbar | 59e476b | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6276 | |
Daniel Dunbar | 8b8683f | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 6277 | /* *** */ |
| 6278 | |
Daniel Dunbar | b036db8 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 6279 | CodeGen::CGObjCRuntime * |
| 6280 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Daniel Dunbar | 303e2c2 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 6281 | return new CGObjCMac(CGM); |
| 6282 | } |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 6283 | |
| 6284 | CodeGen::CGObjCRuntime * |
Fariborz Jahanian | b15a3d5 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 6285 | CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Fariborz Jahanian | 7139404 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 6286 | return new CGObjCNonFragileABIMac(CGM); |
Fariborz Jahanian | 279eda6 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 6287 | } |