Daniel Dunbar | c17a4d3 | 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 | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 15 | |
Daniel Dunbar | 198bcb4 | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 16 | #include "CGRecordLayout.h" |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 17 | #include "CodeGenModule.h" |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 18 | #include "CodeGenFunction.h" |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 20 | #include "clang/AST/Decl.h" |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 22 | #include "clang/AST/RecordLayout.h" |
Chris Lattner | 16f0049 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 23 | #include "clang/AST/StmtObjC.h" |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 24 | #include "clang/Basic/LangOptions.h" |
Daniel Dunbar | 2feefe8 | 2010-02-01 21:07:33 +0000 | [diff] [blame] | 25 | #include "clang/CodeGen/CodeGenOptions.h" |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 26 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 27 | #include "llvm/Intrinsics.h" |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 28 | #include "llvm/LLVMContext.h" |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 29 | #include "llvm/Module.h" |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/DenseSet.h" |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SetVector.h" |
| 32 | #include "llvm/ADT/SmallString.h" |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallPtrSet.h" |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 34 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetData.h" |
Torok Edwin | f42e4a6 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 36 | #include <cstdio> |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 37 | |
| 38 | using namespace clang; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 39 | using namespace CodeGen; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 40 | |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 41 | // Common CGObjCRuntime functions, these don't belong here, but they |
| 42 | // don't belong in CGObjCRuntime either so we will live with it for |
| 43 | // now. |
| 44 | |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 45 | static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM, |
| 46 | const ObjCInterfaceDecl *OID, |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 47 | const ObjCImplementationDecl *ID, |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 48 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | e83be12 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 49 | const ObjCInterfaceDecl *Container = Ivar->getContainingInterface(); |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 50 | |
Daniel Dunbar | 61ac1d2 | 2010-04-02 22:29:40 +0000 | [diff] [blame] | 51 | // FIXME: We should eliminate the need to have ObjCImplementationDecl passed |
| 52 | // in here; it should never be necessary because that should be the lexical |
| 53 | // decl context for the ivar. |
Daniel Dunbar | 3a2c80f | 2010-04-02 15:43:29 +0000 | [diff] [blame] | 54 | |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 55 | // If we know have an implementation (and the ivar is in it) then |
| 56 | // look up in the implementation layout. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 57 | const ASTRecordLayout *RL; |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 58 | if (ID && ID->getClassInterface() == Container) |
| 59 | RL = &CGM.getContext().getASTObjCImplementationLayout(ID); |
| 60 | else |
| 61 | RL = &CGM.getContext().getASTObjCInterfaceLayout(Container); |
Daniel Dunbar | e83be12 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 62 | |
| 63 | // Compute field index. |
| 64 | // |
| 65 | // FIXME: The index here is closely tied to how ASTContext::getObjCLayout is |
| 66 | // implemented. This should be fixed to get the information from the layout |
| 67 | // directly. |
| 68 | unsigned Index = 0; |
| 69 | llvm::SmallVector<ObjCIvarDecl*, 16> Ivars; |
| 70 | CGM.getContext().ShallowCollectObjCIvars(Container, Ivars); |
| 71 | for (unsigned k = 0, e = Ivars.size(); k != e; ++k) { |
| 72 | if (Ivar == Ivars[k]) |
| 73 | break; |
| 74 | ++Index; |
| 75 | } |
| 76 | assert(Index != Ivars.size() && "Ivar is not inside container!"); |
| 77 | |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 78 | return RL->getFieldOffset(Index); |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM, |
| 82 | const ObjCInterfaceDecl *OID, |
| 83 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 84 | return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8; |
| 85 | } |
| 86 | |
| 87 | uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM, |
| 88 | const ObjCImplementationDecl *OID, |
| 89 | const ObjCIvarDecl *Ivar) { |
| 90 | return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8; |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF, |
| 94 | const ObjCInterfaceDecl *OID, |
| 95 | llvm::Value *BaseValue, |
| 96 | const ObjCIvarDecl *Ivar, |
| 97 | unsigned CVRQualifiers, |
| 98 | llvm::Value *Offset) { |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 99 | // Compute (type*) ( (char *) BaseValue + Offset) |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 100 | const llvm::Type *I8Ptr = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 101 | QualType IvarTy = Ivar->getType(); |
| 102 | const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 103 | llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 104 | V = CGF.Builder.CreateGEP(V, Offset, "add.ptr"); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 105 | V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 106 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 107 | Qualifiers Quals = CGF.MakeQualifiers(IvarTy); |
| 108 | Quals.addCVRQualifiers(CVRQualifiers); |
| 109 | |
Daniel Dunbar | 56229f5 | 2010-04-05 16:20:33 +0000 | [diff] [blame] | 110 | if (!Ivar->isBitField()) |
| 111 | return LValue::MakeAddr(V, Quals); |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 112 | |
Daniel Dunbar | 56229f5 | 2010-04-05 16:20:33 +0000 | [diff] [blame] | 113 | // We need to compute the bit offset for the bit-field, the offset is to the |
| 114 | // byte. Note, there is a subtle invariant here: we can only call this routine |
| 115 | // on non-synthesized ivars but we may be called for synthesized ivars. |
| 116 | // However, a synthesized ivar can never be a bit-field, so this is safe. |
| 117 | uint64_t BitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar) % 8; |
| 118 | uint64_t BitFieldSize = |
| 119 | Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 120 | |
Daniel Dunbar | f0fe5bc | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 121 | // Allocate a new CGBitFieldInfo object to describe this access. |
| 122 | // |
| 123 | // FIXME: This is incredibly wasteful, these should be uniqued or part of some |
| 124 | // layout object. However, this is blocked on other cleanups to the |
| 125 | // Objective-C code, so for now we just live with allocating a bunch of these |
| 126 | // objects. |
Daniel Dunbar | f0fe5bc | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 127 | |
Daniel Dunbar | ab970f9 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 128 | // We always construct a single, possibly unaligned, access for this case. |
Daniel Dunbar | 2df2569 | 2010-04-15 05:09:32 +0000 | [diff] [blame] | 129 | CGBitFieldInfo::AccessInfo AI; |
Daniel Dunbar | ab970f9 | 2010-04-13 20:58:55 +0000 | [diff] [blame] | 130 | AI.FieldIndex = 0; |
| 131 | AI.FieldByteOffset = 0; |
| 132 | AI.FieldBitStart = BitOffset; |
| 133 | AI.AccessWidth = CGF.CGM.getContext().getTypeSize(IvarTy); |
| 134 | AI.AccessAlignment = 0; |
| 135 | AI.TargetBitOffset = 0; |
| 136 | AI.TargetBitWidth = BitFieldSize; |
| 137 | |
Daniel Dunbar | 2df2569 | 2010-04-15 05:09:32 +0000 | [diff] [blame] | 138 | CGBitFieldInfo *Info = |
| 139 | new (CGF.CGM.getContext()) CGBitFieldInfo(BitFieldSize, 1, &AI, |
| 140 | IvarTy->isSignedIntegerType()); |
| 141 | |
Daniel Dunbar | 56229f5 | 2010-04-05 16:20:33 +0000 | [diff] [blame] | 142 | // FIXME: We need to set a very conservative alignment on this, or make sure |
| 143 | // that the runtime is doing the right thing. |
Daniel Dunbar | efbf487 | 2010-04-06 01:07:44 +0000 | [diff] [blame] | 144 | return LValue::MakeBitfield(V, *Info, Quals.getCVRQualifiers()); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /// |
| 148 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 149 | namespace { |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 150 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 151 | typedef std::vector<llvm::Constant*> ConstantVector; |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 152 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 153 | // FIXME: We should find a nicer way to make the labels for metadata, string |
| 154 | // concatenation is lame. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 155 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 156 | class ObjCCommonTypesHelper { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 157 | protected: |
| 158 | llvm::LLVMContext &VMContext; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 159 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 160 | private: |
| 161 | llvm::Constant *getMessageSendFn() const { |
| 162 | // id objc_msgSend (id, SEL, ...) |
| 163 | std::vector<const llvm::Type*> Params; |
| 164 | Params.push_back(ObjectPtrTy); |
| 165 | Params.push_back(SelectorPtrTy); |
| 166 | return |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 167 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 168 | Params, true), |
| 169 | "objc_msgSend"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 170 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 171 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 172 | llvm::Constant *getMessageSendStretFn() const { |
| 173 | // id objc_msgSend_stret (id, SEL, ...) |
| 174 | std::vector<const llvm::Type*> Params; |
| 175 | Params.push_back(ObjectPtrTy); |
| 176 | Params.push_back(SelectorPtrTy); |
| 177 | return |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 178 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 179 | Params, true), |
| 180 | "objc_msgSend_stret"); |
| 181 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 182 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 183 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 184 | llvm::Constant *getMessageSendFpretFn() const { |
| 185 | // FIXME: This should be long double on x86_64? |
| 186 | // [double | long double] objc_msgSend_fpret(id self, SEL op, ...) |
| 187 | std::vector<const llvm::Type*> Params; |
| 188 | Params.push_back(ObjectPtrTy); |
| 189 | Params.push_back(SelectorPtrTy); |
| 190 | return |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 191 | CGM.CreateRuntimeFunction(llvm::FunctionType::get( |
| 192 | llvm::Type::getDoubleTy(VMContext), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 193 | Params, |
| 194 | true), |
| 195 | "objc_msgSend_fpret"); |
| 196 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 197 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 198 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 199 | llvm::Constant *getMessageSendSuperFn() const { |
| 200 | // id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
| 201 | const char *SuperName = "objc_msgSendSuper"; |
| 202 | std::vector<const llvm::Type*> Params; |
| 203 | Params.push_back(SuperPtrTy); |
| 204 | Params.push_back(SelectorPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 205 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 206 | Params, true), |
| 207 | SuperName); |
| 208 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 209 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 210 | llvm::Constant *getMessageSendSuperFn2() const { |
| 211 | // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...) |
| 212 | const char *SuperName = "objc_msgSendSuper2"; |
| 213 | std::vector<const llvm::Type*> Params; |
| 214 | Params.push_back(SuperPtrTy); |
| 215 | Params.push_back(SelectorPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 216 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 217 | Params, true), |
| 218 | SuperName); |
| 219 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 220 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 221 | llvm::Constant *getMessageSendSuperStretFn() const { |
| 222 | // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super, |
| 223 | // SEL op, ...) |
| 224 | std::vector<const llvm::Type*> Params; |
| 225 | Params.push_back(Int8PtrTy); |
| 226 | Params.push_back(SuperPtrTy); |
| 227 | Params.push_back(SelectorPtrTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 228 | return CGM.CreateRuntimeFunction( |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 229 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 230 | Params, true), |
| 231 | "objc_msgSendSuper_stret"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 232 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 233 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 234 | llvm::Constant *getMessageSendSuperStretFn2() const { |
| 235 | // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super, |
| 236 | // SEL op, ...) |
| 237 | std::vector<const llvm::Type*> Params; |
| 238 | Params.push_back(Int8PtrTy); |
| 239 | Params.push_back(SuperPtrTy); |
| 240 | Params.push_back(SelectorPtrTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 241 | return CGM.CreateRuntimeFunction( |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 242 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 243 | Params, true), |
| 244 | "objc_msgSendSuper2_stret"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 245 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 246 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 247 | llvm::Constant *getMessageSendSuperFpretFn() const { |
| 248 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 249 | return getMessageSendSuperFn(); |
| 250 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 251 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 252 | llvm::Constant *getMessageSendSuperFpretFn2() const { |
| 253 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 254 | return getMessageSendSuperFn2(); |
| 255 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 256 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 257 | protected: |
| 258 | CodeGen::CodeGenModule &CGM; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 259 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 260 | public: |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 261 | const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 262 | const llvm::Type *Int8PtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 263 | |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 264 | /// ObjectPtrTy - LLVM type for object handles (typeof(id)) |
| 265 | const llvm::Type *ObjectPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 266 | |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 267 | /// PtrObjectPtrTy - LLVM type for id * |
| 268 | const llvm::Type *PtrObjectPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 269 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 270 | /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL)) |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 271 | const llvm::Type *SelectorPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 272 | /// ProtocolPtrTy - LLVM type for external protocol handles |
| 273 | /// (typeof(Protocol)) |
| 274 | const llvm::Type *ExternalProtocolPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 275 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 276 | // SuperCTy - clang type for struct objc_super. |
| 277 | QualType SuperCTy; |
| 278 | // SuperPtrCTy - clang type for struct objc_super *. |
| 279 | QualType SuperPtrCTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 280 | |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 281 | /// SuperTy - LLVM type for struct objc_super. |
| 282 | const llvm::StructType *SuperTy; |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 283 | /// SuperPtrTy - LLVM type for struct objc_super *. |
| 284 | const llvm::Type *SuperPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 285 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 286 | /// PropertyTy - LLVM type for struct objc_property (struct _prop_t |
| 287 | /// in GCC parlance). |
| 288 | const llvm::StructType *PropertyTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 289 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 290 | /// PropertyListTy - LLVM type for struct objc_property_list |
| 291 | /// (_prop_list_t in GCC parlance). |
| 292 | const llvm::StructType *PropertyListTy; |
| 293 | /// PropertyListPtrTy - LLVM type for struct objc_property_list*. |
| 294 | const llvm::Type *PropertyListPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 295 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 296 | // MethodTy - LLVM type for struct objc_method. |
| 297 | const llvm::StructType *MethodTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 298 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 299 | /// CacheTy - LLVM type for struct objc_cache. |
| 300 | const llvm::Type *CacheTy; |
| 301 | /// CachePtrTy - LLVM type for struct objc_cache *. |
| 302 | const llvm::Type *CachePtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 303 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 304 | llvm::Constant *getGetPropertyFn() { |
| 305 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 306 | ASTContext &Ctx = CGM.getContext(); |
| 307 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 308 | llvm::SmallVector<CanQualType,4> Params; |
| 309 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 310 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 311 | Params.push_back(IdType); |
| 312 | Params.push_back(SelType); |
| 313 | Params.push_back(Ctx.LongTy); |
| 314 | Params.push_back(Ctx.BoolTy); |
| 315 | const llvm::FunctionType *FTy = |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 316 | Types.GetFunctionType(Types.getFunctionInfo(IdType, Params, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 317 | FunctionType::ExtInfo()), |
| 318 | false); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 319 | return CGM.CreateRuntimeFunction(FTy, "objc_getProperty"); |
| 320 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 321 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 322 | llvm::Constant *getSetPropertyFn() { |
| 323 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 324 | ASTContext &Ctx = CGM.getContext(); |
| 325 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 326 | llvm::SmallVector<CanQualType,6> Params; |
| 327 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 328 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 329 | Params.push_back(IdType); |
| 330 | Params.push_back(SelType); |
| 331 | Params.push_back(Ctx.LongTy); |
| 332 | Params.push_back(IdType); |
| 333 | Params.push_back(Ctx.BoolTy); |
| 334 | Params.push_back(Ctx.BoolTy); |
| 335 | const llvm::FunctionType *FTy = |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 336 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 337 | FunctionType::ExtInfo()), |
| 338 | false); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 339 | return CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); |
| 340 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 341 | |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 342 | |
| 343 | llvm::Constant *getCopyStructFn() { |
| 344 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 345 | ASTContext &Ctx = CGM.getContext(); |
| 346 | // void objc_copyStruct (void *, const void *, size_t, bool, bool) |
| 347 | llvm::SmallVector<CanQualType,5> Params; |
| 348 | Params.push_back(Ctx.VoidPtrTy); |
| 349 | Params.push_back(Ctx.VoidPtrTy); |
| 350 | Params.push_back(Ctx.LongTy); |
| 351 | Params.push_back(Ctx.BoolTy); |
| 352 | Params.push_back(Ctx.BoolTy); |
| 353 | const llvm::FunctionType *FTy = |
| 354 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params, |
| 355 | FunctionType::ExtInfo()), |
| 356 | false); |
| 357 | return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct"); |
| 358 | } |
| 359 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 360 | llvm::Constant *getEnumerationMutationFn() { |
Daniel Dunbar | c1ab900 | 2009-07-11 20:32:50 +0000 | [diff] [blame] | 361 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 362 | ASTContext &Ctx = CGM.getContext(); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 363 | // void objc_enumerationMutation (id) |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 364 | llvm::SmallVector<CanQualType,1> Params; |
| 365 | Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType())); |
Daniel Dunbar | c1ab900 | 2009-07-11 20:32:50 +0000 | [diff] [blame] | 366 | const llvm::FunctionType *FTy = |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 367 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 368 | FunctionType::ExtInfo()), |
| 369 | false); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 370 | return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation"); |
| 371 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 372 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 373 | /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 374 | llvm::Constant *getGcReadWeakFn() { |
| 375 | // id objc_read_weak (id *) |
| 376 | std::vector<const llvm::Type*> Args; |
| 377 | Args.push_back(ObjectPtrTy->getPointerTo()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 378 | llvm::FunctionType *FTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 379 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 380 | return CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 383 | /// GcAssignWeakFn -- LLVM objc_assign_weak function. |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 384 | llvm::Constant *getGcAssignWeakFn() { |
| 385 | // id objc_assign_weak (id, id *) |
| 386 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 387 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 388 | llvm::FunctionType *FTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 389 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 390 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 391 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 392 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 393 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 394 | llvm::Constant *getGcAssignGlobalFn() { |
| 395 | // id objc_assign_global(id, id *) |
| 396 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 397 | Args.push_back(ObjectPtrTy->getPointerTo()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 398 | llvm::FunctionType *FTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 399 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 400 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 401 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 402 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 403 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 404 | llvm::Constant *getGcAssignIvarFn() { |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 405 | // id objc_assign_ivar(id, id *, ptrdiff_t) |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 406 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 407 | Args.push_back(ObjectPtrTy->getPointerTo()); |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 408 | Args.push_back(LongTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 409 | llvm::FunctionType *FTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 410 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 411 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 412 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 413 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 414 | /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function. |
| 415 | llvm::Constant *GcMemmoveCollectableFn() { |
| 416 | // void *objc_memmove_collectable(void *dst, const void *src, size_t size) |
| 417 | std::vector<const llvm::Type*> Args(1, Int8PtrTy); |
| 418 | Args.push_back(Int8PtrTy); |
| 419 | Args.push_back(LongTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 420 | llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 421 | return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable"); |
| 422 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 423 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 424 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 425 | llvm::Constant *getGcAssignStrongCastFn() { |
| 426 | // id objc_assign_global(id, id *) |
| 427 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 428 | Args.push_back(ObjectPtrTy->getPointerTo()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 429 | llvm::FunctionType *FTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 430 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 431 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); |
| 432 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 433 | |
| 434 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 435 | llvm::Constant *getExceptionThrowFn() { |
| 436 | // void objc_exception_throw(id) |
| 437 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 438 | llvm::FunctionType *FTy = |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 439 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 440 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
| 441 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 442 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 443 | /// SyncEnterFn - LLVM object_sync_enter function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 444 | llvm::Constant *getSyncEnterFn() { |
| 445 | // void objc_sync_enter (id) |
| 446 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 447 | llvm::FunctionType *FTy = |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 448 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 449 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 450 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 451 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 452 | /// SyncExitFn - LLVM object_sync_exit function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 453 | llvm::Constant *getSyncExitFn() { |
| 454 | // void objc_sync_exit (id) |
| 455 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 456 | llvm::FunctionType *FTy = |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 457 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 458 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
| 459 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 460 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 461 | llvm::Constant *getSendFn(bool IsSuper) const { |
| 462 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFn(); |
| 463 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 464 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 465 | llvm::Constant *getSendFn2(bool IsSuper) const { |
| 466 | return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn(); |
| 467 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 468 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 469 | llvm::Constant *getSendStretFn(bool IsSuper) const { |
| 470 | return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn(); |
| 471 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 472 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 473 | llvm::Constant *getSendStretFn2(bool IsSuper) const { |
| 474 | return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn(); |
| 475 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 476 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 477 | llvm::Constant *getSendFpretFn(bool IsSuper) const { |
| 478 | return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn(); |
| 479 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 480 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 481 | llvm::Constant *getSendFpretFn2(bool IsSuper) const { |
| 482 | return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn(); |
| 483 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 484 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 485 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
| 486 | ~ObjCCommonTypesHelper(){} |
| 487 | }; |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 488 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 489 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 490 | /// construction of varies types used during ObjC generation. |
| 491 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 492 | public: |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 493 | /// SymtabTy - LLVM type for struct objc_symtab. |
| 494 | const llvm::StructType *SymtabTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 495 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
| 496 | const llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 497 | /// ModuleTy - LLVM type for struct objc_module. |
| 498 | const llvm::StructType *ModuleTy; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 499 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 500 | /// ProtocolTy - LLVM type for struct objc_protocol. |
| 501 | const llvm::StructType *ProtocolTy; |
| 502 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
| 503 | const llvm::Type *ProtocolPtrTy; |
| 504 | /// ProtocolExtensionTy - LLVM type for struct |
| 505 | /// objc_protocol_extension. |
| 506 | const llvm::StructType *ProtocolExtensionTy; |
| 507 | /// ProtocolExtensionTy - LLVM type for struct |
| 508 | /// objc_protocol_extension *. |
| 509 | const llvm::Type *ProtocolExtensionPtrTy; |
| 510 | /// MethodDescriptionTy - LLVM type for struct |
| 511 | /// objc_method_description. |
| 512 | const llvm::StructType *MethodDescriptionTy; |
| 513 | /// MethodDescriptionListTy - LLVM type for struct |
| 514 | /// objc_method_description_list. |
| 515 | const llvm::StructType *MethodDescriptionListTy; |
| 516 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 517 | /// objc_method_description_list *. |
| 518 | const llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 519 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
| 520 | const llvm::Type *ProtocolListTy; |
| 521 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
| 522 | const llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 523 | /// CategoryTy - LLVM type for struct objc_category. |
| 524 | const llvm::StructType *CategoryTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 525 | /// ClassTy - LLVM type for struct objc_class. |
| 526 | const llvm::StructType *ClassTy; |
| 527 | /// ClassPtrTy - LLVM type for struct objc_class *. |
| 528 | const llvm::Type *ClassPtrTy; |
| 529 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
| 530 | const llvm::StructType *ClassExtensionTy; |
| 531 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
| 532 | const llvm::Type *ClassExtensionPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 533 | // IvarTy - LLVM type for struct objc_ivar. |
| 534 | const llvm::StructType *IvarTy; |
| 535 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
| 536 | const llvm::Type *IvarListTy; |
| 537 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
| 538 | const llvm::Type *IvarListPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 539 | /// MethodListTy - LLVM type for struct objc_method_list. |
| 540 | const llvm::Type *MethodListTy; |
| 541 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
| 542 | const llvm::Type *MethodListPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 543 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 544 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
| 545 | const llvm::Type *ExceptionDataTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 546 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 547 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 548 | llvm::Constant *getExceptionTryEnterFn() { |
| 549 | std::vector<const llvm::Type*> Params; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 550 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 551 | return CGM.CreateRuntimeFunction( |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 552 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 553 | Params, false), |
| 554 | "objc_exception_try_enter"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 555 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 556 | |
| 557 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 558 | llvm::Constant *getExceptionTryExitFn() { |
| 559 | std::vector<const llvm::Type*> Params; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 560 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 561 | return CGM.CreateRuntimeFunction( |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 562 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 563 | Params, false), |
| 564 | "objc_exception_try_exit"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 565 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 566 | |
| 567 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 568 | llvm::Constant *getExceptionExtractFn() { |
| 569 | std::vector<const llvm::Type*> Params; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 570 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 571 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 572 | Params, false), |
| 573 | "objc_exception_extract"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 574 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 575 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 576 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 577 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 578 | llvm::Constant *getExceptionMatchFn() { |
| 579 | std::vector<const llvm::Type*> Params; |
| 580 | Params.push_back(ClassPtrTy); |
| 581 | Params.push_back(ObjectPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 582 | return CGM.CreateRuntimeFunction( |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 583 | llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 584 | Params, false), |
| 585 | "objc_exception_match"); |
| 586 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 587 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 588 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 589 | /// SetJmpFn - LLVM _setjmp function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 590 | llvm::Constant *getSetJmpFn() { |
| 591 | std::vector<const llvm::Type*> Params; |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 592 | Params.push_back(llvm::Type::getInt32PtrTy(VMContext)); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 593 | return |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 594 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext), |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 595 | Params, false), |
| 596 | "_setjmp"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 597 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 598 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 599 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 600 | public: |
| 601 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 602 | ~ObjCTypesHelper() {} |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 603 | }; |
| 604 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 605 | /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 606 | /// modern abi |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 607 | class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 608 | public: |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 609 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 610 | // MethodListnfABITy - LLVM for struct _method_list_t |
| 611 | const llvm::StructType *MethodListnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 612 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 613 | // MethodListnfABIPtrTy - LLVM for struct _method_list_t* |
| 614 | const llvm::Type *MethodListnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 615 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 616 | // ProtocolnfABITy = LLVM for struct _protocol_t |
| 617 | const llvm::StructType *ProtocolnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 618 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 619 | // ProtocolnfABIPtrTy = LLVM for struct _protocol_t* |
| 620 | const llvm::Type *ProtocolnfABIPtrTy; |
| 621 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 622 | // ProtocolListnfABITy - LLVM for struct _objc_protocol_list |
| 623 | const llvm::StructType *ProtocolListnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 624 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 625 | // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list* |
| 626 | const llvm::Type *ProtocolListnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 627 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 628 | // ClassnfABITy - LLVM for struct _class_t |
| 629 | const llvm::StructType *ClassnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 630 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 631 | // ClassnfABIPtrTy - LLVM for struct _class_t* |
| 632 | const llvm::Type *ClassnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 633 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 634 | // IvarnfABITy - LLVM for struct _ivar_t |
| 635 | const llvm::StructType *IvarnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 636 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 637 | // IvarListnfABITy - LLVM for struct _ivar_list_t |
| 638 | const llvm::StructType *IvarListnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 639 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 640 | // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t* |
| 641 | const llvm::Type *IvarListnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 642 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 643 | // ClassRonfABITy - LLVM for struct _class_ro_t |
| 644 | const llvm::StructType *ClassRonfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 645 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 646 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 647 | const llvm::Type *ImpnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 648 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 649 | // CategorynfABITy - LLVM for struct _category_t |
| 650 | const llvm::StructType *CategorynfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 651 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 652 | // New types for nonfragile abi messaging. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 653 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 654 | // MessageRefTy - LLVM for: |
| 655 | // struct _message_ref_t { |
| 656 | // IMP messenger; |
| 657 | // SEL name; |
| 658 | // }; |
| 659 | const llvm::StructType *MessageRefTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 660 | // MessageRefCTy - clang type for struct _message_ref_t |
| 661 | QualType MessageRefCTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 662 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 663 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 664 | const llvm::Type *MessageRefPtrTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 665 | // MessageRefCPtrTy - clang type for struct _message_ref_t* |
| 666 | QualType MessageRefCPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 667 | |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 668 | // MessengerTy - Type of the messenger (shown as IMP above) |
| 669 | const llvm::FunctionType *MessengerTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 670 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 671 | // SuperMessageRefTy - LLVM for: |
| 672 | // struct _super_message_ref_t { |
| 673 | // SUPER_IMP messenger; |
| 674 | // SEL name; |
| 675 | // }; |
| 676 | const llvm::StructType *SuperMessageRefTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 677 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 678 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 679 | const llvm::Type *SuperMessageRefPtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 680 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 681 | llvm::Constant *getMessageSendFixupFn() { |
| 682 | // id objc_msgSend_fixup(id, struct message_ref_t*, ...) |
| 683 | std::vector<const llvm::Type*> Params; |
| 684 | Params.push_back(ObjectPtrTy); |
| 685 | Params.push_back(MessageRefPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 686 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 687 | Params, true), |
| 688 | "objc_msgSend_fixup"); |
| 689 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 690 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 691 | llvm::Constant *getMessageSendFpretFixupFn() { |
| 692 | // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...) |
| 693 | std::vector<const llvm::Type*> Params; |
| 694 | Params.push_back(ObjectPtrTy); |
| 695 | Params.push_back(MessageRefPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 696 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 697 | Params, true), |
| 698 | "objc_msgSend_fpret_fixup"); |
| 699 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 700 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 701 | llvm::Constant *getMessageSendStretFixupFn() { |
| 702 | // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...) |
| 703 | std::vector<const llvm::Type*> Params; |
| 704 | Params.push_back(ObjectPtrTy); |
| 705 | Params.push_back(MessageRefPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 706 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 707 | Params, true), |
| 708 | "objc_msgSend_stret_fixup"); |
| 709 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 710 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 711 | llvm::Constant *getMessageSendIdFixupFn() { |
| 712 | // id objc_msgSendId_fixup(id, struct message_ref_t*, ...) |
| 713 | std::vector<const llvm::Type*> Params; |
| 714 | Params.push_back(ObjectPtrTy); |
| 715 | Params.push_back(MessageRefPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 716 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 717 | Params, true), |
| 718 | "objc_msgSendId_fixup"); |
| 719 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 720 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 721 | llvm::Constant *getMessageSendIdStretFixupFn() { |
| 722 | // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...) |
| 723 | std::vector<const llvm::Type*> Params; |
| 724 | Params.push_back(ObjectPtrTy); |
| 725 | Params.push_back(MessageRefPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 726 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 727 | Params, true), |
| 728 | "objc_msgSendId_stret_fixup"); |
| 729 | } |
| 730 | llvm::Constant *getMessageSendSuper2FixupFn() { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 731 | // id objc_msgSendSuper2_fixup (struct objc_super *, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 732 | // struct _super_message_ref_t*, ...) |
| 733 | std::vector<const llvm::Type*> Params; |
| 734 | Params.push_back(SuperPtrTy); |
| 735 | Params.push_back(SuperMessageRefPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 736 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 737 | Params, true), |
| 738 | "objc_msgSendSuper2_fixup"); |
| 739 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 740 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 741 | llvm::Constant *getMessageSendSuper2StretFixupFn() { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 742 | // id objc_msgSendSuper2_stret_fixup(struct objc_super *, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 743 | // struct _super_message_ref_t*, ...) |
| 744 | std::vector<const llvm::Type*> Params; |
| 745 | Params.push_back(SuperPtrTy); |
| 746 | Params.push_back(SuperMessageRefPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 747 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 748 | Params, true), |
| 749 | "objc_msgSendSuper2_stret_fixup"); |
| 750 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 751 | |
| 752 | |
| 753 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 754 | /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C |
| 755 | /// exception personality function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 756 | llvm::Value *getEHPersonalityPtr() { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 757 | llvm::Constant *Personality = |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 758 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext), |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 759 | true), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 760 | "__objc_personality_v0"); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 761 | return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 762 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 763 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 764 | llvm::Constant *getUnwindResumeOrRethrowFn() { |
| 765 | std::vector<const llvm::Type*> Params; |
| 766 | Params.push_back(Int8PtrTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 767 | return CGM.CreateRuntimeFunction( |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 768 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 769 | Params, false), |
Daniel Dunbar | b2987d1 | 2010-02-10 18:49:11 +0000 | [diff] [blame] | 770 | (CGM.getLangOptions().SjLjExceptions ? "_Unwind_SjLj_Resume" : |
| 771 | "_Unwind_Resume_or_Rethrow")); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 772 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 773 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 774 | llvm::Constant *getObjCEndCatchFn() { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 775 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Chris Lattner | b59761b | 2009-07-01 04:13:52 +0000 | [diff] [blame] | 776 | false), |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 777 | "objc_end_catch"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 778 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 779 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 780 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 781 | llvm::Constant *getObjCBeginCatchFn() { |
| 782 | std::vector<const llvm::Type*> Params; |
| 783 | Params.push_back(Int8PtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 784 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy, |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 785 | Params, false), |
| 786 | "objc_begin_catch"); |
| 787 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 788 | |
| 789 | const llvm::StructType *EHTypeTy; |
| 790 | const llvm::Type *EHTypePtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 791 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 792 | ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); |
| 793 | ~ObjCNonFragileABITypesHelper(){} |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 794 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 795 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 796 | class CGObjCCommonMac : public CodeGen::CGObjCRuntime { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 797 | public: |
| 798 | // FIXME - accessibility |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 799 | class GC_IVAR { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 800 | public: |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 801 | unsigned ivar_bytepos; |
| 802 | unsigned ivar_size; |
| 803 | GC_IVAR(unsigned bytepos = 0, unsigned size = 0) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 804 | : ivar_bytepos(bytepos), ivar_size(size) {} |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 805 | |
| 806 | // Allow sorting based on byte pos. |
| 807 | bool operator<(const GC_IVAR &b) const { |
| 808 | return ivar_bytepos < b.ivar_bytepos; |
| 809 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 810 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 811 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 812 | class SKIP_SCAN { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 813 | public: |
| 814 | unsigned skip; |
| 815 | unsigned scan; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 816 | SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0) |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 817 | : skip(_skip), scan(_scan) {} |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 818 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 819 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 820 | protected: |
| 821 | CodeGen::CodeGenModule &CGM; |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 822 | llvm::LLVMContext &VMContext; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 823 | // FIXME! May not be needing this after all. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 824 | unsigned ObjCABI; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 825 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 826 | // gc ivar layout bitmap calculation helper caches. |
| 827 | llvm::SmallVector<GC_IVAR, 16> SkipIvars; |
| 828 | llvm::SmallVector<GC_IVAR, 16> IvarsInfo; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 829 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 830 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 831 | /// DefinedSymbols and FinishModule(). |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 832 | llvm::SetVector<IdentifierInfo*> LazySymbols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 833 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 834 | /// DefinedSymbols - External symbols which are defined by this |
| 835 | /// module. The symbols in this list and LazySymbols are used to add |
| 836 | /// special linker symbols which ensure that Objective-C modules are |
| 837 | /// linked properly. |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 838 | llvm::SetVector<IdentifierInfo*> DefinedSymbols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 839 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 840 | /// ClassNames - uniqued class names. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 841 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 842 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 843 | /// MethodVarNames - uniqued method variable names. |
| 844 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 845 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 846 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 847 | /// a StringMap here because have no other unique reference. |
| 848 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 849 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 850 | /// MethodDefinitions - map of methods which have been defined in |
| 851 | /// this translation unit. |
| 852 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 853 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 854 | /// PropertyNames - uniqued method variable names. |
| 855 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 856 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 857 | /// ClassReferences - uniqued class references. |
| 858 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 859 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 860 | /// SelectorReferences - uniqued selector references. |
| 861 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 862 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 863 | /// Protocols - Protocols for which an objc_protocol structure has |
| 864 | /// been emitted. Forward declarations are handled by creating an |
| 865 | /// empty structure whose initializer is filled in when/if defined. |
| 866 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 867 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 868 | /// DefinedProtocols - Protocols which have actually been |
| 869 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 870 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 871 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 872 | /// DefinedClasses - List of defined classes. |
| 873 | std::vector<llvm::GlobalValue*> DefinedClasses; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 874 | |
| 875 | /// DefinedNonLazyClasses - List of defined "non-lazy" classes. |
| 876 | std::vector<llvm::GlobalValue*> DefinedNonLazyClasses; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 877 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 878 | /// DefinedCategories - List of defined categories. |
| 879 | std::vector<llvm::GlobalValue*> DefinedCategories; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 880 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 881 | /// DefinedNonLazyCategories - List of defined "non-lazy" categories. |
| 882 | std::vector<llvm::GlobalValue*> DefinedNonLazyCategories; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 883 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 884 | /// GetNameForMethod - Return a name for the given method. |
| 885 | /// \param[out] NameOut - The return value. |
| 886 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 887 | const ObjCContainerDecl *CD, |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 888 | llvm::SmallVectorImpl<char> &NameOut); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 889 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 890 | /// GetMethodVarName - Return a unique constant for the given |
| 891 | /// selector's name. The return value has type char *. |
| 892 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 893 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
| 894 | llvm::Constant *GetMethodVarName(const std::string &Name); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 895 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 896 | /// GetMethodVarType - Return a unique constant for the given |
| 897 | /// selector's name. The return value has type char *. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 898 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 899 | // FIXME: This is a horrible name. |
| 900 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 901 | llvm::Constant *GetMethodVarType(const FieldDecl *D); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 902 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 903 | /// GetPropertyName - Return a unique constant for the given |
| 904 | /// name. The return value has type char *. |
| 905 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 906 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 907 | // FIXME: This can be dropped once string functions are unified. |
| 908 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 909 | const Decl *Container); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 910 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 911 | /// GetClassName - Return a unique constant for the given selector's |
| 912 | /// name. The return value has type char *. |
| 913 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 914 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 915 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 916 | /// implementation for the __strong or __weak case. |
| 917 | /// |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 918 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
| 919 | bool ForStrongLayout); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 920 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 921 | void BuildAggrIvarRecordLayout(const RecordType *RT, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 922 | unsigned int BytePos, bool ForStrongLayout, |
| 923 | bool &HasUnion); |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 924 | void BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 925 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 926 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 927 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 928 | unsigned int BytePos, bool ForStrongLayout, |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 929 | bool &HasUnion); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 930 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 931 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 932 | /// ivar layout bitmap. |
| 933 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 934 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 935 | |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 936 | /// EmitPropertyList - Emit the given property list. The return |
| 937 | /// value has type PropertyListPtrTy. |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 938 | llvm::Constant *EmitPropertyList(llvm::Twine Name, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 939 | const Decl *Container, |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 940 | const ObjCContainerDecl *OCD, |
| 941 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 942 | |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 943 | /// PushProtocolProperties - Push protocol's property on the input stack. |
| 944 | void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet, |
| 945 | std::vector<llvm::Constant*> &Properties, |
| 946 | const Decl *Container, |
| 947 | const ObjCProtocolDecl *PROTO, |
| 948 | const ObjCCommonTypesHelper &ObjCTypes); |
| 949 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 950 | /// GetProtocolRef - Return a reference to the internal protocol |
| 951 | /// description, creating an empty one if it has not been |
| 952 | /// defined. The return value has type ProtocolPtrTy. |
| 953 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 954 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 955 | /// CreateMetadataVar - Create a global variable with internal |
| 956 | /// linkage for use by the Objective-C runtime. |
| 957 | /// |
| 958 | /// This is a convenience wrapper which not only creates the |
| 959 | /// variable, but also sets the section and alignment and adds the |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 960 | /// global to the "llvm.used" list. |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 961 | /// |
| 962 | /// \param Name - The variable name. |
| 963 | /// \param Init - The variable initializer; this is also used to |
| 964 | /// define the type of the variable. |
| 965 | /// \param Section - The section the variable should go into, or 0. |
| 966 | /// \param Align - The alignment for the variable, or 0. |
| 967 | /// \param AddToUsed - Whether the variable should be added to |
Daniel Dunbar | c158306 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 968 | /// "llvm.used". |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 969 | llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name, |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 970 | llvm::Constant *Init, |
| 971 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 972 | unsigned Align, |
| 973 | bool AddToUsed); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 974 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 975 | CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame^] | 976 | ReturnValueSlot Return, |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 977 | QualType ResultType, |
| 978 | llvm::Value *Sel, |
| 979 | llvm::Value *Arg0, |
| 980 | QualType Arg0Ty, |
| 981 | bool IsSuper, |
| 982 | const CallArgList &CallArgs, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 983 | const ObjCMethodDecl *OMD, |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 984 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 985 | |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 986 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 987 | /// level information. |
| 988 | void EmitImageInfo(); |
| 989 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 990 | public: |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 991 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 992 | CGM(cgm), VMContext(cgm.getLLVMContext()) { } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 993 | |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 994 | virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 995 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 996 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 997 | const ObjCContainerDecl *CD=0); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 998 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 999 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1000 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1001 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1002 | /// declaration, emitting it if necessary. The return value has type |
| 1003 | /// ProtocolPtrTy. |
| 1004 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1005 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1006 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1007 | /// object for the given declaration, emitting it if needed. These |
| 1008 | /// forward references will be filled in with empty bodies if no |
| 1009 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1010 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1011 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1012 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1013 | class CGObjCMac : public CGObjCCommonMac { |
| 1014 | private: |
| 1015 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1016 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1017 | /// EmitModuleInfo - Another marker encoding module level |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1018 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1019 | void EmitModuleInfo(); |
| 1020 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1021 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 1022 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1023 | llvm::Constant *EmitModuleSymbols(); |
| 1024 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1025 | /// FinishModule - Write out global data structures at the end of |
| 1026 | /// processing a translation unit. |
| 1027 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1028 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1029 | /// EmitClassExtension - Generate the class extension structure used |
| 1030 | /// to store the weak ivar layout and properties. The return value |
| 1031 | /// has type ClassExtensionPtrTy. |
| 1032 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 1033 | |
| 1034 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1035 | /// for the given class. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1036 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1037 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 1038 | |
| 1039 | /// EmitSuperClassRef - Emits reference to class's main metadata class. |
| 1040 | llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1041 | |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1042 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame^] | 1043 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1044 | QualType ResultType, |
| 1045 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1046 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1047 | QualType Arg0Ty, |
| 1048 | bool IsSuper, |
| 1049 | const CallArgList &CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1050 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1051 | /// EmitIvarList - Emit the ivar list for the given |
| 1052 | /// implementation. If ForClass is true the list of class ivars |
| 1053 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1054 | /// interface ivars will be emitted. The return value has type |
| 1055 | /// IvarListPtrTy. |
| 1056 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1057 | bool ForClass); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1058 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1059 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 1060 | /// for the metaclass of the given interface. The return value has |
| 1061 | /// type ClassPtrTy. |
| 1062 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 1063 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1064 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1065 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1066 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1067 | llvm::Constant *Protocols, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1068 | const ConstantVector &Methods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1069 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1070 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1071 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1072 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1073 | |
| 1074 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1075 | /// implementation. The return value has type MethodListPtrTy. |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1076 | llvm::Constant *EmitMethodList(llvm::Twine Name, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1077 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1078 | const ConstantVector &Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1079 | |
| 1080 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1081 | /// method declarations. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1082 | /// - TypeName: The name for the type containing the methods. |
| 1083 | /// - IsProtocol: True iff these methods are for a protocol. |
| 1084 | /// - ClassMethds: True iff these are class methods. |
| 1085 | /// - Required: When true, only "required" methods are |
| 1086 | /// listed. Similarly, when false only "optional" methods are |
| 1087 | /// listed. For classes this should always be true. |
| 1088 | /// - begin, end: The method list to output. |
| 1089 | /// |
| 1090 | /// The return value has type MethodDescriptionListPtrTy. |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1091 | llvm::Constant *EmitMethodDescList(llvm::Twine Name, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1092 | const char *Section, |
| 1093 | const ConstantVector &Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1094 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1095 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1096 | /// declaration, emitting it if necessary. The return value has type |
| 1097 | /// ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1098 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1099 | |
| 1100 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1101 | /// object for the given declaration, emitting it if needed. These |
| 1102 | /// forward references will be filled in with empty bodies if no |
| 1103 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1104 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1105 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1106 | /// EmitProtocolExtension - Generate the protocol extension |
| 1107 | /// structure used to store optional instance and class methods, and |
| 1108 | /// protocol properties. The return value has type |
| 1109 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1110 | llvm::Constant * |
| 1111 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1112 | const ConstantVector &OptInstanceMethods, |
| 1113 | const ConstantVector &OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1114 | |
| 1115 | /// EmitProtocolList - Generate the list of referenced |
| 1116 | /// protocols. The return value has type ProtocolListPtrTy. |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1117 | llvm::Constant *EmitProtocolList(llvm::Twine Name, |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1118 | ObjCProtocolDecl::protocol_iterator begin, |
| 1119 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1120 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1121 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1122 | /// for the given selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1123 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1124 | |
| 1125 | public: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1126 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1127 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1128 | virtual llvm::Function *ModuleInitFunction(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1129 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1130 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame^] | 1131 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1132 | QualType ResultType, |
| 1133 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1134 | llvm::Value *Receiver, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1135 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1136 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1137 | const ObjCMethodDecl *Method); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1138 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1139 | virtual CodeGen::RValue |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1140 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame^] | 1141 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1142 | QualType ResultType, |
| 1143 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1144 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1145 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1146 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1147 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1148 | const CallArgList &CallArgs, |
| 1149 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1150 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1151 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1152 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1153 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1154 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel); |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1155 | |
| 1156 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1157 | /// untyped one. |
| 1158 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1159 | const ObjCMethodDecl *Method); |
| 1160 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1161 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1162 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1163 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1164 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1165 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1166 | const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1167 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1168 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 1169 | virtual llvm::Constant *GetPropertySetFunction(); |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1170 | virtual llvm::Constant *GetCopyStructFunction(); |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1171 | virtual llvm::Constant *EnumerationMutationFunction(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1172 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 1173 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1174 | const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1175 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 1176 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1177 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1178 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1179 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1180 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1181 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 1182 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 1183 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1184 | llvm::Value *src, llvm::Value *dest, |
| 1185 | llvm::Value *ivarOffset); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1186 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 1187 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1188 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1189 | llvm::Value *dest, llvm::Value *src, |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 1190 | QualType Ty); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1191 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1192 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1193 | QualType ObjectTy, |
| 1194 | llvm::Value *BaseValue, |
| 1195 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1196 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1197 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1198 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1199 | const ObjCIvarDecl *Ivar); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1200 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1201 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1202 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1203 | private: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1204 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1205 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 1206 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1207 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1208 | /// SuperClassReferences - uniqued super class references. |
| 1209 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1210 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1211 | /// MetaClassReferences - uniqued meta class references. |
| 1212 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1213 | |
| 1214 | /// EHTypeReferences - uniqued class ehtype references. |
| 1215 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1216 | |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1217 | /// NonLegacyDispatchMethods - List of methods for which we do *not* generate |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1218 | /// legacy messaging dispatch. |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1219 | llvm::DenseSet<Selector> NonLegacyDispatchMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1220 | |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 1221 | /// DefinedMetaClasses - List of defined meta-classes. |
| 1222 | std::vector<llvm::GlobalValue*> DefinedMetaClasses; |
| 1223 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1224 | /// LegacyDispatchedSelector - Returns true if SEL is not in the list of |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1225 | /// NonLegacyDispatchMethods; false otherwise. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1226 | bool LegacyDispatchedSelector(Selector Sel); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1227 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1228 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 1229 | /// processing a translation unit. |
| 1230 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1231 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 1232 | /// AddModuleClassList - Add the given list of class pointers to the |
| 1233 | /// module with the provided symbol and section names. |
| 1234 | void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container, |
| 1235 | const char *SymbolName, |
| 1236 | const char *SectionName); |
| 1237 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1238 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 1239 | unsigned InstanceStart, |
| 1240 | unsigned InstanceSize, |
| 1241 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1242 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1243 | llvm::Constant *IsAGV, |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1244 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 1245 | llvm::Constant *ClassRoGV, |
| 1246 | bool HiddenVisibility); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1247 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1248 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1249 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 1250 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1251 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1252 | /// EmitMethodList - Emit the method list for the given |
| 1253 | /// implementation. The return value has type MethodListnfABITy. |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1254 | llvm::Constant *EmitMethodList(llvm::Twine Name, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1255 | const char *Section, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 1256 | const ConstantVector &Methods); |
| 1257 | /// EmitIvarList - Emit the ivar list for the given |
| 1258 | /// implementation. If ForClass is true the list of class ivars |
| 1259 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1260 | /// interface ivars will be emitted. The return value has type |
| 1261 | /// IvarListnfABIPtrTy. |
| 1262 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1263 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1264 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 1265 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 1266 | unsigned long int offset); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1267 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1268 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1269 | /// declaration, emitting it if necessary. The return value has type |
| 1270 | /// ProtocolPtrTy. |
| 1271 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1272 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1273 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1274 | /// object for the given declaration, emitting it if needed. These |
| 1275 | /// forward references will be filled in with empty bodies if no |
| 1276 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1277 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1278 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1279 | /// EmitProtocolList - Generate the list of referenced |
| 1280 | /// protocols. The return value has type ProtocolListPtrTy. |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1281 | llvm::Constant *EmitProtocolList(llvm::Twine Name, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1282 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1283 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1284 | |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1285 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame^] | 1286 | ReturnValueSlot Return, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1287 | QualType ResultType, |
| 1288 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 1289 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1290 | QualType Arg0Ty, |
| 1291 | bool IsSuper, |
| 1292 | const CallArgList &CallArgs); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 1293 | |
| 1294 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1295 | /// class of the given name. |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 1296 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1297 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1298 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1299 | /// for the given class reference. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1300 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1301 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1302 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1303 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1304 | /// for the given super class reference. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1305 | llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder, |
| 1306 | const ObjCInterfaceDecl *ID); |
| 1307 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1308 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 1309 | /// meta-data |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1310 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1311 | const ObjCInterfaceDecl *ID); |
| 1312 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1313 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 1314 | /// the given ivar. |
| 1315 | /// |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 1316 | llvm::GlobalVariable * ObjCIvarOffsetVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1317 | const ObjCInterfaceDecl *ID, |
| 1318 | const ObjCIvarDecl *Ivar); |
| 1319 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1320 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1321 | /// for the given selector. |
| 1322 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1323 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1324 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1325 | /// interface. The return value has type EHTypePtrTy. |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1326 | llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 1327 | bool ForDefinition); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1328 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1329 | const char *getMetaclassSymbolPrefix() const { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1330 | return "OBJC_METACLASS_$_"; |
| 1331 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1332 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1333 | const char *getClassSymbolPrefix() const { |
| 1334 | return "OBJC_CLASS_$_"; |
| 1335 | } |
| 1336 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 1337 | void GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1338 | uint32_t &InstanceStart, |
| 1339 | uint32_t &InstanceSize); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1340 | |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1341 | // Shamelessly stolen from Analysis/CFRefCount.cpp |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1342 | Selector GetNullarySelector(const char* name) const { |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1343 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1344 | return CGM.getContext().Selectors.getSelector(0, &II); |
| 1345 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1346 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1347 | Selector GetUnarySelector(const char* name) const { |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1348 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1349 | return CGM.getContext().Selectors.getSelector(1, &II); |
| 1350 | } |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1351 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1352 | /// ImplementationIsNonLazy - Check whether the given category or |
| 1353 | /// class implementation is "non-lazy". |
Fariborz Jahanian | ecfbdcb | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 1354 | bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1355 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1356 | public: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1357 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1358 | // FIXME. All stubs for now! |
| 1359 | virtual llvm::Function *ModuleInitFunction(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1360 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1361 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame^] | 1362 | ReturnValueSlot Return, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1363 | QualType ResultType, |
| 1364 | Selector Sel, |
| 1365 | llvm::Value *Receiver, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1366 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1367 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1368 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1369 | |
| 1370 | virtual CodeGen::RValue |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1371 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame^] | 1372 | ReturnValueSlot Return, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1373 | QualType ResultType, |
| 1374 | Selector Sel, |
| 1375 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1376 | bool isCategoryImpl, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1377 | llvm::Value *Receiver, |
| 1378 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1379 | const CallArgList &CallArgs, |
| 1380 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1381 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1382 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1383 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1384 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1385 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel) |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1386 | { return EmitSelector(Builder, Sel); } |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1387 | |
| 1388 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1389 | /// untyped one. |
| 1390 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1391 | const ObjCMethodDecl *Method) |
| 1392 | { return EmitSelector(Builder, Method->getSelector()); } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1393 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 1394 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1395 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1396 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1397 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 1398 | const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1399 | |
| 1400 | virtual llvm::Constant *GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1401 | return ObjCTypes.getGetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1402 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1403 | virtual llvm::Constant *GetPropertySetFunction() { |
| 1404 | return ObjCTypes.getSetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1405 | } |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1406 | |
| 1407 | virtual llvm::Constant *GetCopyStructFunction() { |
| 1408 | return ObjCTypes.getCopyStructFn(); |
| 1409 | } |
| 1410 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1411 | virtual llvm::Constant *EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1412 | return ObjCTypes.getEnumerationMutationFn(); |
Daniel Dunbar | 28ed084 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 1413 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1414 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1415 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 1416 | const Stmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1417 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 1418 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1419 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1420 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1421 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1422 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1423 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1424 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1425 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1426 | llvm::Value *src, llvm::Value *dest, |
| 1427 | llvm::Value *ivarOffset); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1428 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1429 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1430 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1431 | llvm::Value *dest, llvm::Value *src, |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 1432 | QualType Ty); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1433 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1434 | QualType ObjectTy, |
| 1435 | llvm::Value *BaseValue, |
| 1436 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1437 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1438 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1439 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1440 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1441 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1442 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1443 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1444 | |
| 1445 | /* *** Helper Functions *** */ |
| 1446 | |
| 1447 | /// getConstantGEP() - Help routine to construct simple GEPs. |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1448 | static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1449 | llvm::Constant *C, |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1450 | unsigned idx0, |
| 1451 | unsigned idx1) { |
| 1452 | llvm::Value *Idxs[] = { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1453 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0), |
| 1454 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1455 | }; |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1456 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1459 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 1460 | /// class has the __objc_exception__ attribute. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1461 | static bool hasObjCExceptionAttribute(ASTContext &Context, |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1462 | const ObjCInterfaceDecl *OID) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1463 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1464 | return true; |
| 1465 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1466 | return hasObjCExceptionAttribute(Context, Super); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1467 | return false; |
| 1468 | } |
| 1469 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1470 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1471 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1472 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1473 | ObjCTypes(cgm) { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1474 | ObjCABI = 1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1475 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1478 | /// GetClass - Return a reference to the class for the given interface |
| 1479 | /// decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1480 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1481 | const ObjCInterfaceDecl *ID) { |
| 1482 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1486 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 1487 | return EmitSelector(Builder, Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1488 | } |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1489 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1490 | *Method) { |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1491 | return EmitSelector(Builder, Method->getSelector()); |
| 1492 | } |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1493 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1494 | /// Generate a constant CFString object. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1495 | /* |
| 1496 | struct __builtin_CFString { |
| 1497 | const int *isa; // point to __CFConstantStringClassReference |
| 1498 | int flags; |
| 1499 | const char *str; |
| 1500 | long length; |
| 1501 | }; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1502 | */ |
| 1503 | |
Fariborz Jahanian | 33e982b | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1504 | /// or Generate a constant NSString object. |
| 1505 | /* |
| 1506 | struct __builtin_NSString { |
| 1507 | const int *isa; // point to __NSConstantStringClassReference |
| 1508 | const char *str; |
| 1509 | unsigned int length; |
| 1510 | }; |
| 1511 | */ |
| 1512 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1513 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 1514 | const StringLiteral *SL) { |
Fariborz Jahanian | 33e982b | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1515 | return (CGM.getLangOptions().NoConstantCFStrings == 0 ? |
| 1516 | CGM.GetAddrOfConstantCFString(SL) : |
| 1517 | CGM.GetAddrOfConstantNSString(SL)); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | /// Generates a message send where the super is the receiver. This is |
| 1521 | /// a message send to self with special delivery semantics indicating |
| 1522 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1523 | CodeGen::RValue |
| 1524 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame^] | 1525 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1526 | QualType ResultType, |
| 1527 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1528 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1529 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1530 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1531 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1532 | const CodeGen::CallArgList &CallArgs, |
| 1533 | const ObjCMethodDecl *Method) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1534 | // Create and init a super structure; this is a (receiver, class) |
| 1535 | // pair we will pass to objc_msgSendSuper. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1536 | llvm::Value *ObjCSuper = |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1537 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1538 | llvm::Value *ReceiverAsObject = |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1539 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1540 | CGF.Builder.CreateStore(ReceiverAsObject, |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1541 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1542 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1543 | // If this is a class message the metaclass is passed as the target. |
| 1544 | llvm::Value *Target; |
| 1545 | if (IsClassMessage) { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1546 | if (isCategoryImpl) { |
| 1547 | // Message sent to 'super' in a class method defined in a category |
| 1548 | // implementation requires an odd treatment. |
| 1549 | // If we are in a class method, we must retrieve the |
| 1550 | // _metaclass_ for the current class, pointed at by |
| 1551 | // the class's "isa" pointer. The following assumes that |
| 1552 | // isa" is the first ivar in a class (which it must be). |
| 1553 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1554 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 1555 | Target = CGF.Builder.CreateLoad(Target); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 1556 | } else { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1557 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 1558 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 1559 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 1560 | Target = Super; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1561 | } |
Fariborz Jahanian | 182f268 | 2009-11-14 02:18:31 +0000 | [diff] [blame] | 1562 | } |
| 1563 | else if (isCategoryImpl) |
| 1564 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1565 | else { |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 1566 | llvm::Value *ClassPtr = EmitSuperClassRef(Class); |
| 1567 | ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1); |
| 1568 | Target = CGF.Builder.CreateLoad(ClassPtr); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1569 | } |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1570 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 1571 | // ObjCTypes types. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1572 | const llvm::Type *ClassTy = |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1573 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1574 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1575 | CGF.Builder.CreateStore(Target, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1576 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame^] | 1577 | return EmitLegacyMessageSend(CGF, Return, ResultType, |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1578 | EmitSelector(CGF.Builder, Sel), |
| 1579 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1580 | true, CallArgs, Method, ObjCTypes); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1581 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1582 | |
| 1583 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1584 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame^] | 1585 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1586 | QualType ResultType, |
| 1587 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1588 | llvm::Value *Receiver, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1589 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1590 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1591 | const ObjCMethodDecl *Method) { |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame^] | 1592 | return EmitLegacyMessageSend(CGF, Return, ResultType, |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1593 | EmitSelector(CGF.Builder, Sel), |
| 1594 | Receiver, CGF.getContext().getObjCIdType(), |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1595 | false, CallArgs, Method, ObjCTypes); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1598 | CodeGen::RValue |
| 1599 | CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame^] | 1600 | ReturnValueSlot Return, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1601 | QualType ResultType, |
| 1602 | llvm::Value *Sel, |
| 1603 | llvm::Value *Arg0, |
| 1604 | QualType Arg0Ty, |
| 1605 | bool IsSuper, |
| 1606 | const CallArgList &CallArgs, |
| 1607 | const ObjCMethodDecl *Method, |
| 1608 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1609 | CallArgList ActualArgs; |
Fariborz Jahanian | d019d96 | 2009-04-24 21:07:43 +0000 | [diff] [blame] | 1610 | if (!IsSuper) |
| 1611 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1612 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1613 | ActualArgs.push_back(std::make_pair(RValue::get(Sel), |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1614 | CGF.getContext().getObjCSelType())); |
| 1615 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1616 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1617 | CodeGenTypes &Types = CGM.getTypes(); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1618 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1619 | FunctionType::ExtInfo()); |
Daniel Dunbar | 6793966 | 2009-09-17 04:01:40 +0000 | [diff] [blame] | 1620 | const llvm::FunctionType *FTy = |
| 1621 | Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1622 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1623 | llvm::Constant *Fn = NULL; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1624 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1625 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1626 | : ObjCTypes.getSendStretFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1627 | } else if (ResultType->isFloatingType()) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1628 | if (ObjCABI == 2) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1629 | if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1630 | BuiltinType::Kind k = BT->getKind(); |
| 1631 | Fn = (k == BuiltinType::LongDouble) ? ObjCTypes.getSendFpretFn2(IsSuper) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1632 | : ObjCTypes.getSendFn2(IsSuper); |
Daniel Dunbar | 42f963d | 2009-06-10 04:38:50 +0000 | [diff] [blame] | 1633 | } else { |
| 1634 | Fn = ObjCTypes.getSendFn2(IsSuper); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1635 | } |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 1636 | } else |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1637 | // FIXME. This currently matches gcc's API for x86-32. May need to change |
| 1638 | // for others if we have their API. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1639 | Fn = ObjCTypes.getSendFpretFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1640 | } else { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1641 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1642 | : ObjCTypes.getSendFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1643 | } |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1644 | assert(Fn && "EmitLegacyMessageSend - unknown API"); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1645 | Fn = llvm::ConstantExpr::getBitCast(Fn, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1646 | llvm::PointerType::getUnqual(FTy)); |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame^] | 1647 | return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1650 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1651 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1652 | // FIXME: I don't understand why gcc generates this, or where it is |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1653 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1654 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1655 | |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1656 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1657 | ObjCTypes.ExternalProtocolPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1660 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1661 | // FIXME: We shouldn't need this, the protocol decl should contain enough |
| 1662 | // information to tell us whether this was a declaration or a definition. |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1663 | DefinedProtocols.insert(PD->getIdentifier()); |
| 1664 | |
| 1665 | // If we have generated a forward reference to this protocol, emit |
| 1666 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 1667 | // emitted. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1668 | if (Protocols.count(PD->getIdentifier())) |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1669 | GetOrEmitProtocol(PD); |
| 1670 | } |
| 1671 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1672 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1673 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 1674 | return GetOrEmitProtocol(PD); |
| 1675 | return GetOrEmitProtocolRef(PD); |
| 1676 | } |
| 1677 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1678 | /* |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1679 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 1680 | struct _objc_protocol { |
| 1681 | struct _objc_protocol_extension *isa; |
| 1682 | char *protocol_name; |
| 1683 | struct _objc_protocol_list *protocol_list; |
| 1684 | struct _objc__method_prototype_list *instance_methods; |
| 1685 | struct _objc__method_prototype_list *class_methods |
| 1686 | }; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1687 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1688 | See EmitProtocolExtension(). |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1689 | */ |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1690 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
| 1691 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1692 | |
| 1693 | // Early exit if a defining object has already been generated. |
| 1694 | if (Entry && Entry->hasInitializer()) |
| 1695 | return Entry; |
| 1696 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1697 | // FIXME: I don't understand why gcc generates this, or where it is |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1698 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1699 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1700 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1701 | // Construct method lists. |
| 1702 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1703 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1704 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1705 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1706 | ObjCMethodDecl *MD = *i; |
| 1707 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1708 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1709 | OptInstanceMethods.push_back(C); |
| 1710 | } else { |
| 1711 | InstanceMethods.push_back(C); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1712 | } |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1715 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1716 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1717 | ObjCMethodDecl *MD = *i; |
| 1718 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1719 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1720 | OptClassMethods.push_back(C); |
| 1721 | } else { |
| 1722 | ClassMethods.push_back(C); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1723 | } |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1726 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1727 | Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1728 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1729 | Values[2] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1730 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1731 | PD->protocol_begin(), |
| 1732 | PD->protocol_end()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1733 | Values[3] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1734 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1735 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1736 | InstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1737 | Values[4] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1738 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1739 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1740 | ClassMethods); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1741 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1742 | Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1743 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1744 | if (Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1745 | // Already created, fix the linkage and update the initializer. |
| 1746 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1747 | Entry->setInitializer(Init); |
| 1748 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1749 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1750 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1751 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1752 | Init, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1753 | "\01L_OBJC_PROTOCOL_" + PD->getName()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1754 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1755 | // FIXME: Is this necessary? Why only for protocol? |
| 1756 | Entry->setAlignment(4); |
| 1757 | } |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 1758 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1759 | |
| 1760 | return Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1763 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1764 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1765 | |
| 1766 | if (!Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1767 | // We use the initializer as a marker of whether this is a forward |
| 1768 | // reference or not. At module finalization we add the empty |
| 1769 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1770 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1771 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1772 | llvm::GlobalValue::ExternalLinkage, |
| 1773 | 0, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1774 | "\01L_OBJC_PROTOCOL_" + PD->getName()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1775 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1776 | // FIXME: Is this necessary? Why only for protocol? |
| 1777 | Entry->setAlignment(4); |
| 1778 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1779 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1780 | return Entry; |
| 1781 | } |
| 1782 | |
| 1783 | /* |
| 1784 | struct _objc_protocol_extension { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1785 | uint32_t size; |
| 1786 | struct objc_method_description_list *optional_instance_methods; |
| 1787 | struct objc_method_description_list *optional_class_methods; |
| 1788 | struct objc_property_list *instance_properties; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1789 | }; |
| 1790 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1791 | llvm::Constant * |
| 1792 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1793 | const ConstantVector &OptInstanceMethods, |
| 1794 | const ConstantVector &OptClassMethods) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1795 | uint64_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1796 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1797 | std::vector<llvm::Constant*> Values(4); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1798 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1799 | Values[1] = |
| 1800 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1801 | + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1802 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1803 | OptInstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1804 | Values[2] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1805 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1806 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1807 | OptClassMethods); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1808 | Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1809 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1810 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1811 | // Return null if no extension bits are used. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1812 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1813 | Values[3]->isNullValue()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1814 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1815 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1816 | llvm::Constant *Init = |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1817 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1818 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1819 | // No special section, but goes in llvm.used |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1820 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1821 | Init, |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1822 | 0, 0, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1823 | } |
| 1824 | |
| 1825 | /* |
| 1826 | struct objc_protocol_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1827 | struct objc_protocol_list *next; |
| 1828 | long count; |
| 1829 | Protocol *list[]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1830 | }; |
| 1831 | */ |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1832 | llvm::Constant * |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1833 | CGObjCMac::EmitProtocolList(llvm::Twine Name, |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1834 | ObjCProtocolDecl::protocol_iterator begin, |
| 1835 | ObjCProtocolDecl::protocol_iterator end) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1836 | std::vector<llvm::Constant*> ProtocolRefs; |
| 1837 | |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1838 | for (; begin != end; ++begin) |
| 1839 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1840 | |
| 1841 | // Just return null for empty protocol lists |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1842 | if (ProtocolRefs.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1843 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1844 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1845 | // This list is null terminated. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1846 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1847 | |
| 1848 | std::vector<llvm::Constant*> Values(3); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1849 | // This field is only used by the runtime. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1850 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1851 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1852 | ProtocolRefs.size() - 1); |
| 1853 | Values[2] = |
| 1854 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 1855 | ProtocolRefs.size()), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1856 | ProtocolRefs); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1857 | |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 1858 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1859 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1860 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1861 | 4, false); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1862 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 1865 | void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet, |
| 1866 | std::vector<llvm::Constant*> &Properties, |
| 1867 | const Decl *Container, |
| 1868 | const ObjCProtocolDecl *PROTO, |
| 1869 | const ObjCCommonTypesHelper &ObjCTypes) { |
| 1870 | std::vector<llvm::Constant*> Prop(2); |
| 1871 | for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(), |
| 1872 | E = PROTO->protocol_end(); P != E; ++P) |
| 1873 | PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes); |
| 1874 | for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(), |
| 1875 | E = PROTO->prop_end(); I != E; ++I) { |
| 1876 | const ObjCPropertyDecl *PD = *I; |
| 1877 | if (!PropertySet.insert(PD->getIdentifier())) |
| 1878 | continue; |
| 1879 | Prop[0] = GetPropertyName(PD->getIdentifier()); |
| 1880 | Prop[1] = GetPropertyTypeString(PD, Container); |
| 1881 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop)); |
| 1882 | } |
| 1883 | } |
| 1884 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1885 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1886 | struct _objc_property { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1887 | const char * const name; |
| 1888 | const char * const attributes; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1889 | }; |
| 1890 | |
| 1891 | struct _objc_property_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1892 | uint32_t entsize; // sizeof (struct _objc_property) |
| 1893 | uint32_t prop_count; |
| 1894 | struct _objc_property[prop_count]; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1895 | }; |
| 1896 | */ |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1897 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1898 | const Decl *Container, |
| 1899 | const ObjCContainerDecl *OCD, |
| 1900 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1901 | std::vector<llvm::Constant*> Properties, Prop(2); |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 1902 | llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1903 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(), |
| 1904 | E = OCD->prop_end(); I != E; ++I) { |
Steve Naroff | 93983f8 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 1905 | const ObjCPropertyDecl *PD = *I; |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 1906 | PropertySet.insert(PD->getIdentifier()); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1907 | Prop[0] = GetPropertyName(PD->getIdentifier()); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1908 | Prop[1] = GetPropertyTypeString(PD, Container); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1909 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1910 | Prop)); |
| 1911 | } |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 1912 | if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) |
| 1913 | for (ObjCInterfaceDecl::protocol_iterator P = OID->protocol_begin(), |
| 1914 | E = OID->protocol_end(); P != E; ++P) |
| 1915 | PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1916 | |
| 1917 | // Return null for empty list. |
| 1918 | if (Properties.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1919 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1920 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1921 | unsigned PropertySize = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1922 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1923 | std::vector<llvm::Constant*> Values(3); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1924 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 1925 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1926 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1927 | Properties.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 1928 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 1929 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1930 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1931 | llvm::GlobalVariable *GV = |
| 1932 | CreateMetadataVar(Name, Init, |
| 1933 | (ObjCABI == 2) ? "__DATA, __objc_const" : |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1934 | "__OBJC,__property,regular,no_dead_strip", |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1935 | (ObjCABI == 2) ? 8 : 4, |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1936 | true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1937 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1938 | } |
| 1939 | |
| 1940 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1941 | struct objc_method_description_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1942 | int count; |
| 1943 | struct objc_method_description list[]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1944 | }; |
| 1945 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1946 | llvm::Constant * |
| 1947 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 1948 | std::vector<llvm::Constant*> Desc(2); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1949 | Desc[0] = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1950 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1951 | ObjCTypes.SelectorPtrTy); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1952 | Desc[1] = GetMethodVarType(MD); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1953 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1954 | Desc); |
| 1955 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1956 | |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1957 | llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1958 | const char *Section, |
| 1959 | const ConstantVector &Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1960 | // Return null for empty list. |
| 1961 | if (Methods.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1962 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1963 | |
| 1964 | std::vector<llvm::Constant*> Values(2); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1965 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1966 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1967 | Methods.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 1968 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 1969 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1970 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1971 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1972 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1973 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1976 | /* |
| 1977 | struct _objc_category { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1978 | char *category_name; |
| 1979 | char *class_name; |
| 1980 | struct _objc_method_list *instance_methods; |
| 1981 | struct _objc_method_list *class_methods; |
| 1982 | struct _objc_protocol_list *protocols; |
| 1983 | uint32_t size; // <rdar://4585769> |
| 1984 | struct _objc_property_list *instance_properties; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1985 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1986 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1987 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1988 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1989 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1990 | // FIXME: This is poor design, the OCD should have a pointer to the category |
| 1991 | // decl. Additionally, note that Category can be null for the @implementation |
| 1992 | // w/o an @interface case. Sema should just create one for us as it does for |
| 1993 | // @implementation so everyone else can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1994 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1995 | const ObjCCategoryDecl *Category = |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1996 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1997 | |
| 1998 | llvm::SmallString<256> ExtName; |
| 1999 | llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_' |
| 2000 | << OCD->getName(); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2001 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2002 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2003 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2004 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2005 | // Instance methods should always be defined. |
| 2006 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 2007 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2008 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2009 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2010 | // Class methods should always be defined. |
| 2011 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 2012 | } |
| 2013 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2014 | std::vector<llvm::Constant*> Values(7); |
| 2015 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 2016 | Values[1] = GetClassName(Interface->getIdentifier()); |
Fariborz Jahanian | 679cd7f | 2009-04-29 20:40:05 +0000 | [diff] [blame] | 2017 | LazySymbols.insert(Interface->getIdentifier()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2018 | Values[2] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2019 | EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2020 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2021 | InstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2022 | Values[3] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2023 | EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2024 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2025 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2026 | if (Category) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2027 | Values[4] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2028 | EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2029 | Category->protocol_begin(), |
| 2030 | Category->protocol_end()); |
| 2031 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2032 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2033 | } |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2034 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2035 | |
| 2036 | // If there is no category @interface then there can be no properties. |
| 2037 | if (Category) { |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2038 | Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2039 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2040 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2041 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2042 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2043 | |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2044 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2045 | Values); |
| 2046 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2047 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2048 | CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init, |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2049 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2050 | 4, true); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2051 | DefinedCategories.push_back(GV); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2054 | // FIXME: Get from somewhere? |
| 2055 | enum ClassFlags { |
| 2056 | eClassFlags_Factory = 0x00001, |
| 2057 | eClassFlags_Meta = 0x00002, |
| 2058 | // <rdr://5142207> |
| 2059 | eClassFlags_HasCXXStructors = 0x02000, |
| 2060 | eClassFlags_Hidden = 0x20000, |
| 2061 | eClassFlags_ABI2_Hidden = 0x00010, |
| 2062 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 2063 | }; |
| 2064 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2065 | /* |
| 2066 | struct _objc_class { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2067 | Class isa; |
| 2068 | Class super_class; |
| 2069 | const char *name; |
| 2070 | long version; |
| 2071 | long info; |
| 2072 | long instance_size; |
| 2073 | struct _objc_ivar_list *ivars; |
| 2074 | struct _objc_method_list *methods; |
| 2075 | struct _objc_cache *cache; |
| 2076 | struct _objc_protocol_list *protocols; |
| 2077 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 2078 | const char *ivar_layout; |
| 2079 | struct _objc_class_ext *ext; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2080 | }; |
| 2081 | |
| 2082 | See EmitClassExtension(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2083 | */ |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2084 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2085 | DefinedSymbols.insert(ID->getIdentifier()); |
| 2086 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2087 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2088 | // FIXME: Gross |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2089 | ObjCInterfaceDecl *Interface = |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2090 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2091 | llvm::Constant *Protocols = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2092 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2093 | Interface->protocol_begin(), |
| 2094 | Interface->protocol_end()); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2095 | unsigned Flags = eClassFlags_Factory; |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2096 | if (ID->getNumIvarInitializers()) |
| 2097 | Flags |= eClassFlags_HasCXXStructors; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2098 | unsigned Size = |
Daniel Dunbar | 2bebbf0 | 2009-05-03 10:46:44 +0000 | [diff] [blame] | 2099 | CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2100 | |
| 2101 | // FIXME: Set CXX-structors flag. |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 2102 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2103 | Flags |= eClassFlags_Hidden; |
| 2104 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2105 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2106 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2107 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2108 | // Instance methods should always be defined. |
| 2109 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 2110 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2111 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2112 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2113 | // Class methods should always be defined. |
| 2114 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 2115 | } |
| 2116 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2117 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2118 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2119 | ObjCPropertyImplDecl *PID = *i; |
| 2120 | |
| 2121 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 2122 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 2123 | |
| 2124 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 2125 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 2126 | InstanceMethods.push_back(C); |
| 2127 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 2128 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 2129 | InstanceMethods.push_back(C); |
| 2130 | } |
| 2131 | } |
| 2132 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2133 | std::vector<llvm::Constant*> Values(12); |
Daniel Dunbar | 5384b09 | 2009-05-03 08:56:52 +0000 | [diff] [blame] | 2134 | Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2135 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2136 | // Record a reference to the super class. |
| 2137 | LazySymbols.insert(Super->getIdentifier()); |
| 2138 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2139 | Values[ 1] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2140 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2141 | ObjCTypes.ClassPtrTy); |
| 2142 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2143 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2144 | } |
| 2145 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 2146 | // Version is always 0. |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2147 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2148 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 2149 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2150 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2151 | Values[ 7] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2152 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2153 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2154 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2155 | // cache is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2156 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2157 | Values[ 9] = Protocols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2158 | Values[10] = BuildIvarLayout(ID, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2159 | Values[11] = EmitClassExtension(ID); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2160 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2161 | Values); |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 2162 | std::string Name("\01L_OBJC_CLASS_"); |
| 2163 | Name += ClassName; |
| 2164 | const char *Section = "__OBJC,__class,regular,no_dead_strip"; |
| 2165 | // Check for a forward reference. |
| 2166 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 2167 | if (GV) { |
| 2168 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2169 | "Forward metaclass reference has incorrect type."); |
| 2170 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 2171 | GV->setInitializer(Init); |
| 2172 | GV->setSection(Section); |
| 2173 | GV->setAlignment(4); |
| 2174 | CGM.AddUsedGlobal(GV); |
| 2175 | } |
| 2176 | else |
| 2177 | GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2178 | DefinedClasses.push_back(GV); |
| 2179 | } |
| 2180 | |
| 2181 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 2182 | llvm::Constant *Protocols, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2183 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2184 | unsigned Flags = eClassFlags_Meta; |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2185 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2186 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 2187 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2188 | Flags |= eClassFlags_Hidden; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2189 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2190 | std::vector<llvm::Constant*> Values(12); |
| 2191 | // The isa for the metaclass is the root of the hierarchy. |
| 2192 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 2193 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 2194 | Root = Super; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2195 | Values[ 0] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2196 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2197 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2198 | // The super class for the metaclass is emitted as the name of the |
| 2199 | // super class. The runtime fixes this up to point to the |
| 2200 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2201 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2202 | Values[ 1] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2203 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2204 | ObjCTypes.ClassPtrTy); |
| 2205 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2206 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2207 | } |
| 2208 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 2209 | // Version is always 0. |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2210 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2211 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 2212 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2213 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2214 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2215 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2216 | "__OBJC,__cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2217 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2218 | // cache is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2219 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2220 | Values[ 9] = Protocols; |
| 2221 | // ivar_layout for metaclass is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2222 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2223 | // The class extension is always unused for metaclasses. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2224 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2225 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2226 | Values); |
| 2227 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2228 | std::string Name("\01L_OBJC_METACLASS_"); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2229 | Name += ID->getNameAsCString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2230 | |
| 2231 | // Check for a forward reference. |
| 2232 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 2233 | if (GV) { |
| 2234 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2235 | "Forward metaclass reference has incorrect type."); |
| 2236 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 2237 | GV->setInitializer(Init); |
| 2238 | } else { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2239 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2240 | llvm::GlobalValue::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2241 | Init, Name); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2242 | } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2243 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2244 | GV->setAlignment(4); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2245 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2246 | |
| 2247 | return GV; |
| 2248 | } |
| 2249 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2250 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2251 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2252 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2253 | // FIXME: Should we look these up somewhere other than the module. Its a bit |
| 2254 | // silly since we only generate these while processing an implementation, so |
| 2255 | // exactly one pointer would work if know when we entered/exitted an |
| 2256 | // implementation block. |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2257 | |
| 2258 | // Check for an existing forward reference. |
Fariborz Jahanian | b0d2794 | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 2259 | // Previously, metaclass with internal linkage may have been defined. |
| 2260 | // pass 'true' as 2nd argument so it is returned. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2261 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, |
| 2262 | true)) { |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2263 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2264 | "Forward metaclass reference has incorrect type."); |
| 2265 | return GV; |
| 2266 | } else { |
| 2267 | // Generate as an external reference to keep a consistent |
| 2268 | // module. This will be patched up when we emit the metaclass. |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2269 | return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2270 | llvm::GlobalValue::ExternalLinkage, |
| 2271 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2272 | Name); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2273 | } |
| 2274 | } |
| 2275 | |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 2276 | llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) { |
| 2277 | std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString(); |
| 2278 | |
| 2279 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, |
| 2280 | true)) { |
| 2281 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2282 | "Forward class metadata reference has incorrect type."); |
| 2283 | return GV; |
| 2284 | } else { |
| 2285 | return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
| 2286 | llvm::GlobalValue::ExternalLinkage, |
| 2287 | 0, |
| 2288 | Name); |
| 2289 | } |
| 2290 | } |
| 2291 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2292 | /* |
| 2293 | struct objc_class_ext { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2294 | uint32_t size; |
| 2295 | const char *weak_ivar_layout; |
| 2296 | struct _objc_property_list *properties; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2297 | }; |
| 2298 | */ |
| 2299 | llvm::Constant * |
| 2300 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2301 | uint64_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2302 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2303 | |
| 2304 | std::vector<llvm::Constant*> Values(3); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2305 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 2306 | Values[1] = BuildIvarLayout(ID, false); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2307 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2308 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2309 | |
| 2310 | // Return null if no extension bits are used. |
| 2311 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2312 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2313 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2314 | llvm::Constant *Init = |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2315 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2316 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2317 | Init, "__OBJC,__class_ext,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2318 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
| 2321 | /* |
| 2322 | struct objc_ivar { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2323 | char *ivar_name; |
| 2324 | char *ivar_type; |
| 2325 | int ivar_offset; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2326 | }; |
| 2327 | |
| 2328 | struct objc_ivar_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2329 | int ivar_count; |
| 2330 | struct objc_ivar list[count]; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2331 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2332 | */ |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2333 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2334 | bool ForClass) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2335 | std::vector<llvm::Constant*> Ivars, Ivar(3); |
| 2336 | |
| 2337 | // When emitting the root class GCC emits ivar entries for the |
| 2338 | // actual class structure. It is not clear if we need to follow this |
| 2339 | // behavior; for now lets try and get away with not doing it. If so, |
| 2340 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 2341 | // for the class. |
| 2342 | if (ForClass) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2343 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2344 | |
| 2345 | ObjCInterfaceDecl *OID = |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2346 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2347 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 2348 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 2349 | CGM.getContext().ShallowCollectObjCIvars(OID, OIvars); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2350 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 2351 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 2352 | ObjCIvarDecl *IVD = OIvars[i]; |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 2353 | // Ignore unnamed bit-fields. |
| 2354 | if (!IVD->getDeclName()) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2355 | continue; |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 2356 | Ivar[0] = GetMethodVarName(IVD->getIdentifier()); |
| 2357 | Ivar[1] = GetMethodVarType(IVD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2358 | Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2359 | ComputeIvarBaseOffset(CGM, OID, IVD)); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2360 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2361 | } |
| 2362 | |
| 2363 | // Return null for empty list. |
| 2364 | if (Ivars.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2365 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2366 | |
| 2367 | std::vector<llvm::Constant*> Values(2); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2368 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2369 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2370 | Ivars.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2371 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 2372 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2373 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2374 | llvm::GlobalVariable *GV; |
| 2375 | if (ForClass) |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2376 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2377 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2378 | 4, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2379 | else |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2380 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2381 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2382 | 4, true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2383 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
| 2386 | /* |
| 2387 | struct objc_method { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2388 | SEL method_name; |
| 2389 | char *method_types; |
| 2390 | void *method; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2391 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2392 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2393 | struct objc_method_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2394 | struct objc_method_list *obsolete; |
| 2395 | int count; |
| 2396 | struct objc_method methods_list[count]; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2397 | }; |
| 2398 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2399 | |
| 2400 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 2401 | /// given method if it has been defined. The result is null if the |
| 2402 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2403 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2404 | // FIXME: Use DenseMap::lookup |
| 2405 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 2406 | if (!Fn) |
| 2407 | return 0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2408 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2409 | std::vector<llvm::Constant*> Method(3); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2410 | Method[0] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2411 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2412 | ObjCTypes.SelectorPtrTy); |
| 2413 | Method[1] = GetMethodVarType(MD); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2414 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2415 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2418 | llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2419 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2420 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2421 | // Return null for empty list. |
| 2422 | if (Methods.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2423 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2424 | |
| 2425 | std::vector<llvm::Constant*> Values(3); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2426 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2427 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2428 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2429 | Methods.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2430 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 2431 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2432 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2433 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2434 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2435 | ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2436 | } |
| 2437 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 2438 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2439 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 2440 | llvm::SmallString<256> Name; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 2441 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2442 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2443 | CodeGenTypes &Types = CGM.getTypes(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2444 | const llvm::FunctionType *MethodTy = |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2445 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2446 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2447 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2448 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 2449 | Name.str(), |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2450 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2451 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2452 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2453 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2454 | } |
| 2455 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2456 | llvm::GlobalVariable * |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2457 | CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name, |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2458 | llvm::Constant *Init, |
| 2459 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2460 | unsigned Align, |
| 2461 | bool AddToUsed) { |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2462 | const llvm::Type *Ty = Init->getType(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2463 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2464 | new llvm::GlobalVariable(CGM.getModule(), Ty, false, |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2465 | llvm::GlobalValue::InternalLinkage, Init, Name); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2466 | if (Section) |
| 2467 | GV->setSection(Section); |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2468 | if (Align) |
| 2469 | GV->setAlignment(Align); |
| 2470 | if (AddToUsed) |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2471 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2472 | return GV; |
| 2473 | } |
| 2474 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2475 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2476 | // Abuse this interface function as a place to finalize. |
| 2477 | FinishModule(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2478 | return NULL; |
| 2479 | } |
| 2480 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2481 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2482 | return ObjCTypes.getGetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2483 | } |
| 2484 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2485 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2486 | return ObjCTypes.getSetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 2489 | llvm::Constant *CGObjCMac::GetCopyStructFunction() { |
| 2490 | return ObjCTypes.getCopyStructFn(); |
| 2491 | } |
| 2492 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2493 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2494 | return ObjCTypes.getEnumerationMutationFn(); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2495 | } |
| 2496 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2497 | /* |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2498 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2499 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 2500 | -- |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2501 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2502 | The basic framework for a @try-catch-finally is as follows: |
| 2503 | { |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2504 | objc_exception_data d; |
| 2505 | id _rethrow = null; |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2506 | bool _call_try_exit = true; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2507 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2508 | objc_exception_try_enter(&d); |
| 2509 | if (!setjmp(d.jmp_buf)) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2510 | ... try body ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2511 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2512 | // exception path |
| 2513 | id _caught = objc_exception_extract(&d); |
| 2514 | |
| 2515 | // enter new try scope for handlers |
| 2516 | if (!setjmp(d.jmp_buf)) { |
| 2517 | ... match exception and execute catch blocks ... |
| 2518 | |
| 2519 | // fell off end, rethrow. |
| 2520 | _rethrow = _caught; |
| 2521 | ... jump-through-finally to finally_rethrow ... |
| 2522 | } else { |
| 2523 | // exception in catch block |
| 2524 | _rethrow = objc_exception_extract(&d); |
| 2525 | _call_try_exit = false; |
| 2526 | ... jump-through-finally to finally_rethrow ... |
| 2527 | } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2528 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2529 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2530 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2531 | finally: |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2532 | if (_call_try_exit) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2533 | objc_exception_try_exit(&d); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2534 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2535 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2536 | ... dispatch to finally destination ... |
| 2537 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2538 | finally_rethrow: |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2539 | objc_exception_throw(_rethrow); |
| 2540 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2541 | finally_end: |
| 2542 | } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2543 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2544 | This framework differs slightly from the one gcc uses, in that gcc |
| 2545 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 2546 | and if the object should be rethrown. This breaks in the face of |
| 2547 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2548 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2549 | We specialize this framework for a few particular circumstances: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2550 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2551 | - If there are no catch blocks, then we avoid emitting the second |
| 2552 | exception handling context. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2553 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2554 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 2555 | e)) we avoid emitting the code to rethrow an uncaught exception. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2556 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2557 | - FIXME: If there is no @finally block we can do a few more |
| 2558 | simplifications. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2559 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2560 | Rethrows and Jumps-Through-Finally |
| 2561 | -- |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2562 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2563 | Support for implicit rethrows and jumping through the finally block is |
| 2564 | handled by storing the current exception-handling context in |
| 2565 | ObjCEHStack. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2566 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2567 | In order to implement proper @finally semantics, we support one basic |
| 2568 | mechanism for jumping through the finally block to an arbitrary |
| 2569 | destination. Constructs which generate exits from a @try or @catch |
| 2570 | block use this mechanism to implement the proper semantics by chaining |
| 2571 | jumps, as necessary. |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2572 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2573 | This mechanism works like the one used for indirect goto: we |
| 2574 | arbitrarily assign an ID to each destination and store the ID for the |
| 2575 | destination in a variable prior to entering the finally block. At the |
| 2576 | end of the finally block we simply create a switch to the proper |
| 2577 | destination. |
| 2578 | |
| 2579 | Code gen for @synchronized(expr) stmt; |
| 2580 | Effectively generating code for: |
| 2581 | objc_sync_enter(expr); |
| 2582 | @try stmt @finally { objc_sync_exit(expr); } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2583 | */ |
| 2584 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2585 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 2586 | const Stmt &S) { |
| 2587 | bool isTry = isa<ObjCAtTryStmt>(S); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2588 | // Create various blocks we refer to for handling @finally. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2589 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2590 | llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit"); |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2591 | llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit"); |
| 2592 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
| 2593 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2594 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2595 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 2596 | // evaluation of the expression must occur before we enter the |
| 2597 | // @synchronized. We can safely avoid a temp here because jumps into |
| 2598 | // @synchronized are illegal & this will dominate uses. |
| 2599 | llvm::Value *SyncArg = 0; |
| 2600 | if (!isTry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2601 | SyncArg = |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2602 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 2603 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 2604 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2605 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2606 | |
| 2607 | // Push an EH context entry, used for handling rethrows and jumps |
| 2608 | // through finally. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2609 | CGF.PushCleanupBlock(FinallyBlock); |
| 2610 | |
Fariborz Jahanian | 8c89ff9 | 2009-12-14 21:35:01 +0000 | [diff] [blame] | 2611 | if (CGF.ObjCEHValueStack.empty()) |
| 2612 | CGF.ObjCEHValueStack.push_back(0); |
| 2613 | // If This is a nested @try, caught exception is that of enclosing @try. |
| 2614 | else |
| 2615 | CGF.ObjCEHValueStack.push_back(CGF.ObjCEHValueStack.back()); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2616 | // Allocate memory for the exception data and rethrow pointer. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2617 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 2618 | "exceptiondata.ptr"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2619 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2620 | "_rethrow"); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2621 | llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca( |
| 2622 | llvm::Type::getInt1Ty(VMContext), |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2623 | "_call_try_exit"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2624 | CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(VMContext), |
| 2625 | CallTryExitPtr); |
| 2626 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2627 | // Enter a new try block and call setjmp. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2628 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2629 | llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0, |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2630 | "jmpbufarray"); |
| 2631 | JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2632 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2633 | JmpBufPtr, "result"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2634 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2635 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 2636 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2637 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2638 | TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2639 | |
| 2640 | // Emit the @try block. |
| 2641 | CGF.EmitBlock(TryBlock); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2642 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 2643 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2644 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2645 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2646 | // Emit the "exception in @try" block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2647 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2648 | |
| 2649 | // Retrieve the exception object. We may emit multiple blocks but |
| 2650 | // nothing can cross this so the value is already in SSA form. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2651 | llvm::Value *Caught = |
| 2652 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2653 | ExceptionData, "caught"); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2654 | CGF.ObjCEHValueStack.back() = Caught; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2655 | if (!isTry) { |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2656 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2657 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext), |
| 2658 | CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2659 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 2660 | } else if (cast<ObjCAtTryStmt>(S).getNumCatchStmts()) { |
| 2661 | const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S); |
| 2662 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2663 | // Enter a new exception try block (in case a @catch block throws |
| 2664 | // an exception). |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2665 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2666 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2667 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2668 | JmpBufPtr, "result"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2669 | llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2670 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2671 | llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch"); |
| 2672 | llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler"); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2673 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2674 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2675 | CGF.EmitBlock(CatchBlock); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2676 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2677 | // Handle catch list. As a special case we check if everything is |
| 2678 | // matched and avoid generating code for falling off the end if |
| 2679 | // so. |
| 2680 | bool AllMatched = false; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 2681 | for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) { |
| 2682 | const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I); |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2683 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2684 | |
Douglas Gregor | c00d8e1 | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 2685 | const VarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2686 | const ObjCObjectPointerType *OPT = 0; |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2687 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2688 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2689 | if (!CatchParam) { |
| 2690 | AllMatched = true; |
| 2691 | } else { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2692 | OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2693 | |
| 2694 | // catch(id e) always matches. |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 2695 | // FIXME: For the time being we also match id<X>; this should |
| 2696 | // be rejected by Sema instead. |
Eli Friedman | 818e96f | 2009-07-11 00:57:02 +0000 | [diff] [blame] | 2697 | if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType())) |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2698 | AllMatched = true; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2699 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2700 | |
| 2701 | if (AllMatched) { |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2702 | if (CatchParam) { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2703 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2704 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2705 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2706 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2707 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2708 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2709 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2710 | break; |
| 2711 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2712 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2713 | assert(OPT && "Unexpected non-object pointer type in @catch"); |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 2714 | const ObjCObjectType *ObjTy = OPT->getObjectType(); |
| 2715 | ObjCInterfaceDecl *IDecl = ObjTy->getInterface(); |
| 2716 | assert(IDecl && "Catch parameter must have Objective-C type!"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2717 | |
| 2718 | // Check if the @catch block matches the exception object. |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 2719 | llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2720 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2721 | llvm::Value *Match = |
| 2722 | CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(), |
| 2723 | Class, Caught, "match"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2724 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2725 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2726 | |
| 2727 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2728 | MatchedBlock, NextCatchBlock); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2729 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2730 | // Emit the @catch block. |
| 2731 | CGF.EmitBlock(MatchedBlock); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2732 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2733 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2734 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2735 | llvm::Value *Tmp = |
| 2736 | CGF.Builder.CreateBitCast(Caught, |
| 2737 | CGF.ConvertType(CatchParam->getType()), |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2738 | "tmp"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2739 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2740 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2741 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2742 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2743 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2744 | CGF.EmitBlock(NextCatchBlock); |
| 2745 | } |
| 2746 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2747 | if (!AllMatched) { |
| 2748 | // None of the handlers caught the exception, so store it to be |
| 2749 | // rethrown at the end of the @finally block. |
| 2750 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2751 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2752 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2753 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2754 | // Emit the exception handler for the @catch blocks. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2755 | CGF.EmitBlock(CatchHandler); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2756 | CGF.Builder.CreateStore( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2757 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2758 | ExceptionData), |
| 2759 | RethrowPtr); |
| 2760 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext), |
| 2761 | CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2762 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2763 | } else { |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2764 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2765 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext), |
| 2766 | CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2767 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2768 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2769 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2770 | // Pop the exception-handling stack entry. It is important to do |
| 2771 | // this now, because the code in the @finally block is not in this |
| 2772 | // context. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2773 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 2774 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2775 | CGF.ObjCEHValueStack.pop_back(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2776 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2777 | // Emit the @finally block. |
| 2778 | CGF.EmitBlock(FinallyBlock); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2779 | llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2780 | |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2781 | CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2782 | |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2783 | CGF.EmitBlock(FinallyExit); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2784 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2785 | |
| 2786 | CGF.EmitBlock(FinallyNoExit); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2787 | if (isTry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2788 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 2789 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2790 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2791 | } else { |
| 2792 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 2793 | // @synchronized. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2794 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg); |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 2795 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2796 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2797 | // Emit the switch block |
| 2798 | if (Info.SwitchBlock) |
| 2799 | CGF.EmitBlock(Info.SwitchBlock); |
| 2800 | if (Info.EndBlock) |
| 2801 | CGF.EmitBlock(Info.EndBlock); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2802 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2803 | CGF.EmitBlock(FinallyRethrow); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2804 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2805 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2806 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2807 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2808 | CGF.EmitBlock(FinallyEnd); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
| 2811 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2812 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2813 | llvm::Value *ExceptionAsObject; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2814 | |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2815 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 2816 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2817 | ExceptionAsObject = |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2818 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 2819 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2820 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2821 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2822 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2823 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2824 | |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2825 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2826 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2827 | |
| 2828 | // Clear the insertion point to indicate we are in unreachable code. |
| 2829 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2832 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2833 | /// object: objc_read_weak (id *src) |
| 2834 | /// |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2835 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2836 | llvm::Value *AddrWeakObj) { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2837 | const llvm::Type* DestTy = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2838 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
| 2839 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, |
| 2840 | ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2841 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2842 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2843 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2844 | return read_weak; |
| 2845 | } |
| 2846 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2847 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 2848 | /// objc_assign_weak (id src, id *dst) |
| 2849 | /// |
| 2850 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2851 | llvm::Value *src, llvm::Value *dst) { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2852 | const llvm::Type * SrcTy = src->getType(); |
| 2853 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2854 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2855 | assert(Size <= 8 && "does not support size > 8"); |
| 2856 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2857 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2858 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2859 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2860 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2861 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 2862 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2863 | src, dst, "weakassign"); |
| 2864 | return; |
| 2865 | } |
| 2866 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2867 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 2868 | /// objc_assign_global (id src, id *dst) |
| 2869 | /// |
| 2870 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2871 | llvm::Value *src, llvm::Value *dst) { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2872 | const llvm::Type * SrcTy = src->getType(); |
| 2873 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2874 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2875 | assert(Size <= 8 && "does not support size > 8"); |
| 2876 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2877 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2878 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2879 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2880 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2881 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2882 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2883 | src, dst, "globalassign"); |
| 2884 | return; |
| 2885 | } |
| 2886 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2887 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 2888 | /// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset) |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2889 | /// |
| 2890 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 2891 | llvm::Value *src, llvm::Value *dst, |
| 2892 | llvm::Value *ivarOffset) { |
| 2893 | assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL"); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2894 | const llvm::Type * SrcTy = src->getType(); |
| 2895 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2896 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2897 | assert(Size <= 8 && "does not support size > 8"); |
| 2898 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2899 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2900 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2901 | } |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2902 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2903 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 2904 | CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(), |
| 2905 | src, dst, ivarOffset); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2906 | return; |
| 2907 | } |
| 2908 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2909 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 2910 | /// objc_assign_strongCast (id src, id *dst) |
| 2911 | /// |
| 2912 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2913 | llvm::Value *src, llvm::Value *dst) { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2914 | const llvm::Type * SrcTy = src->getType(); |
| 2915 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2916 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2917 | assert(Size <= 8 && "does not support size > 8"); |
| 2918 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2919 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2920 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2921 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2922 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2923 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2924 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2925 | src, dst, "weakassign"); |
| 2926 | return; |
| 2927 | } |
| 2928 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2929 | void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2930 | llvm::Value *DestPtr, |
| 2931 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 2932 | QualType Ty) { |
| 2933 | // Get size info for this aggregate. |
| 2934 | std::pair<uint64_t, unsigned> TypeInfo = CGM.getContext().getTypeInfo(Ty); |
Fariborz Jahanian | 736d8a6 | 2009-09-10 16:20:31 +0000 | [diff] [blame] | 2935 | unsigned long size = TypeInfo.first/8; |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2936 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 2937 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2938 | llvm::Value *N = llvm::ConstantInt::get(ObjCTypes.LongTy, size); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2939 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), |
| 2940 | DestPtr, SrcPtr, N); |
| 2941 | return; |
| 2942 | } |
| 2943 | |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2944 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 2945 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2946 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 2947 | QualType ObjectTy, |
| 2948 | llvm::Value *BaseValue, |
| 2949 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2950 | unsigned CVRQualifiers) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2951 | const ObjCInterfaceDecl *ID = |
| 2952 | ObjectTy->getAs<ObjCObjectType>()->getInterface(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2953 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 2954 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2955 | } |
| 2956 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2957 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 2958 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2959 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2960 | uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2961 | return llvm::ConstantInt::get( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2962 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 2963 | Offset); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2966 | /* *** Private Interface *** */ |
| 2967 | |
| 2968 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 2969 | /// level information. |
| 2970 | /// |
| 2971 | /// See: <rdr://4810609&4810587&4810587> |
| 2972 | /// struct IMAGE_INFO { |
| 2973 | /// unsigned version; |
| 2974 | /// unsigned flags; |
| 2975 | /// }; |
| 2976 | enum ImageInfoFlags { |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 2977 | eImageInfo_FixAndContinue = (1 << 0), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2978 | eImageInfo_GarbageCollected = (1 << 1), |
| 2979 | eImageInfo_GCOnly = (1 << 2), |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2980 | eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set. |
| 2981 | |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 2982 | // A flag indicating that the module has no instances of a @synthesize of a |
| 2983 | // superclass variable. <rdar://problem/6803242> |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2984 | eImageInfo_CorrectedSynthesize = (1 << 4) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2985 | }; |
| 2986 | |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 2987 | void CGObjCCommonMac::EmitImageInfo() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2988 | unsigned version = 0; // Version is unused? |
| 2989 | unsigned flags = 0; |
| 2990 | |
| 2991 | // FIXME: Fix and continue? |
| 2992 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 2993 | flags |= eImageInfo_GarbageCollected; |
| 2994 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 2995 | flags |= eImageInfo_GCOnly; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2996 | |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2997 | // We never allow @synthesize of a superclass property. |
| 2998 | flags |= eImageInfo_CorrectedSynthesize; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2999 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3000 | // Emitted as int[2]; |
| 3001 | llvm::Constant *values[2] = { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3002 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), version), |
| 3003 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), flags) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3004 | }; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3005 | llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext), 2); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3006 | |
| 3007 | const char *Section; |
| 3008 | if (ObjCABI == 1) |
| 3009 | Section = "__OBJC, __image_info,regular"; |
| 3010 | else |
| 3011 | Section = "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3012 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3013 | CreateMetadataVar("\01L_OBJC_IMAGE_INFO", |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 3014 | llvm::ConstantArray::get(AT, values, 2), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3015 | Section, |
| 3016 | 0, |
| 3017 | true); |
| 3018 | GV->setConstant(true); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3019 | } |
| 3020 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3021 | |
| 3022 | // struct objc_module { |
| 3023 | // unsigned long version; |
| 3024 | // unsigned long size; |
| 3025 | // const char *name; |
| 3026 | // Symtab symtab; |
| 3027 | // }; |
| 3028 | |
| 3029 | // FIXME: Get from somewhere |
| 3030 | static const int ModuleVersion = 7; |
| 3031 | |
| 3032 | void CGObjCMac::EmitModuleInfo() { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3033 | uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3034 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3035 | std::vector<llvm::Constant*> Values(4); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3036 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion); |
| 3037 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 3038 | // This used to be the filename, now it is unused. <rdr://4327263> |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3039 | Values[2] = GetClassName(&CGM.getContext().Idents.get("")); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3040 | Values[3] = EmitModuleSymbols(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3041 | CreateMetadataVar("\01L_OBJC_MODULES", |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3042 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3043 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 3044 | 4, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3045 | } |
| 3046 | |
| 3047 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3048 | unsigned NumClasses = DefinedClasses.size(); |
| 3049 | unsigned NumCategories = DefinedCategories.size(); |
| 3050 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3051 | // Return null if no symbols were defined. |
| 3052 | if (!NumClasses && !NumCategories) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3053 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3054 | |
| 3055 | std::vector<llvm::Constant*> Values(5); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3056 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3057 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3058 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 3059 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3060 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3061 | // The runtime expects exactly the list of defined classes followed |
| 3062 | // by the list of defined categories, in a single array. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3063 | std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3064 | for (unsigned i=0; i<NumClasses; i++) |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3065 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3066 | ObjCTypes.Int8PtrTy); |
| 3067 | for (unsigned i=0; i<NumCategories; i++) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3068 | Symbols[NumClasses + i] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3069 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3070 | ObjCTypes.Int8PtrTy); |
| 3071 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3072 | Values[4] = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3073 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3074 | NumClasses + NumCategories), |
| 3075 | Symbols); |
| 3076 | |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 3077 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3078 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3079 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3080 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 3081 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3082 | 4, true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3083 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3084 | } |
| 3085 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3086 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3087 | const ObjCInterfaceDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3088 | LazySymbols.insert(ID->getIdentifier()); |
| 3089 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3090 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3091 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3092 | if (!Entry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3093 | llvm::Constant *Casted = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3094 | llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3095 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3096 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3097 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 3098 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3099 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3100 | } |
| 3101 | |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 3102 | return Builder.CreateLoad(Entry, "tmp"); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3103 | } |
| 3104 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 3105 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3106 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3107 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3108 | if (!Entry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3109 | llvm::Constant *Casted = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3110 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3111 | ObjCTypes.SelectorPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3112 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3113 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 3114 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3115 | 4, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3116 | } |
| 3117 | |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 3118 | return Builder.CreateLoad(Entry, "tmp"); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3119 | } |
| 3120 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3121 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3122 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3123 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3124 | if (!Entry) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3125 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3126 | llvm::ConstantArray::get(VMContext, |
| 3127 | Ident->getNameStart()), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3128 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3129 | 1, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3130 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3131 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3132 | } |
| 3133 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 3134 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 3135 | /// ivar layout bitmap. |
| 3136 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3137 | const ObjCCommonTypesHelper &ObjCTypes) { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3138 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 3139 | } |
| 3140 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3141 | static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) { |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3142 | if (FQT.isObjCGCStrong()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3143 | return Qualifiers::Strong; |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3144 | |
| 3145 | if (FQT.isObjCGCWeak()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3146 | return Qualifiers::Weak; |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3147 | |
Fariborz Jahanian | 039e6a1 | 2009-09-11 17:39:05 +0000 | [diff] [blame] | 3148 | if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3149 | return Qualifiers::Strong; |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3150 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3151 | if (const PointerType *PT = FQT->getAs<PointerType>()) |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3152 | return GetGCAttrTypeForType(Ctx, PT->getPointeeType()); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3153 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3154 | return Qualifiers::GCNone; |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3155 | } |
| 3156 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3157 | void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3158 | unsigned int BytePos, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3159 | bool ForStrongLayout, |
| 3160 | bool &HasUnion) { |
| 3161 | const RecordDecl *RD = RT->getDecl(); |
| 3162 | // FIXME - Use iterator. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3163 | llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end()); |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3164 | const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3165 | const llvm::StructLayout *RecLayout = |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3166 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3167 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3168 | BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos, |
| 3169 | ForStrongLayout, HasUnion); |
| 3170 | } |
| 3171 | |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 3172 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3173 | const llvm::StructLayout *Layout, |
| 3174 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3175 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3176 | unsigned int BytePos, bool ForStrongLayout, |
| 3177 | bool &HasUnion) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3178 | bool IsUnion = (RD && RD->isUnion()); |
| 3179 | uint64_t MaxUnionIvarSize = 0; |
| 3180 | uint64_t MaxSkippedUnionIvarSize = 0; |
| 3181 | FieldDecl *MaxField = 0; |
| 3182 | FieldDecl *MaxSkippedField = 0; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3183 | FieldDecl *LastFieldBitfield = 0; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3184 | uint64_t MaxFieldOffset = 0; |
| 3185 | uint64_t MaxSkippedFieldOffset = 0; |
| 3186 | uint64_t LastBitfieldOffset = 0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3187 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3188 | if (RecFields.empty()) |
| 3189 | return; |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3190 | unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0); |
| 3191 | unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth(); |
| 3192 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3193 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3194 | FieldDecl *Field = RecFields[i]; |
Daniel Dunbar | e05cc98 | 2009-05-03 23:35:23 +0000 | [diff] [blame] | 3195 | uint64_t FieldOffset; |
Anders Carlsson | fc514ab | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 3196 | if (RD) { |
Daniel Dunbar | f8f8eba | 2010-04-14 17:02:21 +0000 | [diff] [blame] | 3197 | // Note that 'i' here is actually the field index inside RD of Field, |
| 3198 | // although this dependency is hidden. |
| 3199 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
| 3200 | FieldOffset = RL.getFieldOffset(i) / 8; |
Anders Carlsson | fc514ab | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 3201 | } else |
Daniel Dunbar | e05cc98 | 2009-05-03 23:35:23 +0000 | [diff] [blame] | 3202 | FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)); |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3203 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3204 | // Skip over unnamed or bitfields |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3205 | if (!Field->getIdentifier() || Field->isBitField()) { |
| 3206 | LastFieldBitfield = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3207 | LastBitfieldOffset = FieldOffset; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3208 | continue; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3209 | } |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3210 | |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3211 | LastFieldBitfield = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3212 | QualType FQT = Field->getType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3213 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3214 | if (FQT->isUnionType()) |
| 3215 | HasUnion = true; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3216 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3217 | BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(), |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3218 | BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3219 | ForStrongLayout, HasUnion); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3220 | continue; |
| 3221 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3222 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3223 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3224 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3225 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3226 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3227 | assert(CArray && "only array with known element size is supported"); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3228 | FQT = CArray->getElementType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3229 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 3230 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3231 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3232 | ElCount *= CArray->getSize().getZExtValue(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3233 | FQT = CArray->getElementType(); |
| 3234 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3235 | |
| 3236 | assert(!FQT->isUnionType() && |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3237 | "layout for array of unions not supported"); |
| 3238 | if (FQT->isRecordType()) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3239 | int OldIndex = IvarsInfo.size() - 1; |
| 3240 | int OldSkIndex = SkipIvars.size() -1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3241 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3242 | const RecordType *RT = FQT->getAs<RecordType>(); |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3243 | BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3244 | ForStrongLayout, HasUnion); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3245 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3246 | // Replicate layout information for each array element. Note that |
| 3247 | // one element is already done. |
| 3248 | uint64_t ElIx = 1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3249 | for (int FirstIndex = IvarsInfo.size() - 1, |
| 3250 | FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3251 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3252 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 3253 | IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx, |
| 3254 | IvarsInfo[i].ivar_size)); |
| 3255 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) |
| 3256 | SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx, |
| 3257 | SkipIvars[i].ivar_size)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3258 | } |
| 3259 | continue; |
| 3260 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3261 | } |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3262 | // At this point, we are done with Record/Union and array there of. |
| 3263 | // For other arrays we are down to its element type. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3264 | Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3265 | |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3266 | unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3267 | if ((ForStrongLayout && GCAttr == Qualifiers::Strong) |
| 3268 | || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) { |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3269 | if (IsUnion) { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3270 | uint64_t UnionIvarSize = FieldSize / WordSizeInBits; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3271 | if (UnionIvarSize > MaxUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3272 | MaxUnionIvarSize = UnionIvarSize; |
| 3273 | MaxField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3274 | MaxFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3275 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3276 | } else { |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3277 | IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3278 | FieldSize / WordSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3279 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3280 | } else if ((ForStrongLayout && |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3281 | (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)) |
| 3282 | || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) { |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3283 | if (IsUnion) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3284 | // FIXME: Why the asymmetry? We divide by word size in bits on other |
| 3285 | // side. |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3286 | uint64_t UnionIvarSize = FieldSize; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3287 | if (UnionIvarSize > MaxSkippedUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3288 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 3289 | MaxSkippedField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3290 | MaxSkippedFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3291 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3292 | } else { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3293 | // FIXME: Why the asymmetry, we divide by byte size in bits here? |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3294 | SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3295 | FieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3296 | } |
| 3297 | } |
| 3298 | } |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3299 | |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3300 | if (LastFieldBitfield) { |
| 3301 | // Last field was a bitfield. Must update skip info. |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3302 | Expr *BitWidth = LastFieldBitfield->getBitWidth(); |
| 3303 | uint64_t BitFieldSize = |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 3304 | BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue(); |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3305 | GC_IVAR skivar; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3306 | skivar.ivar_bytepos = BytePos + LastBitfieldOffset; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3307 | skivar.ivar_size = (BitFieldSize / ByteSizeInBits) |
| 3308 | + ((BitFieldSize % ByteSizeInBits) != 0); |
| 3309 | SkipIvars.push_back(skivar); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3310 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3311 | |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3312 | if (MaxField) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3313 | IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3314 | MaxUnionIvarSize)); |
| 3315 | if (MaxSkippedField) |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3316 | SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3317 | MaxSkippedUnionIvarSize)); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3318 | } |
| 3319 | |
| 3320 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 3321 | /// implementation for the __strong or __weak case. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3322 | /// The layout map displays which words in ivar list must be skipped |
| 3323 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 3324 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3325 | /// of words to skip and right nibble is count of words to scan. So, each |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3326 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3327 | /// represented by a 0x00 byte which also ends the string. |
| 3328 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 3329 | /// - id, Class |
| 3330 | /// - object * |
| 3331 | /// - __strong anything |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3332 | /// |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3333 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 3334 | /// - __weak anything |
| 3335 | /// |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3336 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3337 | const ObjCImplementationDecl *OMD, |
| 3338 | bool ForStrongLayout) { |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3339 | bool hasUnion = false; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3340 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3341 | unsigned int WordsToScan, WordsToSkip; |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 3342 | const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3343 | if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3344 | return llvm::Constant::getNullValue(PtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3345 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3346 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3347 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3348 | CGM.getContext().CollectObjCIvars(OI, RecFields); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3349 | |
Daniel Dunbar | 3715328 | 2009-05-04 04:10:48 +0000 | [diff] [blame] | 3350 | // Add this implementations synthesized ivars. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 3351 | llvm::SmallVector<ObjCIvarDecl*, 16> Ivars; |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 3352 | CGM.getContext().CollectNonClassIvars(OI, Ivars); |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 3353 | for (unsigned k = 0, e = Ivars.size(); k != e; ++k) |
| 3354 | RecFields.push_back(cast<FieldDecl>(Ivars[k])); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3355 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3356 | if (RecFields.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3357 | return llvm::Constant::getNullValue(PtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3358 | |
| 3359 | SkipIvars.clear(); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3360 | IvarsInfo.clear(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3361 | |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 3362 | BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion); |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3363 | if (IvarsInfo.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3364 | return llvm::Constant::getNullValue(PtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3365 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3366 | // Sort on byte position in case we encounterred a union nested in |
| 3367 | // the ivar list. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3368 | if (hasUnion && !IvarsInfo.empty()) |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 3369 | std::sort(IvarsInfo.begin(), IvarsInfo.end()); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3370 | if (hasUnion && !SkipIvars.empty()) |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 3371 | std::sort(SkipIvars.begin(), SkipIvars.end()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3372 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3373 | // Build the string of skip/scan nibbles |
Fariborz Jahanian | 8c2f2d1 | 2009-04-24 17:15:27 +0000 | [diff] [blame] | 3374 | llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3375 | unsigned int WordSize = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3376 | CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3377 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 3378 | WordsToSkip = 0; |
| 3379 | WordsToScan = IvarsInfo[0].ivar_size; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3380 | } else { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3381 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 3382 | WordsToScan = IvarsInfo[0].ivar_size; |
| 3383 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3384 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3385 | unsigned int TailPrevGCObjC = |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3386 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3387 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3388 | // consecutive 'scanned' object pointers. |
| 3389 | WordsToScan += IvarsInfo[i].ivar_size; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3390 | } else { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3391 | // Skip over 'gc'able object pointer which lay over each other. |
| 3392 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 3393 | continue; |
| 3394 | // Must skip over 1 or more words. We save current skip/scan values |
| 3395 | // and start a new pair. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3396 | SKIP_SCAN SkScan; |
| 3397 | SkScan.skip = WordsToSkip; |
| 3398 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3399 | SkipScanIvars.push_back(SkScan); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3400 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3401 | // Skip the hole. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3402 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 3403 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3404 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3405 | WordsToSkip = 0; |
| 3406 | WordsToScan = IvarsInfo[i].ivar_size; |
| 3407 | } |
| 3408 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3409 | if (WordsToScan > 0) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3410 | SKIP_SCAN SkScan; |
| 3411 | SkScan.skip = WordsToSkip; |
| 3412 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3413 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3414 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3415 | |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3416 | if (!SkipIvars.empty()) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3417 | unsigned int LastIndex = SkipIvars.size()-1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3418 | int LastByteSkipped = |
| 3419 | SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3420 | LastIndex = IvarsInfo.size()-1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3421 | int LastByteScanned = |
| 3422 | IvarsInfo[LastIndex].ivar_bytepos + |
| 3423 | IvarsInfo[LastIndex].ivar_size * WordSize; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3424 | // Compute number of bytes to skip at the tail end of the last ivar scanned. |
Benjamin Kramer | 54d76db | 2009-12-25 15:43:36 +0000 | [diff] [blame] | 3425 | if (LastByteSkipped > LastByteScanned) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3426 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3427 | SKIP_SCAN SkScan; |
| 3428 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
| 3429 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3430 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3431 | } |
| 3432 | } |
| 3433 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 3434 | // as 0xMN. |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3435 | int SkipScan = SkipScanIvars.size()-1; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3436 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3437 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 3438 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 3439 | // 0xM0 followed by 0x0N detected. |
| 3440 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 3441 | for (int j = i+1; j < SkipScan; j++) |
| 3442 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 3443 | --SkipScan; |
| 3444 | } |
| 3445 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3446 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3447 | // Generate the string. |
| 3448 | std::string BitMap; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3449 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3450 | unsigned char byte; |
| 3451 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 3452 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 3453 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 3454 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
| 3455 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3456 | // first skip big. |
| 3457 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 3458 | BitMap += (unsigned char)(0xf0); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3459 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3460 | // next (skip small, scan) |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3461 | if (skip_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3462 | byte = skip_small << 4; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3463 | if (scan_big > 0) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3464 | byte |= 0xf; |
| 3465 | --scan_big; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3466 | } else if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3467 | byte |= scan_small; |
| 3468 | scan_small = 0; |
| 3469 | } |
| 3470 | BitMap += byte; |
| 3471 | } |
| 3472 | // next scan big |
| 3473 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 3474 | BitMap += (unsigned char)(0x0f); |
| 3475 | // last scan small |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3476 | if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3477 | byte = scan_small; |
| 3478 | BitMap += byte; |
| 3479 | } |
| 3480 | } |
| 3481 | // null terminate string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3482 | unsigned char zero = 0; |
| 3483 | BitMap += zero; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3484 | |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3485 | if (CGM.getLangOptions().ObjCGCBitmapPrint) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3486 | printf("\n%s ivar layout for class '%s': ", |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3487 | ForStrongLayout ? "strong" : "weak", |
| 3488 | OMD->getClassInterface()->getNameAsCString()); |
| 3489 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
| 3490 | for (unsigned i = 0; i < BitMap.size(); i++) |
| 3491 | if (!(s[i] & 0xf0)) |
| 3492 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3493 | else |
| 3494 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3495 | printf("\n"); |
| 3496 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3497 | llvm::GlobalVariable * Entry = |
| 3498 | CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3499 | llvm::ConstantArray::get(VMContext, BitMap.c_str()), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3500 | "__TEXT,__cstring,cstring_literals", |
| 3501 | 1, true); |
| 3502 | return getConstantGEP(VMContext, Entry, 0, 0); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3503 | } |
| 3504 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3505 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3506 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 3507 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3508 | // FIXME: Avoid std::string copying. |
| 3509 | if (!Entry) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3510 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3511 | llvm::ConstantArray::get(VMContext, Sel.getAsString()), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3512 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3513 | 1, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3514 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3515 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3516 | } |
| 3517 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3518 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3519 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3520 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 3521 | } |
| 3522 | |
| 3523 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3524 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3525 | return GetMethodVarName(&CGM.getContext().Idents.get(Name)); |
| 3526 | } |
| 3527 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 3528 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3529 | std::string TypeStr; |
| 3530 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 3531 | |
| 3532 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3533 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3534 | if (!Entry) |
| 3535 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3536 | llvm::ConstantArray::get(VMContext, TypeStr), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3537 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3538 | 1, true); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3539 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3540 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3541 | } |
| 3542 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3543 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3544 | std::string TypeStr; |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3545 | CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D), |
| 3546 | TypeStr); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3547 | |
| 3548 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 3549 | |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3550 | if (!Entry) |
| 3551 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3552 | llvm::ConstantArray::get(VMContext, TypeStr), |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3553 | "__TEXT,__cstring,cstring_literals", |
| 3554 | 1, true); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3555 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3556 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3557 | } |
| 3558 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3559 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3560 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3561 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3562 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3563 | if (!Entry) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3564 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3565 | llvm::ConstantArray::get(VMContext, |
| 3566 | Ident->getNameStart()), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3567 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3568 | 1, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3569 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3570 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3571 | } |
| 3572 | |
| 3573 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3574 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3575 | llvm::Constant * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3576 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 3577 | const Decl *Container) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3578 | std::string TypeStr; |
| 3579 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3580 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 3581 | } |
| 3582 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3583 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3584 | const ObjCContainerDecl *CD, |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 3585 | llvm::SmallVectorImpl<char> &Name) { |
| 3586 | llvm::raw_svector_ostream OS(Name); |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 3587 | assert (CD && "Missing container decl in GetNameForMethod"); |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 3588 | OS << '\01' << (D->isInstanceMethod() ? '-' : '+') |
| 3589 | << '[' << CD->getName(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3590 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 3591 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 3592 | OS << '(' << CID << ')'; |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 3593 | OS << ' ' << D->getSelector().getAsString() << ']'; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3594 | } |
| 3595 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3596 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3597 | EmitModuleInfo(); |
| 3598 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3599 | // Emit the dummy bodies for any protocols which were referenced but |
| 3600 | // never defined. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3601 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 3602 | I = Protocols.begin(), e = Protocols.end(); I != e; ++I) { |
| 3603 | if (I->second->hasInitializer()) |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3604 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3605 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3606 | std::vector<llvm::Constant*> Values(5); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3607 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 3608 | Values[1] = GetClassName(I->first); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3609 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3610 | Values[3] = Values[4] = |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3611 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 3612 | I->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3613 | I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3614 | Values)); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 3615 | CGM.AddUsedGlobal(I->second); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3616 | } |
| 3617 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3618 | // Add assembler directives to add lazy undefined symbol references |
| 3619 | // for classes which are referenced but not defined. This is |
| 3620 | // important for correct linker interaction. |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 3621 | // |
| 3622 | // FIXME: It would be nice if we had an LLVM construct for this. |
| 3623 | if (!LazySymbols.empty() || !DefinedSymbols.empty()) { |
| 3624 | llvm::SmallString<256> Asm; |
| 3625 | Asm += CGM.getModule().getModuleInlineAsm(); |
| 3626 | if (!Asm.empty() && Asm.back() != '\n') |
| 3627 | Asm += '\n'; |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3628 | |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 3629 | llvm::raw_svector_ostream OS(Asm); |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 3630 | for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(), |
| 3631 | e = DefinedSymbols.end(); I != e; ++I) |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 3632 | OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n" |
| 3633 | << "\t.globl .objc_class_name_" << (*I)->getName() << "\n"; |
Chris Lattner | afd5eda | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 3634 | for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(), |
| 3635 | e = LazySymbols.end(); I != e; ++I) |
| 3636 | OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n"; |
| 3637 | |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 3638 | CGM.getModule().setModuleInlineAsm(OS.str()); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3639 | } |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3640 | } |
| 3641 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3642 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3643 | : CGObjCCommonMac(cgm), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3644 | ObjCTypes(cgm) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3645 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3646 | ObjCABI = 2; |
| 3647 | } |
| 3648 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3649 | /* *** */ |
| 3650 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3651 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3652 | : VMContext(cgm.getLLVMContext()), CGM(cgm) { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3653 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3654 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3655 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3656 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3657 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3658 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3659 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 3660 | Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3661 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3662 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3663 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3664 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3665 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3666 | // FIXME: It would be nice to unify this with the opaque type, so that the IR |
| 3667 | // comes out a bit cleaner. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3668 | const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3669 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3670 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3671 | // I'm not sure I like this. The implicit coordination is a bit |
| 3672 | // gross. We should solve this in a reasonable fashion because this |
| 3673 | // is a pretty common task (match some runtime data structure with |
| 3674 | // an LLVM data structure). |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3675 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3676 | // FIXME: This is leaked. |
| 3677 | // FIXME: Merge with rewriter code? |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3678 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3679 | // struct _objc_super { |
| 3680 | // id self; |
| 3681 | // Class cls; |
| 3682 | // } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3683 | RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct, |
Daniel Dunbar | daa3ac5 | 2010-04-29 16:29:11 +0000 | [diff] [blame] | 3684 | Ctx.getTranslationUnitDecl(), |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3685 | SourceLocation(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3686 | &Ctx.Idents.get("_objc_super")); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3687 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 3688 | Ctx.getObjCIdType(), 0, 0, false)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3689 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 3690 | Ctx.getObjCClassType(), 0, 0, false)); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3691 | RD->completeDefinition(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3692 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3693 | SuperCTy = Ctx.getTagDeclType(RD); |
| 3694 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3695 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3696 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3697 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 3698 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3699 | // struct _prop_t { |
| 3700 | // char *name; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3701 | // char *attributes; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3702 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3703 | PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3704 | CGM.getModule().addTypeName("struct._prop_t", |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3705 | PropertyTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3706 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3707 | // struct _prop_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3708 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3709 | // uint32_t count_of_properties; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3710 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3711 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3712 | PropertyListTy = llvm::StructType::get(VMContext, IntTy, |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3713 | IntTy, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3714 | llvm::ArrayType::get(PropertyTy, 0), |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3715 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3716 | CGM.getModule().addTypeName("struct._prop_list_t", |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3717 | PropertyListTy); |
| 3718 | // struct _prop_list_t * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3719 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3720 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3721 | // struct _objc_method { |
| 3722 | // SEL _cmd; |
| 3723 | // char *method_type; |
| 3724 | // char *_imp; |
| 3725 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3726 | MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy, |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3727 | Int8PtrTy, |
| 3728 | Int8PtrTy, |
| 3729 | NULL); |
| 3730 | CGM.getModule().addTypeName("struct._objc_method", MethodTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3731 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3732 | // struct _objc_cache * |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 3733 | CacheTy = llvm::OpaqueType::get(VMContext); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3734 | CGM.getModule().addTypeName("struct._objc_cache", CacheTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3735 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3736 | } |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3737 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3738 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3739 | : ObjCCommonTypesHelper(cgm) { |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3740 | // struct _objc_method_description { |
| 3741 | // SEL name; |
| 3742 | // char *types; |
| 3743 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3744 | MethodDescriptionTy = |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3745 | llvm::StructType::get(VMContext, SelectorPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3746 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3747 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3748 | CGM.getModule().addTypeName("struct._objc_method_description", |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3749 | MethodDescriptionTy); |
| 3750 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3751 | // struct _objc_method_description_list { |
| 3752 | // int count; |
| 3753 | // struct _objc_method_description[1]; |
| 3754 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3755 | MethodDescriptionListTy = |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3756 | llvm::StructType::get(VMContext, IntTy, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3757 | llvm::ArrayType::get(MethodDescriptionTy, 0), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3758 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3759 | CGM.getModule().addTypeName("struct._objc_method_description_list", |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3760 | MethodDescriptionListTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3761 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3762 | // struct _objc_method_description_list * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3763 | MethodDescriptionListPtrTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3764 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3765 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3766 | // Protocol description structures |
| 3767 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3768 | // struct _objc_protocol_extension { |
| 3769 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 3770 | // struct _objc_method_description_list *optional_instance_methods; |
| 3771 | // struct _objc_method_description_list *optional_class_methods; |
| 3772 | // struct _objc_property_list *instance_properties; |
| 3773 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3774 | ProtocolExtensionTy = |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3775 | llvm::StructType::get(VMContext, IntTy, |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3776 | MethodDescriptionListPtrTy, |
| 3777 | MethodDescriptionListPtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3778 | PropertyListPtrTy, |
| 3779 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3780 | CGM.getModule().addTypeName("struct._objc_protocol_extension", |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3781 | ProtocolExtensionTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3782 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3783 | // struct _objc_protocol_extension * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3784 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3785 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3786 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3787 | |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 3788 | llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext); |
| 3789 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3790 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3791 | const llvm::Type *T = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3792 | llvm::StructType::get(VMContext, |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3793 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3794 | LongTy, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3795 | llvm::ArrayType::get(ProtocolTyHolder, 0), |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3796 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3797 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T); |
| 3798 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3799 | // struct _objc_protocol { |
| 3800 | // struct _objc_protocol_extension *isa; |
| 3801 | // char *protocol_name; |
| 3802 | // struct _objc_protocol **_objc_protocol_list; |
| 3803 | // struct _objc_method_description_list *instance_methods; |
| 3804 | // struct _objc_method_description_list *class_methods; |
| 3805 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3806 | T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3807 | Int8PtrTy, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3808 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3809 | MethodDescriptionListPtrTy, |
| 3810 | MethodDescriptionListPtrTy, |
| 3811 | NULL); |
| 3812 | cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T); |
| 3813 | |
| 3814 | ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3815 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3816 | ProtocolListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3817 | // struct _objc_protocol_list * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3818 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3819 | |
| 3820 | ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get()); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3821 | CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3822 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3823 | |
| 3824 | // Class description structures |
| 3825 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3826 | // struct _objc_ivar { |
| 3827 | // char *ivar_name; |
| 3828 | // char *ivar_type; |
| 3829 | // int ivar_offset; |
| 3830 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3831 | IvarTy = llvm::StructType::get(VMContext, Int8PtrTy, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3832 | Int8PtrTy, |
| 3833 | IntTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3834 | NULL); |
| 3835 | CGM.getModule().addTypeName("struct._objc_ivar", IvarTy); |
| 3836 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3837 | // struct _objc_ivar_list * |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 3838 | IvarListTy = llvm::OpaqueType::get(VMContext); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3839 | CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3840 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3841 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3842 | // struct _objc_method_list * |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 3843 | MethodListTy = llvm::OpaqueType::get(VMContext); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3844 | CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3845 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3846 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3847 | // struct _objc_class_extension * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3848 | ClassExtensionTy = |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3849 | llvm::StructType::get(VMContext, IntTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3850 | Int8PtrTy, |
| 3851 | PropertyListPtrTy, |
| 3852 | NULL); |
| 3853 | CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3854 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3855 | |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 3856 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3857 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3858 | // struct _objc_class { |
| 3859 | // Class isa; |
| 3860 | // Class super_class; |
| 3861 | // char *name; |
| 3862 | // long version; |
| 3863 | // long info; |
| 3864 | // long instance_size; |
| 3865 | // struct _objc_ivar_list *ivars; |
| 3866 | // struct _objc_method_list *methods; |
| 3867 | // struct _objc_cache *cache; |
| 3868 | // struct _objc_protocol_list *protocols; |
| 3869 | // char *ivar_layout; |
| 3870 | // struct _objc_class_ext *ext; |
| 3871 | // }; |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3872 | T = llvm::StructType::get(VMContext, |
| 3873 | llvm::PointerType::getUnqual(ClassTyHolder), |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3874 | llvm::PointerType::getUnqual(ClassTyHolder), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3875 | Int8PtrTy, |
| 3876 | LongTy, |
| 3877 | LongTy, |
| 3878 | LongTy, |
| 3879 | IvarListPtrTy, |
| 3880 | MethodListPtrTy, |
| 3881 | CachePtrTy, |
| 3882 | ProtocolListPtrTy, |
| 3883 | Int8PtrTy, |
| 3884 | ClassExtensionPtrTy, |
| 3885 | NULL); |
| 3886 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3887 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3888 | ClassTy = cast<llvm::StructType>(ClassTyHolder.get()); |
| 3889 | CGM.getModule().addTypeName("struct._objc_class", ClassTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3890 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3891 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3892 | // struct _objc_category { |
| 3893 | // char *category_name; |
| 3894 | // char *class_name; |
| 3895 | // struct _objc_method_list *instance_method; |
| 3896 | // struct _objc_method_list *class_method; |
| 3897 | // uint32_t size; // sizeof(struct _objc_category) |
| 3898 | // struct _objc_property_list *instance_properties;// category's @property |
| 3899 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3900 | CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3901 | Int8PtrTy, |
| 3902 | MethodListPtrTy, |
| 3903 | MethodListPtrTy, |
| 3904 | ProtocolListPtrTy, |
| 3905 | IntTy, |
| 3906 | PropertyListPtrTy, |
| 3907 | NULL); |
| 3908 | CGM.getModule().addTypeName("struct._objc_category", CategoryTy); |
| 3909 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3910 | // Global metadata structures |
| 3911 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3912 | // struct _objc_symtab { |
| 3913 | // long sel_ref_cnt; |
| 3914 | // SEL *refs; |
| 3915 | // short cls_def_cnt; |
| 3916 | // short cat_def_cnt; |
| 3917 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 3918 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3919 | SymtabTy = llvm::StructType::get(VMContext, LongTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3920 | SelectorPtrTy, |
| 3921 | ShortTy, |
| 3922 | ShortTy, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3923 | llvm::ArrayType::get(Int8PtrTy, 0), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3924 | NULL); |
| 3925 | CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3926 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3927 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3928 | // struct _objc_module { |
| 3929 | // long version; |
| 3930 | // long size; // sizeof(struct _objc_module) |
| 3931 | // char *name; |
| 3932 | // struct _objc_symtab* symtab; |
| 3933 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3934 | ModuleTy = |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3935 | llvm::StructType::get(VMContext, LongTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3936 | LongTy, |
| 3937 | Int8PtrTy, |
| 3938 | SymtabPtrTy, |
| 3939 | NULL); |
| 3940 | CGM.getModule().addTypeName("struct._objc_module", ModuleTy); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3941 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3942 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3943 | // FIXME: This is the size of the setjmp buffer and should be target |
| 3944 | // specific. 18 is what's used on 32-bit X86. |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3945 | uint64_t SetJmpBufferSize = 18; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3946 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3947 | // Exceptions |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3948 | const llvm::Type *StackPtrTy = llvm::ArrayType::get( |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 3949 | llvm::Type::getInt8PtrTy(VMContext), 4); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3950 | |
| 3951 | ExceptionDataTy = |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3952 | llvm::StructType::get(VMContext, llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext), |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3953 | SetJmpBufferSize), |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3954 | StackPtrTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3955 | CGM.getModule().addTypeName("struct._objc_exception_data", |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3956 | ExceptionDataTy); |
| 3957 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3958 | } |
| 3959 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3960 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3961 | : ObjCCommonTypesHelper(cgm) { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3962 | // struct _method_list_t { |
| 3963 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 3964 | // uint32_t method_count; |
| 3965 | // struct _objc_method method_list[method_count]; |
| 3966 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3967 | MethodListnfABITy = llvm::StructType::get(VMContext, IntTy, |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3968 | IntTy, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3969 | llvm::ArrayType::get(MethodTy, 0), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3970 | NULL); |
| 3971 | CGM.getModule().addTypeName("struct.__method_list_t", |
| 3972 | MethodListnfABITy); |
| 3973 | // struct method_list_t * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3974 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3975 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3976 | // struct _protocol_t { |
| 3977 | // id isa; // NULL |
| 3978 | // const char * const protocol_name; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3979 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3980 | // const struct method_list_t * const instance_methods; |
| 3981 | // const struct method_list_t * const class_methods; |
| 3982 | // const struct method_list_t *optionalInstanceMethods; |
| 3983 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3984 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3985 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 3986 | // const uint32_t flags; // = 0 |
| 3987 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3988 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3989 | // Holder for struct _protocol_list_t * |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 3990 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3991 | |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3992 | ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy, |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3993 | Int8PtrTy, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3994 | llvm::PointerType::getUnqual( |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3995 | ProtocolListTyHolder), |
| 3996 | MethodListnfABIPtrTy, |
| 3997 | MethodListnfABIPtrTy, |
| 3998 | MethodListnfABIPtrTy, |
| 3999 | MethodListnfABIPtrTy, |
| 4000 | PropertyListPtrTy, |
| 4001 | IntTy, |
| 4002 | IntTy, |
| 4003 | NULL); |
| 4004 | CGM.getModule().addTypeName("struct._protocol_t", |
| 4005 | ProtocolnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4006 | |
| 4007 | // struct _protocol_t* |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4008 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4009 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4010 | // struct _protocol_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4011 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4012 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4013 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4014 | ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4015 | llvm::ArrayType::get( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4016 | ProtocolnfABIPtrTy, 0), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4017 | NULL); |
| 4018 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 4019 | ProtocolListnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4020 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4021 | ProtocolListnfABITy); |
| 4022 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4023 | // struct _objc_protocol_list* |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4024 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4025 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4026 | // struct _ivar_t { |
| 4027 | // unsigned long int *offset; // pointer to ivar offset location |
| 4028 | // char *name; |
| 4029 | // char *type; |
| 4030 | // uint32_t alignment; |
| 4031 | // uint32_t size; |
| 4032 | // } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4033 | IvarnfABITy = llvm::StructType::get(VMContext, |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4034 | llvm::PointerType::getUnqual(LongTy), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4035 | Int8PtrTy, |
| 4036 | Int8PtrTy, |
| 4037 | IntTy, |
| 4038 | IntTy, |
| 4039 | NULL); |
| 4040 | CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4041 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4042 | // struct _ivar_list_t { |
| 4043 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 4044 | // uint32 count; |
| 4045 | // struct _iver_t list[count]; |
| 4046 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4047 | IvarListnfABITy = llvm::StructType::get(VMContext, IntTy, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4048 | IntTy, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4049 | llvm::ArrayType::get( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4050 | IvarnfABITy, 0), |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4051 | NULL); |
| 4052 | CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4053 | |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4054 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4055 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4056 | // struct _class_ro_t { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4057 | // uint32_t const flags; |
| 4058 | // uint32_t const instanceStart; |
| 4059 | // uint32_t const instanceSize; |
| 4060 | // uint32_t const reserved; // only when building for 64bit targets |
| 4061 | // const uint8_t * const ivarLayout; |
| 4062 | // const char *const name; |
| 4063 | // const struct _method_list_t * const baseMethods; |
| 4064 | // const struct _objc_protocol_list *const baseProtocols; |
| 4065 | // const struct _ivar_list_t *const ivars; |
| 4066 | // const uint8_t * const weakIvarLayout; |
| 4067 | // const struct _prop_list_t * const properties; |
| 4068 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4069 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4070 | // FIXME. Add 'reserved' field in 64bit abi mode! |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4071 | ClassRonfABITy = llvm::StructType::get(VMContext, IntTy, |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4072 | IntTy, |
| 4073 | IntTy, |
| 4074 | Int8PtrTy, |
| 4075 | Int8PtrTy, |
| 4076 | MethodListnfABIPtrTy, |
| 4077 | ProtocolListnfABIPtrTy, |
| 4078 | IvarListnfABIPtrTy, |
| 4079 | Int8PtrTy, |
| 4080 | PropertyListPtrTy, |
| 4081 | NULL); |
| 4082 | CGM.getModule().addTypeName("struct._class_ro_t", |
| 4083 | ClassRonfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4084 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4085 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 4086 | std::vector<const llvm::Type*> Params; |
| 4087 | Params.push_back(ObjectPtrTy); |
| 4088 | Params.push_back(SelectorPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4089 | ImpnfABITy = llvm::PointerType::getUnqual( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4090 | llvm::FunctionType::get(ObjectPtrTy, Params, false)); |
| 4091 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4092 | // struct _class_t { |
| 4093 | // struct _class_t *isa; |
| 4094 | // struct _class_t * const superclass; |
| 4095 | // void *cache; |
| 4096 | // IMP *vtable; |
| 4097 | // struct class_ro_t *ro; |
| 4098 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4099 | |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 4100 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4101 | ClassnfABITy = |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4102 | llvm::StructType::get(VMContext, |
| 4103 | llvm::PointerType::getUnqual(ClassTyHolder), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4104 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 4105 | CachePtrTy, |
| 4106 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 4107 | llvm::PointerType::getUnqual(ClassRonfABITy), |
| 4108 | NULL); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4109 | CGM.getModule().addTypeName("struct._class_t", ClassnfABITy); |
| 4110 | |
| 4111 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4112 | ClassnfABITy); |
| 4113 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4114 | // LLVM for struct _class_t * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4115 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4116 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4117 | // struct _category_t { |
| 4118 | // const char * const name; |
| 4119 | // struct _class_t *const cls; |
| 4120 | // const struct _method_list_t * const instance_methods; |
| 4121 | // const struct _method_list_t * const class_methods; |
| 4122 | // const struct _protocol_list_t * const protocols; |
| 4123 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 4124 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4125 | CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4126 | ClassnfABIPtrTy, |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 4127 | MethodListnfABIPtrTy, |
| 4128 | MethodListnfABIPtrTy, |
| 4129 | ProtocolListnfABIPtrTy, |
| 4130 | PropertyListPtrTy, |
| 4131 | NULL); |
| 4132 | CGM.getModule().addTypeName("struct._category_t", CategorynfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4133 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4134 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4135 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 4136 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4137 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4138 | // MessageRefTy - LLVM for: |
| 4139 | // struct _message_ref_t { |
| 4140 | // IMP messenger; |
| 4141 | // SEL name; |
| 4142 | // }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4143 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4144 | // First the clang type for struct _message_ref_t |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4145 | RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct, |
Daniel Dunbar | daa3ac5 | 2010-04-29 16:29:11 +0000 | [diff] [blame] | 4146 | Ctx.getTranslationUnitDecl(), |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4147 | SourceLocation(), |
| 4148 | &Ctx.Idents.get("_message_ref_t")); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4149 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 4150 | Ctx.VoidPtrTy, 0, 0, false)); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4151 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 4152 | Ctx.getObjCSelType(), 0, 0, false)); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4153 | RD->completeDefinition(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4154 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4155 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 4156 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 4157 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4158 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4159 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4160 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4161 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4162 | // SuperMessageRefTy - LLVM for: |
| 4163 | // struct _super_message_ref_t { |
| 4164 | // SUPER_IMP messenger; |
| 4165 | // SEL name; |
| 4166 | // }; |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4167 | SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy, |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4168 | SelectorPtrTy, |
| 4169 | NULL); |
| 4170 | CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4171 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4172 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4173 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
| 4174 | |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 4175 | |
| 4176 | // struct objc_typeinfo { |
| 4177 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 4178 | // const char* name; // c++ typeinfo string |
| 4179 | // Class cls; |
| 4180 | // }; |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4181 | EHTypeTy = llvm::StructType::get(VMContext, |
| 4182 | llvm::PointerType::getUnqual(Int8PtrTy), |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 4183 | Int8PtrTy, |
| 4184 | ClassnfABIPtrTy, |
| 4185 | NULL); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 4186 | CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4187 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4188 | } |
| 4189 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4190 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4191 | FinishNonFragileABIModule(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4192 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4193 | return NULL; |
| 4194 | } |
| 4195 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4196 | void CGObjCNonFragileABIMac::AddModuleClassList(const |
| 4197 | std::vector<llvm::GlobalValue*> |
| 4198 | &Container, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4199 | const char *SymbolName, |
| 4200 | const char *SectionName) { |
| 4201 | unsigned NumClasses = Container.size(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4202 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4203 | if (!NumClasses) |
| 4204 | return; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4205 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4206 | std::vector<llvm::Constant*> Symbols(NumClasses); |
| 4207 | for (unsigned i=0; i<NumClasses; i++) |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4208 | Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i], |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4209 | ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4210 | llvm::Constant* Init = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4211 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4212 | NumClasses), |
| 4213 | Symbols); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4214 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4215 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4216 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4217 | llvm::GlobalValue::InternalLinkage, |
| 4218 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4219 | SymbolName); |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 4220 | GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType())); |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4221 | GV->setSection(SectionName); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4222 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4223 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4224 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4225 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 4226 | // nonfragile abi has no module definition. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4227 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4228 | // Build list of all implemented class addresses in array |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4229 | // L_OBJC_LABEL_CLASS_$. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4230 | AddModuleClassList(DefinedClasses, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4231 | "\01L_OBJC_LABEL_CLASS_$", |
| 4232 | "__DATA, __objc_classlist, regular, no_dead_strip"); |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4233 | |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4234 | for (unsigned i = 0; i < DefinedClasses.size(); i++) { |
| 4235 | llvm::GlobalValue *IMPLGV = DefinedClasses[i]; |
| 4236 | if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage) |
| 4237 | continue; |
| 4238 | IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4239 | } |
| 4240 | |
Fariborz Jahanian | 0e93d25 | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 4241 | for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) { |
| 4242 | llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i]; |
| 4243 | if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage) |
| 4244 | continue; |
| 4245 | IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 4246 | } |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4247 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4248 | AddModuleClassList(DefinedNonLazyClasses, |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4249 | "\01L_OBJC_LABEL_NONLAZY_CLASS_$", |
| 4250 | "__DATA, __objc_nlclslist, regular, no_dead_strip"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4251 | |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4252 | // Build list of all implemented category addresses in array |
| 4253 | // L_OBJC_LABEL_CATEGORY_$. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4254 | AddModuleClassList(DefinedCategories, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4255 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 4256 | "__DATA, __objc_catlist, regular, no_dead_strip"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4257 | AddModuleClassList(DefinedNonLazyCategories, |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4258 | "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$", |
| 4259 | "__DATA, __objc_nlcatlist, regular, no_dead_strip"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4260 | |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 4261 | EmitImageInfo(); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4262 | } |
| 4263 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4264 | /// LegacyDispatchedSelector - Returns true if SEL is not in the list of |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4265 | /// NonLegacyDispatchMethods; false otherwise. What this means is that |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4266 | /// except for the 19 selectors in the list, we generate 32bit-style |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4267 | /// message dispatch call for all the rest. |
| 4268 | /// |
| 4269 | bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) { |
Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4270 | switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) { |
| 4271 | default: |
| 4272 | assert(0 && "Invalid dispatch method!"); |
| 4273 | case CodeGenOptions::Legacy: |
Daniel Dunbar | 2feefe8 | 2010-02-01 21:07:33 +0000 | [diff] [blame] | 4274 | return true; |
Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4275 | case CodeGenOptions::NonLegacy: |
Fariborz Jahanian | 776dbf9 | 2010-04-19 17:53:30 +0000 | [diff] [blame] | 4276 | return false; |
Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4277 | case CodeGenOptions::Mixed: |
| 4278 | break; |
| 4279 | } |
| 4280 | |
| 4281 | // If so, see whether this selector is in the white-list of things which must |
| 4282 | // use the new dispatch convention. We lazily build a dense set for this. |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4283 | if (NonLegacyDispatchMethods.empty()) { |
| 4284 | NonLegacyDispatchMethods.insert(GetNullarySelector("alloc")); |
| 4285 | NonLegacyDispatchMethods.insert(GetNullarySelector("class")); |
| 4286 | NonLegacyDispatchMethods.insert(GetNullarySelector("self")); |
| 4287 | NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped")); |
| 4288 | NonLegacyDispatchMethods.insert(GetNullarySelector("length")); |
| 4289 | NonLegacyDispatchMethods.insert(GetNullarySelector("count")); |
| 4290 | NonLegacyDispatchMethods.insert(GetNullarySelector("retain")); |
| 4291 | NonLegacyDispatchMethods.insert(GetNullarySelector("release")); |
| 4292 | NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease")); |
| 4293 | NonLegacyDispatchMethods.insert(GetNullarySelector("hash")); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4294 | |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4295 | NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone")); |
| 4296 | NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass")); |
| 4297 | NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector")); |
| 4298 | NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey")); |
| 4299 | NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex")); |
| 4300 | NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString")); |
| 4301 | NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual")); |
| 4302 | NonLegacyDispatchMethods.insert(GetUnarySelector("addObject")); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4303 | // "countByEnumeratingWithState:objects:count" |
Fariborz Jahanian | be53be4 | 2009-05-13 16:19:02 +0000 | [diff] [blame] | 4304 | IdentifierInfo *KeyIdents[] = { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4305 | &CGM.getContext().Idents.get("countByEnumeratingWithState"), |
| 4306 | &CGM.getContext().Idents.get("objects"), |
| 4307 | &CGM.getContext().Idents.get("count") |
Fariborz Jahanian | be53be4 | 2009-05-13 16:19:02 +0000 | [diff] [blame] | 4308 | }; |
| 4309 | NonLegacyDispatchMethods.insert( |
| 4310 | CGM.getContext().Selectors.getSelector(3, KeyIdents)); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4311 | } |
Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4312 | |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4313 | return (NonLegacyDispatchMethods.count(Sel) == 0); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4314 | } |
| 4315 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4316 | // Metadata flags |
| 4317 | enum MetaDataDlags { |
| 4318 | CLS = 0x0, |
| 4319 | CLS_META = 0x1, |
| 4320 | CLS_ROOT = 0x2, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4321 | OBJC2_CLS_HIDDEN = 0x10, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4322 | CLS_EXCEPTION = 0x20 |
| 4323 | }; |
| 4324 | /// BuildClassRoTInitializer - generate meta-data for: |
| 4325 | /// struct _class_ro_t { |
| 4326 | /// uint32_t const flags; |
| 4327 | /// uint32_t const instanceStart; |
| 4328 | /// uint32_t const instanceSize; |
| 4329 | /// uint32_t const reserved; // only when building for 64bit targets |
| 4330 | /// const uint8_t * const ivarLayout; |
| 4331 | /// const char *const name; |
| 4332 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4333 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4334 | /// const struct _ivar_list_t *const ivars; |
| 4335 | /// const uint8_t * const weakIvarLayout; |
| 4336 | /// const struct _prop_list_t * const properties; |
| 4337 | /// } |
| 4338 | /// |
| 4339 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4340 | unsigned flags, |
| 4341 | unsigned InstanceStart, |
| 4342 | unsigned InstanceSize, |
| 4343 | const ObjCImplementationDecl *ID) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4344 | std::string ClassName = ID->getNameAsString(); |
| 4345 | std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets! |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4346 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 4347 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 4348 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4349 | // FIXME. For 64bit targets add 0 here. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4350 | Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4351 | : BuildIvarLayout(ID, true); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4352 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4353 | // const struct _method_list_t * const baseMethods; |
| 4354 | std::vector<llvm::Constant*> Methods; |
| 4355 | std::string MethodListName("\01l_OBJC_$_"); |
| 4356 | if (flags & CLS_META) { |
| 4357 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4358 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4359 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4360 | // Class methods should always be defined. |
| 4361 | Methods.push_back(GetMethodConstant(*i)); |
| 4362 | } |
| 4363 | } else { |
| 4364 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4365 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4366 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4367 | // Instance methods should always be defined. |
| 4368 | Methods.push_back(GetMethodConstant(*i)); |
| 4369 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4370 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4371 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4372 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4373 | |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4374 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 4375 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4376 | |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4377 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 4378 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4379 | Methods.push_back(C); |
| 4380 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 4381 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4382 | Methods.push_back(C); |
| 4383 | } |
| 4384 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4385 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4386 | Values[ 5] = EmitMethodList(MethodListName, |
| 4387 | "__DATA, __objc_const", Methods); |
| 4388 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4389 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4390 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4391 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4392 | + OID->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4393 | OID->protocol_begin(), |
| 4394 | OID->protocol_end()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4395 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4396 | if (flags & CLS_META) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4397 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4398 | else |
| 4399 | Values[ 7] = EmitIvarList(ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4400 | Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4401 | : BuildIvarLayout(ID, false); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4402 | if (flags & CLS_META) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4403 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4404 | else |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4405 | Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(), |
| 4406 | ID, ID->getClassInterface(), ObjCTypes); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4407 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4408 | Values); |
| 4409 | llvm::GlobalVariable *CLASS_RO_GV = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4410 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false, |
| 4411 | llvm::GlobalValue::InternalLinkage, |
| 4412 | Init, |
| 4413 | (flags & CLS_META) ? |
| 4414 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 4415 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4416 | CLASS_RO_GV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 4417 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4418 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4419 | return CLASS_RO_GV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4420 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4421 | } |
| 4422 | |
| 4423 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 4424 | /// for the given ClassName for: |
| 4425 | /// struct _class_t { |
| 4426 | /// struct _class_t *isa; |
| 4427 | /// struct _class_t * const superclass; |
| 4428 | /// void *cache; |
| 4429 | /// IMP *vtable; |
| 4430 | /// struct class_ro_t *ro; |
| 4431 | /// } |
| 4432 | /// |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4433 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4434 | std::string &ClassName, |
| 4435 | llvm::Constant *IsAGV, |
| 4436 | llvm::Constant *SuperClassGV, |
| 4437 | llvm::Constant *ClassRoGV, |
| 4438 | bool HiddenVisibility) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4439 | std::vector<llvm::Constant*> Values(5); |
| 4440 | Values[0] = IsAGV; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4441 | Values[1] = SuperClassGV; |
| 4442 | if (!Values[1]) |
| 4443 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4444 | Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar |
| 4445 | Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar |
| 4446 | Values[4] = ClassRoGV; // &CLASS_RO_GV |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4447 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4448 | Values); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4449 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 4450 | GV->setInitializer(Init); |
Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 4451 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4452 | GV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 4453 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4454 | if (HiddenVisibility) |
| 4455 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4456 | return GV; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4457 | } |
| 4458 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4459 | bool |
Fariborz Jahanian | ecfbdcb | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 4460 | CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4461 | return OD->getClassMethod(GetNullarySelector("load")) != 0; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4462 | } |
| 4463 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4464 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4465 | uint32_t &InstanceStart, |
| 4466 | uint32_t &InstanceSize) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4467 | const ASTRecordLayout &RL = |
Daniel Dunbar | b4c79e0 | 2009-05-04 21:26:30 +0000 | [diff] [blame] | 4468 | CGM.getContext().getASTObjCImplementationLayout(OID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4469 | |
Daniel Dunbar | 6e8575b | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 4470 | // InstanceSize is really instance end. |
Anders Carlsson | 243a685 | 2009-07-18 21:26:44 +0000 | [diff] [blame] | 4471 | InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8; |
Daniel Dunbar | 6e8575b | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 4472 | |
| 4473 | // If there are no fields, the start is the same as the end. |
| 4474 | if (!RL.getFieldCount()) |
| 4475 | InstanceStart = InstanceSize; |
| 4476 | else |
| 4477 | InstanceStart = RL.getFieldOffset(0) / 8; |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4478 | } |
| 4479 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4480 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 4481 | std::string ClassName = ID->getNameAsString(); |
| 4482 | if (!ObjCEmptyCacheVar) { |
| 4483 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4484 | CGM.getModule(), |
| 4485 | ObjCTypes.CacheTy, |
| 4486 | false, |
| 4487 | llvm::GlobalValue::ExternalLinkage, |
| 4488 | 0, |
| 4489 | "_objc_empty_cache"); |
| 4490 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4491 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4492 | CGM.getModule(), |
| 4493 | ObjCTypes.ImpnfABITy, |
| 4494 | false, |
| 4495 | llvm::GlobalValue::ExternalLinkage, |
| 4496 | 0, |
| 4497 | "_objc_empty_vtable"); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4498 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4499 | assert(ID->getClassInterface() && |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4500 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 4501 | // FIXME: Is this correct (that meta class size is never computed)? |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4502 | uint32_t InstanceStart = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4503 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4504 | uint32_t InstanceSize = InstanceStart; |
| 4505 | uint32_t flags = CLS_META; |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4506 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 4507 | std::string ObjCClassName(getClassSymbolPrefix()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4508 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4509 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4510 | |
| 4511 | bool classIsHidden = |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4512 | CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4513 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4514 | flags |= OBJC2_CLS_HIDDEN; |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 4515 | if (ID->getNumIvarInitializers()) |
| 4516 | flags |= eClassFlags_ABI2_HasCXXStructors; |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4517 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4518 | // class is root |
| 4519 | flags |= CLS_ROOT; |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4520 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4521 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4522 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4523 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4524 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 4525 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 4526 | Root = Super; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4527 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); |
Fariborz Jahanian | 0e93d25 | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 4528 | if (Root->hasAttr<WeakImportAttr>()) |
| 4529 | IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4530 | // work on super class metadata symbol. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4531 | std::string SuperClassName = |
Fariborz Jahanian | 0e93d25 | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 4532 | ObjCMetaClassName + |
| 4533 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4534 | SuperClassGV = GetClassGlobal(SuperClassName); |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4535 | if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>()) |
| 4536 | SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4537 | } |
| 4538 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 4539 | InstanceStart, |
| 4540 | InstanceSize,ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4541 | std::string TClassName = ObjCMetaClassName + ClassName; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4542 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4543 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 4544 | classIsHidden); |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4545 | DefinedMetaClasses.push_back(MetaTClass); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4546 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4547 | // Metadata for the class |
| 4548 | flags = CLS; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4549 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4550 | flags |= OBJC2_CLS_HIDDEN; |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 4551 | if (ID->getNumIvarInitializers()) |
| 4552 | flags |= eClassFlags_ABI2_HasCXXStructors; |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4553 | |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 4554 | if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface())) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4555 | flags |= CLS_EXCEPTION; |
| 4556 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4557 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4558 | flags |= CLS_ROOT; |
| 4559 | SuperClassGV = 0; |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 4560 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4561 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4562 | std::string RootClassName = |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4563 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4564 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4565 | if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>()) |
| 4566 | SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4567 | } |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4568 | GetClassSizeInfo(ID, InstanceStart, InstanceSize); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4569 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4570 | InstanceStart, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4571 | InstanceSize, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4572 | ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4573 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4574 | TClassName = ObjCClassName + ClassName; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4575 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4576 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 4577 | classIsHidden); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4578 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4579 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4580 | // Determine if this class is also "non-lazy". |
| 4581 | if (ImplementationIsNonLazy(ID)) |
| 4582 | DefinedNonLazyClasses.push_back(ClassMD); |
| 4583 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4584 | // Force the definition of the EHType if necessary. |
| 4585 | if (flags & CLS_EXCEPTION) |
| 4586 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4587 | } |
| 4588 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4589 | /// GenerateProtocolRef - This routine is called to generate code for |
| 4590 | /// a protocol reference expression; as in: |
| 4591 | /// @code |
| 4592 | /// @protocol(Proto1); |
| 4593 | /// @endcode |
| 4594 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 4595 | /// which will hold address of the protocol meta-data. |
| 4596 | /// |
| 4597 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4598 | const ObjCProtocolDecl *PD) { |
| 4599 | |
Fariborz Jahanian | 960cd06 | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 4600 | // This routine is called for @protocol only. So, we must build definition |
| 4601 | // of protocol's meta-data (not a reference to it!) |
| 4602 | // |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4603 | llvm::Constant *Init = |
| 4604 | llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
| 4605 | ObjCTypes.ExternalProtocolPtrTy); |
| 4606 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4607 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
| 4608 | ProtocolName += PD->getNameAsCString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4609 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4610 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 4611 | if (PTGV) |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 4612 | return Builder.CreateLoad(PTGV, "tmp"); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4613 | PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4614 | CGM.getModule(), |
| 4615 | Init->getType(), false, |
| 4616 | llvm::GlobalValue::WeakAnyLinkage, |
| 4617 | Init, |
| 4618 | ProtocolName); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4619 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 4620 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4621 | CGM.AddUsedGlobal(PTGV); |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 4622 | return Builder.CreateLoad(PTGV, "tmp"); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4623 | } |
| 4624 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4625 | /// GenerateCategory - Build metadata for a category implementation. |
| 4626 | /// struct _category_t { |
| 4627 | /// const char * const name; |
| 4628 | /// struct _class_t *const cls; |
| 4629 | /// const struct _method_list_t * const instance_methods; |
| 4630 | /// const struct _method_list_t * const class_methods; |
| 4631 | /// const struct _protocol_list_t * const protocols; |
| 4632 | /// const struct _prop_list_t * const properties; |
| 4633 | /// } |
| 4634 | /// |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4635 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4636 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4637 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4638 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
| 4639 | "_$_" + OCD->getNameAsString()); |
| 4640 | std::string ExtClassName(getClassSymbolPrefix() + |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4641 | Interface->getNameAsString()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4642 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4643 | std::vector<llvm::Constant*> Values(6); |
| 4644 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 4645 | // meta-class entry symbol |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4646 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Fariborz Jahanian | 2cdcc4c | 2009-11-17 22:02:21 +0000 | [diff] [blame] | 4647 | if (Interface->hasAttr<WeakImportAttr>()) |
| 4648 | ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
| 4649 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4650 | Values[1] = ClassGV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4651 | std::vector<llvm::Constant*> Methods; |
| 4652 | std::string MethodListName(Prefix); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4653 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4654 | "_$_" + OCD->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4655 | |
| 4656 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4657 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4658 | // Instance methods should always be defined. |
| 4659 | Methods.push_back(GetMethodConstant(*i)); |
| 4660 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4661 | |
| 4662 | Values[2] = EmitMethodList(MethodListName, |
| 4663 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4664 | Methods); |
| 4665 | |
| 4666 | MethodListName = Prefix; |
| 4667 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 4668 | OCD->getNameAsString(); |
| 4669 | Methods.clear(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4670 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4671 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4672 | // Class methods should always be defined. |
| 4673 | Methods.push_back(GetMethodConstant(*i)); |
| 4674 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4675 | |
| 4676 | Values[3] = EmitMethodList(MethodListName, |
| 4677 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4678 | Methods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4679 | const ObjCCategoryDecl *Category = |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4680 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4681 | if (Category) { |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4682 | llvm::SmallString<256> ExtName; |
| 4683 | llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_" |
| 4684 | << OCD->getName(); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4685 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4686 | + Interface->getName() + "_$_" |
| 4687 | + Category->getName(), |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4688 | Category->protocol_begin(), |
| 4689 | Category->protocol_end()); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4690 | Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(), |
| 4691 | OCD, Category, ObjCTypes); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 4692 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4693 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4694 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4695 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4696 | |
| 4697 | llvm::Constant *Init = |
| 4698 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4699 | Values); |
| 4700 | llvm::GlobalVariable *GCATV |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4701 | = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4702 | false, |
| 4703 | llvm::GlobalValue::InternalLinkage, |
| 4704 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4705 | ExtCatName); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4706 | GCATV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 4707 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4708 | GCATV->setSection("__DATA, __objc_const"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4709 | CGM.AddUsedGlobal(GCATV); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4710 | DefinedCategories.push_back(GCATV); |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4711 | |
| 4712 | // Determine if this category is also "non-lazy". |
| 4713 | if (ImplementationIsNonLazy(OCD)) |
| 4714 | DefinedNonLazyCategories.push_back(GCATV); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4715 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4716 | |
| 4717 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 4718 | /// given method if it has been defined. The result is null if the |
| 4719 | /// method has not been defined. The return value has type MethodPtrTy. |
| 4720 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4721 | const ObjCMethodDecl *MD) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4722 | // FIXME: Use DenseMap::lookup |
| 4723 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 4724 | if (!Fn) |
| 4725 | return 0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4726 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4727 | std::vector<llvm::Constant*> Method(3); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4728 | Method[0] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4729 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4730 | ObjCTypes.SelectorPtrTy); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4731 | Method[1] = GetMethodVarType(MD); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4732 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4733 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4734 | } |
| 4735 | |
| 4736 | /// EmitMethodList - Build meta-data for method declarations |
| 4737 | /// struct _method_list_t { |
| 4738 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 4739 | /// uint32_t method_count; |
| 4740 | /// struct _objc_method method_list[method_count]; |
| 4741 | /// } |
| 4742 | /// |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4743 | llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name, |
| 4744 | const char *Section, |
| 4745 | const ConstantVector &Methods) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4746 | // Return null for empty list. |
| 4747 | if (Methods.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4748 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4749 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4750 | std::vector<llvm::Constant*> Values(3); |
| 4751 | // sizeof(struct _objc_method) |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4752 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4753 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4754 | // method_count |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4755 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4756 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4757 | Methods.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 4758 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 4759 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4760 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4761 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4762 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4763 | llvm::GlobalValue::InternalLinkage, |
| 4764 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4765 | Name); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4766 | GV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 4767 | CGM.getTargetData().getABITypeAlignment(Init->getType())); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4768 | GV->setSection(Section); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4769 | CGM.AddUsedGlobal(GV); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4770 | return llvm::ConstantExpr::getBitCast(GV, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4771 | ObjCTypes.MethodListnfABIPtrTy); |
| 4772 | } |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4773 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4774 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 4775 | /// the given ivar. |
Daniel Dunbar | e83be12 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 4776 | llvm::GlobalVariable * |
| 4777 | CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID, |
| 4778 | const ObjCIvarDecl *Ivar) { |
| 4779 | const ObjCInterfaceDecl *Container = Ivar->getContainingInterface(); |
Daniel Dunbar | a81419d | 2009-05-05 00:36:57 +0000 | [diff] [blame] | 4780 | std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() + |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4781 | '.' + Ivar->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4782 | llvm::GlobalVariable *IvarOffsetGV = |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4783 | CGM.getModule().getGlobalVariable(Name); |
| 4784 | if (!IvarOffsetGV) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4785 | IvarOffsetGV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4786 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4787 | false, |
| 4788 | llvm::GlobalValue::ExternalLinkage, |
| 4789 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4790 | Name); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4791 | return IvarOffsetGV; |
| 4792 | } |
| 4793 | |
Daniel Dunbar | e83be12 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 4794 | llvm::Constant * |
| 4795 | CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
| 4796 | const ObjCIvarDecl *Ivar, |
| 4797 | unsigned long int Offset) { |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4798 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4799 | IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy, |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4800 | Offset)); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4801 | IvarOffsetGV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 4802 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy)); |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4803 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 4804 | // FIXME: This matches gcc, but shouldn't the visibility be set on the use as |
| 4805 | // well (i.e., in ObjCIvarOffsetVariable). |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4806 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 4807 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
| 4808 | CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden) |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4809 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4810 | else |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4811 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4812 | IvarOffsetGV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4813 | return IvarOffsetGV; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4814 | } |
| 4815 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4816 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 4817 | /// implementation. The return value has type |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4818 | /// IvarListnfABIPtrTy. |
| 4819 | /// struct _ivar_t { |
| 4820 | /// unsigned long int *offset; // pointer to ivar offset location |
| 4821 | /// char *name; |
| 4822 | /// char *type; |
| 4823 | /// uint32_t alignment; |
| 4824 | /// uint32_t size; |
| 4825 | /// } |
| 4826 | /// struct _ivar_list_t { |
| 4827 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 4828 | /// uint32 count; |
| 4829 | /// struct _iver_t list[count]; |
| 4830 | /// } |
| 4831 | /// |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4832 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4833 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4834 | const ObjCImplementationDecl *ID) { |
| 4835 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4836 | std::vector<llvm::Constant*> Ivars, Ivar(5); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4837 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4838 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4839 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4840 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4841 | // FIXME. Consolidate this with similar code in GenerateClass. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4842 | |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4843 | // Collect declared and synthesized ivars in a small vector. |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4844 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 4845 | CGM.getContext().ShallowCollectObjCIvars(OID, OIvars); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4846 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4847 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 4848 | ObjCIvarDecl *IVD = OIvars[i]; |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 4849 | // Ignore unnamed bit-fields. |
| 4850 | if (!IVD->getDeclName()) |
| 4851 | continue; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4852 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD, |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4853 | ComputeIvarBaseOffset(CGM, ID, IVD)); |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4854 | Ivar[1] = GetMethodVarName(IVD->getIdentifier()); |
| 4855 | Ivar[2] = GetMethodVarType(IVD); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4856 | const llvm::Type *FieldTy = |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4857 | CGM.getTypes().ConvertTypeForMem(IVD->getType()); |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4858 | unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4859 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4860 | IVD->getType().getTypePtr()) >> 3; |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4861 | Align = llvm::Log2_32(Align); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4862 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4863 | // NOTE. Size of a bitfield does not match gcc's, because of the |
| 4864 | // way bitfields are treated special in each. But I am told that |
| 4865 | // 'size' for bitfield ivars is ignored by the runtime so it does |
| 4866 | // not matter. If it matters, there is enough info to get the |
| 4867 | // bitfield right! |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4868 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4869 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4870 | } |
| 4871 | // Return null for empty list. |
| 4872 | if (Ivars.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4873 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4874 | std::vector<llvm::Constant*> Values(3); |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4875 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4876 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4877 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4878 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4879 | Ivars.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 4880 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 4881 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4882 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 4883 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4884 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4885 | llvm::GlobalValue::InternalLinkage, |
| 4886 | Init, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4887 | Prefix + OID->getName()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4888 | GV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 4889 | CGM.getTargetData().getABITypeAlignment(Init->getType())); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4890 | GV->setSection("__DATA, __objc_const"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4891 | |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4892 | CGM.AddUsedGlobal(GV); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4893 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4894 | } |
| 4895 | |
| 4896 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4897 | const ObjCProtocolDecl *PD) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4898 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4899 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4900 | if (!Entry) { |
| 4901 | // We use the initializer as a marker of whether this is a forward |
| 4902 | // reference or not. At module finalization we add the empty |
| 4903 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4904 | Entry = |
| 4905 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false, |
| 4906 | llvm::GlobalValue::ExternalLinkage, |
| 4907 | 0, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4908 | "\01l_OBJC_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4909 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4910 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4911 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4912 | return Entry; |
| 4913 | } |
| 4914 | |
| 4915 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 4916 | /// @code |
| 4917 | /// struct _protocol_t { |
| 4918 | /// id isa; // NULL |
| 4919 | /// const char * const protocol_name; |
| 4920 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 4921 | /// const struct method_list_t * const instance_methods; |
| 4922 | /// const struct method_list_t * const class_methods; |
| 4923 | /// const struct method_list_t *optionalInstanceMethods; |
| 4924 | /// const struct method_list_t *optionalClassMethods; |
| 4925 | /// const struct _prop_list_t * properties; |
| 4926 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 4927 | /// const uint32_t flags; // = 0 |
| 4928 | /// } |
| 4929 | /// @endcode |
| 4930 | /// |
| 4931 | |
| 4932 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4933 | const ObjCProtocolDecl *PD) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4934 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4935 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4936 | // Early exit if a defining object has already been generated. |
| 4937 | if (Entry && Entry->hasInitializer()) |
| 4938 | return Entry; |
| 4939 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4940 | // Construct method lists. |
| 4941 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 4942 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4943 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4944 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4945 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4946 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4947 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4948 | OptInstanceMethods.push_back(C); |
| 4949 | } else { |
| 4950 | InstanceMethods.push_back(C); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4951 | } |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4952 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4953 | |
| 4954 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4955 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4956 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4957 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4958 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4959 | OptClassMethods.push_back(C); |
| 4960 | } else { |
| 4961 | ClassMethods.push_back(C); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4962 | } |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4963 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4964 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4965 | std::vector<llvm::Constant*> Values(10); |
| 4966 | // isa is NULL |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4967 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4968 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4969 | Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(), |
| 4970 | PD->protocol_begin(), |
| 4971 | PD->protocol_end()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4972 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4973 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4974 | + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4975 | "__DATA, __objc_const", |
| 4976 | InstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4977 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4978 | + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4979 | "__DATA, __objc_const", |
| 4980 | ClassMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4981 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4982 | + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4983 | "__DATA, __objc_const", |
| 4984 | OptInstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4985 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4986 | + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4987 | "__DATA, __objc_const", |
| 4988 | OptClassMethods); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4989 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4990 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4991 | uint32_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4992 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4993 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4994 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4995 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4996 | Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4997 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4998 | if (Entry) { |
| 4999 | // Already created, fix the linkage and update the initializer. |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 5000 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5001 | Entry->setInitializer(Init); |
| 5002 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5003 | Entry = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5004 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, |
| 5005 | false, llvm::GlobalValue::WeakAnyLinkage, Init, |
| 5006 | "\01l_OBJC_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5007 | Entry->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5008 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5009 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5010 | } |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5011 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5012 | CGM.AddUsedGlobal(Entry); |
| 5013 | |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5014 | // Use this protocol meta-data to build protocol list table in section |
| 5015 | // __DATA, __objc_protolist |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5016 | llvm::GlobalVariable *PTGV = |
| 5017 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy, |
| 5018 | false, llvm::GlobalValue::WeakAnyLinkage, Entry, |
| 5019 | "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5020 | PTGV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5021 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 5022 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5023 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5024 | CGM.AddUsedGlobal(PTGV); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5025 | return Entry; |
| 5026 | } |
| 5027 | |
| 5028 | /// EmitProtocolList - Generate protocol list meta-data: |
| 5029 | /// @code |
| 5030 | /// struct _protocol_list_t { |
| 5031 | /// long protocol_count; // Note, this is 32/64 bit |
| 5032 | /// struct _protocol_t[protocol_count]; |
| 5033 | /// } |
| 5034 | /// @endcode |
| 5035 | /// |
| 5036 | llvm::Constant * |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5037 | CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name, |
| 5038 | ObjCProtocolDecl::protocol_iterator begin, |
| 5039 | ObjCProtocolDecl::protocol_iterator end) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5040 | std::vector<llvm::Constant*> ProtocolRefs; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5041 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5042 | // Just return null for empty protocol lists |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5043 | if (begin == end) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5044 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5045 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5046 | // FIXME: We shouldn't need to do this lookup here, should we? |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5047 | llvm::SmallString<256> TmpName; |
| 5048 | Name.toVector(TmpName); |
| 5049 | llvm::GlobalVariable *GV = |
| 5050 | CGM.getModule().getGlobalVariable(TmpName.str(), true); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5051 | if (GV) |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5052 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5053 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5054 | for (; begin != end; ++begin) |
| 5055 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 5056 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5057 | // This list is null terminated. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5058 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5059 | ObjCTypes.ProtocolnfABIPtrTy)); |
| 5060 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5061 | std::vector<llvm::Constant*> Values(2); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5062 | Values[0] = |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5063 | llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5064 | Values[1] = |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 5065 | llvm::ConstantArray::get( |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5066 | llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5067 | ProtocolRefs.size()), |
| 5068 | ProtocolRefs); |
| 5069 | |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 5070 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5071 | GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5072 | llvm::GlobalValue::InternalLinkage, |
| 5073 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5074 | Name); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5075 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5076 | GV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5077 | CGM.getTargetData().getABITypeAlignment(Init->getType())); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5078 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5079 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5080 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5081 | } |
| 5082 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5083 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 5084 | /// struct _objc_method { |
| 5085 | /// SEL _cmd; |
| 5086 | /// char *method_type; |
| 5087 | /// char *_imp; |
| 5088 | /// } |
| 5089 | |
| 5090 | llvm::Constant * |
| 5091 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 5092 | std::vector<llvm::Constant*> Desc(3); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5093 | Desc[0] = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5094 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 5095 | ObjCTypes.SelectorPtrTy); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5096 | Desc[1] = GetMethodVarType(MD); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5097 | // Protocol methods have no implementation. So, this entry is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5098 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5099 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5100 | } |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5101 | |
| 5102 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 5103 | /// This code gen. amounts to generating code for: |
| 5104 | /// @code |
| 5105 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 5106 | /// @encode |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5107 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 5108 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 5109 | CodeGen::CodeGenFunction &CGF, |
| 5110 | QualType ObjectTy, |
| 5111 | llvm::Value *BaseValue, |
| 5112 | const ObjCIvarDecl *Ivar, |
| 5113 | unsigned CVRQualifiers) { |
| 5114 | ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 5115 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 5116 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5117 | } |
| 5118 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 5119 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5120 | CodeGen::CodeGenFunction &CGF, |
| 5121 | const ObjCInterfaceDecl *Interface, |
| 5122 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 5123 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 5124 | } |
| 5125 | |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5126 | CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5127 | CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame^] | 5128 | ReturnValueSlot Return, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5129 | QualType ResultType, |
| 5130 | Selector Sel, |
| 5131 | llvm::Value *Receiver, |
| 5132 | QualType Arg0Ty, |
| 5133 | bool IsSuper, |
| 5134 | const CallArgList &CallArgs) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5135 | // FIXME. Even though IsSuper is passes. This function doese not handle calls |
| 5136 | // to 'super' receivers. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5137 | CodeGenTypes &Types = CGM.getTypes(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5138 | llvm::Value *Arg0 = Receiver; |
| 5139 | if (!IsSuper) |
| 5140 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5141 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5142 | // Find the message function name. |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5143 | // FIXME. This is too much work to get the ABI-specific result type needed to |
| 5144 | // find the message name. |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 5145 | const CGFunctionInfo &FnInfo |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 5146 | = Types.getFunctionInfo(ResultType, CallArgList(), |
| 5147 | FunctionType::ExtInfo()); |
Fariborz Jahanian | 70b51c7 | 2009-04-30 23:08:58 +0000 | [diff] [blame] | 5148 | llvm::Constant *Fn = 0; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5149 | std::string Name("\01l_"); |
| 5150 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5151 | #if 0 |
| 5152 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5153 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5154 | Fn = ObjCTypes.getMessageSendIdStretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5155 | // FIXME. Is there a better way of getting these names. |
| 5156 | // They are available in RuntimeFunctions vector pair. |
| 5157 | Name += "objc_msgSendId_stret_fixup"; |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5158 | } else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5159 | #endif |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5160 | if (IsSuper) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5161 | Fn = ObjCTypes.getMessageSendSuper2StretFixupFn(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5162 | Name += "objc_msgSendSuper2_stret_fixup"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5163 | } else { |
| 5164 | Fn = ObjCTypes.getMessageSendStretFixupFn(); |
| 5165 | Name += "objc_msgSend_stret_fixup"; |
| 5166 | } |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5167 | } else if (!IsSuper && ResultType->isFloatingType()) { |
Daniel Dunbar | c0183e8 | 2009-06-26 18:32:06 +0000 | [diff] [blame] | 5168 | if (ResultType->isSpecificBuiltinType(BuiltinType::LongDouble)) { |
| 5169 | Fn = ObjCTypes.getMessageSendFpretFixupFn(); |
| 5170 | Name += "objc_msgSend_fpret_fixup"; |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5171 | } else { |
Daniel Dunbar | c0183e8 | 2009-06-26 18:32:06 +0000 | [diff] [blame] | 5172 | Fn = ObjCTypes.getMessageSendFixupFn(); |
| 5173 | Name += "objc_msgSend_fixup"; |
Fariborz Jahanian | 5b2bad0 | 2009-04-30 16:31:11 +0000 | [diff] [blame] | 5174 | } |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5175 | } else { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5176 | #if 0 |
| 5177 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5178 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5179 | Fn = ObjCTypes.getMessageSendIdFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5180 | Name += "objc_msgSendId_fixup"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5181 | } else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5182 | #endif |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5183 | if (IsSuper) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5184 | Fn = ObjCTypes.getMessageSendSuper2FixupFn(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5185 | Name += "objc_msgSendSuper2_fixup"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5186 | } else { |
| 5187 | Fn = ObjCTypes.getMessageSendFixupFn(); |
| 5188 | Name += "objc_msgSend_fixup"; |
| 5189 | } |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5190 | } |
Fariborz Jahanian | 70b51c7 | 2009-04-30 23:08:58 +0000 | [diff] [blame] | 5191 | assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5192 | Name += '_'; |
| 5193 | std::string SelName(Sel.getAsString()); |
| 5194 | // Replace all ':' in selector name with '_' ouch! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5195 | for (unsigned i = 0; i < SelName.size(); i++) |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5196 | if (SelName[i] == ':') |
| 5197 | SelName[i] = '_'; |
| 5198 | Name += SelName; |
| 5199 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5200 | if (!GV) { |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5201 | // Build message ref table entry. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5202 | std::vector<llvm::Constant*> Values(2); |
| 5203 | Values[0] = Fn; |
| 5204 | Values[1] = GetMethodVarName(Sel); |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 5205 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5206 | GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 5207 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5208 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5209 | Name); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5210 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | f59c1a6 | 2009-04-15 19:04:46 +0000 | [diff] [blame] | 5211 | GV->setAlignment(16); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5212 | GV->setSection("__DATA, __objc_msgrefs, coalesced"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5213 | } |
| 5214 | llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5215 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5216 | CallArgList ActualArgs; |
| 5217 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5218 | ActualArgs.push_back(std::make_pair(RValue::get(Arg1), |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5219 | ObjCTypes.MessageRefCPtrTy)); |
| 5220 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 5221 | const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 5222 | FunctionType::ExtInfo()); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5223 | llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0); |
| 5224 | Callee = CGF.Builder.CreateLoad(Callee); |
Fariborz Jahanian | 3ab75bd | 2009-02-14 21:25:36 +0000 | [diff] [blame] | 5225 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5226 | Callee = CGF.Builder.CreateBitCast(Callee, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5227 | llvm::PointerType::getUnqual(FTy)); |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame^] | 5228 | return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5229 | } |
| 5230 | |
| 5231 | /// Generate code for a message send expression in the nonfragile abi. |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5232 | CodeGen::RValue |
| 5233 | CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame^] | 5234 | ReturnValueSlot Return, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5235 | QualType ResultType, |
| 5236 | Selector Sel, |
| 5237 | llvm::Value *Receiver, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5238 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 5239 | const ObjCInterfaceDecl *Class, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5240 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5241 | return LegacyDispatchedSelector(Sel) |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame^] | 5242 | ? EmitLegacyMessageSend(CGF, Return, ResultType, |
| 5243 | EmitSelector(CGF.Builder, Sel), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5244 | Receiver, CGF.getContext().getObjCIdType(), |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5245 | false, CallArgs, Method, ObjCTypes) |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame^] | 5246 | : EmitMessageSend(CGF, Return, ResultType, Sel, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5247 | Receiver, CGF.getContext().getObjCIdType(), |
| 5248 | false, CallArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5249 | } |
| 5250 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5251 | llvm::GlobalVariable * |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5252 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5253 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5254 | |
Daniel Dunbar | dfff230 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 5255 | if (!GV) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5256 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5257 | false, llvm::GlobalValue::ExternalLinkage, |
| 5258 | 0, Name); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5259 | } |
| 5260 | |
| 5261 | return GV; |
| 5262 | } |
| 5263 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5264 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
| 5265 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5266 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5267 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5268 | if (!Entry) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5269 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5270 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5271 | Entry = |
| 5272 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5273 | false, llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5274 | ClassGV, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5275 | "\01L_OBJC_CLASSLIST_REFERENCES_$_"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5276 | Entry->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5277 | CGM.getTargetData().getABITypeAlignment( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5278 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5279 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5280 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5281 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5282 | |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 5283 | return Builder.CreateLoad(Entry, "tmp"); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5284 | } |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5285 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5286 | llvm::Value * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5287 | CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5288 | const ObjCInterfaceDecl *ID) { |
| 5289 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5290 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5291 | if (!Entry) { |
| 5292 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5293 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5294 | Entry = |
| 5295 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5296 | false, llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5297 | ClassGV, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5298 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5299 | Entry->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5300 | CGM.getTargetData().getABITypeAlignment( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5301 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5302 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5303 | CGM.AddUsedGlobal(Entry); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5304 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5305 | |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 5306 | return Builder.CreateLoad(Entry, "tmp"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5307 | } |
| 5308 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5309 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 5310 | /// meta-data |
| 5311 | /// |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5312 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 5313 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5314 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 5315 | if (Entry) |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 5316 | return Builder.CreateLoad(Entry, "tmp"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5317 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5318 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5319 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5320 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5321 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5322 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5323 | MetaClassGV, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5324 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5325 | Entry->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5326 | CGM.getTargetData().getABITypeAlignment( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5327 | ObjCTypes.ClassnfABIPtrTy)); |
| 5328 | |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5329 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5330 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5331 | |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 5332 | return Builder.CreateLoad(Entry, "tmp"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5333 | } |
| 5334 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5335 | /// GetClass - Return a reference to the class for the given interface |
| 5336 | /// decl. |
| 5337 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 5338 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 269f8bc | 2009-11-17 22:42:00 +0000 | [diff] [blame] | 5339 | if (ID->hasAttr<WeakImportAttr>()) { |
| 5340 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5341 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
| 5342 | ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
| 5343 | } |
| 5344 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5345 | return EmitClassRef(Builder, ID); |
| 5346 | } |
| 5347 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5348 | /// Generates a message send where the super is the receiver. This is |
| 5349 | /// a message send to self with special delivery semantics indicating |
| 5350 | /// which class's method should be called. |
| 5351 | CodeGen::RValue |
| 5352 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame^] | 5353 | ReturnValueSlot Return, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5354 | QualType ResultType, |
| 5355 | Selector Sel, |
| 5356 | const ObjCInterfaceDecl *Class, |
| 5357 | bool isCategoryImpl, |
| 5358 | llvm::Value *Receiver, |
| 5359 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5360 | const CodeGen::CallArgList &CallArgs, |
| 5361 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5362 | // ... |
| 5363 | // Create and init a super structure; this is a (receiver, class) |
| 5364 | // pair we will pass to objc_msgSendSuper. |
| 5365 | llvm::Value *ObjCSuper = |
| 5366 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5367 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5368 | llvm::Value *ReceiverAsObject = |
| 5369 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 5370 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 5371 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5372 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5373 | // If this is a class message the metaclass is passed as the target. |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5374 | llvm::Value *Target; |
| 5375 | if (IsClassMessage) { |
| 5376 | if (isCategoryImpl) { |
| 5377 | // Message sent to "super' in a class method defined in |
| 5378 | // a category implementation. |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5379 | Target = EmitClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5380 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 5381 | Target = CGF.Builder.CreateLoad(Target); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5382 | } else |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5383 | Target = EmitMetaClassRef(CGF.Builder, Class); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5384 | } else |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5385 | Target = EmitSuperClassRef(CGF.Builder, Class); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5386 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5387 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 5388 | // ObjCTypes types. |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5389 | const llvm::Type *ClassTy = |
| 5390 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 5391 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 5392 | CGF.Builder.CreateStore(Target, |
| 5393 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5394 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5395 | return (LegacyDispatchedSelector(Sel)) |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame^] | 5396 | ? EmitLegacyMessageSend(CGF, Return, ResultType, |
| 5397 | EmitSelector(CGF.Builder, Sel), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5398 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5399 | true, CallArgs, Method, ObjCTypes) |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame^] | 5400 | : EmitMessageSend(CGF, Return, ResultType, Sel, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5401 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 5402 | true, CallArgs); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5403 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5404 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5405 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5406 | Selector Sel) { |
| 5407 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5408 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5409 | if (!Entry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5410 | llvm::Constant *Casted = |
| 5411 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 5412 | ObjCTypes.SelectorPtrTy); |
| 5413 | Entry = |
| 5414 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false, |
| 5415 | llvm::GlobalValue::InternalLinkage, |
| 5416 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5417 | Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5418 | CGM.AddUsedGlobal(Entry); |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5419 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5420 | |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 5421 | return Builder.CreateLoad(Entry, "tmp"); |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5422 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5423 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 5424 | /// objc_assign_ivar (id src, id *dst, ptrdiff_t) |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5425 | /// |
| 5426 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5427 | llvm::Value *src, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 5428 | llvm::Value *dst, |
| 5429 | llvm::Value *ivarOffset) { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5430 | const llvm::Type * SrcTy = src->getType(); |
| 5431 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5432 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5433 | assert(Size <= 8 && "does not support size > 8"); |
| 5434 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5435 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5436 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5437 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5438 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5439 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 5440 | CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(), |
| 5441 | src, dst, ivarOffset); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5442 | return; |
| 5443 | } |
| 5444 | |
| 5445 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 5446 | /// objc_assign_strongCast (id src, id *dst) |
| 5447 | /// |
| 5448 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5449 | CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5450 | llvm::Value *src, llvm::Value *dst) { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5451 | const llvm::Type * SrcTy = src->getType(); |
| 5452 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5453 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5454 | assert(Size <= 8 && "does not support size > 8"); |
| 5455 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5456 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5457 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5458 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5459 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5460 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5461 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5462 | src, dst, "weakassign"); |
| 5463 | return; |
| 5464 | } |
| 5465 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 5466 | void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5467 | CodeGen::CodeGenFunction &CGF, |
| 5468 | llvm::Value *DestPtr, |
| 5469 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 5470 | QualType Ty) { |
| 5471 | // Get size info for this aggregate. |
| 5472 | std::pair<uint64_t, unsigned> TypeInfo = CGM.getContext().getTypeInfo(Ty); |
Fariborz Jahanian | 736d8a6 | 2009-09-10 16:20:31 +0000 | [diff] [blame] | 5473 | unsigned long size = TypeInfo.first/8; |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 5474 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 5475 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5476 | llvm::Value *N = llvm::ConstantInt::get(ObjCTypes.LongTy, size); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 5477 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), |
| 5478 | DestPtr, SrcPtr, N); |
| 5479 | return; |
| 5480 | } |
| 5481 | |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5482 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 5483 | /// object: objc_read_weak (id *src) |
| 5484 | /// |
| 5485 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5486 | CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5487 | llvm::Value *AddrWeakObj) { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5488 | const llvm::Type* DestTy = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5489 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
| 5490 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 5491 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5492 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5493 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5494 | return read_weak; |
| 5495 | } |
| 5496 | |
| 5497 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 5498 | /// objc_assign_weak (id src, id *dst) |
| 5499 | /// |
| 5500 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5501 | llvm::Value *src, llvm::Value *dst) { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5502 | const llvm::Type * SrcTy = src->getType(); |
| 5503 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5504 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5505 | assert(Size <= 8 && "does not support size > 8"); |
| 5506 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5507 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5508 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5509 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5510 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5511 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 5512 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5513 | src, dst, "weakassign"); |
| 5514 | return; |
| 5515 | } |
| 5516 | |
| 5517 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 5518 | /// objc_assign_global (id src, id *dst) |
| 5519 | /// |
| 5520 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5521 | llvm::Value *src, llvm::Value *dst) { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5522 | const llvm::Type * SrcTy = src->getType(); |
| 5523 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5524 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5525 | assert(Size <= 8 && "does not support size > 8"); |
| 5526 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5527 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5528 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5529 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5530 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5531 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5532 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5533 | src, dst, "globalassign"); |
| 5534 | return; |
| 5535 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5536 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5537 | void |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5538 | CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 5539 | const Stmt &S) { |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5540 | bool isTry = isa<ObjCAtTryStmt>(S); |
| 5541 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 5542 | llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5543 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5544 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5545 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5546 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
| 5547 | |
| 5548 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 5549 | // evaluation of the expression must occur before we enter the |
| 5550 | // @synchronized. We can safely avoid a temp here because jumps into |
| 5551 | // @synchronized are illegal & this will dominate uses. |
| 5552 | llvm::Value *SyncArg = 0; |
| 5553 | if (!isTry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5554 | SyncArg = |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5555 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 5556 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5557 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5558 | } |
| 5559 | |
| 5560 | // Push an EH context entry, used for handling rethrows and jumps |
| 5561 | // through finally. |
| 5562 | CGF.PushCleanupBlock(FinallyBlock); |
| 5563 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5564 | CGF.setInvokeDest(TryHandler); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5565 | |
| 5566 | CGF.EmitBlock(TryBlock); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5567 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 5568 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5569 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5570 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5571 | // Emit the exception handler. |
| 5572 | |
| 5573 | CGF.EmitBlock(TryHandler); |
| 5574 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5575 | llvm::Value *llvm_eh_exception = |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5576 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
Duncan Sands | 790b20e | 2009-10-14 16:13:30 +0000 | [diff] [blame] | 5577 | llvm::Value *llvm_eh_selector = |
| 5578 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector); |
| 5579 | llvm::Value *llvm_eh_typeid_for = |
| 5580 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5581 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5582 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow"); |
| 5583 | |
| 5584 | llvm::SmallVector<llvm::Value*, 8> SelectorArgs; |
| 5585 | SelectorArgs.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5586 | SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5587 | |
| 5588 | // Construct the lists of (type, catch body) to handle. |
Douglas Gregor | c00d8e1 | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 5589 | llvm::SmallVector<std::pair<const VarDecl*, const Stmt*>, 8> Handlers; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5590 | bool HasCatchAll = false; |
| 5591 | if (isTry) { |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 5592 | const ObjCAtTryStmt &AtTry = cast<ObjCAtTryStmt>(S); |
| 5593 | for (unsigned I = 0, N = AtTry.getNumCatchStmts(); I != N; ++I) { |
| 5594 | const ObjCAtCatchStmt *CatchStmt = AtTry.getCatchStmt(I); |
Douglas Gregor | c00d8e1 | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 5595 | const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl(); |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 5596 | Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody())); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5597 | |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 5598 | // catch(...) always matches. |
| 5599 | if (!CatchDecl) { |
| 5600 | // Use i8* null here to signal this is a catch all, not a cleanup. |
| 5601 | llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 5602 | SelectorArgs.push_back(Null); |
| 5603 | HasCatchAll = true; |
| 5604 | break; |
| 5605 | } |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5606 | |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 5607 | if (CatchDecl->getType()->isObjCIdType() || |
| 5608 | CatchDecl->getType()->isObjCQualifiedIdType()) { |
| 5609 | llvm::Value *IDEHType = |
| 5610 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 5611 | if (!IDEHType) |
| 5612 | IDEHType = |
| 5613 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, |
| 5614 | false, |
| 5615 | llvm::GlobalValue::ExternalLinkage, |
| 5616 | 0, "OBJC_EHTYPE_id"); |
| 5617 | SelectorArgs.push_back(IDEHType); |
| 5618 | } else { |
| 5619 | // All other types should be Objective-C interface pointer types. |
| 5620 | const ObjCObjectPointerType *PT = |
| 5621 | CatchDecl->getType()->getAs<ObjCObjectPointerType>(); |
| 5622 | assert(PT && "Invalid @catch type."); |
| 5623 | const ObjCInterfaceType *IT = PT->getInterfaceType(); |
| 5624 | assert(IT && "Invalid @catch type."); |
| 5625 | llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false); |
| 5626 | SelectorArgs.push_back(EHType); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5627 | } |
| 5628 | } |
| 5629 | } |
| 5630 | |
| 5631 | // We use a cleanup unless there was already a catch all. |
| 5632 | if (!HasCatchAll) { |
Daniel Dunbar | e3d25ab | 2009-08-03 21:20:07 +0000 | [diff] [blame] | 5633 | // Even though this is a cleanup, treat it as a catch all to avoid the C++ |
| 5634 | // personality behavior of terminating the process if only cleanups are |
| 5635 | // found in the exception handling stack. |
| 5636 | SelectorArgs.push_back(llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy)); |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5637 | Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5638 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5639 | |
| 5640 | llvm::Value *Selector = |
Duncan Sands | 790b20e | 2009-10-14 16:13:30 +0000 | [diff] [blame] | 5641 | CGF.Builder.CreateCall(llvm_eh_selector, |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5642 | SelectorArgs.begin(), SelectorArgs.end(), |
| 5643 | "selector"); |
| 5644 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
Douglas Gregor | c00d8e1 | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 5645 | const VarDecl *CatchParam = Handlers[i].first; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5646 | const Stmt *CatchBody = Handlers[i].second; |
| 5647 | |
| 5648 | llvm::BasicBlock *Next = 0; |
| 5649 | |
| 5650 | // The last handler always matches. |
| 5651 | if (i + 1 != e) { |
| 5652 | assert(CatchParam && "Only last handler can be a catch all."); |
| 5653 | |
| 5654 | llvm::BasicBlock *Match = CGF.createBasicBlock("match"); |
| 5655 | Next = CGF.createBasicBlock("catch.next"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5656 | llvm::Value *Id = |
Duncan Sands | 790b20e | 2009-10-14 16:13:30 +0000 | [diff] [blame] | 5657 | CGF.Builder.CreateCall(llvm_eh_typeid_for, |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5658 | CGF.Builder.CreateBitCast(SelectorArgs[i+2], |
| 5659 | ObjCTypes.Int8PtrTy)); |
| 5660 | CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id), |
| 5661 | Match, Next); |
| 5662 | |
| 5663 | CGF.EmitBlock(Match); |
| 5664 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5665 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5666 | if (CatchBody) { |
| 5667 | llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end"); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5668 | |
| 5669 | // Cleanups must call objc_end_catch. |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5670 | CGF.PushCleanupBlock(MatchEnd); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5671 | |
| 5672 | llvm::Value *ExcObject = |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5673 | CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5674 | |
| 5675 | // Bind the catch parameter if it exists. |
| 5676 | if (CatchParam) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5677 | ExcObject = |
| 5678 | CGF.Builder.CreateBitCast(ExcObject, |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5679 | CGF.ConvertType(CatchParam->getType())); |
| 5680 | // CatchParam is a ParmVarDecl because of the grammar |
| 5681 | // construction used to handle this, but for codegen purposes |
| 5682 | // we treat this as a local decl. |
| 5683 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
| 5684 | CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5685 | } |
| 5686 | |
Daniel Dunbar | ba6940a | 2010-04-23 19:12:24 +0000 | [diff] [blame] | 5687 | // Exceptions inside the catch block must be rethrown. We set a special |
| 5688 | // purpose invoke destination for this which just collects the thrown |
| 5689 | // exception and overwrites the object in RethrowPtr, branches through the |
| 5690 | // match.end to make sure we call objc_end_catch, before branching to the |
| 5691 | // rethrow handler. |
| 5692 | llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler"); |
| 5693 | CGF.setInvokeDest(MatchHandler); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5694 | CGF.ObjCEHValueStack.push_back(ExcObject); |
| 5695 | CGF.EmitStmt(CatchBody); |
| 5696 | CGF.ObjCEHValueStack.pop_back(); |
Daniel Dunbar | ba6940a | 2010-04-23 19:12:24 +0000 | [diff] [blame] | 5697 | CGF.setInvokeDest(0); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5698 | |
| 5699 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5700 | |
Daniel Dunbar | ba6940a | 2010-04-23 19:12:24 +0000 | [diff] [blame] | 5701 | // Don't emit the extra match handler if there we no unprotected calls in |
| 5702 | // the catch block. |
| 5703 | if (MatchHandler->use_empty()) { |
| 5704 | delete MatchHandler; |
| 5705 | } else { |
| 5706 | CGF.EmitBlock(MatchHandler); |
| 5707 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5708 | // We are required to emit this call to satisfy LLVM, even |
| 5709 | // though we don't use the result. |
| 5710 | CGF.Builder.CreateCall3(llvm_eh_selector, |
| 5711 | Exc, ObjCTypes.getEHPersonalityPtr(), |
| 5712 | llvm::ConstantInt::get( |
| 5713 | llvm::Type::getInt32Ty(VMContext), 0), |
| 5714 | "unused_eh_selector"); |
| 5715 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5716 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5717 | } |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5718 | |
| 5719 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5720 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5721 | CGF.EmitBlock(MatchEnd); |
| 5722 | |
| 5723 | // Unfortunately, we also have to generate another EH frame here |
| 5724 | // in case this throws. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5725 | llvm::BasicBlock *MatchEndHandler = |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5726 | CGF.createBasicBlock("match.end.handler"); |
| 5727 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5728 | CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(), |
Daniel Dunbar | ba6940a | 2010-04-23 19:12:24 +0000 | [diff] [blame] | 5729 | Cont, MatchEndHandler); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5730 | |
| 5731 | CGF.EmitBlock(Cont); |
| 5732 | if (Info.SwitchBlock) |
| 5733 | CGF.EmitBlock(Info.SwitchBlock); |
| 5734 | if (Info.EndBlock) |
| 5735 | CGF.EmitBlock(Info.EndBlock); |
| 5736 | |
| 5737 | CGF.EmitBlock(MatchEndHandler); |
Daniel Dunbar | ba6940a | 2010-04-23 19:12:24 +0000 | [diff] [blame] | 5738 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5739 | // We are required to emit this call to satisfy LLVM, even |
| 5740 | // though we don't use the result. |
Daniel Dunbar | ba6940a | 2010-04-23 19:12:24 +0000 | [diff] [blame] | 5741 | CGF.Builder.CreateCall3(llvm_eh_selector, |
| 5742 | Exc, ObjCTypes.getEHPersonalityPtr(), |
| 5743 | llvm::ConstantInt::get( |
| 5744 | llvm::Type::getInt32Ty(VMContext), 0), |
| 5745 | "unused_eh_selector"); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5746 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5747 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5748 | |
| 5749 | if (Next) |
| 5750 | CGF.EmitBlock(Next); |
| 5751 | } else { |
| 5752 | assert(!Next && "catchup should be last handler."); |
| 5753 | |
| 5754 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5755 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5756 | } |
| 5757 | } |
| 5758 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5759 | // Pop the cleanup entry, the @finally is outside this cleanup |
| 5760 | // scope. |
| 5761 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5762 | CGF.setInvokeDest(PrevLandingPad); |
| 5763 | |
| 5764 | CGF.EmitBlock(FinallyBlock); |
| 5765 | |
| 5766 | if (isTry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5767 | if (const ObjCAtFinallyStmt* FinallyStmt = |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5768 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 5769 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 5770 | } else { |
| 5771 | // Emit 'objc_sync_exit(expr)' as finally's sole statement for |
| 5772 | // @synchronized. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5773 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5774 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5775 | |
| 5776 | if (Info.SwitchBlock) |
| 5777 | CGF.EmitBlock(Info.SwitchBlock); |
| 5778 | if (Info.EndBlock) |
| 5779 | CGF.EmitBlock(Info.EndBlock); |
| 5780 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5781 | // Branch around the rethrow code. |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5782 | CGF.EmitBranch(FinallyEnd); |
| 5783 | |
Daniel Dunbar | b76db23 | 2010-04-23 19:12:32 +0000 | [diff] [blame] | 5784 | // Generate the rethrow code, taking care to use an invoke if we are in a |
| 5785 | // nested exception scope. |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5786 | CGF.EmitBlock(FinallyRethrow); |
Daniel Dunbar | b76db23 | 2010-04-23 19:12:32 +0000 | [diff] [blame] | 5787 | if (PrevLandingPad) { |
| 5788 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
| 5789 | CGF.Builder.CreateInvoke(ObjCTypes.getUnwindResumeOrRethrowFn(), |
| 5790 | Cont, PrevLandingPad, |
| 5791 | CGF.Builder.CreateLoad(RethrowPtr)); |
| 5792 | CGF.EmitBlock(Cont); |
| 5793 | } else { |
| 5794 | CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(), |
| 5795 | CGF.Builder.CreateLoad(RethrowPtr)); |
| 5796 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5797 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5798 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5799 | CGF.EmitBlock(FinallyEnd); |
| 5800 | } |
| 5801 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5802 | /// EmitThrowStmt - Generate code for a throw statement. |
| 5803 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 5804 | const ObjCAtThrowStmt &S) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5805 | llvm::Value *Exception; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5806 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5807 | Exception = CGF.EmitScalarExpr(ThrowExpr); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5808 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5809 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5810 | "Unexpected rethrow outside @catch block."); |
| 5811 | Exception = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5812 | } |
| 5813 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5814 | llvm::Value *ExceptionAsObject = |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5815 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 5816 | llvm::BasicBlock *InvokeDest = CGF.getInvokeDest(); |
| 5817 | if (InvokeDest) { |
| 5818 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5819 | CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5820 | Cont, InvokeDest, |
| 5821 | &ExceptionAsObject, &ExceptionAsObject + 1); |
| 5822 | CGF.EmitBlock(Cont); |
| 5823 | } else |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5824 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5825 | CGF.Builder.CreateUnreachable(); |
| 5826 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5827 | // Clear the insertion point to indicate we are in unreachable code. |
| 5828 | CGF.Builder.ClearInsertionPoint(); |
| 5829 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5830 | |
| 5831 | llvm::Value * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5832 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5833 | bool ForDefinition) { |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5834 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5835 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5836 | // If we don't need a definition, return the entry if found or check |
| 5837 | // if we use an external reference. |
| 5838 | if (!ForDefinition) { |
| 5839 | if (Entry) |
| 5840 | return Entry; |
Daniel Dunbar | 7e075cb | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 5841 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5842 | // If this type (or a super class) has the __objc_exception__ |
| 5843 | // attribute, emit an external reference. |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 5844 | if (hasObjCExceptionAttribute(CGM.getContext(), ID)) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5845 | return Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5846 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5847 | llvm::GlobalValue::ExternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5848 | 0, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5849 | ("OBJC_EHTYPE_$_" + |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 5850 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5851 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5852 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5853 | // Otherwise we need to either make a new entry or fill in the |
| 5854 | // initializer. |
| 5855 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5856 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5857 | std::string VTableName = "objc_ehtype_vtable"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5858 | llvm::GlobalVariable *VTableGV = |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5859 | CGM.getModule().getGlobalVariable(VTableName); |
| 5860 | if (!VTableGV) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5861 | VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy, |
| 5862 | false, |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5863 | llvm::GlobalValue::ExternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5864 | 0, VTableName); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5865 | |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 5866 | llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5867 | |
| 5868 | std::vector<llvm::Constant*> Values(3); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 5869 | Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5870 | Values[1] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5871 | Values[2] = GetClassGlobal(ClassName); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5872 | llvm::Constant *Init = |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5873 | llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5874 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5875 | if (Entry) { |
| 5876 | Entry->setInitializer(Init); |
| 5877 | } else { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5878 | Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5879 | llvm::GlobalValue::WeakAnyLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5880 | Init, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5881 | ("OBJC_EHTYPE_$_" + |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 5882 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5883 | } |
| 5884 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 5885 | if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden) |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5886 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5887 | Entry->setAlignment(CGM.getTargetData().getABITypeAlignment( |
| 5888 | ObjCTypes.EHTypeTy)); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5889 | |
| 5890 | if (ForDefinition) { |
| 5891 | Entry->setSection("__DATA,__objc_const"); |
| 5892 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 5893 | } else { |
| 5894 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 5895 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5896 | |
| 5897 | return Entry; |
| 5898 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5899 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5900 | /* *** */ |
| 5901 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5902 | CodeGen::CGObjCRuntime * |
| 5903 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 5904 | return new CGObjCMac(CGM); |
| 5905 | } |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5906 | |
| 5907 | CodeGen::CGObjCRuntime * |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5908 | CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5909 | return new CGObjCNonFragileABIMac(CGM); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5910 | } |