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. |
| 127 | unsigned FieldNo = 0; // This value is unused. |
| 128 | CGBitFieldInfo *Info = |
Daniel Dunbar | 7f28964 | 2010-04-08 02:59:45 +0000 | [diff] [blame] | 129 | new (CGF.CGM.getContext()) CGBitFieldInfo( |
| 130 | LTy, FieldNo, BitOffset, BitFieldSize, IvarTy->isSignedIntegerType()); |
Daniel Dunbar | f0fe5bc | 2010-04-05 21:36:35 +0000 | [diff] [blame] | 131 | |
Daniel Dunbar | ab970f9 | 2010-04-13 20:58:55 +0000 | [diff] [blame^] | 132 | // We always construct a single, possibly unaligned, access for this case. |
| 133 | Info->setNumComponents(1); |
| 134 | CGBitFieldInfo::AccessInfo &AI = Info->getComponent(0); |
| 135 | AI.FieldIndex = 0; |
| 136 | AI.FieldByteOffset = 0; |
| 137 | AI.FieldBitStart = BitOffset; |
| 138 | AI.AccessWidth = CGF.CGM.getContext().getTypeSize(IvarTy); |
| 139 | AI.AccessAlignment = 0; |
| 140 | AI.TargetBitOffset = 0; |
| 141 | AI.TargetBitWidth = BitFieldSize; |
| 142 | |
Daniel Dunbar | 56229f5 | 2010-04-05 16:20:33 +0000 | [diff] [blame] | 143 | // FIXME: We need to set a very conservative alignment on this, or make sure |
| 144 | // that the runtime is doing the right thing. |
Daniel Dunbar | efbf487 | 2010-04-06 01:07:44 +0000 | [diff] [blame] | 145 | return LValue::MakeBitfield(V, *Info, Quals.getCVRQualifiers()); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /// |
| 149 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 150 | namespace { |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 151 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 152 | typedef std::vector<llvm::Constant*> ConstantVector; |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 153 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 154 | // FIXME: We should find a nicer way to make the labels for metadata, string |
| 155 | // concatenation is lame. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 156 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 157 | class ObjCCommonTypesHelper { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 158 | protected: |
| 159 | llvm::LLVMContext &VMContext; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 160 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 161 | private: |
| 162 | llvm::Constant *getMessageSendFn() const { |
| 163 | // id objc_msgSend (id, SEL, ...) |
| 164 | std::vector<const llvm::Type*> Params; |
| 165 | Params.push_back(ObjectPtrTy); |
| 166 | Params.push_back(SelectorPtrTy); |
| 167 | return |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 168 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 169 | Params, true), |
| 170 | "objc_msgSend"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 171 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 172 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 173 | llvm::Constant *getMessageSendStretFn() const { |
| 174 | // id objc_msgSend_stret (id, SEL, ...) |
| 175 | std::vector<const llvm::Type*> Params; |
| 176 | Params.push_back(ObjectPtrTy); |
| 177 | Params.push_back(SelectorPtrTy); |
| 178 | return |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 179 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 180 | Params, true), |
| 181 | "objc_msgSend_stret"); |
| 182 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 183 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 184 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 185 | llvm::Constant *getMessageSendFpretFn() const { |
| 186 | // FIXME: This should be long double on x86_64? |
| 187 | // [double | long double] objc_msgSend_fpret(id self, SEL op, ...) |
| 188 | std::vector<const llvm::Type*> Params; |
| 189 | Params.push_back(ObjectPtrTy); |
| 190 | Params.push_back(SelectorPtrTy); |
| 191 | return |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 192 | CGM.CreateRuntimeFunction(llvm::FunctionType::get( |
| 193 | llvm::Type::getDoubleTy(VMContext), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 194 | Params, |
| 195 | true), |
| 196 | "objc_msgSend_fpret"); |
| 197 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 198 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 199 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 200 | llvm::Constant *getMessageSendSuperFn() const { |
| 201 | // id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
| 202 | const char *SuperName = "objc_msgSendSuper"; |
| 203 | std::vector<const llvm::Type*> Params; |
| 204 | Params.push_back(SuperPtrTy); |
| 205 | Params.push_back(SelectorPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 206 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 207 | Params, true), |
| 208 | SuperName); |
| 209 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 210 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 211 | llvm::Constant *getMessageSendSuperFn2() const { |
| 212 | // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...) |
| 213 | const char *SuperName = "objc_msgSendSuper2"; |
| 214 | std::vector<const llvm::Type*> Params; |
| 215 | Params.push_back(SuperPtrTy); |
| 216 | Params.push_back(SelectorPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 217 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 218 | Params, true), |
| 219 | SuperName); |
| 220 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 221 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 222 | llvm::Constant *getMessageSendSuperStretFn() const { |
| 223 | // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super, |
| 224 | // SEL op, ...) |
| 225 | std::vector<const llvm::Type*> Params; |
| 226 | Params.push_back(Int8PtrTy); |
| 227 | Params.push_back(SuperPtrTy); |
| 228 | Params.push_back(SelectorPtrTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 229 | return CGM.CreateRuntimeFunction( |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 230 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 231 | Params, true), |
| 232 | "objc_msgSendSuper_stret"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 233 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 234 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 235 | llvm::Constant *getMessageSendSuperStretFn2() const { |
| 236 | // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super, |
| 237 | // SEL op, ...) |
| 238 | std::vector<const llvm::Type*> Params; |
| 239 | Params.push_back(Int8PtrTy); |
| 240 | Params.push_back(SuperPtrTy); |
| 241 | Params.push_back(SelectorPtrTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 242 | return CGM.CreateRuntimeFunction( |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 243 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 244 | Params, true), |
| 245 | "objc_msgSendSuper2_stret"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 246 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 247 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 248 | llvm::Constant *getMessageSendSuperFpretFn() const { |
| 249 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 250 | return getMessageSendSuperFn(); |
| 251 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 252 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 253 | llvm::Constant *getMessageSendSuperFpretFn2() const { |
| 254 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 255 | return getMessageSendSuperFn2(); |
| 256 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 257 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 258 | protected: |
| 259 | CodeGen::CodeGenModule &CGM; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 260 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 261 | public: |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 262 | const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 263 | const llvm::Type *Int8PtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 264 | |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 265 | /// ObjectPtrTy - LLVM type for object handles (typeof(id)) |
| 266 | const llvm::Type *ObjectPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 267 | |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 268 | /// PtrObjectPtrTy - LLVM type for id * |
| 269 | const llvm::Type *PtrObjectPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 270 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 271 | /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL)) |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 272 | const llvm::Type *SelectorPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 273 | /// ProtocolPtrTy - LLVM type for external protocol handles |
| 274 | /// (typeof(Protocol)) |
| 275 | const llvm::Type *ExternalProtocolPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 276 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 277 | // SuperCTy - clang type for struct objc_super. |
| 278 | QualType SuperCTy; |
| 279 | // SuperPtrCTy - clang type for struct objc_super *. |
| 280 | QualType SuperPtrCTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 281 | |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 282 | /// SuperTy - LLVM type for struct objc_super. |
| 283 | const llvm::StructType *SuperTy; |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 284 | /// SuperPtrTy - LLVM type for struct objc_super *. |
| 285 | const llvm::Type *SuperPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 286 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 287 | /// PropertyTy - LLVM type for struct objc_property (struct _prop_t |
| 288 | /// in GCC parlance). |
| 289 | const llvm::StructType *PropertyTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 290 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 291 | /// PropertyListTy - LLVM type for struct objc_property_list |
| 292 | /// (_prop_list_t in GCC parlance). |
| 293 | const llvm::StructType *PropertyListTy; |
| 294 | /// PropertyListPtrTy - LLVM type for struct objc_property_list*. |
| 295 | const llvm::Type *PropertyListPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 296 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 297 | // MethodTy - LLVM type for struct objc_method. |
| 298 | const llvm::StructType *MethodTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 299 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 300 | /// CacheTy - LLVM type for struct objc_cache. |
| 301 | const llvm::Type *CacheTy; |
| 302 | /// CachePtrTy - LLVM type for struct objc_cache *. |
| 303 | const llvm::Type *CachePtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 304 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 305 | llvm::Constant *getGetPropertyFn() { |
| 306 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 307 | ASTContext &Ctx = CGM.getContext(); |
| 308 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 309 | llvm::SmallVector<CanQualType,4> Params; |
| 310 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 311 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 312 | Params.push_back(IdType); |
| 313 | Params.push_back(SelType); |
| 314 | Params.push_back(Ctx.LongTy); |
| 315 | Params.push_back(Ctx.BoolTy); |
| 316 | const llvm::FunctionType *FTy = |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 317 | Types.GetFunctionType(Types.getFunctionInfo(IdType, Params, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 318 | FunctionType::ExtInfo()), |
| 319 | false); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 320 | return CGM.CreateRuntimeFunction(FTy, "objc_getProperty"); |
| 321 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 322 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 323 | llvm::Constant *getSetPropertyFn() { |
| 324 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 325 | ASTContext &Ctx = CGM.getContext(); |
| 326 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 327 | llvm::SmallVector<CanQualType,6> Params; |
| 328 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 329 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 330 | Params.push_back(IdType); |
| 331 | Params.push_back(SelType); |
| 332 | Params.push_back(Ctx.LongTy); |
| 333 | Params.push_back(IdType); |
| 334 | Params.push_back(Ctx.BoolTy); |
| 335 | Params.push_back(Ctx.BoolTy); |
| 336 | const llvm::FunctionType *FTy = |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 337 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 338 | FunctionType::ExtInfo()), |
| 339 | false); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 340 | return CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); |
| 341 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 342 | |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 343 | |
| 344 | llvm::Constant *getCopyStructFn() { |
| 345 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 346 | ASTContext &Ctx = CGM.getContext(); |
| 347 | // void objc_copyStruct (void *, const void *, size_t, bool, bool) |
| 348 | llvm::SmallVector<CanQualType,5> Params; |
| 349 | Params.push_back(Ctx.VoidPtrTy); |
| 350 | Params.push_back(Ctx.VoidPtrTy); |
| 351 | Params.push_back(Ctx.LongTy); |
| 352 | Params.push_back(Ctx.BoolTy); |
| 353 | Params.push_back(Ctx.BoolTy); |
| 354 | const llvm::FunctionType *FTy = |
| 355 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params, |
| 356 | FunctionType::ExtInfo()), |
| 357 | false); |
| 358 | return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct"); |
| 359 | } |
| 360 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 361 | llvm::Constant *getEnumerationMutationFn() { |
Daniel Dunbar | c1ab900 | 2009-07-11 20:32:50 +0000 | [diff] [blame] | 362 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 363 | ASTContext &Ctx = CGM.getContext(); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 364 | // void objc_enumerationMutation (id) |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 365 | llvm::SmallVector<CanQualType,1> Params; |
| 366 | Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType())); |
Daniel Dunbar | c1ab900 | 2009-07-11 20:32:50 +0000 | [diff] [blame] | 367 | const llvm::FunctionType *FTy = |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 368 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 369 | FunctionType::ExtInfo()), |
| 370 | false); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 371 | return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation"); |
| 372 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 373 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 374 | /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 375 | llvm::Constant *getGcReadWeakFn() { |
| 376 | // id objc_read_weak (id *) |
| 377 | std::vector<const llvm::Type*> Args; |
| 378 | Args.push_back(ObjectPtrTy->getPointerTo()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 379 | llvm::FunctionType *FTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 380 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 381 | return CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 384 | /// GcAssignWeakFn -- LLVM objc_assign_weak function. |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 385 | llvm::Constant *getGcAssignWeakFn() { |
| 386 | // id objc_assign_weak (id, id *) |
| 387 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 388 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 389 | llvm::FunctionType *FTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 390 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 391 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 392 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 393 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 394 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 395 | llvm::Constant *getGcAssignGlobalFn() { |
| 396 | // id objc_assign_global(id, id *) |
| 397 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 398 | Args.push_back(ObjectPtrTy->getPointerTo()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 399 | llvm::FunctionType *FTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 400 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 401 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 402 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 403 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 404 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 405 | llvm::Constant *getGcAssignIvarFn() { |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 406 | // id objc_assign_ivar(id, id *, ptrdiff_t) |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 407 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 408 | Args.push_back(ObjectPtrTy->getPointerTo()); |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 409 | Args.push_back(LongTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 410 | llvm::FunctionType *FTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 411 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 412 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 413 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 414 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 415 | /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function. |
| 416 | llvm::Constant *GcMemmoveCollectableFn() { |
| 417 | // void *objc_memmove_collectable(void *dst, const void *src, size_t size) |
| 418 | std::vector<const llvm::Type*> Args(1, Int8PtrTy); |
| 419 | Args.push_back(Int8PtrTy); |
| 420 | Args.push_back(LongTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 421 | llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 422 | return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable"); |
| 423 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 424 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 425 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 426 | llvm::Constant *getGcAssignStrongCastFn() { |
| 427 | // id objc_assign_global(id, id *) |
| 428 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 429 | Args.push_back(ObjectPtrTy->getPointerTo()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 430 | llvm::FunctionType *FTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 431 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 432 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); |
| 433 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 434 | |
| 435 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 436 | llvm::Constant *getExceptionThrowFn() { |
| 437 | // void objc_exception_throw(id) |
| 438 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 439 | llvm::FunctionType *FTy = |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 440 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 441 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
| 442 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 443 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 444 | /// SyncEnterFn - LLVM object_sync_enter function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 445 | llvm::Constant *getSyncEnterFn() { |
| 446 | // void objc_sync_enter (id) |
| 447 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 448 | llvm::FunctionType *FTy = |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 449 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 450 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 451 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 452 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 453 | /// SyncExitFn - LLVM object_sync_exit function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 454 | llvm::Constant *getSyncExitFn() { |
| 455 | // void objc_sync_exit (id) |
| 456 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 457 | llvm::FunctionType *FTy = |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 458 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 459 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
| 460 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 461 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 462 | llvm::Constant *getSendFn(bool IsSuper) const { |
| 463 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFn(); |
| 464 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 465 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 466 | llvm::Constant *getSendFn2(bool IsSuper) const { |
| 467 | return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn(); |
| 468 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 469 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 470 | llvm::Constant *getSendStretFn(bool IsSuper) const { |
| 471 | return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn(); |
| 472 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 473 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 474 | llvm::Constant *getSendStretFn2(bool IsSuper) const { |
| 475 | return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn(); |
| 476 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 477 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 478 | llvm::Constant *getSendFpretFn(bool IsSuper) const { |
| 479 | return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn(); |
| 480 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 481 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 482 | llvm::Constant *getSendFpretFn2(bool IsSuper) const { |
| 483 | return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn(); |
| 484 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 485 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 486 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
| 487 | ~ObjCCommonTypesHelper(){} |
| 488 | }; |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 489 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 490 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 491 | /// construction of varies types used during ObjC generation. |
| 492 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 493 | public: |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 494 | /// SymtabTy - LLVM type for struct objc_symtab. |
| 495 | const llvm::StructType *SymtabTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 496 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
| 497 | const llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 498 | /// ModuleTy - LLVM type for struct objc_module. |
| 499 | const llvm::StructType *ModuleTy; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 500 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 501 | /// ProtocolTy - LLVM type for struct objc_protocol. |
| 502 | const llvm::StructType *ProtocolTy; |
| 503 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
| 504 | const llvm::Type *ProtocolPtrTy; |
| 505 | /// ProtocolExtensionTy - LLVM type for struct |
| 506 | /// objc_protocol_extension. |
| 507 | const llvm::StructType *ProtocolExtensionTy; |
| 508 | /// ProtocolExtensionTy - LLVM type for struct |
| 509 | /// objc_protocol_extension *. |
| 510 | const llvm::Type *ProtocolExtensionPtrTy; |
| 511 | /// MethodDescriptionTy - LLVM type for struct |
| 512 | /// objc_method_description. |
| 513 | const llvm::StructType *MethodDescriptionTy; |
| 514 | /// MethodDescriptionListTy - LLVM type for struct |
| 515 | /// objc_method_description_list. |
| 516 | const llvm::StructType *MethodDescriptionListTy; |
| 517 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 518 | /// objc_method_description_list *. |
| 519 | const llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 520 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
| 521 | const llvm::Type *ProtocolListTy; |
| 522 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
| 523 | const llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 524 | /// CategoryTy - LLVM type for struct objc_category. |
| 525 | const llvm::StructType *CategoryTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 526 | /// ClassTy - LLVM type for struct objc_class. |
| 527 | const llvm::StructType *ClassTy; |
| 528 | /// ClassPtrTy - LLVM type for struct objc_class *. |
| 529 | const llvm::Type *ClassPtrTy; |
| 530 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
| 531 | const llvm::StructType *ClassExtensionTy; |
| 532 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
| 533 | const llvm::Type *ClassExtensionPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 534 | // IvarTy - LLVM type for struct objc_ivar. |
| 535 | const llvm::StructType *IvarTy; |
| 536 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
| 537 | const llvm::Type *IvarListTy; |
| 538 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
| 539 | const llvm::Type *IvarListPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 540 | /// MethodListTy - LLVM type for struct objc_method_list. |
| 541 | const llvm::Type *MethodListTy; |
| 542 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
| 543 | const llvm::Type *MethodListPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 544 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 545 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
| 546 | const llvm::Type *ExceptionDataTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 547 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 548 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 549 | llvm::Constant *getExceptionTryEnterFn() { |
| 550 | std::vector<const llvm::Type*> Params; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 551 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 552 | return CGM.CreateRuntimeFunction( |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 553 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 554 | Params, false), |
| 555 | "objc_exception_try_enter"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 556 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 557 | |
| 558 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 559 | llvm::Constant *getExceptionTryExitFn() { |
| 560 | std::vector<const llvm::Type*> Params; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 561 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 562 | return CGM.CreateRuntimeFunction( |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 563 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 564 | Params, false), |
| 565 | "objc_exception_try_exit"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 566 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 567 | |
| 568 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 569 | llvm::Constant *getExceptionExtractFn() { |
| 570 | std::vector<const llvm::Type*> Params; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 571 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 572 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 573 | Params, false), |
| 574 | "objc_exception_extract"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 575 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 576 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 577 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 578 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 579 | llvm::Constant *getExceptionMatchFn() { |
| 580 | std::vector<const llvm::Type*> Params; |
| 581 | Params.push_back(ClassPtrTy); |
| 582 | Params.push_back(ObjectPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 583 | return CGM.CreateRuntimeFunction( |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 584 | llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 585 | Params, false), |
| 586 | "objc_exception_match"); |
| 587 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 588 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 589 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 590 | /// SetJmpFn - LLVM _setjmp function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 591 | llvm::Constant *getSetJmpFn() { |
| 592 | std::vector<const llvm::Type*> Params; |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 593 | Params.push_back(llvm::Type::getInt32PtrTy(VMContext)); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 594 | return |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 595 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext), |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 596 | Params, false), |
| 597 | "_setjmp"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 598 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 599 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 600 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 601 | public: |
| 602 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 603 | ~ObjCTypesHelper() {} |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 604 | }; |
| 605 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 606 | /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 607 | /// modern abi |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 608 | class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 609 | public: |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 610 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 611 | // MethodListnfABITy - LLVM for struct _method_list_t |
| 612 | const llvm::StructType *MethodListnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 613 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 614 | // MethodListnfABIPtrTy - LLVM for struct _method_list_t* |
| 615 | const llvm::Type *MethodListnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 616 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 617 | // ProtocolnfABITy = LLVM for struct _protocol_t |
| 618 | const llvm::StructType *ProtocolnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 619 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 620 | // ProtocolnfABIPtrTy = LLVM for struct _protocol_t* |
| 621 | const llvm::Type *ProtocolnfABIPtrTy; |
| 622 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 623 | // ProtocolListnfABITy - LLVM for struct _objc_protocol_list |
| 624 | const llvm::StructType *ProtocolListnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 625 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 626 | // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list* |
| 627 | const llvm::Type *ProtocolListnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 628 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 629 | // ClassnfABITy - LLVM for struct _class_t |
| 630 | const llvm::StructType *ClassnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 631 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 632 | // ClassnfABIPtrTy - LLVM for struct _class_t* |
| 633 | const llvm::Type *ClassnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 634 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 635 | // IvarnfABITy - LLVM for struct _ivar_t |
| 636 | const llvm::StructType *IvarnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 637 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 638 | // IvarListnfABITy - LLVM for struct _ivar_list_t |
| 639 | const llvm::StructType *IvarListnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 640 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 641 | // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t* |
| 642 | const llvm::Type *IvarListnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 643 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 644 | // ClassRonfABITy - LLVM for struct _class_ro_t |
| 645 | const llvm::StructType *ClassRonfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 646 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 647 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 648 | const llvm::Type *ImpnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 649 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 650 | // CategorynfABITy - LLVM for struct _category_t |
| 651 | const llvm::StructType *CategorynfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 652 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 653 | // New types for nonfragile abi messaging. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 654 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 655 | // MessageRefTy - LLVM for: |
| 656 | // struct _message_ref_t { |
| 657 | // IMP messenger; |
| 658 | // SEL name; |
| 659 | // }; |
| 660 | const llvm::StructType *MessageRefTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 661 | // MessageRefCTy - clang type for struct _message_ref_t |
| 662 | QualType MessageRefCTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 663 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 664 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 665 | const llvm::Type *MessageRefPtrTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 666 | // MessageRefCPtrTy - clang type for struct _message_ref_t* |
| 667 | QualType MessageRefCPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 668 | |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 669 | // MessengerTy - Type of the messenger (shown as IMP above) |
| 670 | const llvm::FunctionType *MessengerTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 671 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 672 | // SuperMessageRefTy - LLVM for: |
| 673 | // struct _super_message_ref_t { |
| 674 | // SUPER_IMP messenger; |
| 675 | // SEL name; |
| 676 | // }; |
| 677 | const llvm::StructType *SuperMessageRefTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 678 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 679 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 680 | const llvm::Type *SuperMessageRefPtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 681 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 682 | llvm::Constant *getMessageSendFixupFn() { |
| 683 | // id objc_msgSend_fixup(id, struct message_ref_t*, ...) |
| 684 | std::vector<const llvm::Type*> Params; |
| 685 | Params.push_back(ObjectPtrTy); |
| 686 | Params.push_back(MessageRefPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 687 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 688 | Params, true), |
| 689 | "objc_msgSend_fixup"); |
| 690 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 691 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 692 | llvm::Constant *getMessageSendFpretFixupFn() { |
| 693 | // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...) |
| 694 | std::vector<const llvm::Type*> Params; |
| 695 | Params.push_back(ObjectPtrTy); |
| 696 | Params.push_back(MessageRefPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 697 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 698 | Params, true), |
| 699 | "objc_msgSend_fpret_fixup"); |
| 700 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 701 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 702 | llvm::Constant *getMessageSendStretFixupFn() { |
| 703 | // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...) |
| 704 | std::vector<const llvm::Type*> Params; |
| 705 | Params.push_back(ObjectPtrTy); |
| 706 | Params.push_back(MessageRefPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 707 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 708 | Params, true), |
| 709 | "objc_msgSend_stret_fixup"); |
| 710 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 711 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 712 | llvm::Constant *getMessageSendIdFixupFn() { |
| 713 | // id objc_msgSendId_fixup(id, struct message_ref_t*, ...) |
| 714 | std::vector<const llvm::Type*> Params; |
| 715 | Params.push_back(ObjectPtrTy); |
| 716 | Params.push_back(MessageRefPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 717 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 718 | Params, true), |
| 719 | "objc_msgSendId_fixup"); |
| 720 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 721 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 722 | llvm::Constant *getMessageSendIdStretFixupFn() { |
| 723 | // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...) |
| 724 | std::vector<const llvm::Type*> Params; |
| 725 | Params.push_back(ObjectPtrTy); |
| 726 | Params.push_back(MessageRefPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 727 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 728 | Params, true), |
| 729 | "objc_msgSendId_stret_fixup"); |
| 730 | } |
| 731 | llvm::Constant *getMessageSendSuper2FixupFn() { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 732 | // id objc_msgSendSuper2_fixup (struct objc_super *, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 733 | // struct _super_message_ref_t*, ...) |
| 734 | std::vector<const llvm::Type*> Params; |
| 735 | Params.push_back(SuperPtrTy); |
| 736 | Params.push_back(SuperMessageRefPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 737 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 738 | Params, true), |
| 739 | "objc_msgSendSuper2_fixup"); |
| 740 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 741 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 742 | llvm::Constant *getMessageSendSuper2StretFixupFn() { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 743 | // id objc_msgSendSuper2_stret_fixup(struct objc_super *, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 744 | // struct _super_message_ref_t*, ...) |
| 745 | std::vector<const llvm::Type*> Params; |
| 746 | Params.push_back(SuperPtrTy); |
| 747 | Params.push_back(SuperMessageRefPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 748 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 749 | Params, true), |
| 750 | "objc_msgSendSuper2_stret_fixup"); |
| 751 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 752 | |
| 753 | |
| 754 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 755 | /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C |
| 756 | /// exception personality function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 757 | llvm::Value *getEHPersonalityPtr() { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 758 | llvm::Constant *Personality = |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 759 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext), |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 760 | true), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 761 | "__objc_personality_v0"); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 762 | return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 763 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 764 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 765 | llvm::Constant *getUnwindResumeOrRethrowFn() { |
| 766 | std::vector<const llvm::Type*> Params; |
| 767 | Params.push_back(Int8PtrTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 768 | return CGM.CreateRuntimeFunction( |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 769 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 770 | Params, false), |
Daniel Dunbar | b2987d1 | 2010-02-10 18:49:11 +0000 | [diff] [blame] | 771 | (CGM.getLangOptions().SjLjExceptions ? "_Unwind_SjLj_Resume" : |
| 772 | "_Unwind_Resume_or_Rethrow")); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 773 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 774 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 775 | llvm::Constant *getObjCEndCatchFn() { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 776 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Chris Lattner | b59761b | 2009-07-01 04:13:52 +0000 | [diff] [blame] | 777 | false), |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 778 | "objc_end_catch"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 779 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 780 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 781 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 782 | llvm::Constant *getObjCBeginCatchFn() { |
| 783 | std::vector<const llvm::Type*> Params; |
| 784 | Params.push_back(Int8PtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 785 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy, |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 786 | Params, false), |
| 787 | "objc_begin_catch"); |
| 788 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 789 | |
| 790 | const llvm::StructType *EHTypeTy; |
| 791 | const llvm::Type *EHTypePtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 792 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 793 | ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); |
| 794 | ~ObjCNonFragileABITypesHelper(){} |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 795 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 796 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 797 | class CGObjCCommonMac : public CodeGen::CGObjCRuntime { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 798 | public: |
| 799 | // FIXME - accessibility |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 800 | class GC_IVAR { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 801 | public: |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 802 | unsigned ivar_bytepos; |
| 803 | unsigned ivar_size; |
| 804 | GC_IVAR(unsigned bytepos = 0, unsigned size = 0) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 805 | : ivar_bytepos(bytepos), ivar_size(size) {} |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 806 | |
| 807 | // Allow sorting based on byte pos. |
| 808 | bool operator<(const GC_IVAR &b) const { |
| 809 | return ivar_bytepos < b.ivar_bytepos; |
| 810 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 811 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 812 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 813 | class SKIP_SCAN { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 814 | public: |
| 815 | unsigned skip; |
| 816 | unsigned scan; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 817 | SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0) |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 818 | : skip(_skip), scan(_scan) {} |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 819 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 820 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 821 | protected: |
| 822 | CodeGen::CodeGenModule &CGM; |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 823 | llvm::LLVMContext &VMContext; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 824 | // FIXME! May not be needing this after all. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 825 | unsigned ObjCABI; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 826 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 827 | // gc ivar layout bitmap calculation helper caches. |
| 828 | llvm::SmallVector<GC_IVAR, 16> SkipIvars; |
| 829 | llvm::SmallVector<GC_IVAR, 16> IvarsInfo; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 830 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 831 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 832 | /// DefinedSymbols and FinishModule(). |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 833 | llvm::SetVector<IdentifierInfo*> LazySymbols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 834 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 835 | /// DefinedSymbols - External symbols which are defined by this |
| 836 | /// module. The symbols in this list and LazySymbols are used to add |
| 837 | /// special linker symbols which ensure that Objective-C modules are |
| 838 | /// linked properly. |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 839 | llvm::SetVector<IdentifierInfo*> DefinedSymbols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 840 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 841 | /// ClassNames - uniqued class names. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 842 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 843 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 844 | /// MethodVarNames - uniqued method variable names. |
| 845 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 846 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 847 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 848 | /// a StringMap here because have no other unique reference. |
| 849 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 850 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 851 | /// MethodDefinitions - map of methods which have been defined in |
| 852 | /// this translation unit. |
| 853 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 854 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 855 | /// PropertyNames - uniqued method variable names. |
| 856 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 857 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 858 | /// ClassReferences - uniqued class references. |
| 859 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 860 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 861 | /// SelectorReferences - uniqued selector references. |
| 862 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 863 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 864 | /// Protocols - Protocols for which an objc_protocol structure has |
| 865 | /// been emitted. Forward declarations are handled by creating an |
| 866 | /// empty structure whose initializer is filled in when/if defined. |
| 867 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 868 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 869 | /// DefinedProtocols - Protocols which have actually been |
| 870 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 871 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 872 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 873 | /// DefinedClasses - List of defined classes. |
| 874 | std::vector<llvm::GlobalValue*> DefinedClasses; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 875 | |
| 876 | /// DefinedNonLazyClasses - List of defined "non-lazy" classes. |
| 877 | std::vector<llvm::GlobalValue*> DefinedNonLazyClasses; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 878 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 879 | /// DefinedCategories - List of defined categories. |
| 880 | std::vector<llvm::GlobalValue*> DefinedCategories; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 881 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 882 | /// DefinedNonLazyCategories - List of defined "non-lazy" categories. |
| 883 | std::vector<llvm::GlobalValue*> DefinedNonLazyCategories; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 884 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 885 | /// GetNameForMethod - Return a name for the given method. |
| 886 | /// \param[out] NameOut - The return value. |
| 887 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 888 | const ObjCContainerDecl *CD, |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 889 | llvm::SmallVectorImpl<char> &NameOut); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 890 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 891 | /// GetMethodVarName - Return a unique constant for the given |
| 892 | /// selector's name. The return value has type char *. |
| 893 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 894 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
| 895 | llvm::Constant *GetMethodVarName(const std::string &Name); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 896 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 897 | /// GetMethodVarType - Return a unique constant for the given |
| 898 | /// selector's name. The return value has type char *. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 899 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 900 | // FIXME: This is a horrible name. |
| 901 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 902 | llvm::Constant *GetMethodVarType(const FieldDecl *D); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 903 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 904 | /// GetPropertyName - Return a unique constant for the given |
| 905 | /// name. The return value has type char *. |
| 906 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 907 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 908 | // FIXME: This can be dropped once string functions are unified. |
| 909 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 910 | const Decl *Container); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 911 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 912 | /// GetClassName - Return a unique constant for the given selector's |
| 913 | /// name. The return value has type char *. |
| 914 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 915 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 916 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 917 | /// implementation for the __strong or __weak case. |
| 918 | /// |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 919 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
| 920 | bool ForStrongLayout); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 921 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 922 | void BuildAggrIvarRecordLayout(const RecordType *RT, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 923 | unsigned int BytePos, bool ForStrongLayout, |
| 924 | bool &HasUnion); |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 925 | void BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 926 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 927 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 928 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 929 | unsigned int BytePos, bool ForStrongLayout, |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 930 | bool &HasUnion); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 931 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 932 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 933 | /// ivar layout bitmap. |
| 934 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 935 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 936 | |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 937 | /// EmitPropertyList - Emit the given property list. The return |
| 938 | /// value has type PropertyListPtrTy. |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 939 | llvm::Constant *EmitPropertyList(llvm::Twine Name, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 940 | const Decl *Container, |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 941 | const ObjCContainerDecl *OCD, |
| 942 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 943 | |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 944 | /// PushProtocolProperties - Push protocol's property on the input stack. |
| 945 | void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet, |
| 946 | std::vector<llvm::Constant*> &Properties, |
| 947 | const Decl *Container, |
| 948 | const ObjCProtocolDecl *PROTO, |
| 949 | const ObjCCommonTypesHelper &ObjCTypes); |
| 950 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 951 | /// GetProtocolRef - Return a reference to the internal protocol |
| 952 | /// description, creating an empty one if it has not been |
| 953 | /// defined. The return value has type ProtocolPtrTy. |
| 954 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 955 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 956 | /// CreateMetadataVar - Create a global variable with internal |
| 957 | /// linkage for use by the Objective-C runtime. |
| 958 | /// |
| 959 | /// This is a convenience wrapper which not only creates the |
| 960 | /// variable, but also sets the section and alignment and adds the |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 961 | /// global to the "llvm.used" list. |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 962 | /// |
| 963 | /// \param Name - The variable name. |
| 964 | /// \param Init - The variable initializer; this is also used to |
| 965 | /// define the type of the variable. |
| 966 | /// \param Section - The section the variable should go into, or 0. |
| 967 | /// \param Align - The alignment for the variable, or 0. |
| 968 | /// \param AddToUsed - Whether the variable should be added to |
Daniel Dunbar | c158306 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 969 | /// "llvm.used". |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 970 | llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name, |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 971 | llvm::Constant *Init, |
| 972 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 973 | unsigned Align, |
| 974 | bool AddToUsed); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 975 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 976 | CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF, |
| 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 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 986 | public: |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 987 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 988 | CGM(cgm), VMContext(cgm.getLLVMContext()) { } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 989 | |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 990 | virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 991 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 992 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 993 | const ObjCContainerDecl *CD=0); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 994 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 995 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 996 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 997 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 998 | /// declaration, emitting it if necessary. The return value has type |
| 999 | /// ProtocolPtrTy. |
| 1000 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1001 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1002 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1003 | /// object for the given declaration, emitting it if needed. These |
| 1004 | /// forward references will be filled in with empty bodies if no |
| 1005 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1006 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1007 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1008 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1009 | class CGObjCMac : public CGObjCCommonMac { |
| 1010 | private: |
| 1011 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1012 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 1013 | /// level information. |
| 1014 | void EmitImageInfo(); |
| 1015 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1016 | /// EmitModuleInfo - Another marker encoding module level |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1017 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1018 | void EmitModuleInfo(); |
| 1019 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1020 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 1021 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1022 | llvm::Constant *EmitModuleSymbols(); |
| 1023 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1024 | /// FinishModule - Write out global data structures at the end of |
| 1025 | /// processing a translation unit. |
| 1026 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1027 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1028 | /// EmitClassExtension - Generate the class extension structure used |
| 1029 | /// to store the weak ivar layout and properties. The return value |
| 1030 | /// has type ClassExtensionPtrTy. |
| 1031 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 1032 | |
| 1033 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1034 | /// for the given class. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1035 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1036 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 1037 | |
| 1038 | /// EmitSuperClassRef - Emits reference to class's main metadata class. |
| 1039 | llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1040 | |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1041 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1042 | QualType ResultType, |
| 1043 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1044 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1045 | QualType Arg0Ty, |
| 1046 | bool IsSuper, |
| 1047 | const CallArgList &CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1048 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1049 | /// EmitIvarList - Emit the ivar list for the given |
| 1050 | /// implementation. If ForClass is true the list of class ivars |
| 1051 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1052 | /// interface ivars will be emitted. The return value has type |
| 1053 | /// IvarListPtrTy. |
| 1054 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1055 | bool ForClass); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1056 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1057 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 1058 | /// for the metaclass of the given interface. The return value has |
| 1059 | /// type ClassPtrTy. |
| 1060 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 1061 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1062 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1063 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1064 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1065 | llvm::Constant *Protocols, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1066 | const ConstantVector &Methods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1067 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1068 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
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 *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1071 | |
| 1072 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1073 | /// implementation. The return value has type MethodListPtrTy. |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1074 | llvm::Constant *EmitMethodList(llvm::Twine Name, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1075 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1076 | const ConstantVector &Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1077 | |
| 1078 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1079 | /// method declarations. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1080 | /// - TypeName: The name for the type containing the methods. |
| 1081 | /// - IsProtocol: True iff these methods are for a protocol. |
| 1082 | /// - ClassMethds: True iff these are class methods. |
| 1083 | /// - Required: When true, only "required" methods are |
| 1084 | /// listed. Similarly, when false only "optional" methods are |
| 1085 | /// listed. For classes this should always be true. |
| 1086 | /// - begin, end: The method list to output. |
| 1087 | /// |
| 1088 | /// The return value has type MethodDescriptionListPtrTy. |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1089 | llvm::Constant *EmitMethodDescList(llvm::Twine Name, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1090 | const char *Section, |
| 1091 | const ConstantVector &Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1092 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1093 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1094 | /// declaration, emitting it if necessary. The return value has type |
| 1095 | /// ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1096 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1097 | |
| 1098 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1099 | /// object for the given declaration, emitting it if needed. These |
| 1100 | /// forward references will be filled in with empty bodies if no |
| 1101 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1102 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1103 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1104 | /// EmitProtocolExtension - Generate the protocol extension |
| 1105 | /// structure used to store optional instance and class methods, and |
| 1106 | /// protocol properties. The return value has type |
| 1107 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1108 | llvm::Constant * |
| 1109 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1110 | const ConstantVector &OptInstanceMethods, |
| 1111 | const ConstantVector &OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1112 | |
| 1113 | /// EmitProtocolList - Generate the list of referenced |
| 1114 | /// protocols. The return value has type ProtocolListPtrTy. |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1115 | llvm::Constant *EmitProtocolList(llvm::Twine Name, |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1116 | ObjCProtocolDecl::protocol_iterator begin, |
| 1117 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1118 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1119 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1120 | /// for the given selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1121 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1122 | |
| 1123 | public: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1124 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1125 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1126 | virtual llvm::Function *ModuleInitFunction(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1127 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1128 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1129 | QualType ResultType, |
| 1130 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1131 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1132 | bool IsClassMessage, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1133 | const CallArgList &CallArgs, |
| 1134 | const ObjCMethodDecl *Method); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1135 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1136 | virtual CodeGen::RValue |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1137 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1138 | QualType ResultType, |
| 1139 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1140 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1141 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1142 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1143 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1144 | const CallArgList &CallArgs, |
| 1145 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1146 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1147 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1148 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1149 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1150 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel); |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1151 | |
| 1152 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1153 | /// untyped one. |
| 1154 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1155 | const ObjCMethodDecl *Method); |
| 1156 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1157 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1158 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1159 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1160 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1161 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1162 | const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1163 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1164 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 1165 | virtual llvm::Constant *GetPropertySetFunction(); |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1166 | virtual llvm::Constant *GetCopyStructFunction(); |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1167 | virtual llvm::Constant *EnumerationMutationFunction(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1168 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 1169 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1170 | const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1171 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 1172 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1173 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1174 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1175 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1176 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1177 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 1178 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 1179 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1180 | llvm::Value *src, llvm::Value *dest, |
| 1181 | llvm::Value *ivarOffset); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1182 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 1183 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1184 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1185 | llvm::Value *dest, llvm::Value *src, |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 1186 | QualType Ty); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1187 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1188 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1189 | QualType ObjectTy, |
| 1190 | llvm::Value *BaseValue, |
| 1191 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1192 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1193 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1194 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1195 | const ObjCIvarDecl *Ivar); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1196 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1197 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1198 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1199 | private: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1200 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1201 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 1202 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1203 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1204 | /// SuperClassReferences - uniqued super class references. |
| 1205 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1206 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1207 | /// MetaClassReferences - uniqued meta class references. |
| 1208 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1209 | |
| 1210 | /// EHTypeReferences - uniqued class ehtype references. |
| 1211 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1212 | |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1213 | /// NonLegacyDispatchMethods - List of methods for which we do *not* generate |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1214 | /// legacy messaging dispatch. |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1215 | llvm::DenseSet<Selector> NonLegacyDispatchMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1216 | |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 1217 | /// DefinedMetaClasses - List of defined meta-classes. |
| 1218 | std::vector<llvm::GlobalValue*> DefinedMetaClasses; |
| 1219 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1220 | /// LegacyDispatchedSelector - Returns true if SEL is not in the list of |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1221 | /// NonLegacyDispatchMethods; false otherwise. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1222 | bool LegacyDispatchedSelector(Selector Sel); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1223 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1224 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 1225 | /// processing a translation unit. |
| 1226 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1227 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 1228 | /// AddModuleClassList - Add the given list of class pointers to the |
| 1229 | /// module with the provided symbol and section names. |
| 1230 | void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container, |
| 1231 | const char *SymbolName, |
| 1232 | const char *SectionName); |
| 1233 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1234 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 1235 | unsigned InstanceStart, |
| 1236 | unsigned InstanceSize, |
| 1237 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1238 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1239 | llvm::Constant *IsAGV, |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1240 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 1241 | llvm::Constant *ClassRoGV, |
| 1242 | bool HiddenVisibility); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1243 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1244 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1245 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 1246 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
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 | /// EmitMethodList - Emit the method list for the given |
| 1249 | /// implementation. The return value has type MethodListnfABITy. |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1250 | llvm::Constant *EmitMethodList(llvm::Twine Name, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1251 | const char *Section, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 1252 | const ConstantVector &Methods); |
| 1253 | /// EmitIvarList - Emit the ivar list for the given |
| 1254 | /// implementation. If ForClass is true the list of class ivars |
| 1255 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1256 | /// interface ivars will be emitted. The return value has type |
| 1257 | /// IvarListnfABIPtrTy. |
| 1258 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1259 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1260 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 1261 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 1262 | unsigned long int offset); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1263 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1264 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1265 | /// declaration, emitting it if necessary. The return value has type |
| 1266 | /// ProtocolPtrTy. |
| 1267 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1268 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1269 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1270 | /// object for the given declaration, emitting it if needed. These |
| 1271 | /// forward references will be filled in with empty bodies if no |
| 1272 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1273 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1274 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1275 | /// EmitProtocolList - Generate the list of referenced |
| 1276 | /// protocols. The return value has type ProtocolListPtrTy. |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1277 | llvm::Constant *EmitProtocolList(llvm::Twine Name, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1278 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1279 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1280 | |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1281 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1282 | QualType ResultType, |
| 1283 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 1284 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1285 | QualType Arg0Ty, |
| 1286 | bool IsSuper, |
| 1287 | const CallArgList &CallArgs); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 1288 | |
| 1289 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1290 | /// class of the given name. |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 1291 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1292 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1293 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1294 | /// for the given class reference. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1295 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1296 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1297 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1298 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1299 | /// for the given super class reference. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1300 | llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder, |
| 1301 | const ObjCInterfaceDecl *ID); |
| 1302 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1303 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 1304 | /// meta-data |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1305 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1306 | const ObjCInterfaceDecl *ID); |
| 1307 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1308 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 1309 | /// the given ivar. |
| 1310 | /// |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 1311 | llvm::GlobalVariable * ObjCIvarOffsetVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1312 | const ObjCInterfaceDecl *ID, |
| 1313 | const ObjCIvarDecl *Ivar); |
| 1314 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1315 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1316 | /// for the given selector. |
| 1317 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1318 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1319 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1320 | /// interface. The return value has type EHTypePtrTy. |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1321 | llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 1322 | bool ForDefinition); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1323 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1324 | const char *getMetaclassSymbolPrefix() const { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1325 | return "OBJC_METACLASS_$_"; |
| 1326 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1327 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1328 | const char *getClassSymbolPrefix() const { |
| 1329 | return "OBJC_CLASS_$_"; |
| 1330 | } |
| 1331 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 1332 | void GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1333 | uint32_t &InstanceStart, |
| 1334 | uint32_t &InstanceSize); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1335 | |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1336 | // Shamelessly stolen from Analysis/CFRefCount.cpp |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1337 | Selector GetNullarySelector(const char* name) const { |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1338 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1339 | return CGM.getContext().Selectors.getSelector(0, &II); |
| 1340 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1341 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1342 | Selector GetUnarySelector(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(1, &II); |
| 1345 | } |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1346 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1347 | /// ImplementationIsNonLazy - Check whether the given category or |
| 1348 | /// class implementation is "non-lazy". |
Fariborz Jahanian | ecfbdcb | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 1349 | bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1350 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1351 | public: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1352 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1353 | // FIXME. All stubs for now! |
| 1354 | virtual llvm::Function *ModuleInitFunction(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1355 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1356 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1357 | QualType ResultType, |
| 1358 | Selector Sel, |
| 1359 | llvm::Value *Receiver, |
| 1360 | bool IsClassMessage, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1361 | const CallArgList &CallArgs, |
| 1362 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1363 | |
| 1364 | virtual CodeGen::RValue |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1365 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 1366 | QualType ResultType, |
| 1367 | Selector Sel, |
| 1368 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1369 | bool isCategoryImpl, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1370 | llvm::Value *Receiver, |
| 1371 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1372 | const CallArgList &CallArgs, |
| 1373 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1374 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1375 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1376 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1377 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1378 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel) |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1379 | { return EmitSelector(Builder, Sel); } |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1380 | |
| 1381 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1382 | /// untyped one. |
| 1383 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1384 | const ObjCMethodDecl *Method) |
| 1385 | { return EmitSelector(Builder, Method->getSelector()); } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1386 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 1387 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1388 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1389 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1390 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 1391 | const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1392 | |
| 1393 | virtual llvm::Constant *GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1394 | return ObjCTypes.getGetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1395 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1396 | virtual llvm::Constant *GetPropertySetFunction() { |
| 1397 | return ObjCTypes.getSetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1398 | } |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1399 | |
| 1400 | virtual llvm::Constant *GetCopyStructFunction() { |
| 1401 | return ObjCTypes.getCopyStructFn(); |
| 1402 | } |
| 1403 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1404 | virtual llvm::Constant *EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1405 | return ObjCTypes.getEnumerationMutationFn(); |
Daniel Dunbar | 28ed084 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 1406 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1407 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1408 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 1409 | const Stmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1410 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 1411 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1412 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1413 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1414 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1415 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1416 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1417 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1418 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1419 | llvm::Value *src, llvm::Value *dest, |
| 1420 | llvm::Value *ivarOffset); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1421 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1422 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1423 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1424 | llvm::Value *dest, llvm::Value *src, |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 1425 | QualType Ty); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1426 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1427 | QualType ObjectTy, |
| 1428 | llvm::Value *BaseValue, |
| 1429 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1430 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1431 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1432 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1433 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1434 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1435 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1436 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1437 | |
| 1438 | /* *** Helper Functions *** */ |
| 1439 | |
| 1440 | /// getConstantGEP() - Help routine to construct simple GEPs. |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1441 | static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1442 | llvm::Constant *C, |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1443 | unsigned idx0, |
| 1444 | unsigned idx1) { |
| 1445 | llvm::Value *Idxs[] = { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1446 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0), |
| 1447 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1448 | }; |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1449 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1452 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 1453 | /// class has the __objc_exception__ attribute. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1454 | static bool hasObjCExceptionAttribute(ASTContext &Context, |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1455 | const ObjCInterfaceDecl *OID) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1456 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1457 | return true; |
| 1458 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1459 | return hasObjCExceptionAttribute(Context, Super); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1460 | return false; |
| 1461 | } |
| 1462 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1463 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1464 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1465 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1466 | ObjCTypes(cgm) { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1467 | ObjCABI = 1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1468 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1471 | /// GetClass - Return a reference to the class for the given interface |
| 1472 | /// decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1473 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1474 | const ObjCInterfaceDecl *ID) { |
| 1475 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1479 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 1480 | return EmitSelector(Builder, Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1481 | } |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1482 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1483 | *Method) { |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1484 | return EmitSelector(Builder, Method->getSelector()); |
| 1485 | } |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1486 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1487 | /// Generate a constant CFString object. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1488 | /* |
| 1489 | struct __builtin_CFString { |
| 1490 | const int *isa; // point to __CFConstantStringClassReference |
| 1491 | int flags; |
| 1492 | const char *str; |
| 1493 | long length; |
| 1494 | }; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1495 | */ |
| 1496 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1497 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 1498 | const StringLiteral *SL) { |
| 1499 | return CGM.GetAddrOfConstantCFString(SL); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | /// Generates a message send where the super is the receiver. This is |
| 1503 | /// a message send to self with special delivery semantics indicating |
| 1504 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1505 | CodeGen::RValue |
| 1506 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1507 | QualType ResultType, |
| 1508 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1509 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1510 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1511 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1512 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1513 | const CodeGen::CallArgList &CallArgs, |
| 1514 | const ObjCMethodDecl *Method) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1515 | // Create and init a super structure; this is a (receiver, class) |
| 1516 | // pair we will pass to objc_msgSendSuper. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1517 | llvm::Value *ObjCSuper = |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1518 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1519 | llvm::Value *ReceiverAsObject = |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1520 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1521 | CGF.Builder.CreateStore(ReceiverAsObject, |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1522 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1523 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1524 | // If this is a class message the metaclass is passed as the target. |
| 1525 | llvm::Value *Target; |
| 1526 | if (IsClassMessage) { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1527 | if (isCategoryImpl) { |
| 1528 | // Message sent to 'super' in a class method defined in a category |
| 1529 | // implementation requires an odd treatment. |
| 1530 | // If we are in a class method, we must retrieve the |
| 1531 | // _metaclass_ for the current class, pointed at by |
| 1532 | // the class's "isa" pointer. The following assumes that |
| 1533 | // isa" is the first ivar in a class (which it must be). |
| 1534 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1535 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 1536 | Target = CGF.Builder.CreateLoad(Target); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 1537 | } else { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1538 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 1539 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 1540 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 1541 | Target = Super; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1542 | } |
Fariborz Jahanian | 182f268 | 2009-11-14 02:18:31 +0000 | [diff] [blame] | 1543 | } |
| 1544 | else if (isCategoryImpl) |
| 1545 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1546 | else { |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 1547 | llvm::Value *ClassPtr = EmitSuperClassRef(Class); |
| 1548 | ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1); |
| 1549 | Target = CGF.Builder.CreateLoad(ClassPtr); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1550 | } |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1551 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 1552 | // ObjCTypes types. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1553 | const llvm::Type *ClassTy = |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1554 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1555 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1556 | CGF.Builder.CreateStore(Target, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1557 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1558 | return EmitLegacyMessageSend(CGF, ResultType, |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1559 | EmitSelector(CGF.Builder, Sel), |
| 1560 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1561 | true, CallArgs, Method, ObjCTypes); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1562 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1563 | |
| 1564 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1565 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1566 | QualType ResultType, |
| 1567 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1568 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1569 | bool IsClassMessage, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1570 | const CallArgList &CallArgs, |
| 1571 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1572 | return EmitLegacyMessageSend(CGF, ResultType, |
| 1573 | EmitSelector(CGF.Builder, Sel), |
| 1574 | Receiver, CGF.getContext().getObjCIdType(), |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1575 | false, CallArgs, Method, ObjCTypes); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1578 | CodeGen::RValue |
| 1579 | CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1580 | QualType ResultType, |
| 1581 | llvm::Value *Sel, |
| 1582 | llvm::Value *Arg0, |
| 1583 | QualType Arg0Ty, |
| 1584 | bool IsSuper, |
| 1585 | const CallArgList &CallArgs, |
| 1586 | const ObjCMethodDecl *Method, |
| 1587 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1588 | CallArgList ActualArgs; |
Fariborz Jahanian | d019d96 | 2009-04-24 21:07:43 +0000 | [diff] [blame] | 1589 | if (!IsSuper) |
| 1590 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1591 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1592 | ActualArgs.push_back(std::make_pair(RValue::get(Sel), |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1593 | CGF.getContext().getObjCSelType())); |
| 1594 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1595 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1596 | CodeGenTypes &Types = CGM.getTypes(); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1597 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1598 | FunctionType::ExtInfo()); |
Daniel Dunbar | 6793966 | 2009-09-17 04:01:40 +0000 | [diff] [blame] | 1599 | const llvm::FunctionType *FTy = |
| 1600 | Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1601 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1602 | llvm::Constant *Fn = NULL; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1603 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1604 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1605 | : ObjCTypes.getSendStretFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1606 | } else if (ResultType->isFloatingType()) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1607 | if (ObjCABI == 2) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1608 | if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1609 | BuiltinType::Kind k = BT->getKind(); |
| 1610 | Fn = (k == BuiltinType::LongDouble) ? ObjCTypes.getSendFpretFn2(IsSuper) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1611 | : ObjCTypes.getSendFn2(IsSuper); |
Daniel Dunbar | 42f963d | 2009-06-10 04:38:50 +0000 | [diff] [blame] | 1612 | } else { |
| 1613 | Fn = ObjCTypes.getSendFn2(IsSuper); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1614 | } |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 1615 | } else |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1616 | // FIXME. This currently matches gcc's API for x86-32. May need to change |
| 1617 | // for others if we have their API. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1618 | Fn = ObjCTypes.getSendFpretFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1619 | } else { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1620 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1621 | : ObjCTypes.getSendFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1622 | } |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1623 | assert(Fn && "EmitLegacyMessageSend - unknown API"); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1624 | Fn = llvm::ConstantExpr::getBitCast(Fn, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1625 | llvm::PointerType::getUnqual(FTy)); |
Anders Carlsson | f3c47c9 | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 1626 | return CGF.EmitCall(FnInfo, Fn, ReturnValueSlot(), ActualArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1629 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1630 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1631 | // 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] | 1632 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1633 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1634 | |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1635 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1636 | ObjCTypes.ExternalProtocolPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1639 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1640 | // FIXME: We shouldn't need this, the protocol decl should contain enough |
| 1641 | // information to tell us whether this was a declaration or a definition. |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1642 | DefinedProtocols.insert(PD->getIdentifier()); |
| 1643 | |
| 1644 | // If we have generated a forward reference to this protocol, emit |
| 1645 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 1646 | // emitted. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1647 | if (Protocols.count(PD->getIdentifier())) |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1648 | GetOrEmitProtocol(PD); |
| 1649 | } |
| 1650 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1651 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1652 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 1653 | return GetOrEmitProtocol(PD); |
| 1654 | return GetOrEmitProtocolRef(PD); |
| 1655 | } |
| 1656 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1657 | /* |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1658 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 1659 | struct _objc_protocol { |
| 1660 | struct _objc_protocol_extension *isa; |
| 1661 | char *protocol_name; |
| 1662 | struct _objc_protocol_list *protocol_list; |
| 1663 | struct _objc__method_prototype_list *instance_methods; |
| 1664 | struct _objc__method_prototype_list *class_methods |
| 1665 | }; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1666 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1667 | See EmitProtocolExtension(). |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1668 | */ |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1669 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
| 1670 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1671 | |
| 1672 | // Early exit if a defining object has already been generated. |
| 1673 | if (Entry && Entry->hasInitializer()) |
| 1674 | return Entry; |
| 1675 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1676 | // 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] | 1677 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1678 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1679 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1680 | // Construct method lists. |
| 1681 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1682 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1683 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1684 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1685 | ObjCMethodDecl *MD = *i; |
| 1686 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1687 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1688 | OptInstanceMethods.push_back(C); |
| 1689 | } else { |
| 1690 | InstanceMethods.push_back(C); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1691 | } |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1694 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1695 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1696 | ObjCMethodDecl *MD = *i; |
| 1697 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1698 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1699 | OptClassMethods.push_back(C); |
| 1700 | } else { |
| 1701 | ClassMethods.push_back(C); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1702 | } |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1705 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1706 | Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1707 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1708 | Values[2] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1709 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1710 | PD->protocol_begin(), |
| 1711 | PD->protocol_end()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1712 | Values[3] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1713 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1714 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1715 | InstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1716 | Values[4] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1717 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1718 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1719 | ClassMethods); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1720 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1721 | Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1722 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1723 | if (Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1724 | // Already created, fix the linkage and update the initializer. |
| 1725 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1726 | Entry->setInitializer(Init); |
| 1727 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1728 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1729 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1730 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1731 | Init, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1732 | "\01L_OBJC_PROTOCOL_" + PD->getName()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1733 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1734 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1735 | // FIXME: Is this necessary? Why only for protocol? |
| 1736 | Entry->setAlignment(4); |
| 1737 | } |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 1738 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1739 | |
| 1740 | return Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1743 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1744 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1745 | |
| 1746 | if (!Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1747 | // We use the initializer as a marker of whether this is a forward |
| 1748 | // reference or not. At module finalization we add the empty |
| 1749 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1750 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1751 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1752 | llvm::GlobalValue::ExternalLinkage, |
| 1753 | 0, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1754 | "\01L_OBJC_PROTOCOL_" + PD->getName()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1755 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1756 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1757 | // FIXME: Is this necessary? Why only for protocol? |
| 1758 | Entry->setAlignment(4); |
| 1759 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1760 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1761 | return Entry; |
| 1762 | } |
| 1763 | |
| 1764 | /* |
| 1765 | struct _objc_protocol_extension { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1766 | uint32_t size; |
| 1767 | struct objc_method_description_list *optional_instance_methods; |
| 1768 | struct objc_method_description_list *optional_class_methods; |
| 1769 | struct objc_property_list *instance_properties; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1770 | }; |
| 1771 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1772 | llvm::Constant * |
| 1773 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1774 | const ConstantVector &OptInstanceMethods, |
| 1775 | const ConstantVector &OptClassMethods) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1776 | uint64_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1777 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1778 | std::vector<llvm::Constant*> Values(4); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1779 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1780 | Values[1] = |
| 1781 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1782 | + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1783 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1784 | OptInstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1785 | Values[2] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1786 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1787 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1788 | OptClassMethods); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1789 | Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1790 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1791 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1792 | // Return null if no extension bits are used. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1793 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1794 | Values[3]->isNullValue()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1795 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1796 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1797 | llvm::Constant *Init = |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1798 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1799 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1800 | // No special section, but goes in llvm.used |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1801 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1802 | Init, |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1803 | 0, 0, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | /* |
| 1807 | struct objc_protocol_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1808 | struct objc_protocol_list *next; |
| 1809 | long count; |
| 1810 | Protocol *list[]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1811 | }; |
| 1812 | */ |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1813 | llvm::Constant * |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1814 | CGObjCMac::EmitProtocolList(llvm::Twine Name, |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1815 | ObjCProtocolDecl::protocol_iterator begin, |
| 1816 | ObjCProtocolDecl::protocol_iterator end) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1817 | std::vector<llvm::Constant*> ProtocolRefs; |
| 1818 | |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1819 | for (; begin != end; ++begin) |
| 1820 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1821 | |
| 1822 | // Just return null for empty protocol lists |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1823 | if (ProtocolRefs.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1824 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1825 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1826 | // This list is null terminated. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1827 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1828 | |
| 1829 | std::vector<llvm::Constant*> Values(3); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1830 | // This field is only used by the runtime. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1831 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1832 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1833 | ProtocolRefs.size() - 1); |
| 1834 | Values[2] = |
| 1835 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 1836 | ProtocolRefs.size()), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1837 | ProtocolRefs); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1838 | |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 1839 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1840 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1841 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1842 | 4, false); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1843 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1844 | } |
| 1845 | |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 1846 | void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet, |
| 1847 | std::vector<llvm::Constant*> &Properties, |
| 1848 | const Decl *Container, |
| 1849 | const ObjCProtocolDecl *PROTO, |
| 1850 | const ObjCCommonTypesHelper &ObjCTypes) { |
| 1851 | std::vector<llvm::Constant*> Prop(2); |
| 1852 | for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(), |
| 1853 | E = PROTO->protocol_end(); P != E; ++P) |
| 1854 | PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes); |
| 1855 | for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(), |
| 1856 | E = PROTO->prop_end(); I != E; ++I) { |
| 1857 | const ObjCPropertyDecl *PD = *I; |
| 1858 | if (!PropertySet.insert(PD->getIdentifier())) |
| 1859 | continue; |
| 1860 | Prop[0] = GetPropertyName(PD->getIdentifier()); |
| 1861 | Prop[1] = GetPropertyTypeString(PD, Container); |
| 1862 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop)); |
| 1863 | } |
| 1864 | } |
| 1865 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1866 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1867 | struct _objc_property { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1868 | const char * const name; |
| 1869 | const char * const attributes; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1870 | }; |
| 1871 | |
| 1872 | struct _objc_property_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1873 | uint32_t entsize; // sizeof (struct _objc_property) |
| 1874 | uint32_t prop_count; |
| 1875 | struct _objc_property[prop_count]; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1876 | }; |
| 1877 | */ |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1878 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1879 | const Decl *Container, |
| 1880 | const ObjCContainerDecl *OCD, |
| 1881 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1882 | std::vector<llvm::Constant*> Properties, Prop(2); |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 1883 | llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1884 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(), |
| 1885 | E = OCD->prop_end(); I != E; ++I) { |
Steve Naroff | 93983f8 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 1886 | const ObjCPropertyDecl *PD = *I; |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 1887 | PropertySet.insert(PD->getIdentifier()); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1888 | Prop[0] = GetPropertyName(PD->getIdentifier()); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1889 | Prop[1] = GetPropertyTypeString(PD, Container); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1890 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1891 | Prop)); |
| 1892 | } |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 1893 | if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) |
| 1894 | for (ObjCInterfaceDecl::protocol_iterator P = OID->protocol_begin(), |
| 1895 | E = OID->protocol_end(); P != E; ++P) |
| 1896 | PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1897 | |
| 1898 | // Return null for empty list. |
| 1899 | if (Properties.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1900 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1901 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1902 | unsigned PropertySize = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1903 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1904 | std::vector<llvm::Constant*> Values(3); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1905 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 1906 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1907 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1908 | Properties.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 1909 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 1910 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1911 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1912 | llvm::GlobalVariable *GV = |
| 1913 | CreateMetadataVar(Name, Init, |
| 1914 | (ObjCABI == 2) ? "__DATA, __objc_const" : |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1915 | "__OBJC,__property,regular,no_dead_strip", |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1916 | (ObjCABI == 2) ? 8 : 4, |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1917 | true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1918 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1922 | struct objc_method_description_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1923 | int count; |
| 1924 | struct objc_method_description list[]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1925 | }; |
| 1926 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1927 | llvm::Constant * |
| 1928 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 1929 | std::vector<llvm::Constant*> Desc(2); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1930 | Desc[0] = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1931 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1932 | ObjCTypes.SelectorPtrTy); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1933 | Desc[1] = GetMethodVarType(MD); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1934 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1935 | Desc); |
| 1936 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1937 | |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1938 | llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1939 | const char *Section, |
| 1940 | const ConstantVector &Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1941 | // Return null for empty list. |
| 1942 | if (Methods.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1943 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1944 | |
| 1945 | std::vector<llvm::Constant*> Values(2); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1946 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1947 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1948 | Methods.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 1949 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 1950 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1951 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1952 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1953 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1954 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1957 | /* |
| 1958 | struct _objc_category { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1959 | char *category_name; |
| 1960 | char *class_name; |
| 1961 | struct _objc_method_list *instance_methods; |
| 1962 | struct _objc_method_list *class_methods; |
| 1963 | struct _objc_protocol_list *protocols; |
| 1964 | uint32_t size; // <rdar://4585769> |
| 1965 | struct _objc_property_list *instance_properties; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1966 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1967 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1968 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1969 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1970 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1971 | // FIXME: This is poor design, the OCD should have a pointer to the category |
| 1972 | // decl. Additionally, note that Category can be null for the @implementation |
| 1973 | // w/o an @interface case. Sema should just create one for us as it does for |
| 1974 | // @implementation so everyone else can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1975 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1976 | const ObjCCategoryDecl *Category = |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1977 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1978 | |
| 1979 | llvm::SmallString<256> ExtName; |
| 1980 | llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_' |
| 1981 | << OCD->getName(); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1982 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1983 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1984 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1985 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1986 | // Instance methods should always be defined. |
| 1987 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1988 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1989 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1990 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1991 | // Class methods should always be defined. |
| 1992 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1993 | } |
| 1994 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1995 | std::vector<llvm::Constant*> Values(7); |
| 1996 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 1997 | Values[1] = GetClassName(Interface->getIdentifier()); |
Fariborz Jahanian | 679cd7f | 2009-04-29 20:40:05 +0000 | [diff] [blame] | 1998 | LazySymbols.insert(Interface->getIdentifier()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1999 | Values[2] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2000 | EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2001 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2002 | InstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2003 | Values[3] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2004 | EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2005 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2006 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2007 | if (Category) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2008 | Values[4] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2009 | EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2010 | Category->protocol_begin(), |
| 2011 | Category->protocol_end()); |
| 2012 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2013 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2014 | } |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2015 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2016 | |
| 2017 | // If there is no category @interface then there can be no properties. |
| 2018 | if (Category) { |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2019 | Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2020 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2021 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2022 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2023 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2024 | |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2025 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2026 | Values); |
| 2027 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2028 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2029 | CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init, |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2030 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2031 | 4, true); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2032 | DefinedCategories.push_back(GV); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2033 | } |
| 2034 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2035 | // FIXME: Get from somewhere? |
| 2036 | enum ClassFlags { |
| 2037 | eClassFlags_Factory = 0x00001, |
| 2038 | eClassFlags_Meta = 0x00002, |
| 2039 | // <rdr://5142207> |
| 2040 | eClassFlags_HasCXXStructors = 0x02000, |
| 2041 | eClassFlags_Hidden = 0x20000, |
| 2042 | eClassFlags_ABI2_Hidden = 0x00010, |
| 2043 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 2044 | }; |
| 2045 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2046 | /* |
| 2047 | struct _objc_class { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2048 | Class isa; |
| 2049 | Class super_class; |
| 2050 | const char *name; |
| 2051 | long version; |
| 2052 | long info; |
| 2053 | long instance_size; |
| 2054 | struct _objc_ivar_list *ivars; |
| 2055 | struct _objc_method_list *methods; |
| 2056 | struct _objc_cache *cache; |
| 2057 | struct _objc_protocol_list *protocols; |
| 2058 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 2059 | const char *ivar_layout; |
| 2060 | struct _objc_class_ext *ext; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2061 | }; |
| 2062 | |
| 2063 | See EmitClassExtension(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2064 | */ |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2065 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2066 | DefinedSymbols.insert(ID->getIdentifier()); |
| 2067 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2068 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2069 | // FIXME: Gross |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2070 | ObjCInterfaceDecl *Interface = |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2071 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2072 | llvm::Constant *Protocols = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2073 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2074 | Interface->protocol_begin(), |
| 2075 | Interface->protocol_end()); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2076 | unsigned Flags = eClassFlags_Factory; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2077 | unsigned Size = |
Daniel Dunbar | 2bebbf0 | 2009-05-03 10:46:44 +0000 | [diff] [blame] | 2078 | CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2079 | |
| 2080 | // FIXME: Set CXX-structors flag. |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 2081 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2082 | Flags |= eClassFlags_Hidden; |
| 2083 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2084 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2085 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2086 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2087 | // Instance methods should always be defined. |
| 2088 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 2089 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2090 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2091 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2092 | // Class methods should always be defined. |
| 2093 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 2094 | } |
| 2095 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2096 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2097 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2098 | ObjCPropertyImplDecl *PID = *i; |
| 2099 | |
| 2100 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 2101 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 2102 | |
| 2103 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 2104 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 2105 | InstanceMethods.push_back(C); |
| 2106 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 2107 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 2108 | InstanceMethods.push_back(C); |
| 2109 | } |
| 2110 | } |
| 2111 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2112 | std::vector<llvm::Constant*> Values(12); |
Daniel Dunbar | 5384b09 | 2009-05-03 08:56:52 +0000 | [diff] [blame] | 2113 | Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2114 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2115 | // Record a reference to the super class. |
| 2116 | LazySymbols.insert(Super->getIdentifier()); |
| 2117 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2118 | Values[ 1] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2119 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2120 | ObjCTypes.ClassPtrTy); |
| 2121 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2122 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2123 | } |
| 2124 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 2125 | // Version is always 0. |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2126 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2127 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 2128 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2129 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2130 | Values[ 7] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2131 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2132 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2133 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2134 | // cache is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2135 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2136 | Values[ 9] = Protocols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2137 | Values[10] = BuildIvarLayout(ID, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2138 | Values[11] = EmitClassExtension(ID); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2139 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2140 | Values); |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 2141 | std::string Name("\01L_OBJC_CLASS_"); |
| 2142 | Name += ClassName; |
| 2143 | const char *Section = "__OBJC,__class,regular,no_dead_strip"; |
| 2144 | // Check for a forward reference. |
| 2145 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 2146 | if (GV) { |
| 2147 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2148 | "Forward metaclass reference has incorrect type."); |
| 2149 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 2150 | GV->setInitializer(Init); |
| 2151 | GV->setSection(Section); |
| 2152 | GV->setAlignment(4); |
| 2153 | CGM.AddUsedGlobal(GV); |
| 2154 | } |
| 2155 | else |
| 2156 | GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2157 | DefinedClasses.push_back(GV); |
| 2158 | } |
| 2159 | |
| 2160 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 2161 | llvm::Constant *Protocols, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2162 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2163 | unsigned Flags = eClassFlags_Meta; |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2164 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2165 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 2166 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2167 | Flags |= eClassFlags_Hidden; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2168 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2169 | std::vector<llvm::Constant*> Values(12); |
| 2170 | // The isa for the metaclass is the root of the hierarchy. |
| 2171 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 2172 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 2173 | Root = Super; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2174 | Values[ 0] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2175 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2176 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2177 | // The super class for the metaclass is emitted as the name of the |
| 2178 | // super class. The runtime fixes this up to point to the |
| 2179 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2180 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2181 | Values[ 1] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2182 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2183 | ObjCTypes.ClassPtrTy); |
| 2184 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2185 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2186 | } |
| 2187 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 2188 | // Version is always 0. |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2189 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2190 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 2191 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2192 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2193 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2194 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2195 | "__OBJC,__cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2196 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2197 | // cache is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2198 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2199 | Values[ 9] = Protocols; |
| 2200 | // ivar_layout for metaclass is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2201 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2202 | // The class extension is always unused for metaclasses. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2203 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2204 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2205 | Values); |
| 2206 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2207 | std::string Name("\01L_OBJC_METACLASS_"); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2208 | Name += ID->getNameAsCString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2209 | |
| 2210 | // Check for a forward reference. |
| 2211 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 2212 | if (GV) { |
| 2213 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2214 | "Forward metaclass reference has incorrect type."); |
| 2215 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 2216 | GV->setInitializer(Init); |
| 2217 | } else { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2218 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2219 | llvm::GlobalValue::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2220 | Init, Name); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2221 | } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2222 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2223 | GV->setAlignment(4); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2224 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2225 | |
| 2226 | return GV; |
| 2227 | } |
| 2228 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2229 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2230 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2231 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2232 | // FIXME: Should we look these up somewhere other than the module. Its a bit |
| 2233 | // silly since we only generate these while processing an implementation, so |
| 2234 | // exactly one pointer would work if know when we entered/exitted an |
| 2235 | // implementation block. |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2236 | |
| 2237 | // Check for an existing forward reference. |
Fariborz Jahanian | b0d2794 | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 2238 | // Previously, metaclass with internal linkage may have been defined. |
| 2239 | // pass 'true' as 2nd argument so it is returned. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2240 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, |
| 2241 | true)) { |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2242 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2243 | "Forward metaclass reference has incorrect type."); |
| 2244 | return GV; |
| 2245 | } else { |
| 2246 | // Generate as an external reference to keep a consistent |
| 2247 | // module. This will be patched up when we emit the metaclass. |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2248 | return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2249 | llvm::GlobalValue::ExternalLinkage, |
| 2250 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2251 | Name); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2252 | } |
| 2253 | } |
| 2254 | |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 2255 | llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) { |
| 2256 | std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString(); |
| 2257 | |
| 2258 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, |
| 2259 | true)) { |
| 2260 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2261 | "Forward class metadata reference has incorrect type."); |
| 2262 | return GV; |
| 2263 | } else { |
| 2264 | return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
| 2265 | llvm::GlobalValue::ExternalLinkage, |
| 2266 | 0, |
| 2267 | Name); |
| 2268 | } |
| 2269 | } |
| 2270 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2271 | /* |
| 2272 | struct objc_class_ext { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2273 | uint32_t size; |
| 2274 | const char *weak_ivar_layout; |
| 2275 | struct _objc_property_list *properties; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2276 | }; |
| 2277 | */ |
| 2278 | llvm::Constant * |
| 2279 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2280 | uint64_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2281 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2282 | |
| 2283 | std::vector<llvm::Constant*> Values(3); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2284 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 2285 | Values[1] = BuildIvarLayout(ID, false); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2286 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2287 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2288 | |
| 2289 | // Return null if no extension bits are used. |
| 2290 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2291 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2292 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2293 | llvm::Constant *Init = |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2294 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2295 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2296 | Init, "__OBJC,__class_ext,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2297 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2298 | } |
| 2299 | |
| 2300 | /* |
| 2301 | struct objc_ivar { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2302 | char *ivar_name; |
| 2303 | char *ivar_type; |
| 2304 | int ivar_offset; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2305 | }; |
| 2306 | |
| 2307 | struct objc_ivar_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2308 | int ivar_count; |
| 2309 | struct objc_ivar list[count]; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2310 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2311 | */ |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2312 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2313 | bool ForClass) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2314 | std::vector<llvm::Constant*> Ivars, Ivar(3); |
| 2315 | |
| 2316 | // When emitting the root class GCC emits ivar entries for the |
| 2317 | // actual class structure. It is not clear if we need to follow this |
| 2318 | // behavior; for now lets try and get away with not doing it. If so, |
| 2319 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 2320 | // for the class. |
| 2321 | if (ForClass) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2322 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2323 | |
| 2324 | ObjCInterfaceDecl *OID = |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2325 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2326 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 2327 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 2328 | CGM.getContext().ShallowCollectObjCIvars(OID, OIvars); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2329 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 2330 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 2331 | ObjCIvarDecl *IVD = OIvars[i]; |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 2332 | // Ignore unnamed bit-fields. |
| 2333 | if (!IVD->getDeclName()) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2334 | continue; |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 2335 | Ivar[0] = GetMethodVarName(IVD->getIdentifier()); |
| 2336 | Ivar[1] = GetMethodVarType(IVD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2337 | Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2338 | ComputeIvarBaseOffset(CGM, OID, IVD)); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2339 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
| 2342 | // Return null for empty list. |
| 2343 | if (Ivars.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2344 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2345 | |
| 2346 | std::vector<llvm::Constant*> Values(2); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2347 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2348 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2349 | Ivars.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2350 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 2351 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2352 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2353 | llvm::GlobalVariable *GV; |
| 2354 | if (ForClass) |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2355 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2356 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2357 | 4, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2358 | else |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2359 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2360 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2361 | 4, true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2362 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2363 | } |
| 2364 | |
| 2365 | /* |
| 2366 | struct objc_method { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2367 | SEL method_name; |
| 2368 | char *method_types; |
| 2369 | void *method; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2370 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2371 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2372 | struct objc_method_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2373 | struct objc_method_list *obsolete; |
| 2374 | int count; |
| 2375 | struct objc_method methods_list[count]; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2376 | }; |
| 2377 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2378 | |
| 2379 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 2380 | /// given method if it has been defined. The result is null if the |
| 2381 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2382 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2383 | // FIXME: Use DenseMap::lookup |
| 2384 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 2385 | if (!Fn) |
| 2386 | return 0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2387 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2388 | std::vector<llvm::Constant*> Method(3); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2389 | Method[0] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2390 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2391 | ObjCTypes.SelectorPtrTy); |
| 2392 | Method[1] = GetMethodVarType(MD); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2393 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2394 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2395 | } |
| 2396 | |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2397 | llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2398 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2399 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2400 | // Return null for empty list. |
| 2401 | if (Methods.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2402 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2403 | |
| 2404 | std::vector<llvm::Constant*> Values(3); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2405 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2406 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2407 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2408 | Methods.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2409 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 2410 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2411 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2412 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2413 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2414 | ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 2417 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2418 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 2419 | llvm::SmallString<256> Name; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 2420 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2421 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2422 | CodeGenTypes &Types = CGM.getTypes(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2423 | const llvm::FunctionType *MethodTy = |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2424 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2425 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2426 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2427 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 2428 | Name.str(), |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2429 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2430 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2431 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2432 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2435 | llvm::GlobalVariable * |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2436 | CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name, |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2437 | llvm::Constant *Init, |
| 2438 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2439 | unsigned Align, |
| 2440 | bool AddToUsed) { |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2441 | const llvm::Type *Ty = Init->getType(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2442 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2443 | new llvm::GlobalVariable(CGM.getModule(), Ty, false, |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2444 | llvm::GlobalValue::InternalLinkage, Init, Name); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2445 | if (Section) |
| 2446 | GV->setSection(Section); |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2447 | if (Align) |
| 2448 | GV->setAlignment(Align); |
| 2449 | if (AddToUsed) |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2450 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2451 | return GV; |
| 2452 | } |
| 2453 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2454 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2455 | // Abuse this interface function as a place to finalize. |
| 2456 | FinishModule(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2457 | return NULL; |
| 2458 | } |
| 2459 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2460 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2461 | return ObjCTypes.getGetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2464 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2465 | return ObjCTypes.getSetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2466 | } |
| 2467 | |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 2468 | llvm::Constant *CGObjCMac::GetCopyStructFunction() { |
| 2469 | return ObjCTypes.getCopyStructFn(); |
| 2470 | } |
| 2471 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2472 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2473 | return ObjCTypes.getEnumerationMutationFn(); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2476 | /* |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2477 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2478 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 2479 | -- |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2480 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2481 | The basic framework for a @try-catch-finally is as follows: |
| 2482 | { |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2483 | objc_exception_data d; |
| 2484 | id _rethrow = null; |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2485 | bool _call_try_exit = true; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2486 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2487 | objc_exception_try_enter(&d); |
| 2488 | if (!setjmp(d.jmp_buf)) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2489 | ... try body ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2490 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2491 | // exception path |
| 2492 | id _caught = objc_exception_extract(&d); |
| 2493 | |
| 2494 | // enter new try scope for handlers |
| 2495 | if (!setjmp(d.jmp_buf)) { |
| 2496 | ... match exception and execute catch blocks ... |
| 2497 | |
| 2498 | // fell off end, rethrow. |
| 2499 | _rethrow = _caught; |
| 2500 | ... jump-through-finally to finally_rethrow ... |
| 2501 | } else { |
| 2502 | // exception in catch block |
| 2503 | _rethrow = objc_exception_extract(&d); |
| 2504 | _call_try_exit = false; |
| 2505 | ... jump-through-finally to finally_rethrow ... |
| 2506 | } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2507 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2508 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2509 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2510 | finally: |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2511 | if (_call_try_exit) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2512 | objc_exception_try_exit(&d); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2513 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2514 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2515 | ... dispatch to finally destination ... |
| 2516 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2517 | finally_rethrow: |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2518 | objc_exception_throw(_rethrow); |
| 2519 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2520 | finally_end: |
| 2521 | } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2522 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2523 | This framework differs slightly from the one gcc uses, in that gcc |
| 2524 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 2525 | and if the object should be rethrown. This breaks in the face of |
| 2526 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2527 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2528 | We specialize this framework for a few particular circumstances: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2529 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2530 | - If there are no catch blocks, then we avoid emitting the second |
| 2531 | exception handling context. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2532 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2533 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 2534 | e)) we avoid emitting the code to rethrow an uncaught exception. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2535 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2536 | - FIXME: If there is no @finally block we can do a few more |
| 2537 | simplifications. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2538 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2539 | Rethrows and Jumps-Through-Finally |
| 2540 | -- |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2541 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2542 | Support for implicit rethrows and jumping through the finally block is |
| 2543 | handled by storing the current exception-handling context in |
| 2544 | ObjCEHStack. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2545 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2546 | In order to implement proper @finally semantics, we support one basic |
| 2547 | mechanism for jumping through the finally block to an arbitrary |
| 2548 | destination. Constructs which generate exits from a @try or @catch |
| 2549 | block use this mechanism to implement the proper semantics by chaining |
| 2550 | jumps, as necessary. |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2551 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2552 | This mechanism works like the one used for indirect goto: we |
| 2553 | arbitrarily assign an ID to each destination and store the ID for the |
| 2554 | destination in a variable prior to entering the finally block. At the |
| 2555 | end of the finally block we simply create a switch to the proper |
| 2556 | destination. |
| 2557 | |
| 2558 | Code gen for @synchronized(expr) stmt; |
| 2559 | Effectively generating code for: |
| 2560 | objc_sync_enter(expr); |
| 2561 | @try stmt @finally { objc_sync_exit(expr); } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2562 | */ |
| 2563 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2564 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 2565 | const Stmt &S) { |
| 2566 | bool isTry = isa<ObjCAtTryStmt>(S); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2567 | // Create various blocks we refer to for handling @finally. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2568 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2569 | llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit"); |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2570 | llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit"); |
| 2571 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
| 2572 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2573 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2574 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 2575 | // evaluation of the expression must occur before we enter the |
| 2576 | // @synchronized. We can safely avoid a temp here because jumps into |
| 2577 | // @synchronized are illegal & this will dominate uses. |
| 2578 | llvm::Value *SyncArg = 0; |
| 2579 | if (!isTry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2580 | SyncArg = |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2581 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 2582 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 2583 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2584 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2585 | |
| 2586 | // Push an EH context entry, used for handling rethrows and jumps |
| 2587 | // through finally. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2588 | CGF.PushCleanupBlock(FinallyBlock); |
| 2589 | |
Fariborz Jahanian | 8c89ff9 | 2009-12-14 21:35:01 +0000 | [diff] [blame] | 2590 | if (CGF.ObjCEHValueStack.empty()) |
| 2591 | CGF.ObjCEHValueStack.push_back(0); |
| 2592 | // If This is a nested @try, caught exception is that of enclosing @try. |
| 2593 | else |
| 2594 | CGF.ObjCEHValueStack.push_back(CGF.ObjCEHValueStack.back()); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2595 | // Allocate memory for the exception data and rethrow pointer. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2596 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 2597 | "exceptiondata.ptr"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2598 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2599 | "_rethrow"); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2600 | llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca( |
| 2601 | llvm::Type::getInt1Ty(VMContext), |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2602 | "_call_try_exit"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2603 | CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(VMContext), |
| 2604 | CallTryExitPtr); |
| 2605 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2606 | // Enter a new try block and call setjmp. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2607 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2608 | llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0, |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2609 | "jmpbufarray"); |
| 2610 | JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2611 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2612 | JmpBufPtr, "result"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2613 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2614 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 2615 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2616 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2617 | TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2618 | |
| 2619 | // Emit the @try block. |
| 2620 | CGF.EmitBlock(TryBlock); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2621 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 2622 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2623 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2624 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2625 | // Emit the "exception in @try" block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2626 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2627 | |
| 2628 | // Retrieve the exception object. We may emit multiple blocks but |
| 2629 | // nothing can cross this so the value is already in SSA form. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2630 | llvm::Value *Caught = |
| 2631 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2632 | ExceptionData, "caught"); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2633 | CGF.ObjCEHValueStack.back() = Caught; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2634 | if (!isTry) { |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2635 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2636 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext), |
| 2637 | CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2638 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2639 | } else if (const ObjCAtCatchStmt* CatchStmt = |
| 2640 | cast<ObjCAtTryStmt>(S).getCatchStmts()) { |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2641 | // Enter a new exception try block (in case a @catch block throws |
| 2642 | // an exception). |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2643 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2644 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2645 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2646 | JmpBufPtr, "result"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2647 | llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2648 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2649 | llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch"); |
| 2650 | llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler"); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2651 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2652 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2653 | CGF.EmitBlock(CatchBlock); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2654 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2655 | // Handle catch list. As a special case we check if everything is |
| 2656 | // matched and avoid generating code for falling off the end if |
| 2657 | // so. |
| 2658 | bool AllMatched = false; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2659 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2660 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2661 | |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2662 | const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2663 | const ObjCObjectPointerType *OPT = 0; |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2664 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2665 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2666 | if (!CatchParam) { |
| 2667 | AllMatched = true; |
| 2668 | } else { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2669 | OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2670 | |
| 2671 | // catch(id e) always matches. |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 2672 | // FIXME: For the time being we also match id<X>; this should |
| 2673 | // be rejected by Sema instead. |
Eli Friedman | 818e96f | 2009-07-11 00:57:02 +0000 | [diff] [blame] | 2674 | if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType())) |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2675 | AllMatched = true; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2676 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2677 | |
| 2678 | if (AllMatched) { |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2679 | if (CatchParam) { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2680 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2681 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2682 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2683 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2684 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2685 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2686 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2687 | break; |
| 2688 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2689 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2690 | assert(OPT && "Unexpected non-object pointer type in @catch"); |
| 2691 | QualType T = OPT->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2692 | const ObjCInterfaceType *ObjCType = T->getAs<ObjCInterfaceType>(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2693 | assert(ObjCType && "Catch parameter must have Objective-C type!"); |
| 2694 | |
| 2695 | // Check if the @catch block matches the exception object. |
| 2696 | llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2697 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2698 | llvm::Value *Match = |
| 2699 | CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(), |
| 2700 | Class, Caught, "match"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2701 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2702 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2703 | |
| 2704 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2705 | MatchedBlock, NextCatchBlock); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2706 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2707 | // Emit the @catch block. |
| 2708 | CGF.EmitBlock(MatchedBlock); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2709 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2710 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2711 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2712 | llvm::Value *Tmp = |
| 2713 | CGF.Builder.CreateBitCast(Caught, |
| 2714 | CGF.ConvertType(CatchParam->getType()), |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2715 | "tmp"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2716 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2717 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2718 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2719 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2720 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2721 | CGF.EmitBlock(NextCatchBlock); |
| 2722 | } |
| 2723 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2724 | if (!AllMatched) { |
| 2725 | // None of the handlers caught the exception, so store it to be |
| 2726 | // rethrown at the end of the @finally block. |
| 2727 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2728 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2729 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2730 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2731 | // Emit the exception handler for the @catch blocks. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2732 | CGF.EmitBlock(CatchHandler); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2733 | CGF.Builder.CreateStore( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2734 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2735 | ExceptionData), |
| 2736 | RethrowPtr); |
| 2737 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext), |
| 2738 | CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2739 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2740 | } else { |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2741 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2742 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext), |
| 2743 | CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2744 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2745 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2746 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2747 | // Pop the exception-handling stack entry. It is important to do |
| 2748 | // this now, because the code in the @finally block is not in this |
| 2749 | // context. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2750 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 2751 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2752 | CGF.ObjCEHValueStack.pop_back(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2753 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2754 | // Emit the @finally block. |
| 2755 | CGF.EmitBlock(FinallyBlock); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2756 | llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2757 | |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2758 | CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2759 | |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2760 | CGF.EmitBlock(FinallyExit); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2761 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2762 | |
| 2763 | CGF.EmitBlock(FinallyNoExit); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2764 | if (isTry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2765 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 2766 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2767 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2768 | } else { |
| 2769 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 2770 | // @synchronized. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2771 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg); |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 2772 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2773 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2774 | // Emit the switch block |
| 2775 | if (Info.SwitchBlock) |
| 2776 | CGF.EmitBlock(Info.SwitchBlock); |
| 2777 | if (Info.EndBlock) |
| 2778 | CGF.EmitBlock(Info.EndBlock); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2779 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2780 | CGF.EmitBlock(FinallyRethrow); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2781 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2782 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2783 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2784 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2785 | CGF.EmitBlock(FinallyEnd); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
| 2788 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2789 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2790 | llvm::Value *ExceptionAsObject; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2791 | |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2792 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 2793 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2794 | ExceptionAsObject = |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2795 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 2796 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2797 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2798 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2799 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2800 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2801 | |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2802 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2803 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2804 | |
| 2805 | // Clear the insertion point to indicate we are in unreachable code. |
| 2806 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2807 | } |
| 2808 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2809 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2810 | /// object: objc_read_weak (id *src) |
| 2811 | /// |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2812 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2813 | llvm::Value *AddrWeakObj) { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2814 | const llvm::Type* DestTy = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2815 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
| 2816 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, |
| 2817 | ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2818 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2819 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2820 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2821 | return read_weak; |
| 2822 | } |
| 2823 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2824 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 2825 | /// objc_assign_weak (id src, id *dst) |
| 2826 | /// |
| 2827 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2828 | llvm::Value *src, llvm::Value *dst) { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2829 | const llvm::Type * SrcTy = src->getType(); |
| 2830 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2831 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2832 | assert(Size <= 8 && "does not support size > 8"); |
| 2833 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2834 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2835 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2836 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2837 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2838 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 2839 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2840 | src, dst, "weakassign"); |
| 2841 | return; |
| 2842 | } |
| 2843 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2844 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 2845 | /// objc_assign_global (id src, id *dst) |
| 2846 | /// |
| 2847 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2848 | llvm::Value *src, llvm::Value *dst) { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2849 | const llvm::Type * SrcTy = src->getType(); |
| 2850 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2851 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2852 | assert(Size <= 8 && "does not support size > 8"); |
| 2853 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2854 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2855 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2856 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2857 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2858 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2859 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2860 | src, dst, "globalassign"); |
| 2861 | return; |
| 2862 | } |
| 2863 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2864 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 2865 | /// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset) |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2866 | /// |
| 2867 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 2868 | llvm::Value *src, llvm::Value *dst, |
| 2869 | llvm::Value *ivarOffset) { |
| 2870 | assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL"); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2871 | const llvm::Type * SrcTy = src->getType(); |
| 2872 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2873 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2874 | assert(Size <= 8 && "does not support size > 8"); |
| 2875 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2876 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2877 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2878 | } |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2879 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2880 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 2881 | CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(), |
| 2882 | src, dst, ivarOffset); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2883 | return; |
| 2884 | } |
| 2885 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2886 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 2887 | /// objc_assign_strongCast (id src, id *dst) |
| 2888 | /// |
| 2889 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2890 | llvm::Value *src, llvm::Value *dst) { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2891 | const llvm::Type * SrcTy = src->getType(); |
| 2892 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2893 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2894 | assert(Size <= 8 && "does not support size > 8"); |
| 2895 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2896 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2897 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2898 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2899 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2900 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2901 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2902 | src, dst, "weakassign"); |
| 2903 | return; |
| 2904 | } |
| 2905 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2906 | void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2907 | llvm::Value *DestPtr, |
| 2908 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 2909 | QualType Ty) { |
| 2910 | // Get size info for this aggregate. |
| 2911 | std::pair<uint64_t, unsigned> TypeInfo = CGM.getContext().getTypeInfo(Ty); |
Fariborz Jahanian | 736d8a6 | 2009-09-10 16:20:31 +0000 | [diff] [blame] | 2912 | unsigned long size = TypeInfo.first/8; |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2913 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 2914 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2915 | llvm::Value *N = llvm::ConstantInt::get(ObjCTypes.LongTy, size); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2916 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), |
| 2917 | DestPtr, SrcPtr, N); |
| 2918 | return; |
| 2919 | } |
| 2920 | |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2921 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 2922 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2923 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 2924 | QualType ObjectTy, |
| 2925 | llvm::Value *BaseValue, |
| 2926 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2927 | unsigned CVRQualifiers) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2928 | const ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCInterfaceType>()->getDecl(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2929 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 2930 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2931 | } |
| 2932 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2933 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 2934 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2935 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2936 | uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2937 | return llvm::ConstantInt::get( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2938 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 2939 | Offset); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2940 | } |
| 2941 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2942 | /* *** Private Interface *** */ |
| 2943 | |
| 2944 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 2945 | /// level information. |
| 2946 | /// |
| 2947 | /// See: <rdr://4810609&4810587&4810587> |
| 2948 | /// struct IMAGE_INFO { |
| 2949 | /// unsigned version; |
| 2950 | /// unsigned flags; |
| 2951 | /// }; |
| 2952 | enum ImageInfoFlags { |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2953 | eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what |
| 2954 | // this implies. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2955 | eImageInfo_GarbageCollected = (1 << 1), |
| 2956 | eImageInfo_GCOnly = (1 << 2), |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2957 | eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set. |
| 2958 | |
| 2959 | // A flag indicating that the module has no instances of an |
| 2960 | // @synthesize of a superclass variable. <rdar://problem/6803242> |
| 2961 | eImageInfo_CorrectedSynthesize = (1 << 4) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2962 | }; |
| 2963 | |
| 2964 | void CGObjCMac::EmitImageInfo() { |
| 2965 | unsigned version = 0; // Version is unused? |
| 2966 | unsigned flags = 0; |
| 2967 | |
| 2968 | // FIXME: Fix and continue? |
| 2969 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 2970 | flags |= eImageInfo_GarbageCollected; |
| 2971 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 2972 | flags |= eImageInfo_GCOnly; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2973 | |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2974 | // We never allow @synthesize of a superclass property. |
| 2975 | flags |= eImageInfo_CorrectedSynthesize; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2976 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2977 | // Emitted as int[2]; |
| 2978 | llvm::Constant *values[2] = { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2979 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), version), |
| 2980 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), flags) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2981 | }; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2982 | llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext), 2); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2983 | |
| 2984 | const char *Section; |
| 2985 | if (ObjCABI == 1) |
| 2986 | Section = "__OBJC, __image_info,regular"; |
| 2987 | else |
| 2988 | Section = "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2989 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2990 | CreateMetadataVar("\01L_OBJC_IMAGE_INFO", |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2991 | llvm::ConstantArray::get(AT, values, 2), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2992 | Section, |
| 2993 | 0, |
| 2994 | true); |
| 2995 | GV->setConstant(true); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2996 | } |
| 2997 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2998 | |
| 2999 | // struct objc_module { |
| 3000 | // unsigned long version; |
| 3001 | // unsigned long size; |
| 3002 | // const char *name; |
| 3003 | // Symtab symtab; |
| 3004 | // }; |
| 3005 | |
| 3006 | // FIXME: Get from somewhere |
| 3007 | static const int ModuleVersion = 7; |
| 3008 | |
| 3009 | void CGObjCMac::EmitModuleInfo() { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3010 | uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3011 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3012 | std::vector<llvm::Constant*> Values(4); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3013 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion); |
| 3014 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 3015 | // This used to be the filename, now it is unused. <rdr://4327263> |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3016 | Values[2] = GetClassName(&CGM.getContext().Idents.get("")); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3017 | Values[3] = EmitModuleSymbols(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3018 | CreateMetadataVar("\01L_OBJC_MODULES", |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3019 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3020 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 3021 | 4, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3022 | } |
| 3023 | |
| 3024 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3025 | unsigned NumClasses = DefinedClasses.size(); |
| 3026 | unsigned NumCategories = DefinedCategories.size(); |
| 3027 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3028 | // Return null if no symbols were defined. |
| 3029 | if (!NumClasses && !NumCategories) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3030 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3031 | |
| 3032 | std::vector<llvm::Constant*> Values(5); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3033 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3034 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3035 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 3036 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3037 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3038 | // The runtime expects exactly the list of defined classes followed |
| 3039 | // by the list of defined categories, in a single array. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3040 | std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3041 | for (unsigned i=0; i<NumClasses; i++) |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3042 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3043 | ObjCTypes.Int8PtrTy); |
| 3044 | for (unsigned i=0; i<NumCategories; i++) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3045 | Symbols[NumClasses + i] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3046 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3047 | ObjCTypes.Int8PtrTy); |
| 3048 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3049 | Values[4] = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3050 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3051 | NumClasses + NumCategories), |
| 3052 | Symbols); |
| 3053 | |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 3054 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3055 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3056 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3057 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 3058 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3059 | 4, true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3060 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3061 | } |
| 3062 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3063 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3064 | const ObjCInterfaceDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3065 | LazySymbols.insert(ID->getIdentifier()); |
| 3066 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3067 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3068 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3069 | if (!Entry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3070 | llvm::Constant *Casted = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3071 | llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3072 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3073 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3074 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 3075 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3076 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3077 | } |
| 3078 | |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 3079 | return Builder.CreateLoad(Entry, "tmp"); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3080 | } |
| 3081 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 3082 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3083 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3084 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3085 | if (!Entry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3086 | llvm::Constant *Casted = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3087 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3088 | ObjCTypes.SelectorPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3089 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3090 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 3091 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3092 | 4, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3093 | } |
| 3094 | |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 3095 | return Builder.CreateLoad(Entry, "tmp"); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3096 | } |
| 3097 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3098 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3099 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3100 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3101 | if (!Entry) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3102 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3103 | llvm::ConstantArray::get(VMContext, |
| 3104 | Ident->getNameStart()), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3105 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3106 | 1, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3107 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3108 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3109 | } |
| 3110 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 3111 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 3112 | /// ivar layout bitmap. |
| 3113 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3114 | const ObjCCommonTypesHelper &ObjCTypes) { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3115 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 3116 | } |
| 3117 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3118 | static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) { |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3119 | if (FQT.isObjCGCStrong()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3120 | return Qualifiers::Strong; |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3121 | |
| 3122 | if (FQT.isObjCGCWeak()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3123 | return Qualifiers::Weak; |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3124 | |
Fariborz Jahanian | 039e6a1 | 2009-09-11 17:39:05 +0000 | [diff] [blame] | 3125 | if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3126 | return Qualifiers::Strong; |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3127 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3128 | if (const PointerType *PT = FQT->getAs<PointerType>()) |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3129 | return GetGCAttrTypeForType(Ctx, PT->getPointeeType()); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3130 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3131 | return Qualifiers::GCNone; |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3132 | } |
| 3133 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3134 | void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3135 | unsigned int BytePos, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3136 | bool ForStrongLayout, |
| 3137 | bool &HasUnion) { |
| 3138 | const RecordDecl *RD = RT->getDecl(); |
| 3139 | // FIXME - Use iterator. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3140 | llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end()); |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3141 | const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3142 | const llvm::StructLayout *RecLayout = |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3143 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3144 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3145 | BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos, |
| 3146 | ForStrongLayout, HasUnion); |
| 3147 | } |
| 3148 | |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 3149 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3150 | const llvm::StructLayout *Layout, |
| 3151 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3152 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3153 | unsigned int BytePos, bool ForStrongLayout, |
| 3154 | bool &HasUnion) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3155 | bool IsUnion = (RD && RD->isUnion()); |
| 3156 | uint64_t MaxUnionIvarSize = 0; |
| 3157 | uint64_t MaxSkippedUnionIvarSize = 0; |
| 3158 | FieldDecl *MaxField = 0; |
| 3159 | FieldDecl *MaxSkippedField = 0; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3160 | FieldDecl *LastFieldBitfield = 0; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3161 | uint64_t MaxFieldOffset = 0; |
| 3162 | uint64_t MaxSkippedFieldOffset = 0; |
| 3163 | uint64_t LastBitfieldOffset = 0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3164 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3165 | if (RecFields.empty()) |
| 3166 | return; |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3167 | unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0); |
| 3168 | unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth(); |
| 3169 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3170 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3171 | FieldDecl *Field = RecFields[i]; |
Daniel Dunbar | e05cc98 | 2009-05-03 23:35:23 +0000 | [diff] [blame] | 3172 | uint64_t FieldOffset; |
Anders Carlsson | fc514ab | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 3173 | if (RD) { |
Daniel Dunbar | 198bcb4 | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 3174 | const CGRecordLayout &RL = |
| 3175 | CGM.getTypes().getCGRecordLayout(Field->getParent()); |
Anders Carlsson | fc514ab | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 3176 | if (Field->isBitField()) { |
Daniel Dunbar | 2eec0b2 | 2010-04-05 16:20:44 +0000 | [diff] [blame] | 3177 | const CGBitFieldInfo &Info = RL.getBitFieldInfo(Field); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3178 | |
| 3179 | const llvm::Type *Ty = |
Anders Carlsson | ef46d18 | 2009-08-25 02:07:02 +0000 | [diff] [blame] | 3180 | CGM.getTypes().ConvertTypeForMemRecursive(Field->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3181 | uint64_t TypeSize = |
Anders Carlsson | ef46d18 | 2009-08-25 02:07:02 +0000 | [diff] [blame] | 3182 | CGM.getTypes().getTargetData().getTypeAllocSize(Ty); |
| 3183 | FieldOffset = Info.FieldNo * TypeSize; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3184 | } else |
| 3185 | FieldOffset = |
Daniel Dunbar | 198bcb4 | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 3186 | Layout->getElementOffset(RL.getLLVMFieldNo(Field)); |
Anders Carlsson | fc514ab | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 3187 | } else |
Daniel Dunbar | e05cc98 | 2009-05-03 23:35:23 +0000 | [diff] [blame] | 3188 | FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)); |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3189 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3190 | // Skip over unnamed or bitfields |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3191 | if (!Field->getIdentifier() || Field->isBitField()) { |
| 3192 | LastFieldBitfield = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3193 | LastBitfieldOffset = FieldOffset; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3194 | continue; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3195 | } |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3196 | |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3197 | LastFieldBitfield = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3198 | QualType FQT = Field->getType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3199 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3200 | if (FQT->isUnionType()) |
| 3201 | HasUnion = true; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3202 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3203 | BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(), |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3204 | BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3205 | ForStrongLayout, HasUnion); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3206 | continue; |
| 3207 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3208 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3209 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3210 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3211 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3212 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3213 | assert(CArray && "only array with known element size is supported"); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3214 | FQT = CArray->getElementType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3215 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 3216 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3217 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3218 | ElCount *= CArray->getSize().getZExtValue(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3219 | FQT = CArray->getElementType(); |
| 3220 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3221 | |
| 3222 | assert(!FQT->isUnionType() && |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3223 | "layout for array of unions not supported"); |
| 3224 | if (FQT->isRecordType()) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3225 | int OldIndex = IvarsInfo.size() - 1; |
| 3226 | int OldSkIndex = SkipIvars.size() -1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3227 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3228 | const RecordType *RT = FQT->getAs<RecordType>(); |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3229 | BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3230 | ForStrongLayout, HasUnion); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3231 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3232 | // Replicate layout information for each array element. Note that |
| 3233 | // one element is already done. |
| 3234 | uint64_t ElIx = 1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3235 | for (int FirstIndex = IvarsInfo.size() - 1, |
| 3236 | FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3237 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3238 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 3239 | IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx, |
| 3240 | IvarsInfo[i].ivar_size)); |
| 3241 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) |
| 3242 | SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx, |
| 3243 | SkipIvars[i].ivar_size)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3244 | } |
| 3245 | continue; |
| 3246 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3247 | } |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3248 | // At this point, we are done with Record/Union and array there of. |
| 3249 | // For other arrays we are down to its element type. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3250 | Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3251 | |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3252 | unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3253 | if ((ForStrongLayout && GCAttr == Qualifiers::Strong) |
| 3254 | || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) { |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3255 | if (IsUnion) { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3256 | uint64_t UnionIvarSize = FieldSize / WordSizeInBits; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3257 | if (UnionIvarSize > MaxUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3258 | MaxUnionIvarSize = UnionIvarSize; |
| 3259 | MaxField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3260 | MaxFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3261 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3262 | } else { |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3263 | IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3264 | FieldSize / WordSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3265 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3266 | } else if ((ForStrongLayout && |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3267 | (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)) |
| 3268 | || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) { |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3269 | if (IsUnion) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3270 | // FIXME: Why the asymmetry? We divide by word size in bits on other |
| 3271 | // side. |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3272 | uint64_t UnionIvarSize = FieldSize; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3273 | if (UnionIvarSize > MaxSkippedUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3274 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 3275 | MaxSkippedField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3276 | MaxSkippedFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3277 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3278 | } else { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3279 | // FIXME: Why the asymmetry, we divide by byte size in bits here? |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3280 | SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3281 | FieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3282 | } |
| 3283 | } |
| 3284 | } |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3285 | |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3286 | if (LastFieldBitfield) { |
| 3287 | // Last field was a bitfield. Must update skip info. |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3288 | Expr *BitWidth = LastFieldBitfield->getBitWidth(); |
| 3289 | uint64_t BitFieldSize = |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 3290 | BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue(); |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3291 | GC_IVAR skivar; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3292 | skivar.ivar_bytepos = BytePos + LastBitfieldOffset; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3293 | skivar.ivar_size = (BitFieldSize / ByteSizeInBits) |
| 3294 | + ((BitFieldSize % ByteSizeInBits) != 0); |
| 3295 | SkipIvars.push_back(skivar); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3296 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3297 | |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3298 | if (MaxField) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3299 | IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3300 | MaxUnionIvarSize)); |
| 3301 | if (MaxSkippedField) |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3302 | SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3303 | MaxSkippedUnionIvarSize)); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3304 | } |
| 3305 | |
| 3306 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 3307 | /// implementation for the __strong or __weak case. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3308 | /// The layout map displays which words in ivar list must be skipped |
| 3309 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 3310 | /// 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] | 3311 | /// 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] | 3312 | /// 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] | 3313 | /// represented by a 0x00 byte which also ends the string. |
| 3314 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 3315 | /// - id, Class |
| 3316 | /// - object * |
| 3317 | /// - __strong anything |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3318 | /// |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3319 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 3320 | /// - __weak anything |
| 3321 | /// |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3322 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3323 | const ObjCImplementationDecl *OMD, |
| 3324 | bool ForStrongLayout) { |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3325 | bool hasUnion = false; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3326 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3327 | unsigned int WordsToScan, WordsToSkip; |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 3328 | const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3329 | if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3330 | return llvm::Constant::getNullValue(PtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3331 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3332 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3333 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3334 | CGM.getContext().CollectObjCIvars(OI, RecFields); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3335 | |
Daniel Dunbar | 3715328 | 2009-05-04 04:10:48 +0000 | [diff] [blame] | 3336 | // Add this implementations synthesized ivars. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 3337 | llvm::SmallVector<ObjCIvarDecl*, 16> Ivars; |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 3338 | CGM.getContext().CollectNonClassIvars(OI, Ivars); |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 3339 | for (unsigned k = 0, e = Ivars.size(); k != e; ++k) |
| 3340 | RecFields.push_back(cast<FieldDecl>(Ivars[k])); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3341 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3342 | if (RecFields.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3343 | return llvm::Constant::getNullValue(PtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3344 | |
| 3345 | SkipIvars.clear(); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3346 | IvarsInfo.clear(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3347 | |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 3348 | BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion); |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3349 | if (IvarsInfo.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3350 | return llvm::Constant::getNullValue(PtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3351 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3352 | // Sort on byte position in case we encounterred a union nested in |
| 3353 | // the ivar list. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3354 | if (hasUnion && !IvarsInfo.empty()) |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 3355 | std::sort(IvarsInfo.begin(), IvarsInfo.end()); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3356 | if (hasUnion && !SkipIvars.empty()) |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 3357 | std::sort(SkipIvars.begin(), SkipIvars.end()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3358 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3359 | // Build the string of skip/scan nibbles |
Fariborz Jahanian | 8c2f2d1 | 2009-04-24 17:15:27 +0000 | [diff] [blame] | 3360 | llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3361 | unsigned int WordSize = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3362 | CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3363 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 3364 | WordsToSkip = 0; |
| 3365 | WordsToScan = IvarsInfo[0].ivar_size; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3366 | } else { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3367 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 3368 | WordsToScan = IvarsInfo[0].ivar_size; |
| 3369 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3370 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3371 | unsigned int TailPrevGCObjC = |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3372 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3373 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3374 | // consecutive 'scanned' object pointers. |
| 3375 | WordsToScan += IvarsInfo[i].ivar_size; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3376 | } else { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3377 | // Skip over 'gc'able object pointer which lay over each other. |
| 3378 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 3379 | continue; |
| 3380 | // Must skip over 1 or more words. We save current skip/scan values |
| 3381 | // and start a new pair. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3382 | SKIP_SCAN SkScan; |
| 3383 | SkScan.skip = WordsToSkip; |
| 3384 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3385 | SkipScanIvars.push_back(SkScan); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3386 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3387 | // Skip the hole. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3388 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 3389 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3390 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3391 | WordsToSkip = 0; |
| 3392 | WordsToScan = IvarsInfo[i].ivar_size; |
| 3393 | } |
| 3394 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3395 | if (WordsToScan > 0) { |
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); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3400 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3401 | |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3402 | if (!SkipIvars.empty()) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3403 | unsigned int LastIndex = SkipIvars.size()-1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3404 | int LastByteSkipped = |
| 3405 | SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3406 | LastIndex = IvarsInfo.size()-1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3407 | int LastByteScanned = |
| 3408 | IvarsInfo[LastIndex].ivar_bytepos + |
| 3409 | IvarsInfo[LastIndex].ivar_size * WordSize; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3410 | // 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] | 3411 | if (LastByteSkipped > LastByteScanned) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3412 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3413 | SKIP_SCAN SkScan; |
| 3414 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
| 3415 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3416 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3417 | } |
| 3418 | } |
| 3419 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 3420 | // as 0xMN. |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3421 | int SkipScan = SkipScanIvars.size()-1; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3422 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3423 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 3424 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 3425 | // 0xM0 followed by 0x0N detected. |
| 3426 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 3427 | for (int j = i+1; j < SkipScan; j++) |
| 3428 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 3429 | --SkipScan; |
| 3430 | } |
| 3431 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3432 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3433 | // Generate the string. |
| 3434 | std::string BitMap; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3435 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3436 | unsigned char byte; |
| 3437 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 3438 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 3439 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 3440 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
| 3441 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3442 | // first skip big. |
| 3443 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 3444 | BitMap += (unsigned char)(0xf0); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3445 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3446 | // next (skip small, scan) |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3447 | if (skip_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3448 | byte = skip_small << 4; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3449 | if (scan_big > 0) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3450 | byte |= 0xf; |
| 3451 | --scan_big; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3452 | } else if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3453 | byte |= scan_small; |
| 3454 | scan_small = 0; |
| 3455 | } |
| 3456 | BitMap += byte; |
| 3457 | } |
| 3458 | // next scan big |
| 3459 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 3460 | BitMap += (unsigned char)(0x0f); |
| 3461 | // last scan small |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3462 | if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3463 | byte = scan_small; |
| 3464 | BitMap += byte; |
| 3465 | } |
| 3466 | } |
| 3467 | // null terminate string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3468 | unsigned char zero = 0; |
| 3469 | BitMap += zero; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3470 | |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3471 | if (CGM.getLangOptions().ObjCGCBitmapPrint) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3472 | printf("\n%s ivar layout for class '%s': ", |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3473 | ForStrongLayout ? "strong" : "weak", |
| 3474 | OMD->getClassInterface()->getNameAsCString()); |
| 3475 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
| 3476 | for (unsigned i = 0; i < BitMap.size(); i++) |
| 3477 | if (!(s[i] & 0xf0)) |
| 3478 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3479 | else |
| 3480 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3481 | printf("\n"); |
| 3482 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3483 | llvm::GlobalVariable * Entry = |
| 3484 | CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3485 | llvm::ConstantArray::get(VMContext, BitMap.c_str()), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3486 | "__TEXT,__cstring,cstring_literals", |
| 3487 | 1, true); |
| 3488 | return getConstantGEP(VMContext, Entry, 0, 0); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3489 | } |
| 3490 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3491 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3492 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 3493 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3494 | // FIXME: Avoid std::string copying. |
| 3495 | if (!Entry) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3496 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3497 | llvm::ConstantArray::get(VMContext, Sel.getAsString()), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3498 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3499 | 1, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3500 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3501 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3504 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3505 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3506 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 3507 | } |
| 3508 | |
| 3509 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3510 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3511 | return GetMethodVarName(&CGM.getContext().Idents.get(Name)); |
| 3512 | } |
| 3513 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 3514 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3515 | std::string TypeStr; |
| 3516 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 3517 | |
| 3518 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3519 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3520 | if (!Entry) |
| 3521 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3522 | llvm::ConstantArray::get(VMContext, TypeStr), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3523 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3524 | 1, true); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3525 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3526 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3527 | } |
| 3528 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3529 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3530 | std::string TypeStr; |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3531 | CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D), |
| 3532 | TypeStr); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3533 | |
| 3534 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 3535 | |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3536 | if (!Entry) |
| 3537 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3538 | llvm::ConstantArray::get(VMContext, TypeStr), |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3539 | "__TEXT,__cstring,cstring_literals", |
| 3540 | 1, true); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3541 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3542 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3543 | } |
| 3544 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3545 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3546 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3547 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3548 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3549 | if (!Entry) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3550 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3551 | llvm::ConstantArray::get(VMContext, |
| 3552 | Ident->getNameStart()), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3553 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3554 | 1, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +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 | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3557 | } |
| 3558 | |
| 3559 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3560 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3561 | llvm::Constant * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3562 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 3563 | const Decl *Container) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3564 | std::string TypeStr; |
| 3565 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3566 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 3567 | } |
| 3568 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3569 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3570 | const ObjCContainerDecl *CD, |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 3571 | llvm::SmallVectorImpl<char> &Name) { |
| 3572 | llvm::raw_svector_ostream OS(Name); |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 3573 | assert (CD && "Missing container decl in GetNameForMethod"); |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 3574 | OS << '\01' << (D->isInstanceMethod() ? '-' : '+') |
| 3575 | << '[' << CD->getName(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3576 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 3577 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) |
| 3578 | OS << '(' << CID->getNameAsString() << ')'; |
| 3579 | OS << ' ' << D->getSelector().getAsString() << ']'; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3580 | } |
| 3581 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3582 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3583 | EmitModuleInfo(); |
| 3584 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3585 | // Emit the dummy bodies for any protocols which were referenced but |
| 3586 | // never defined. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3587 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 3588 | I = Protocols.begin(), e = Protocols.end(); I != e; ++I) { |
| 3589 | if (I->second->hasInitializer()) |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3590 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3591 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3592 | std::vector<llvm::Constant*> Values(5); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3593 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 3594 | Values[1] = GetClassName(I->first); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3595 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3596 | Values[3] = Values[4] = |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3597 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 3598 | I->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3599 | I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3600 | Values)); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 3601 | CGM.AddUsedGlobal(I->second); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3602 | } |
| 3603 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3604 | // Add assembler directives to add lazy undefined symbol references |
| 3605 | // for classes which are referenced but not defined. This is |
| 3606 | // important for correct linker interaction. |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 3607 | // |
| 3608 | // FIXME: It would be nice if we had an LLVM construct for this. |
| 3609 | if (!LazySymbols.empty() || !DefinedSymbols.empty()) { |
| 3610 | llvm::SmallString<256> Asm; |
| 3611 | Asm += CGM.getModule().getModuleInlineAsm(); |
| 3612 | if (!Asm.empty() && Asm.back() != '\n') |
| 3613 | Asm += '\n'; |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3614 | |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 3615 | llvm::raw_svector_ostream OS(Asm); |
| 3616 | for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(), |
| 3617 | e = LazySymbols.end(); I != e; ++I) |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 3618 | OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n"; |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 3619 | for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(), |
| 3620 | e = DefinedSymbols.end(); I != e; ++I) |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 3621 | OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n" |
| 3622 | << "\t.globl .objc_class_name_" << (*I)->getName() << "\n"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3623 | |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 3624 | CGM.getModule().setModuleInlineAsm(OS.str()); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3625 | } |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3626 | } |
| 3627 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3628 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3629 | : CGObjCCommonMac(cgm), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3630 | ObjCTypes(cgm) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3631 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3632 | ObjCABI = 2; |
| 3633 | } |
| 3634 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3635 | /* *** */ |
| 3636 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3637 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3638 | : VMContext(cgm.getLLVMContext()), CGM(cgm) { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3639 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3640 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3641 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3642 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3643 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3644 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3645 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 3646 | Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3647 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3648 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3649 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3650 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3651 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3652 | // FIXME: It would be nice to unify this with the opaque type, so that the IR |
| 3653 | // comes out a bit cleaner. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3654 | const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3655 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3656 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3657 | // I'm not sure I like this. The implicit coordination is a bit |
| 3658 | // gross. We should solve this in a reasonable fashion because this |
| 3659 | // is a pretty common task (match some runtime data structure with |
| 3660 | // an LLVM data structure). |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3661 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3662 | // FIXME: This is leaked. |
| 3663 | // FIXME: Merge with rewriter code? |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3664 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3665 | // struct _objc_super { |
| 3666 | // id self; |
| 3667 | // Class cls; |
| 3668 | // } |
| 3669 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3670 | SourceLocation(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3671 | &Ctx.Idents.get("_objc_super")); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3672 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 3673 | Ctx.getObjCIdType(), 0, 0, false)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3674 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 3675 | Ctx.getObjCClassType(), 0, 0, false)); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3676 | RD->completeDefinition(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3677 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3678 | SuperCTy = Ctx.getTagDeclType(RD); |
| 3679 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3680 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3681 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3682 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 3683 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3684 | // struct _prop_t { |
| 3685 | // char *name; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3686 | // char *attributes; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3687 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3688 | PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3689 | CGM.getModule().addTypeName("struct._prop_t", |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3690 | PropertyTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3691 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3692 | // struct _prop_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3693 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3694 | // uint32_t count_of_properties; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3695 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3696 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3697 | PropertyListTy = llvm::StructType::get(VMContext, IntTy, |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3698 | IntTy, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3699 | llvm::ArrayType::get(PropertyTy, 0), |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3700 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3701 | CGM.getModule().addTypeName("struct._prop_list_t", |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3702 | PropertyListTy); |
| 3703 | // struct _prop_list_t * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3704 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3705 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3706 | // struct _objc_method { |
| 3707 | // SEL _cmd; |
| 3708 | // char *method_type; |
| 3709 | // char *_imp; |
| 3710 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3711 | MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy, |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3712 | Int8PtrTy, |
| 3713 | Int8PtrTy, |
| 3714 | NULL); |
| 3715 | CGM.getModule().addTypeName("struct._objc_method", MethodTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3716 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3717 | // struct _objc_cache * |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 3718 | CacheTy = llvm::OpaqueType::get(VMContext); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3719 | CGM.getModule().addTypeName("struct._objc_cache", CacheTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3720 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3721 | } |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3722 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3723 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3724 | : ObjCCommonTypesHelper(cgm) { |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3725 | // struct _objc_method_description { |
| 3726 | // SEL name; |
| 3727 | // char *types; |
| 3728 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3729 | MethodDescriptionTy = |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3730 | llvm::StructType::get(VMContext, SelectorPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3731 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3732 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3733 | CGM.getModule().addTypeName("struct._objc_method_description", |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3734 | MethodDescriptionTy); |
| 3735 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3736 | // struct _objc_method_description_list { |
| 3737 | // int count; |
| 3738 | // struct _objc_method_description[1]; |
| 3739 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3740 | MethodDescriptionListTy = |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3741 | llvm::StructType::get(VMContext, IntTy, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3742 | llvm::ArrayType::get(MethodDescriptionTy, 0), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3743 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3744 | CGM.getModule().addTypeName("struct._objc_method_description_list", |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3745 | MethodDescriptionListTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3746 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3747 | // struct _objc_method_description_list * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3748 | MethodDescriptionListPtrTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3749 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3750 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3751 | // Protocol description structures |
| 3752 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3753 | // struct _objc_protocol_extension { |
| 3754 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 3755 | // struct _objc_method_description_list *optional_instance_methods; |
| 3756 | // struct _objc_method_description_list *optional_class_methods; |
| 3757 | // struct _objc_property_list *instance_properties; |
| 3758 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3759 | ProtocolExtensionTy = |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3760 | llvm::StructType::get(VMContext, IntTy, |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3761 | MethodDescriptionListPtrTy, |
| 3762 | MethodDescriptionListPtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3763 | PropertyListPtrTy, |
| 3764 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3765 | CGM.getModule().addTypeName("struct._objc_protocol_extension", |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3766 | ProtocolExtensionTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3767 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3768 | // struct _objc_protocol_extension * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3769 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3770 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3771 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3772 | |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 3773 | llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext); |
| 3774 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3775 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3776 | const llvm::Type *T = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3777 | llvm::StructType::get(VMContext, |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3778 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3779 | LongTy, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3780 | llvm::ArrayType::get(ProtocolTyHolder, 0), |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3781 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3782 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T); |
| 3783 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3784 | // struct _objc_protocol { |
| 3785 | // struct _objc_protocol_extension *isa; |
| 3786 | // char *protocol_name; |
| 3787 | // struct _objc_protocol **_objc_protocol_list; |
| 3788 | // struct _objc_method_description_list *instance_methods; |
| 3789 | // struct _objc_method_description_list *class_methods; |
| 3790 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3791 | T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3792 | Int8PtrTy, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3793 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3794 | MethodDescriptionListPtrTy, |
| 3795 | MethodDescriptionListPtrTy, |
| 3796 | NULL); |
| 3797 | cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T); |
| 3798 | |
| 3799 | ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3800 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3801 | ProtocolListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3802 | // struct _objc_protocol_list * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3803 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3804 | |
| 3805 | ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get()); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3806 | CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3807 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3808 | |
| 3809 | // Class description structures |
| 3810 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3811 | // struct _objc_ivar { |
| 3812 | // char *ivar_name; |
| 3813 | // char *ivar_type; |
| 3814 | // int ivar_offset; |
| 3815 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3816 | IvarTy = llvm::StructType::get(VMContext, Int8PtrTy, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3817 | Int8PtrTy, |
| 3818 | IntTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3819 | NULL); |
| 3820 | CGM.getModule().addTypeName("struct._objc_ivar", IvarTy); |
| 3821 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3822 | // struct _objc_ivar_list * |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 3823 | IvarListTy = llvm::OpaqueType::get(VMContext); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3824 | CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3825 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3826 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3827 | // struct _objc_method_list * |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 3828 | MethodListTy = llvm::OpaqueType::get(VMContext); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3829 | CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3830 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3831 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3832 | // struct _objc_class_extension * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3833 | ClassExtensionTy = |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3834 | llvm::StructType::get(VMContext, IntTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3835 | Int8PtrTy, |
| 3836 | PropertyListPtrTy, |
| 3837 | NULL); |
| 3838 | CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3839 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3840 | |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 3841 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3842 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3843 | // struct _objc_class { |
| 3844 | // Class isa; |
| 3845 | // Class super_class; |
| 3846 | // char *name; |
| 3847 | // long version; |
| 3848 | // long info; |
| 3849 | // long instance_size; |
| 3850 | // struct _objc_ivar_list *ivars; |
| 3851 | // struct _objc_method_list *methods; |
| 3852 | // struct _objc_cache *cache; |
| 3853 | // struct _objc_protocol_list *protocols; |
| 3854 | // char *ivar_layout; |
| 3855 | // struct _objc_class_ext *ext; |
| 3856 | // }; |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3857 | T = llvm::StructType::get(VMContext, |
| 3858 | llvm::PointerType::getUnqual(ClassTyHolder), |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3859 | llvm::PointerType::getUnqual(ClassTyHolder), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3860 | Int8PtrTy, |
| 3861 | LongTy, |
| 3862 | LongTy, |
| 3863 | LongTy, |
| 3864 | IvarListPtrTy, |
| 3865 | MethodListPtrTy, |
| 3866 | CachePtrTy, |
| 3867 | ProtocolListPtrTy, |
| 3868 | Int8PtrTy, |
| 3869 | ClassExtensionPtrTy, |
| 3870 | NULL); |
| 3871 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3872 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3873 | ClassTy = cast<llvm::StructType>(ClassTyHolder.get()); |
| 3874 | CGM.getModule().addTypeName("struct._objc_class", ClassTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3875 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3876 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3877 | // struct _objc_category { |
| 3878 | // char *category_name; |
| 3879 | // char *class_name; |
| 3880 | // struct _objc_method_list *instance_method; |
| 3881 | // struct _objc_method_list *class_method; |
| 3882 | // uint32_t size; // sizeof(struct _objc_category) |
| 3883 | // struct _objc_property_list *instance_properties;// category's @property |
| 3884 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3885 | CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3886 | Int8PtrTy, |
| 3887 | MethodListPtrTy, |
| 3888 | MethodListPtrTy, |
| 3889 | ProtocolListPtrTy, |
| 3890 | IntTy, |
| 3891 | PropertyListPtrTy, |
| 3892 | NULL); |
| 3893 | CGM.getModule().addTypeName("struct._objc_category", CategoryTy); |
| 3894 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3895 | // Global metadata structures |
| 3896 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3897 | // struct _objc_symtab { |
| 3898 | // long sel_ref_cnt; |
| 3899 | // SEL *refs; |
| 3900 | // short cls_def_cnt; |
| 3901 | // short cat_def_cnt; |
| 3902 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 3903 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3904 | SymtabTy = llvm::StructType::get(VMContext, LongTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3905 | SelectorPtrTy, |
| 3906 | ShortTy, |
| 3907 | ShortTy, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3908 | llvm::ArrayType::get(Int8PtrTy, 0), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3909 | NULL); |
| 3910 | CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3911 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3912 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3913 | // struct _objc_module { |
| 3914 | // long version; |
| 3915 | // long size; // sizeof(struct _objc_module) |
| 3916 | // char *name; |
| 3917 | // struct _objc_symtab* symtab; |
| 3918 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3919 | ModuleTy = |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3920 | llvm::StructType::get(VMContext, LongTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3921 | LongTy, |
| 3922 | Int8PtrTy, |
| 3923 | SymtabPtrTy, |
| 3924 | NULL); |
| 3925 | CGM.getModule().addTypeName("struct._objc_module", ModuleTy); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3926 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3927 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3928 | // FIXME: This is the size of the setjmp buffer and should be target |
| 3929 | // specific. 18 is what's used on 32-bit X86. |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3930 | uint64_t SetJmpBufferSize = 18; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3931 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3932 | // Exceptions |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3933 | const llvm::Type *StackPtrTy = llvm::ArrayType::get( |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 3934 | llvm::Type::getInt8PtrTy(VMContext), 4); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3935 | |
| 3936 | ExceptionDataTy = |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3937 | llvm::StructType::get(VMContext, llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext), |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3938 | SetJmpBufferSize), |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3939 | StackPtrTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3940 | CGM.getModule().addTypeName("struct._objc_exception_data", |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3941 | ExceptionDataTy); |
| 3942 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3943 | } |
| 3944 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3945 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3946 | : ObjCCommonTypesHelper(cgm) { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3947 | // struct _method_list_t { |
| 3948 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 3949 | // uint32_t method_count; |
| 3950 | // struct _objc_method method_list[method_count]; |
| 3951 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3952 | MethodListnfABITy = llvm::StructType::get(VMContext, IntTy, |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3953 | IntTy, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3954 | llvm::ArrayType::get(MethodTy, 0), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3955 | NULL); |
| 3956 | CGM.getModule().addTypeName("struct.__method_list_t", |
| 3957 | MethodListnfABITy); |
| 3958 | // struct method_list_t * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3959 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3960 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3961 | // struct _protocol_t { |
| 3962 | // id isa; // NULL |
| 3963 | // const char * const protocol_name; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3964 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3965 | // const struct method_list_t * const instance_methods; |
| 3966 | // const struct method_list_t * const class_methods; |
| 3967 | // const struct method_list_t *optionalInstanceMethods; |
| 3968 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3969 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3970 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 3971 | // const uint32_t flags; // = 0 |
| 3972 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3973 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3974 | // Holder for struct _protocol_list_t * |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 3975 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3976 | |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3977 | ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy, |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3978 | Int8PtrTy, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3979 | llvm::PointerType::getUnqual( |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3980 | ProtocolListTyHolder), |
| 3981 | MethodListnfABIPtrTy, |
| 3982 | MethodListnfABIPtrTy, |
| 3983 | MethodListnfABIPtrTy, |
| 3984 | MethodListnfABIPtrTy, |
| 3985 | PropertyListPtrTy, |
| 3986 | IntTy, |
| 3987 | IntTy, |
| 3988 | NULL); |
| 3989 | CGM.getModule().addTypeName("struct._protocol_t", |
| 3990 | ProtocolnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3991 | |
| 3992 | // struct _protocol_t* |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3993 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3994 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3995 | // struct _protocol_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3996 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3997 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3998 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3999 | ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4000 | llvm::ArrayType::get( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4001 | ProtocolnfABIPtrTy, 0), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4002 | NULL); |
| 4003 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 4004 | ProtocolListnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4005 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4006 | ProtocolListnfABITy); |
| 4007 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4008 | // struct _objc_protocol_list* |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4009 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4010 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4011 | // struct _ivar_t { |
| 4012 | // unsigned long int *offset; // pointer to ivar offset location |
| 4013 | // char *name; |
| 4014 | // char *type; |
| 4015 | // uint32_t alignment; |
| 4016 | // uint32_t size; |
| 4017 | // } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4018 | IvarnfABITy = llvm::StructType::get(VMContext, |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4019 | llvm::PointerType::getUnqual(LongTy), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4020 | Int8PtrTy, |
| 4021 | Int8PtrTy, |
| 4022 | IntTy, |
| 4023 | IntTy, |
| 4024 | NULL); |
| 4025 | CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4026 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4027 | // struct _ivar_list_t { |
| 4028 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 4029 | // uint32 count; |
| 4030 | // struct _iver_t list[count]; |
| 4031 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4032 | IvarListnfABITy = llvm::StructType::get(VMContext, IntTy, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4033 | IntTy, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4034 | llvm::ArrayType::get( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4035 | IvarnfABITy, 0), |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4036 | NULL); |
| 4037 | CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4038 | |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4039 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4040 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4041 | // struct _class_ro_t { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4042 | // uint32_t const flags; |
| 4043 | // uint32_t const instanceStart; |
| 4044 | // uint32_t const instanceSize; |
| 4045 | // uint32_t const reserved; // only when building for 64bit targets |
| 4046 | // const uint8_t * const ivarLayout; |
| 4047 | // const char *const name; |
| 4048 | // const struct _method_list_t * const baseMethods; |
| 4049 | // const struct _objc_protocol_list *const baseProtocols; |
| 4050 | // const struct _ivar_list_t *const ivars; |
| 4051 | // const uint8_t * const weakIvarLayout; |
| 4052 | // const struct _prop_list_t * const properties; |
| 4053 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4054 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4055 | // FIXME. Add 'reserved' field in 64bit abi mode! |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4056 | ClassRonfABITy = llvm::StructType::get(VMContext, IntTy, |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4057 | IntTy, |
| 4058 | IntTy, |
| 4059 | Int8PtrTy, |
| 4060 | Int8PtrTy, |
| 4061 | MethodListnfABIPtrTy, |
| 4062 | ProtocolListnfABIPtrTy, |
| 4063 | IvarListnfABIPtrTy, |
| 4064 | Int8PtrTy, |
| 4065 | PropertyListPtrTy, |
| 4066 | NULL); |
| 4067 | CGM.getModule().addTypeName("struct._class_ro_t", |
| 4068 | ClassRonfABITy); |
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 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 4071 | std::vector<const llvm::Type*> Params; |
| 4072 | Params.push_back(ObjectPtrTy); |
| 4073 | Params.push_back(SelectorPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4074 | ImpnfABITy = llvm::PointerType::getUnqual( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4075 | llvm::FunctionType::get(ObjectPtrTy, Params, false)); |
| 4076 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4077 | // struct _class_t { |
| 4078 | // struct _class_t *isa; |
| 4079 | // struct _class_t * const superclass; |
| 4080 | // void *cache; |
| 4081 | // IMP *vtable; |
| 4082 | // struct class_ro_t *ro; |
| 4083 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4084 | |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 4085 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4086 | ClassnfABITy = |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4087 | llvm::StructType::get(VMContext, |
| 4088 | llvm::PointerType::getUnqual(ClassTyHolder), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4089 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 4090 | CachePtrTy, |
| 4091 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 4092 | llvm::PointerType::getUnqual(ClassRonfABITy), |
| 4093 | NULL); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4094 | CGM.getModule().addTypeName("struct._class_t", ClassnfABITy); |
| 4095 | |
| 4096 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4097 | ClassnfABITy); |
| 4098 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4099 | // LLVM for struct _class_t * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4100 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4101 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4102 | // struct _category_t { |
| 4103 | // const char * const name; |
| 4104 | // struct _class_t *const cls; |
| 4105 | // const struct _method_list_t * const instance_methods; |
| 4106 | // const struct _method_list_t * const class_methods; |
| 4107 | // const struct _protocol_list_t * const protocols; |
| 4108 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 4109 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4110 | CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4111 | ClassnfABIPtrTy, |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 4112 | MethodListnfABIPtrTy, |
| 4113 | MethodListnfABIPtrTy, |
| 4114 | ProtocolListnfABIPtrTy, |
| 4115 | PropertyListPtrTy, |
| 4116 | NULL); |
| 4117 | CGM.getModule().addTypeName("struct._category_t", CategorynfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4118 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4119 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4120 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 4121 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4122 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4123 | // MessageRefTy - LLVM for: |
| 4124 | // struct _message_ref_t { |
| 4125 | // IMP messenger; |
| 4126 | // SEL name; |
| 4127 | // }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4128 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4129 | // First the clang type for struct _message_ref_t |
| 4130 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 4131 | SourceLocation(), |
| 4132 | &Ctx.Idents.get("_message_ref_t")); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4133 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 4134 | Ctx.VoidPtrTy, 0, 0, false)); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4135 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 4136 | Ctx.getObjCSelType(), 0, 0, false)); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4137 | RD->completeDefinition(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4138 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4139 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 4140 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 4141 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4142 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4143 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4144 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4145 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4146 | // SuperMessageRefTy - LLVM for: |
| 4147 | // struct _super_message_ref_t { |
| 4148 | // SUPER_IMP messenger; |
| 4149 | // SEL name; |
| 4150 | // }; |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4151 | SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy, |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4152 | SelectorPtrTy, |
| 4153 | NULL); |
| 4154 | CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4155 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4156 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4157 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
| 4158 | |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 4159 | |
| 4160 | // struct objc_typeinfo { |
| 4161 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 4162 | // const char* name; // c++ typeinfo string |
| 4163 | // Class cls; |
| 4164 | // }; |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4165 | EHTypeTy = llvm::StructType::get(VMContext, |
| 4166 | llvm::PointerType::getUnqual(Int8PtrTy), |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 4167 | Int8PtrTy, |
| 4168 | ClassnfABIPtrTy, |
| 4169 | NULL); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 4170 | CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4171 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4172 | } |
| 4173 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4174 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4175 | FinishNonFragileABIModule(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4176 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4177 | return NULL; |
| 4178 | } |
| 4179 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4180 | void CGObjCNonFragileABIMac::AddModuleClassList(const |
| 4181 | std::vector<llvm::GlobalValue*> |
| 4182 | &Container, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4183 | const char *SymbolName, |
| 4184 | const char *SectionName) { |
| 4185 | unsigned NumClasses = Container.size(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4186 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4187 | if (!NumClasses) |
| 4188 | return; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4189 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4190 | std::vector<llvm::Constant*> Symbols(NumClasses); |
| 4191 | for (unsigned i=0; i<NumClasses; i++) |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4192 | Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i], |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4193 | ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4194 | llvm::Constant* Init = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4195 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4196 | NumClasses), |
| 4197 | Symbols); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4198 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4199 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4200 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4201 | llvm::GlobalValue::InternalLinkage, |
| 4202 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4203 | SymbolName); |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4204 | GV->setAlignment(8); |
| 4205 | GV->setSection(SectionName); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4206 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4207 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4208 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4209 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 4210 | // nonfragile abi has no module definition. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4211 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4212 | // Build list of all implemented class addresses in array |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4213 | // L_OBJC_LABEL_CLASS_$. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4214 | AddModuleClassList(DefinedClasses, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4215 | "\01L_OBJC_LABEL_CLASS_$", |
| 4216 | "__DATA, __objc_classlist, regular, no_dead_strip"); |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4217 | |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4218 | for (unsigned i = 0; i < DefinedClasses.size(); i++) { |
| 4219 | llvm::GlobalValue *IMPLGV = DefinedClasses[i]; |
| 4220 | if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage) |
| 4221 | continue; |
| 4222 | IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4223 | } |
| 4224 | |
Fariborz Jahanian | 0e93d25 | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 4225 | for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) { |
| 4226 | llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i]; |
| 4227 | if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage) |
| 4228 | continue; |
| 4229 | IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 4230 | } |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4231 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4232 | AddModuleClassList(DefinedNonLazyClasses, |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4233 | "\01L_OBJC_LABEL_NONLAZY_CLASS_$", |
| 4234 | "__DATA, __objc_nlclslist, regular, no_dead_strip"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4235 | |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4236 | // Build list of all implemented category addresses in array |
| 4237 | // L_OBJC_LABEL_CATEGORY_$. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4238 | AddModuleClassList(DefinedCategories, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4239 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 4240 | "__DATA, __objc_catlist, regular, no_dead_strip"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4241 | AddModuleClassList(DefinedNonLazyCategories, |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4242 | "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$", |
| 4243 | "__DATA, __objc_nlcatlist, regular, no_dead_strip"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4244 | |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4245 | // static int L_OBJC_IMAGE_INFO[2] = { 0, flags }; |
| 4246 | // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0 |
| 4247 | std::vector<llvm::Constant*> Values(2); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4248 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0); |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4249 | unsigned int flags = 0; |
Fariborz Jahanian | 66a5c2c | 2009-02-24 23:34:44 +0000 | [diff] [blame] | 4250 | // FIXME: Fix and continue? |
| 4251 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 4252 | flags |= eImageInfo_GarbageCollected; |
| 4253 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 4254 | flags |= eImageInfo_GCOnly; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4255 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 4256 | llvm::Constant* Init = llvm::ConstantArray::get( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4257 | llvm::ArrayType::get(ObjCTypes.IntTy, 2), |
| 4258 | Values); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4259 | llvm::GlobalVariable *IMGV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4260 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4261 | llvm::GlobalValue::InternalLinkage, |
| 4262 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4263 | "\01L_OBJC_IMAGE_INFO"); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4264 | IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); |
Daniel Dunbar | 325f758 | 2009-04-23 08:03:21 +0000 | [diff] [blame] | 4265 | IMGV->setConstant(true); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4266 | CGM.AddUsedGlobal(IMGV); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4267 | } |
| 4268 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4269 | /// LegacyDispatchedSelector - Returns true if SEL is not in the list of |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4270 | /// NonLegacyDispatchMethods; false otherwise. What this means is that |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4271 | /// except for the 19 selectors in the list, we generate 32bit-style |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4272 | /// message dispatch call for all the rest. |
| 4273 | /// |
| 4274 | bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) { |
Daniel Dunbar | 2feefe8 | 2010-02-01 21:07:33 +0000 | [diff] [blame] | 4275 | if (CGM.getCodeGenOpts().ObjCLegacyDispatch) |
| 4276 | return true; |
| 4277 | |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4278 | if (NonLegacyDispatchMethods.empty()) { |
| 4279 | NonLegacyDispatchMethods.insert(GetNullarySelector("alloc")); |
| 4280 | NonLegacyDispatchMethods.insert(GetNullarySelector("class")); |
| 4281 | NonLegacyDispatchMethods.insert(GetNullarySelector("self")); |
| 4282 | NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped")); |
| 4283 | NonLegacyDispatchMethods.insert(GetNullarySelector("length")); |
| 4284 | NonLegacyDispatchMethods.insert(GetNullarySelector("count")); |
| 4285 | NonLegacyDispatchMethods.insert(GetNullarySelector("retain")); |
| 4286 | NonLegacyDispatchMethods.insert(GetNullarySelector("release")); |
| 4287 | NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease")); |
| 4288 | NonLegacyDispatchMethods.insert(GetNullarySelector("hash")); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4289 | |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4290 | NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone")); |
| 4291 | NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass")); |
| 4292 | NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector")); |
| 4293 | NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey")); |
| 4294 | NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex")); |
| 4295 | NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString")); |
| 4296 | NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual")); |
| 4297 | NonLegacyDispatchMethods.insert(GetUnarySelector("addObject")); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4298 | // "countByEnumeratingWithState:objects:count" |
Fariborz Jahanian | be53be4 | 2009-05-13 16:19:02 +0000 | [diff] [blame] | 4299 | IdentifierInfo *KeyIdents[] = { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4300 | &CGM.getContext().Idents.get("countByEnumeratingWithState"), |
| 4301 | &CGM.getContext().Idents.get("objects"), |
| 4302 | &CGM.getContext().Idents.get("count") |
Fariborz Jahanian | be53be4 | 2009-05-13 16:19:02 +0000 | [diff] [blame] | 4303 | }; |
| 4304 | NonLegacyDispatchMethods.insert( |
| 4305 | CGM.getContext().Selectors.getSelector(3, KeyIdents)); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4306 | } |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4307 | return (NonLegacyDispatchMethods.count(Sel) == 0); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4308 | } |
| 4309 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4310 | // Metadata flags |
| 4311 | enum MetaDataDlags { |
| 4312 | CLS = 0x0, |
| 4313 | CLS_META = 0x1, |
| 4314 | CLS_ROOT = 0x2, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4315 | OBJC2_CLS_HIDDEN = 0x10, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4316 | CLS_EXCEPTION = 0x20 |
| 4317 | }; |
| 4318 | /// BuildClassRoTInitializer - generate meta-data for: |
| 4319 | /// struct _class_ro_t { |
| 4320 | /// uint32_t const flags; |
| 4321 | /// uint32_t const instanceStart; |
| 4322 | /// uint32_t const instanceSize; |
| 4323 | /// uint32_t const reserved; // only when building for 64bit targets |
| 4324 | /// const uint8_t * const ivarLayout; |
| 4325 | /// const char *const name; |
| 4326 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4327 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4328 | /// const struct _ivar_list_t *const ivars; |
| 4329 | /// const uint8_t * const weakIvarLayout; |
| 4330 | /// const struct _prop_list_t * const properties; |
| 4331 | /// } |
| 4332 | /// |
| 4333 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4334 | unsigned flags, |
| 4335 | unsigned InstanceStart, |
| 4336 | unsigned InstanceSize, |
| 4337 | const ObjCImplementationDecl *ID) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4338 | std::string ClassName = ID->getNameAsString(); |
| 4339 | std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets! |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4340 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 4341 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 4342 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4343 | // FIXME. For 64bit targets add 0 here. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4344 | Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4345 | : BuildIvarLayout(ID, true); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4346 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4347 | // const struct _method_list_t * const baseMethods; |
| 4348 | std::vector<llvm::Constant*> Methods; |
| 4349 | std::string MethodListName("\01l_OBJC_$_"); |
| 4350 | if (flags & CLS_META) { |
| 4351 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4352 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4353 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4354 | // Class methods should always be defined. |
| 4355 | Methods.push_back(GetMethodConstant(*i)); |
| 4356 | } |
| 4357 | } else { |
| 4358 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4359 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4360 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4361 | // Instance methods should always be defined. |
| 4362 | Methods.push_back(GetMethodConstant(*i)); |
| 4363 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4364 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4365 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4366 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4367 | |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4368 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 4369 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4370 | |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4371 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 4372 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4373 | Methods.push_back(C); |
| 4374 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 4375 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4376 | Methods.push_back(C); |
| 4377 | } |
| 4378 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4379 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4380 | Values[ 5] = EmitMethodList(MethodListName, |
| 4381 | "__DATA, __objc_const", Methods); |
| 4382 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4383 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4384 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4385 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4386 | + OID->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4387 | OID->protocol_begin(), |
| 4388 | OID->protocol_end()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4389 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4390 | if (flags & CLS_META) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4391 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4392 | else |
| 4393 | Values[ 7] = EmitIvarList(ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4394 | Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4395 | : BuildIvarLayout(ID, false); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4396 | if (flags & CLS_META) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4397 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4398 | else |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4399 | Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(), |
| 4400 | ID, ID->getClassInterface(), ObjCTypes); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4401 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4402 | Values); |
| 4403 | llvm::GlobalVariable *CLASS_RO_GV = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4404 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false, |
| 4405 | llvm::GlobalValue::InternalLinkage, |
| 4406 | Init, |
| 4407 | (flags & CLS_META) ? |
| 4408 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 4409 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4410 | CLASS_RO_GV->setAlignment( |
| 4411 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4412 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4413 | return CLASS_RO_GV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4414 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4415 | } |
| 4416 | |
| 4417 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 4418 | /// for the given ClassName for: |
| 4419 | /// struct _class_t { |
| 4420 | /// struct _class_t *isa; |
| 4421 | /// struct _class_t * const superclass; |
| 4422 | /// void *cache; |
| 4423 | /// IMP *vtable; |
| 4424 | /// struct class_ro_t *ro; |
| 4425 | /// } |
| 4426 | /// |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4427 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4428 | std::string &ClassName, |
| 4429 | llvm::Constant *IsAGV, |
| 4430 | llvm::Constant *SuperClassGV, |
| 4431 | llvm::Constant *ClassRoGV, |
| 4432 | bool HiddenVisibility) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4433 | std::vector<llvm::Constant*> Values(5); |
| 4434 | Values[0] = IsAGV; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4435 | Values[1] = SuperClassGV; |
| 4436 | if (!Values[1]) |
| 4437 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4438 | Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar |
| 4439 | Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar |
| 4440 | Values[4] = ClassRoGV; // &CLASS_RO_GV |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4441 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4442 | Values); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4443 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 4444 | GV->setInitializer(Init); |
Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 4445 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4446 | GV->setAlignment( |
| 4447 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4448 | if (HiddenVisibility) |
| 4449 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4450 | return GV; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4451 | } |
| 4452 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4453 | bool |
Fariborz Jahanian | ecfbdcb | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 4454 | CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4455 | return OD->getClassMethod(GetNullarySelector("load")) != 0; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4456 | } |
| 4457 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4458 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4459 | uint32_t &InstanceStart, |
| 4460 | uint32_t &InstanceSize) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4461 | const ASTRecordLayout &RL = |
Daniel Dunbar | b4c79e0 | 2009-05-04 21:26:30 +0000 | [diff] [blame] | 4462 | CGM.getContext().getASTObjCImplementationLayout(OID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4463 | |
Daniel Dunbar | 6e8575b | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 4464 | // InstanceSize is really instance end. |
Anders Carlsson | 243a685 | 2009-07-18 21:26:44 +0000 | [diff] [blame] | 4465 | InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8; |
Daniel Dunbar | 6e8575b | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 4466 | |
| 4467 | // If there are no fields, the start is the same as the end. |
| 4468 | if (!RL.getFieldCount()) |
| 4469 | InstanceStart = InstanceSize; |
| 4470 | else |
| 4471 | InstanceStart = RL.getFieldOffset(0) / 8; |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4472 | } |
| 4473 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4474 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 4475 | std::string ClassName = ID->getNameAsString(); |
| 4476 | if (!ObjCEmptyCacheVar) { |
| 4477 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4478 | CGM.getModule(), |
| 4479 | ObjCTypes.CacheTy, |
| 4480 | false, |
| 4481 | llvm::GlobalValue::ExternalLinkage, |
| 4482 | 0, |
| 4483 | "_objc_empty_cache"); |
| 4484 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4485 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4486 | CGM.getModule(), |
| 4487 | ObjCTypes.ImpnfABITy, |
| 4488 | false, |
| 4489 | llvm::GlobalValue::ExternalLinkage, |
| 4490 | 0, |
| 4491 | "_objc_empty_vtable"); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4492 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4493 | assert(ID->getClassInterface() && |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4494 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 4495 | // FIXME: Is this correct (that meta class size is never computed)? |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4496 | uint32_t InstanceStart = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4497 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4498 | uint32_t InstanceSize = InstanceStart; |
| 4499 | uint32_t flags = CLS_META; |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4500 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 4501 | std::string ObjCClassName(getClassSymbolPrefix()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4502 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4503 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4504 | |
| 4505 | bool classIsHidden = |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4506 | CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4507 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4508 | flags |= OBJC2_CLS_HIDDEN; |
| 4509 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4510 | // class is root |
| 4511 | flags |= CLS_ROOT; |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4512 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4513 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4514 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4515 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4516 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 4517 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 4518 | Root = Super; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4519 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); |
Fariborz Jahanian | 0e93d25 | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 4520 | if (Root->hasAttr<WeakImportAttr>()) |
| 4521 | IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4522 | // work on super class metadata symbol. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4523 | std::string SuperClassName = |
Fariborz Jahanian | 0e93d25 | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 4524 | ObjCMetaClassName + |
| 4525 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4526 | SuperClassGV = GetClassGlobal(SuperClassName); |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4527 | if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>()) |
| 4528 | SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4529 | } |
| 4530 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 4531 | InstanceStart, |
| 4532 | InstanceSize,ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4533 | std::string TClassName = ObjCMetaClassName + ClassName; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4534 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4535 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 4536 | classIsHidden); |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4537 | DefinedMetaClasses.push_back(MetaTClass); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4538 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4539 | // Metadata for the class |
| 4540 | flags = CLS; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4541 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4542 | flags |= OBJC2_CLS_HIDDEN; |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4543 | |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 4544 | if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface())) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4545 | flags |= CLS_EXCEPTION; |
| 4546 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4547 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4548 | flags |= CLS_ROOT; |
| 4549 | SuperClassGV = 0; |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 4550 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4551 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4552 | std::string RootClassName = |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4553 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4554 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4555 | if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>()) |
| 4556 | SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4557 | } |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4558 | GetClassSizeInfo(ID, InstanceStart, InstanceSize); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4559 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4560 | InstanceStart, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4561 | InstanceSize, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4562 | ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4563 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4564 | TClassName = ObjCClassName + ClassName; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4565 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4566 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 4567 | classIsHidden); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4568 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4569 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4570 | // Determine if this class is also "non-lazy". |
| 4571 | if (ImplementationIsNonLazy(ID)) |
| 4572 | DefinedNonLazyClasses.push_back(ClassMD); |
| 4573 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4574 | // Force the definition of the EHType if necessary. |
| 4575 | if (flags & CLS_EXCEPTION) |
| 4576 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4577 | } |
| 4578 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4579 | /// GenerateProtocolRef - This routine is called to generate code for |
| 4580 | /// a protocol reference expression; as in: |
| 4581 | /// @code |
| 4582 | /// @protocol(Proto1); |
| 4583 | /// @endcode |
| 4584 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 4585 | /// which will hold address of the protocol meta-data. |
| 4586 | /// |
| 4587 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4588 | const ObjCProtocolDecl *PD) { |
| 4589 | |
Fariborz Jahanian | 960cd06 | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 4590 | // This routine is called for @protocol only. So, we must build definition |
| 4591 | // of protocol's meta-data (not a reference to it!) |
| 4592 | // |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4593 | llvm::Constant *Init = |
| 4594 | llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
| 4595 | ObjCTypes.ExternalProtocolPtrTy); |
| 4596 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4597 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
| 4598 | ProtocolName += PD->getNameAsCString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4599 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4600 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 4601 | if (PTGV) |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 4602 | return Builder.CreateLoad(PTGV, "tmp"); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4603 | PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4604 | CGM.getModule(), |
| 4605 | Init->getType(), false, |
| 4606 | llvm::GlobalValue::WeakAnyLinkage, |
| 4607 | Init, |
| 4608 | ProtocolName); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4609 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 4610 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4611 | CGM.AddUsedGlobal(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 | } |
| 4614 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4615 | /// GenerateCategory - Build metadata for a category implementation. |
| 4616 | /// struct _category_t { |
| 4617 | /// const char * const name; |
| 4618 | /// struct _class_t *const cls; |
| 4619 | /// const struct _method_list_t * const instance_methods; |
| 4620 | /// const struct _method_list_t * const class_methods; |
| 4621 | /// const struct _protocol_list_t * const protocols; |
| 4622 | /// const struct _prop_list_t * const properties; |
| 4623 | /// } |
| 4624 | /// |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4625 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4626 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4627 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4628 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
| 4629 | "_$_" + OCD->getNameAsString()); |
| 4630 | std::string ExtClassName(getClassSymbolPrefix() + |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4631 | Interface->getNameAsString()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4632 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4633 | std::vector<llvm::Constant*> Values(6); |
| 4634 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 4635 | // meta-class entry symbol |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4636 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Fariborz Jahanian | 2cdcc4c | 2009-11-17 22:02:21 +0000 | [diff] [blame] | 4637 | if (Interface->hasAttr<WeakImportAttr>()) |
| 4638 | ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
| 4639 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4640 | Values[1] = ClassGV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4641 | std::vector<llvm::Constant*> Methods; |
| 4642 | std::string MethodListName(Prefix); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4643 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4644 | "_$_" + OCD->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4645 | |
| 4646 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4647 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4648 | // Instance methods should always be defined. |
| 4649 | Methods.push_back(GetMethodConstant(*i)); |
| 4650 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4651 | |
| 4652 | Values[2] = EmitMethodList(MethodListName, |
| 4653 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4654 | Methods); |
| 4655 | |
| 4656 | MethodListName = Prefix; |
| 4657 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 4658 | OCD->getNameAsString(); |
| 4659 | Methods.clear(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4660 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4661 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4662 | // Class methods should always be defined. |
| 4663 | Methods.push_back(GetMethodConstant(*i)); |
| 4664 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4665 | |
| 4666 | Values[3] = EmitMethodList(MethodListName, |
| 4667 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4668 | Methods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4669 | const ObjCCategoryDecl *Category = |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4670 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4671 | if (Category) { |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4672 | llvm::SmallString<256> ExtName; |
| 4673 | llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_" |
| 4674 | << OCD->getName(); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4675 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4676 | + Interface->getName() + "_$_" |
| 4677 | + Category->getName(), |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4678 | Category->protocol_begin(), |
| 4679 | Category->protocol_end()); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4680 | Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(), |
| 4681 | OCD, Category, ObjCTypes); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 4682 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4683 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4684 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4685 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4686 | |
| 4687 | llvm::Constant *Init = |
| 4688 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4689 | Values); |
| 4690 | llvm::GlobalVariable *GCATV |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4691 | = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4692 | false, |
| 4693 | llvm::GlobalValue::InternalLinkage, |
| 4694 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4695 | ExtCatName); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4696 | GCATV->setAlignment( |
| 4697 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4698 | GCATV->setSection("__DATA, __objc_const"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4699 | CGM.AddUsedGlobal(GCATV); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4700 | DefinedCategories.push_back(GCATV); |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4701 | |
| 4702 | // Determine if this category is also "non-lazy". |
| 4703 | if (ImplementationIsNonLazy(OCD)) |
| 4704 | DefinedNonLazyCategories.push_back(GCATV); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4705 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4706 | |
| 4707 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 4708 | /// given method if it has been defined. The result is null if the |
| 4709 | /// method has not been defined. The return value has type MethodPtrTy. |
| 4710 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4711 | const ObjCMethodDecl *MD) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4712 | // FIXME: Use DenseMap::lookup |
| 4713 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 4714 | if (!Fn) |
| 4715 | return 0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4716 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4717 | std::vector<llvm::Constant*> Method(3); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4718 | Method[0] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4719 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4720 | ObjCTypes.SelectorPtrTy); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4721 | Method[1] = GetMethodVarType(MD); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4722 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4723 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4724 | } |
| 4725 | |
| 4726 | /// EmitMethodList - Build meta-data for method declarations |
| 4727 | /// struct _method_list_t { |
| 4728 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 4729 | /// uint32_t method_count; |
| 4730 | /// struct _objc_method method_list[method_count]; |
| 4731 | /// } |
| 4732 | /// |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4733 | llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name, |
| 4734 | const char *Section, |
| 4735 | const ConstantVector &Methods) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4736 | // Return null for empty list. |
| 4737 | if (Methods.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4738 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4739 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4740 | std::vector<llvm::Constant*> Values(3); |
| 4741 | // sizeof(struct _objc_method) |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4742 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4743 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4744 | // method_count |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4745 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4746 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4747 | Methods.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 4748 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 4749 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4750 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4751 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4752 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4753 | llvm::GlobalValue::InternalLinkage, |
| 4754 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4755 | Name); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4756 | GV->setAlignment( |
| 4757 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4758 | GV->setSection(Section); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4759 | CGM.AddUsedGlobal(GV); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4760 | return llvm::ConstantExpr::getBitCast(GV, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4761 | ObjCTypes.MethodListnfABIPtrTy); |
| 4762 | } |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4763 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4764 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 4765 | /// the given ivar. |
Daniel Dunbar | e83be12 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 4766 | llvm::GlobalVariable * |
| 4767 | CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID, |
| 4768 | const ObjCIvarDecl *Ivar) { |
| 4769 | const ObjCInterfaceDecl *Container = Ivar->getContainingInterface(); |
Daniel Dunbar | a81419d | 2009-05-05 00:36:57 +0000 | [diff] [blame] | 4770 | std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() + |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4771 | '.' + Ivar->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4772 | llvm::GlobalVariable *IvarOffsetGV = |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4773 | CGM.getModule().getGlobalVariable(Name); |
| 4774 | if (!IvarOffsetGV) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4775 | IvarOffsetGV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4776 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4777 | false, |
| 4778 | llvm::GlobalValue::ExternalLinkage, |
| 4779 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4780 | Name); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4781 | return IvarOffsetGV; |
| 4782 | } |
| 4783 | |
Daniel Dunbar | e83be12 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 4784 | llvm::Constant * |
| 4785 | CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
| 4786 | const ObjCIvarDecl *Ivar, |
| 4787 | unsigned long int Offset) { |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4788 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4789 | IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy, |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4790 | Offset)); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4791 | IvarOffsetGV->setAlignment( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4792 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy)); |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4793 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 4794 | // FIXME: This matches gcc, but shouldn't the visibility be set on the use as |
| 4795 | // well (i.e., in ObjCIvarOffsetVariable). |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4796 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 4797 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
| 4798 | CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden) |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4799 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4800 | else |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4801 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4802 | IvarOffsetGV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4803 | return IvarOffsetGV; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4804 | } |
| 4805 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4806 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 4807 | /// implementation. The return value has type |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4808 | /// IvarListnfABIPtrTy. |
| 4809 | /// struct _ivar_t { |
| 4810 | /// unsigned long int *offset; // pointer to ivar offset location |
| 4811 | /// char *name; |
| 4812 | /// char *type; |
| 4813 | /// uint32_t alignment; |
| 4814 | /// uint32_t size; |
| 4815 | /// } |
| 4816 | /// struct _ivar_list_t { |
| 4817 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 4818 | /// uint32 count; |
| 4819 | /// struct _iver_t list[count]; |
| 4820 | /// } |
| 4821 | /// |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4822 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4823 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4824 | const ObjCImplementationDecl *ID) { |
| 4825 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4826 | std::vector<llvm::Constant*> Ivars, Ivar(5); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4827 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4828 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4829 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4830 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4831 | // FIXME. Consolidate this with similar code in GenerateClass. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4832 | |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4833 | // Collect declared and synthesized ivars in a small vector. |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4834 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 4835 | CGM.getContext().ShallowCollectObjCIvars(OID, OIvars); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4836 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4837 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 4838 | ObjCIvarDecl *IVD = OIvars[i]; |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 4839 | // Ignore unnamed bit-fields. |
| 4840 | if (!IVD->getDeclName()) |
| 4841 | continue; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4842 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD, |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4843 | ComputeIvarBaseOffset(CGM, ID, IVD)); |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4844 | Ivar[1] = GetMethodVarName(IVD->getIdentifier()); |
| 4845 | Ivar[2] = GetMethodVarType(IVD); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4846 | const llvm::Type *FieldTy = |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4847 | CGM.getTypes().ConvertTypeForMem(IVD->getType()); |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4848 | unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4849 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4850 | IVD->getType().getTypePtr()) >> 3; |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4851 | Align = llvm::Log2_32(Align); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4852 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4853 | // NOTE. Size of a bitfield does not match gcc's, because of the |
| 4854 | // way bitfields are treated special in each. But I am told that |
| 4855 | // 'size' for bitfield ivars is ignored by the runtime so it does |
| 4856 | // not matter. If it matters, there is enough info to get the |
| 4857 | // bitfield right! |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4858 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4859 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4860 | } |
| 4861 | // Return null for empty list. |
| 4862 | if (Ivars.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4863 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4864 | std::vector<llvm::Constant*> Values(3); |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4865 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4866 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4867 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4868 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4869 | Ivars.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 4870 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 4871 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4872 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 4873 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4874 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4875 | llvm::GlobalValue::InternalLinkage, |
| 4876 | Init, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4877 | Prefix + OID->getName()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4878 | GV->setAlignment( |
| 4879 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4880 | GV->setSection("__DATA, __objc_const"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4881 | |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4882 | CGM.AddUsedGlobal(GV); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4883 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4884 | } |
| 4885 | |
| 4886 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4887 | const ObjCProtocolDecl *PD) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4888 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4889 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4890 | if (!Entry) { |
| 4891 | // We use the initializer as a marker of whether this is a forward |
| 4892 | // reference or not. At module finalization we add the empty |
| 4893 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4894 | Entry = |
| 4895 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false, |
| 4896 | llvm::GlobalValue::ExternalLinkage, |
| 4897 | 0, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4898 | "\01l_OBJC_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4899 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4900 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4901 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4902 | return Entry; |
| 4903 | } |
| 4904 | |
| 4905 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 4906 | /// @code |
| 4907 | /// struct _protocol_t { |
| 4908 | /// id isa; // NULL |
| 4909 | /// const char * const protocol_name; |
| 4910 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 4911 | /// const struct method_list_t * const instance_methods; |
| 4912 | /// const struct method_list_t * const class_methods; |
| 4913 | /// const struct method_list_t *optionalInstanceMethods; |
| 4914 | /// const struct method_list_t *optionalClassMethods; |
| 4915 | /// const struct _prop_list_t * properties; |
| 4916 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 4917 | /// const uint32_t flags; // = 0 |
| 4918 | /// } |
| 4919 | /// @endcode |
| 4920 | /// |
| 4921 | |
| 4922 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4923 | const ObjCProtocolDecl *PD) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4924 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4925 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4926 | // Early exit if a defining object has already been generated. |
| 4927 | if (Entry && Entry->hasInitializer()) |
| 4928 | return Entry; |
| 4929 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4930 | // Construct method lists. |
| 4931 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 4932 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4933 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4934 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4935 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4936 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4937 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4938 | OptInstanceMethods.push_back(C); |
| 4939 | } else { |
| 4940 | InstanceMethods.push_back(C); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4941 | } |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4942 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4943 | |
| 4944 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4945 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4946 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4947 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4948 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4949 | OptClassMethods.push_back(C); |
| 4950 | } else { |
| 4951 | ClassMethods.push_back(C); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4952 | } |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4953 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4954 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4955 | std::vector<llvm::Constant*> Values(10); |
| 4956 | // isa is NULL |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4957 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4958 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4959 | Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(), |
| 4960 | PD->protocol_begin(), |
| 4961 | PD->protocol_end()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4962 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4963 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4964 | + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4965 | "__DATA, __objc_const", |
| 4966 | InstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4967 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4968 | + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4969 | "__DATA, __objc_const", |
| 4970 | ClassMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4971 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4972 | + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4973 | "__DATA, __objc_const", |
| 4974 | OptInstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4975 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4976 | + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4977 | "__DATA, __objc_const", |
| 4978 | OptClassMethods); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4979 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4980 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4981 | uint32_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4982 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4983 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4984 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4985 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4986 | Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4987 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4988 | if (Entry) { |
| 4989 | // Already created, fix the linkage and update the initializer. |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4990 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4991 | Entry->setInitializer(Init); |
| 4992 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4993 | Entry = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4994 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, |
| 4995 | false, llvm::GlobalValue::WeakAnyLinkage, Init, |
| 4996 | "\01l_OBJC_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4997 | Entry->setAlignment( |
| 4998 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4999 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5000 | } |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5001 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5002 | CGM.AddUsedGlobal(Entry); |
| 5003 | |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5004 | // Use this protocol meta-data to build protocol list table in section |
| 5005 | // __DATA, __objc_protolist |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5006 | llvm::GlobalVariable *PTGV = |
| 5007 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy, |
| 5008 | false, llvm::GlobalValue::WeakAnyLinkage, Entry, |
| 5009 | "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5010 | PTGV->setAlignment( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5011 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 5012 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5013 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5014 | CGM.AddUsedGlobal(PTGV); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5015 | return Entry; |
| 5016 | } |
| 5017 | |
| 5018 | /// EmitProtocolList - Generate protocol list meta-data: |
| 5019 | /// @code |
| 5020 | /// struct _protocol_list_t { |
| 5021 | /// long protocol_count; // Note, this is 32/64 bit |
| 5022 | /// struct _protocol_t[protocol_count]; |
| 5023 | /// } |
| 5024 | /// @endcode |
| 5025 | /// |
| 5026 | llvm::Constant * |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5027 | CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name, |
| 5028 | ObjCProtocolDecl::protocol_iterator begin, |
| 5029 | ObjCProtocolDecl::protocol_iterator end) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5030 | std::vector<llvm::Constant*> ProtocolRefs; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5031 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5032 | // Just return null for empty protocol lists |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5033 | if (begin == end) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5034 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5035 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5036 | // FIXME: We shouldn't need to do this lookup here, should we? |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5037 | llvm::SmallString<256> TmpName; |
| 5038 | Name.toVector(TmpName); |
| 5039 | llvm::GlobalVariable *GV = |
| 5040 | CGM.getModule().getGlobalVariable(TmpName.str(), true); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5041 | if (GV) |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5042 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5043 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5044 | for (; begin != end; ++begin) |
| 5045 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 5046 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5047 | // This list is null terminated. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5048 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5049 | ObjCTypes.ProtocolnfABIPtrTy)); |
| 5050 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5051 | std::vector<llvm::Constant*> Values(2); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5052 | Values[0] = |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5053 | llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5054 | Values[1] = |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 5055 | llvm::ConstantArray::get( |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5056 | llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5057 | ProtocolRefs.size()), |
| 5058 | ProtocolRefs); |
| 5059 | |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 5060 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5061 | GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5062 | llvm::GlobalValue::InternalLinkage, |
| 5063 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5064 | Name); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5065 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5066 | GV->setAlignment( |
| 5067 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5068 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5069 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5070 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5071 | } |
| 5072 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5073 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 5074 | /// struct _objc_method { |
| 5075 | /// SEL _cmd; |
| 5076 | /// char *method_type; |
| 5077 | /// char *_imp; |
| 5078 | /// } |
| 5079 | |
| 5080 | llvm::Constant * |
| 5081 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 5082 | std::vector<llvm::Constant*> Desc(3); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5083 | Desc[0] = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5084 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 5085 | ObjCTypes.SelectorPtrTy); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5086 | Desc[1] = GetMethodVarType(MD); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5087 | // Protocol methods have no implementation. So, this entry is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5088 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5089 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5090 | } |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5091 | |
| 5092 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 5093 | /// This code gen. amounts to generating code for: |
| 5094 | /// @code |
| 5095 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 5096 | /// @encode |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5097 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 5098 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5099 | CodeGen::CodeGenFunction &CGF, |
| 5100 | QualType ObjectTy, |
| 5101 | llvm::Value *BaseValue, |
| 5102 | const ObjCIvarDecl *Ivar, |
| 5103 | unsigned CVRQualifiers) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5104 | const ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCInterfaceType>()->getDecl(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 5105 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 5106 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5107 | } |
| 5108 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 5109 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5110 | CodeGen::CodeGenFunction &CGF, |
| 5111 | const ObjCInterfaceDecl *Interface, |
| 5112 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 5113 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 5114 | } |
| 5115 | |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5116 | CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5117 | CodeGen::CodeGenFunction &CGF, |
| 5118 | QualType ResultType, |
| 5119 | Selector Sel, |
| 5120 | llvm::Value *Receiver, |
| 5121 | QualType Arg0Ty, |
| 5122 | bool IsSuper, |
| 5123 | const CallArgList &CallArgs) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5124 | // FIXME. Even though IsSuper is passes. This function doese not handle calls |
| 5125 | // to 'super' receivers. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5126 | CodeGenTypes &Types = CGM.getTypes(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5127 | llvm::Value *Arg0 = Receiver; |
| 5128 | if (!IsSuper) |
| 5129 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5130 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5131 | // Find the message function name. |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5132 | // FIXME. This is too much work to get the ABI-specific result type needed to |
| 5133 | // find the message name. |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 5134 | const CGFunctionInfo &FnInfo |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 5135 | = Types.getFunctionInfo(ResultType, CallArgList(), |
| 5136 | FunctionType::ExtInfo()); |
Fariborz Jahanian | 70b51c7 | 2009-04-30 23:08:58 +0000 | [diff] [blame] | 5137 | llvm::Constant *Fn = 0; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5138 | std::string Name("\01l_"); |
| 5139 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5140 | #if 0 |
| 5141 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5142 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5143 | Fn = ObjCTypes.getMessageSendIdStretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5144 | // FIXME. Is there a better way of getting these names. |
| 5145 | // They are available in RuntimeFunctions vector pair. |
| 5146 | Name += "objc_msgSendId_stret_fixup"; |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5147 | } else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5148 | #endif |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5149 | if (IsSuper) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5150 | Fn = ObjCTypes.getMessageSendSuper2StretFixupFn(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5151 | Name += "objc_msgSendSuper2_stret_fixup"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5152 | } else { |
| 5153 | Fn = ObjCTypes.getMessageSendStretFixupFn(); |
| 5154 | Name += "objc_msgSend_stret_fixup"; |
| 5155 | } |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5156 | } else if (!IsSuper && ResultType->isFloatingType()) { |
Daniel Dunbar | c0183e8 | 2009-06-26 18:32:06 +0000 | [diff] [blame] | 5157 | if (ResultType->isSpecificBuiltinType(BuiltinType::LongDouble)) { |
| 5158 | Fn = ObjCTypes.getMessageSendFpretFixupFn(); |
| 5159 | Name += "objc_msgSend_fpret_fixup"; |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5160 | } else { |
Daniel Dunbar | c0183e8 | 2009-06-26 18:32:06 +0000 | [diff] [blame] | 5161 | Fn = ObjCTypes.getMessageSendFixupFn(); |
| 5162 | Name += "objc_msgSend_fixup"; |
Fariborz Jahanian | 5b2bad0 | 2009-04-30 16:31:11 +0000 | [diff] [blame] | 5163 | } |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5164 | } else { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5165 | #if 0 |
| 5166 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5167 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5168 | Fn = ObjCTypes.getMessageSendIdFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5169 | Name += "objc_msgSendId_fixup"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5170 | } else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5171 | #endif |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5172 | if (IsSuper) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5173 | Fn = ObjCTypes.getMessageSendSuper2FixupFn(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5174 | Name += "objc_msgSendSuper2_fixup"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5175 | } else { |
| 5176 | Fn = ObjCTypes.getMessageSendFixupFn(); |
| 5177 | Name += "objc_msgSend_fixup"; |
| 5178 | } |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5179 | } |
Fariborz Jahanian | 70b51c7 | 2009-04-30 23:08:58 +0000 | [diff] [blame] | 5180 | assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5181 | Name += '_'; |
| 5182 | std::string SelName(Sel.getAsString()); |
| 5183 | // Replace all ':' in selector name with '_' ouch! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5184 | for (unsigned i = 0; i < SelName.size(); i++) |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5185 | if (SelName[i] == ':') |
| 5186 | SelName[i] = '_'; |
| 5187 | Name += SelName; |
| 5188 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5189 | if (!GV) { |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5190 | // Build message ref table entry. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5191 | std::vector<llvm::Constant*> Values(2); |
| 5192 | Values[0] = Fn; |
| 5193 | Values[1] = GetMethodVarName(Sel); |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 5194 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false); |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5195 | GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 5196 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5197 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5198 | Name); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5199 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | f59c1a6 | 2009-04-15 19:04:46 +0000 | [diff] [blame] | 5200 | GV->setAlignment(16); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5201 | GV->setSection("__DATA, __objc_msgrefs, coalesced"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5202 | } |
| 5203 | llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5204 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5205 | CallArgList ActualArgs; |
| 5206 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5207 | ActualArgs.push_back(std::make_pair(RValue::get(Arg1), |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5208 | ObjCTypes.MessageRefCPtrTy)); |
| 5209 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 5210 | const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 5211 | FunctionType::ExtInfo()); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5212 | llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0); |
| 5213 | Callee = CGF.Builder.CreateLoad(Callee); |
Fariborz Jahanian | 3ab75bd | 2009-02-14 21:25:36 +0000 | [diff] [blame] | 5214 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5215 | Callee = CGF.Builder.CreateBitCast(Callee, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5216 | llvm::PointerType::getUnqual(FTy)); |
Anders Carlsson | f3c47c9 | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 5217 | return CGF.EmitCall(FnInfo1, Callee, ReturnValueSlot(), ActualArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5218 | } |
| 5219 | |
| 5220 | /// Generate code for a message send expression in the nonfragile abi. |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5221 | CodeGen::RValue |
| 5222 | CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
| 5223 | QualType ResultType, |
| 5224 | Selector Sel, |
| 5225 | llvm::Value *Receiver, |
| 5226 | bool IsClassMessage, |
| 5227 | const CallArgList &CallArgs, |
| 5228 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5229 | return LegacyDispatchedSelector(Sel) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5230 | ? EmitLegacyMessageSend(CGF, ResultType, EmitSelector(CGF.Builder, Sel), |
| 5231 | Receiver, CGF.getContext().getObjCIdType(), |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5232 | false, CallArgs, Method, ObjCTypes) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5233 | : EmitMessageSend(CGF, ResultType, Sel, |
| 5234 | Receiver, CGF.getContext().getObjCIdType(), |
| 5235 | false, CallArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5236 | } |
| 5237 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5238 | llvm::GlobalVariable * |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5239 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5240 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5241 | |
Daniel Dunbar | dfff230 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 5242 | if (!GV) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5243 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5244 | false, llvm::GlobalValue::ExternalLinkage, |
| 5245 | 0, Name); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5246 | } |
| 5247 | |
| 5248 | return GV; |
| 5249 | } |
| 5250 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5251 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
| 5252 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5253 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5254 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5255 | if (!Entry) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5256 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5257 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5258 | Entry = |
| 5259 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5260 | false, llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5261 | ClassGV, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5262 | "\01L_OBJC_CLASSLIST_REFERENCES_$_"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5263 | Entry->setAlignment( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5264 | CGM.getTargetData().getPrefTypeAlignment( |
| 5265 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5266 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5267 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5268 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5269 | |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 5270 | return Builder.CreateLoad(Entry, "tmp"); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5271 | } |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5272 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5273 | llvm::Value * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5274 | CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5275 | const ObjCInterfaceDecl *ID) { |
| 5276 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5277 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5278 | if (!Entry) { |
| 5279 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5280 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5281 | Entry = |
| 5282 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5283 | false, llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5284 | ClassGV, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5285 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5286 | Entry->setAlignment( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5287 | CGM.getTargetData().getPrefTypeAlignment( |
| 5288 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5289 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5290 | CGM.AddUsedGlobal(Entry); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5291 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5292 | |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 5293 | return Builder.CreateLoad(Entry, "tmp"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5294 | } |
| 5295 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5296 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 5297 | /// meta-data |
| 5298 | /// |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5299 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 5300 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5301 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 5302 | if (Entry) |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 5303 | return Builder.CreateLoad(Entry, "tmp"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5304 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5305 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5306 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5307 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5308 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5309 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5310 | MetaClassGV, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5311 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5312 | Entry->setAlignment( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5313 | CGM.getTargetData().getPrefTypeAlignment( |
| 5314 | ObjCTypes.ClassnfABIPtrTy)); |
| 5315 | |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5316 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5317 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5318 | |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 5319 | return Builder.CreateLoad(Entry, "tmp"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5320 | } |
| 5321 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5322 | /// GetClass - Return a reference to the class for the given interface |
| 5323 | /// decl. |
| 5324 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 5325 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 269f8bc | 2009-11-17 22:42:00 +0000 | [diff] [blame] | 5326 | if (ID->hasAttr<WeakImportAttr>()) { |
| 5327 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5328 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
| 5329 | ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
| 5330 | } |
| 5331 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5332 | return EmitClassRef(Builder, ID); |
| 5333 | } |
| 5334 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5335 | /// Generates a message send where the super is the receiver. This is |
| 5336 | /// a message send to self with special delivery semantics indicating |
| 5337 | /// which class's method should be called. |
| 5338 | CodeGen::RValue |
| 5339 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5340 | QualType ResultType, |
| 5341 | Selector Sel, |
| 5342 | const ObjCInterfaceDecl *Class, |
| 5343 | bool isCategoryImpl, |
| 5344 | llvm::Value *Receiver, |
| 5345 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5346 | const CodeGen::CallArgList &CallArgs, |
| 5347 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5348 | // ... |
| 5349 | // Create and init a super structure; this is a (receiver, class) |
| 5350 | // pair we will pass to objc_msgSendSuper. |
| 5351 | llvm::Value *ObjCSuper = |
| 5352 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5353 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5354 | llvm::Value *ReceiverAsObject = |
| 5355 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 5356 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 5357 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5358 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5359 | // 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] | 5360 | llvm::Value *Target; |
| 5361 | if (IsClassMessage) { |
| 5362 | if (isCategoryImpl) { |
| 5363 | // Message sent to "super' in a class method defined in |
| 5364 | // a category implementation. |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5365 | Target = EmitClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5366 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 5367 | Target = CGF.Builder.CreateLoad(Target); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5368 | } else |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5369 | Target = EmitMetaClassRef(CGF.Builder, Class); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5370 | } else |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5371 | Target = EmitSuperClassRef(CGF.Builder, Class); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5372 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5373 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 5374 | // ObjCTypes types. |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5375 | const llvm::Type *ClassTy = |
| 5376 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 5377 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 5378 | CGF.Builder.CreateStore(Target, |
| 5379 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5380 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5381 | return (LegacyDispatchedSelector(Sel)) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5382 | ? EmitLegacyMessageSend(CGF, ResultType,EmitSelector(CGF.Builder, Sel), |
| 5383 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5384 | true, CallArgs, Method, ObjCTypes) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5385 | : EmitMessageSend(CGF, ResultType, Sel, |
| 5386 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 5387 | true, CallArgs); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5388 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5389 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5390 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5391 | Selector Sel) { |
| 5392 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5393 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5394 | if (!Entry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5395 | llvm::Constant *Casted = |
| 5396 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 5397 | ObjCTypes.SelectorPtrTy); |
| 5398 | Entry = |
| 5399 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false, |
| 5400 | llvm::GlobalValue::InternalLinkage, |
| 5401 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5402 | Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5403 | CGM.AddUsedGlobal(Entry); |
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 | |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 5406 | return Builder.CreateLoad(Entry, "tmp"); |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5407 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5408 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 5409 | /// objc_assign_ivar (id src, id *dst, ptrdiff_t) |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5410 | /// |
| 5411 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5412 | llvm::Value *src, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 5413 | llvm::Value *dst, |
| 5414 | llvm::Value *ivarOffset) { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5415 | const llvm::Type * SrcTy = src->getType(); |
| 5416 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5417 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5418 | assert(Size <= 8 && "does not support size > 8"); |
| 5419 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5420 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5421 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5422 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5423 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5424 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 5425 | CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(), |
| 5426 | src, dst, ivarOffset); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5427 | return; |
| 5428 | } |
| 5429 | |
| 5430 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 5431 | /// objc_assign_strongCast (id src, id *dst) |
| 5432 | /// |
| 5433 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5434 | CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5435 | llvm::Value *src, llvm::Value *dst) { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5436 | const llvm::Type * SrcTy = src->getType(); |
| 5437 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5438 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5439 | assert(Size <= 8 && "does not support size > 8"); |
| 5440 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5441 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5442 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5443 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5444 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5445 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5446 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5447 | src, dst, "weakassign"); |
| 5448 | return; |
| 5449 | } |
| 5450 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 5451 | void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5452 | CodeGen::CodeGenFunction &CGF, |
| 5453 | llvm::Value *DestPtr, |
| 5454 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 5455 | QualType Ty) { |
| 5456 | // Get size info for this aggregate. |
| 5457 | std::pair<uint64_t, unsigned> TypeInfo = CGM.getContext().getTypeInfo(Ty); |
Fariborz Jahanian | 736d8a6 | 2009-09-10 16:20:31 +0000 | [diff] [blame] | 5458 | unsigned long size = TypeInfo.first/8; |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 5459 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 5460 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5461 | llvm::Value *N = llvm::ConstantInt::get(ObjCTypes.LongTy, size); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 5462 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), |
| 5463 | DestPtr, SrcPtr, N); |
| 5464 | return; |
| 5465 | } |
| 5466 | |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5467 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 5468 | /// object: objc_read_weak (id *src) |
| 5469 | /// |
| 5470 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5471 | CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5472 | llvm::Value *AddrWeakObj) { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5473 | const llvm::Type* DestTy = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5474 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
| 5475 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 5476 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5477 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5478 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5479 | return read_weak; |
| 5480 | } |
| 5481 | |
| 5482 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 5483 | /// objc_assign_weak (id src, id *dst) |
| 5484 | /// |
| 5485 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5486 | llvm::Value *src, llvm::Value *dst) { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5487 | const llvm::Type * SrcTy = src->getType(); |
| 5488 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5489 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5490 | assert(Size <= 8 && "does not support size > 8"); |
| 5491 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5492 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5493 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5494 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5495 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5496 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 5497 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5498 | src, dst, "weakassign"); |
| 5499 | return; |
| 5500 | } |
| 5501 | |
| 5502 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 5503 | /// objc_assign_global (id src, id *dst) |
| 5504 | /// |
| 5505 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5506 | llvm::Value *src, llvm::Value *dst) { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5507 | const llvm::Type * SrcTy = src->getType(); |
| 5508 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5509 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5510 | assert(Size <= 8 && "does not support size > 8"); |
| 5511 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5512 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5513 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5514 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5515 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5516 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5517 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5518 | src, dst, "globalassign"); |
| 5519 | return; |
| 5520 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5521 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5522 | void |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5523 | CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 5524 | const Stmt &S) { |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5525 | bool isTry = isa<ObjCAtTryStmt>(S); |
| 5526 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 5527 | llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5528 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5529 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5530 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5531 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
| 5532 | |
| 5533 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 5534 | // evaluation of the expression must occur before we enter the |
| 5535 | // @synchronized. We can safely avoid a temp here because jumps into |
| 5536 | // @synchronized are illegal & this will dominate uses. |
| 5537 | llvm::Value *SyncArg = 0; |
| 5538 | if (!isTry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5539 | SyncArg = |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5540 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 5541 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5542 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5543 | } |
| 5544 | |
| 5545 | // Push an EH context entry, used for handling rethrows and jumps |
| 5546 | // through finally. |
| 5547 | CGF.PushCleanupBlock(FinallyBlock); |
| 5548 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5549 | CGF.setInvokeDest(TryHandler); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5550 | |
| 5551 | CGF.EmitBlock(TryBlock); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5552 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 5553 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5554 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5555 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5556 | // Emit the exception handler. |
| 5557 | |
| 5558 | CGF.EmitBlock(TryHandler); |
| 5559 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5560 | llvm::Value *llvm_eh_exception = |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5561 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
Duncan Sands | 790b20e | 2009-10-14 16:13:30 +0000 | [diff] [blame] | 5562 | llvm::Value *llvm_eh_selector = |
| 5563 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector); |
| 5564 | llvm::Value *llvm_eh_typeid_for = |
| 5565 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5566 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5567 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow"); |
| 5568 | |
| 5569 | llvm::SmallVector<llvm::Value*, 8> SelectorArgs; |
| 5570 | SelectorArgs.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5571 | SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5572 | |
| 5573 | // Construct the lists of (type, catch body) to handle. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5574 | llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5575 | bool HasCatchAll = false; |
| 5576 | if (isTry) { |
| 5577 | if (const ObjCAtCatchStmt* CatchStmt = |
| 5578 | cast<ObjCAtTryStmt>(S).getCatchStmts()) { |
| 5579 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5580 | const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5581 | Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody())); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5582 | |
| 5583 | // catch(...) always matches. |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5584 | if (!CatchDecl) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5585 | // Use i8* null here to signal this is a catch all, not a cleanup. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5586 | llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5587 | SelectorArgs.push_back(Null); |
| 5588 | HasCatchAll = true; |
| 5589 | break; |
| 5590 | } |
| 5591 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5592 | if (CatchDecl->getType()->isObjCIdType() || |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5593 | CatchDecl->getType()->isObjCQualifiedIdType()) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5594 | llvm::Value *IDEHType = |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5595 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 5596 | if (!IDEHType) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5597 | IDEHType = |
| 5598 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, |
| 5599 | false, |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5600 | llvm::GlobalValue::ExternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5601 | 0, "OBJC_EHTYPE_id"); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5602 | SelectorArgs.push_back(IDEHType); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5603 | } else { |
Fariborz Jahanian | 99438a7 | 2009-07-27 23:12:41 +0000 | [diff] [blame] | 5604 | // All other types should be Objective-C interface pointer types. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5605 | const ObjCObjectPointerType *PT = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5606 | CatchDecl->getType()->getAs<ObjCObjectPointerType>(); |
Fariborz Jahanian | 99438a7 | 2009-07-27 23:12:41 +0000 | [diff] [blame] | 5607 | assert(PT && "Invalid @catch type."); |
| 5608 | const ObjCInterfaceType *IT = PT->getInterfaceType(); |
| 5609 | assert(IT && "Invalid @catch type."); |
| 5610 | llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false); |
| 5611 | SelectorArgs.push_back(EHType); |
| 5612 | } |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5613 | } |
| 5614 | } |
| 5615 | } |
| 5616 | |
| 5617 | // We use a cleanup unless there was already a catch all. |
| 5618 | if (!HasCatchAll) { |
Daniel Dunbar | e3d25ab | 2009-08-03 21:20:07 +0000 | [diff] [blame] | 5619 | // Even though this is a cleanup, treat it as a catch all to avoid the C++ |
| 5620 | // personality behavior of terminating the process if only cleanups are |
| 5621 | // found in the exception handling stack. |
| 5622 | SelectorArgs.push_back(llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy)); |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5623 | Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5624 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5625 | |
| 5626 | llvm::Value *Selector = |
Duncan Sands | 790b20e | 2009-10-14 16:13:30 +0000 | [diff] [blame] | 5627 | CGF.Builder.CreateCall(llvm_eh_selector, |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5628 | SelectorArgs.begin(), SelectorArgs.end(), |
| 5629 | "selector"); |
| 5630 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5631 | const ParmVarDecl *CatchParam = Handlers[i].first; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5632 | const Stmt *CatchBody = Handlers[i].second; |
| 5633 | |
| 5634 | llvm::BasicBlock *Next = 0; |
| 5635 | |
| 5636 | // The last handler always matches. |
| 5637 | if (i + 1 != e) { |
| 5638 | assert(CatchParam && "Only last handler can be a catch all."); |
| 5639 | |
| 5640 | llvm::BasicBlock *Match = CGF.createBasicBlock("match"); |
| 5641 | Next = CGF.createBasicBlock("catch.next"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5642 | llvm::Value *Id = |
Duncan Sands | 790b20e | 2009-10-14 16:13:30 +0000 | [diff] [blame] | 5643 | CGF.Builder.CreateCall(llvm_eh_typeid_for, |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5644 | CGF.Builder.CreateBitCast(SelectorArgs[i+2], |
| 5645 | ObjCTypes.Int8PtrTy)); |
| 5646 | CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id), |
| 5647 | Match, Next); |
| 5648 | |
| 5649 | CGF.EmitBlock(Match); |
| 5650 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5651 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5652 | if (CatchBody) { |
| 5653 | llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end"); |
| 5654 | llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler"); |
| 5655 | |
| 5656 | // Cleanups must call objc_end_catch. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5657 | // |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5658 | // FIXME: It seems incorrect for objc_begin_catch to be inside this |
| 5659 | // context, but this matches gcc. |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5660 | CGF.PushCleanupBlock(MatchEnd); |
| 5661 | CGF.setInvokeDest(MatchHandler); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5662 | |
| 5663 | llvm::Value *ExcObject = |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5664 | CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5665 | |
| 5666 | // Bind the catch parameter if it exists. |
| 5667 | if (CatchParam) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5668 | ExcObject = |
| 5669 | CGF.Builder.CreateBitCast(ExcObject, |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5670 | CGF.ConvertType(CatchParam->getType())); |
| 5671 | // CatchParam is a ParmVarDecl because of the grammar |
| 5672 | // construction used to handle this, but for codegen purposes |
| 5673 | // we treat this as a local decl. |
| 5674 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
| 5675 | CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5676 | } |
| 5677 | |
| 5678 | CGF.ObjCEHValueStack.push_back(ExcObject); |
| 5679 | CGF.EmitStmt(CatchBody); |
| 5680 | CGF.ObjCEHValueStack.pop_back(); |
| 5681 | |
| 5682 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5683 | |
| 5684 | CGF.EmitBlock(MatchHandler); |
| 5685 | |
| 5686 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5687 | // We are required to emit this call to satisfy LLVM, even |
| 5688 | // though we don't use the result. |
| 5689 | llvm::SmallVector<llvm::Value*, 8> Args; |
| 5690 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5691 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 5692 | Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5693 | 0)); |
Duncan Sands | 790b20e | 2009-10-14 16:13:30 +0000 | [diff] [blame] | 5694 | CGF.Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5695 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5696 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5697 | |
| 5698 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5699 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5700 | CGF.EmitBlock(MatchEnd); |
| 5701 | |
| 5702 | // Unfortunately, we also have to generate another EH frame here |
| 5703 | // in case this throws. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5704 | llvm::BasicBlock *MatchEndHandler = |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5705 | CGF.createBasicBlock("match.end.handler"); |
| 5706 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5707 | CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5708 | Cont, MatchEndHandler, |
| 5709 | Args.begin(), Args.begin()); |
| 5710 | |
| 5711 | CGF.EmitBlock(Cont); |
| 5712 | if (Info.SwitchBlock) |
| 5713 | CGF.EmitBlock(Info.SwitchBlock); |
| 5714 | if (Info.EndBlock) |
| 5715 | CGF.EmitBlock(Info.EndBlock); |
| 5716 | |
| 5717 | CGF.EmitBlock(MatchEndHandler); |
| 5718 | Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5719 | // We are required to emit this call to satisfy LLVM, even |
| 5720 | // though we don't use the result. |
| 5721 | Args.clear(); |
| 5722 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5723 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 5724 | Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5725 | 0)); |
Duncan Sands | 790b20e | 2009-10-14 16:13:30 +0000 | [diff] [blame] | 5726 | CGF.Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5727 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5728 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5729 | |
| 5730 | if (Next) |
| 5731 | CGF.EmitBlock(Next); |
| 5732 | } else { |
| 5733 | assert(!Next && "catchup should be last handler."); |
| 5734 | |
| 5735 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5736 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5737 | } |
| 5738 | } |
| 5739 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5740 | // Pop the cleanup entry, the @finally is outside this cleanup |
| 5741 | // scope. |
| 5742 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5743 | CGF.setInvokeDest(PrevLandingPad); |
| 5744 | |
| 5745 | CGF.EmitBlock(FinallyBlock); |
| 5746 | |
| 5747 | if (isTry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5748 | if (const ObjCAtFinallyStmt* FinallyStmt = |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5749 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 5750 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 5751 | } else { |
| 5752 | // Emit 'objc_sync_exit(expr)' as finally's sole statement for |
| 5753 | // @synchronized. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5754 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5755 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5756 | |
| 5757 | if (Info.SwitchBlock) |
| 5758 | CGF.EmitBlock(Info.SwitchBlock); |
| 5759 | if (Info.EndBlock) |
| 5760 | CGF.EmitBlock(Info.EndBlock); |
| 5761 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5762 | // Branch around the rethrow code. |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5763 | CGF.EmitBranch(FinallyEnd); |
| 5764 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5765 | CGF.EmitBlock(FinallyRethrow); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5766 | CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5767 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5768 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5769 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5770 | CGF.EmitBlock(FinallyEnd); |
| 5771 | } |
| 5772 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5773 | /// EmitThrowStmt - Generate code for a throw statement. |
| 5774 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 5775 | const ObjCAtThrowStmt &S) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5776 | llvm::Value *Exception; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5777 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5778 | Exception = CGF.EmitScalarExpr(ThrowExpr); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5779 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5780 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5781 | "Unexpected rethrow outside @catch block."); |
| 5782 | Exception = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5783 | } |
| 5784 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5785 | llvm::Value *ExceptionAsObject = |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5786 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 5787 | llvm::BasicBlock *InvokeDest = CGF.getInvokeDest(); |
| 5788 | if (InvokeDest) { |
| 5789 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5790 | CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5791 | Cont, InvokeDest, |
| 5792 | &ExceptionAsObject, &ExceptionAsObject + 1); |
| 5793 | CGF.EmitBlock(Cont); |
| 5794 | } else |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5795 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5796 | CGF.Builder.CreateUnreachable(); |
| 5797 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5798 | // Clear the insertion point to indicate we are in unreachable code. |
| 5799 | CGF.Builder.ClearInsertionPoint(); |
| 5800 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5801 | |
| 5802 | llvm::Value * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5803 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5804 | bool ForDefinition) { |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5805 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5806 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5807 | // If we don't need a definition, return the entry if found or check |
| 5808 | // if we use an external reference. |
| 5809 | if (!ForDefinition) { |
| 5810 | if (Entry) |
| 5811 | return Entry; |
Daniel Dunbar | 7e075cb | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 5812 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5813 | // If this type (or a super class) has the __objc_exception__ |
| 5814 | // attribute, emit an external reference. |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 5815 | if (hasObjCExceptionAttribute(CGM.getContext(), ID)) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5816 | return Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5817 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5818 | llvm::GlobalValue::ExternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5819 | 0, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5820 | ("OBJC_EHTYPE_$_" + |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 5821 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5822 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5823 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5824 | // Otherwise we need to either make a new entry or fill in the |
| 5825 | // initializer. |
| 5826 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5827 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5828 | std::string VTableName = "objc_ehtype_vtable"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5829 | llvm::GlobalVariable *VTableGV = |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5830 | CGM.getModule().getGlobalVariable(VTableName); |
| 5831 | if (!VTableGV) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5832 | VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy, |
| 5833 | false, |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5834 | llvm::GlobalValue::ExternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5835 | 0, VTableName); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5836 | |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 5837 | llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5838 | |
| 5839 | std::vector<llvm::Constant*> Values(3); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 5840 | Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5841 | Values[1] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5842 | Values[2] = GetClassGlobal(ClassName); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5843 | llvm::Constant *Init = |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5844 | llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5845 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5846 | if (Entry) { |
| 5847 | Entry->setInitializer(Init); |
| 5848 | } else { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5849 | Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5850 | llvm::GlobalValue::WeakAnyLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5851 | Init, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5852 | ("OBJC_EHTYPE_$_" + |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 5853 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5854 | } |
| 5855 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 5856 | if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden) |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5857 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5858 | Entry->setAlignment(8); |
| 5859 | |
| 5860 | if (ForDefinition) { |
| 5861 | Entry->setSection("__DATA,__objc_const"); |
| 5862 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 5863 | } else { |
| 5864 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 5865 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5866 | |
| 5867 | return Entry; |
| 5868 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5869 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5870 | /* *** */ |
| 5871 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5872 | CodeGen::CGObjCRuntime * |
| 5873 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 5874 | return new CGObjCMac(CGM); |
| 5875 | } |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5876 | |
| 5877 | CodeGen::CGObjCRuntime * |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5878 | CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5879 | return new CGObjCNonFragileABIMac(CGM); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5880 | } |