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 | |
| 16 | #include "CodeGenModule.h" |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 17 | #include "CodeGenFunction.h" |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 19 | #include "clang/AST/Decl.h" |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 21 | #include "clang/AST/RecordLayout.h" |
Chris Lattner | 16f0049 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 22 | #include "clang/AST/StmtObjC.h" |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 23 | #include "clang/Basic/LangOptions.h" |
| 24 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 25 | #include "llvm/Intrinsics.h" |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 26 | #include "llvm/LLVMContext.h" |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 27 | #include "llvm/Module.h" |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/DenseSet.h" |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetData.h" |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 30 | #include <sstream> |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 31 | |
| 32 | using namespace clang; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 33 | using namespace CodeGen; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 34 | |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 35 | // Common CGObjCRuntime functions, these don't belong here, but they |
| 36 | // don't belong in CGObjCRuntime either so we will live with it for |
| 37 | // now. |
| 38 | |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 39 | /// FindIvarInterface - Find the interface containing the ivar. |
Daniel Dunbar | a243578 | 2009-04-22 12:00:04 +0000 | [diff] [blame] | 40 | /// |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 41 | /// FIXME: We shouldn't need to do this, the containing context should |
| 42 | /// be fixed. |
| 43 | static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context, |
| 44 | const ObjCInterfaceDecl *OID, |
| 45 | const ObjCIvarDecl *OIVD, |
| 46 | unsigned &Index) { |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 47 | // FIXME: The index here is closely tied to how |
| 48 | // ASTContext::getObjCLayout is implemented. This should be fixed to |
| 49 | // get the information from the layout directly. |
| 50 | Index = 0; |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 51 | llvm::SmallVector<ObjCIvarDecl*, 16> Ivars; |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 52 | Context.ShallowCollectObjCIvars(OID, Ivars); |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 53 | for (unsigned k = 0, e = Ivars.size(); k != e; ++k) { |
| 54 | if (OIVD == Ivars[k]) |
| 55 | return OID; |
| 56 | ++Index; |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 57 | } |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 58 | |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 59 | // Otherwise check in the super class. |
Daniel Dunbar | a81419d | 2009-05-05 00:36:57 +0000 | [diff] [blame] | 60 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 61 | return FindIvarInterface(Context, Super, OIVD, Index); |
| 62 | |
| 63 | return 0; |
Daniel Dunbar | a243578 | 2009-04-22 12:00:04 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 66 | static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM, |
| 67 | const ObjCInterfaceDecl *OID, |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 68 | const ObjCImplementationDecl *ID, |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 69 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 70 | unsigned Index; |
| 71 | const ObjCInterfaceDecl *Container = |
| 72 | FindIvarInterface(CGM.getContext(), OID, Ivar, Index); |
| 73 | assert(Container && "Unable to find ivar container"); |
| 74 | |
| 75 | // If we know have an implementation (and the ivar is in it) then |
| 76 | // look up in the implementation layout. |
| 77 | const ASTRecordLayout *RL; |
| 78 | if (ID && ID->getClassInterface() == Container) |
| 79 | RL = &CGM.getContext().getASTObjCImplementationLayout(ID); |
| 80 | else |
| 81 | RL = &CGM.getContext().getASTObjCInterfaceLayout(Container); |
| 82 | return RL->getFieldOffset(Index); |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM, |
| 86 | const ObjCInterfaceDecl *OID, |
| 87 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 88 | return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8; |
| 89 | } |
| 90 | |
| 91 | uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM, |
| 92 | const ObjCImplementationDecl *OID, |
| 93 | const ObjCIvarDecl *Ivar) { |
| 94 | return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8; |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF, |
| 98 | const ObjCInterfaceDecl *OID, |
| 99 | llvm::Value *BaseValue, |
| 100 | const ObjCIvarDecl *Ivar, |
| 101 | unsigned CVRQualifiers, |
| 102 | llvm::Value *Offset) { |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 103 | // Compute (type*) ( (char *) BaseValue + Offset) |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 104 | llvm::LLVMContext &VMContext = CGF.getLLVMContext(); |
| 105 | llvm::Type *I8Ptr = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty); |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 106 | QualType IvarTy = Ivar->getType(); |
| 107 | const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 108 | llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 109 | V = CGF.Builder.CreateGEP(V, Offset, "add.ptr"); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 110 | V = CGF.Builder.CreateBitCast(V, VMContext.getPointerTypeUnqual(LTy)); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 111 | |
| 112 | if (Ivar->isBitField()) { |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 113 | // We need to compute the bit offset for the bit-field, the offset |
| 114 | // is to the byte. Note, there is a subtle invariant here: we can |
| 115 | // only call this routine on non-sythesized ivars but we may be |
| 116 | // called for synthesized ivars. However, a synthesized ivar can |
| 117 | // never be a bit-field so this is safe. |
| 118 | uint64_t BitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar) % 8; |
| 119 | |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 120 | uint64_t BitFieldSize = |
| 121 | Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue(); |
| 122 | return LValue::MakeBitfield(V, BitOffset, BitFieldSize, |
Daniel Dunbar | e38df86 | 2009-05-03 07:52:00 +0000 | [diff] [blame] | 123 | IvarTy->isSignedIntegerType(), |
| 124 | IvarTy.getCVRQualifiers()|CVRQualifiers); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 127 | LValue LV = LValue::MakeAddr(V, IvarTy.getCVRQualifiers()|CVRQualifiers, |
| 128 | CGF.CGM.getContext().getObjCGCAttrKind(IvarTy)); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 129 | LValue::SetObjCIvar(LV, true); |
| 130 | return LV; |
| 131 | } |
| 132 | |
| 133 | /// |
| 134 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 135 | namespace { |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 136 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 137 | typedef std::vector<llvm::Constant*> ConstantVector; |
| 138 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 139 | // FIXME: We should find a nicer way to make the labels for metadata, string |
| 140 | // concatenation is lame. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 141 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 142 | class ObjCCommonTypesHelper { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 143 | protected: |
| 144 | llvm::LLVMContext &VMContext; |
| 145 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 146 | private: |
| 147 | llvm::Constant *getMessageSendFn() const { |
| 148 | // id objc_msgSend (id, SEL, ...) |
| 149 | std::vector<const llvm::Type*> Params; |
| 150 | Params.push_back(ObjectPtrTy); |
| 151 | Params.push_back(SelectorPtrTy); |
| 152 | return |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 153 | CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy, |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 154 | Params, true), |
| 155 | "objc_msgSend"); |
| 156 | } |
| 157 | |
| 158 | llvm::Constant *getMessageSendStretFn() const { |
| 159 | // id objc_msgSend_stret (id, SEL, ...) |
| 160 | std::vector<const llvm::Type*> Params; |
| 161 | Params.push_back(ObjectPtrTy); |
| 162 | Params.push_back(SelectorPtrTy); |
| 163 | return |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 164 | CGM.CreateRuntimeFunction(VMContext.getFunctionType(llvm::Type::VoidTy, |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 165 | Params, true), |
| 166 | "objc_msgSend_stret"); |
| 167 | |
| 168 | } |
| 169 | |
| 170 | llvm::Constant *getMessageSendFpretFn() const { |
| 171 | // FIXME: This should be long double on x86_64? |
| 172 | // [double | long double] objc_msgSend_fpret(id self, SEL op, ...) |
| 173 | std::vector<const llvm::Type*> Params; |
| 174 | Params.push_back(ObjectPtrTy); |
| 175 | Params.push_back(SelectorPtrTy); |
| 176 | return |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 177 | CGM.CreateRuntimeFunction(VMContext.getFunctionType(llvm::Type::DoubleTy, |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 178 | Params, |
| 179 | true), |
| 180 | "objc_msgSend_fpret"); |
| 181 | |
| 182 | } |
| 183 | |
| 184 | llvm::Constant *getMessageSendSuperFn() const { |
| 185 | // id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
| 186 | const char *SuperName = "objc_msgSendSuper"; |
| 187 | std::vector<const llvm::Type*> Params; |
| 188 | Params.push_back(SuperPtrTy); |
| 189 | Params.push_back(SelectorPtrTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 190 | return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy, |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 191 | Params, true), |
| 192 | SuperName); |
| 193 | } |
| 194 | |
| 195 | llvm::Constant *getMessageSendSuperFn2() const { |
| 196 | // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...) |
| 197 | const char *SuperName = "objc_msgSendSuper2"; |
| 198 | std::vector<const llvm::Type*> Params; |
| 199 | Params.push_back(SuperPtrTy); |
| 200 | Params.push_back(SelectorPtrTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 201 | return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy, |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 202 | Params, true), |
| 203 | SuperName); |
| 204 | } |
| 205 | |
| 206 | llvm::Constant *getMessageSendSuperStretFn() const { |
| 207 | // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super, |
| 208 | // SEL op, ...) |
| 209 | std::vector<const llvm::Type*> Params; |
| 210 | Params.push_back(Int8PtrTy); |
| 211 | Params.push_back(SuperPtrTy); |
| 212 | Params.push_back(SelectorPtrTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 213 | return CGM.CreateRuntimeFunction( |
| 214 | VMContext.getFunctionType(llvm::Type::VoidTy, |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 215 | Params, true), |
| 216 | "objc_msgSendSuper_stret"); |
| 217 | } |
| 218 | |
| 219 | llvm::Constant *getMessageSendSuperStretFn2() const { |
| 220 | // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super, |
| 221 | // SEL op, ...) |
| 222 | std::vector<const llvm::Type*> Params; |
| 223 | Params.push_back(Int8PtrTy); |
| 224 | Params.push_back(SuperPtrTy); |
| 225 | Params.push_back(SelectorPtrTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 226 | return CGM.CreateRuntimeFunction( |
| 227 | VMContext.getFunctionType(llvm::Type::VoidTy, |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 228 | Params, true), |
| 229 | "objc_msgSendSuper2_stret"); |
| 230 | } |
| 231 | |
| 232 | llvm::Constant *getMessageSendSuperFpretFn() const { |
| 233 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 234 | return getMessageSendSuperFn(); |
| 235 | } |
| 236 | |
| 237 | llvm::Constant *getMessageSendSuperFpretFn2() const { |
| 238 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 239 | return getMessageSendSuperFn2(); |
| 240 | } |
| 241 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 242 | protected: |
| 243 | CodeGen::CodeGenModule &CGM; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 244 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 245 | public: |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 246 | const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 247 | const llvm::Type *Int8PtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 248 | |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 249 | /// ObjectPtrTy - LLVM type for object handles (typeof(id)) |
| 250 | const llvm::Type *ObjectPtrTy; |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 251 | |
| 252 | /// PtrObjectPtrTy - LLVM type for id * |
| 253 | const llvm::Type *PtrObjectPtrTy; |
| 254 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 255 | /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL)) |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 256 | const llvm::Type *SelectorPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 257 | /// ProtocolPtrTy - LLVM type for external protocol handles |
| 258 | /// (typeof(Protocol)) |
| 259 | const llvm::Type *ExternalProtocolPtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 260 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 261 | // SuperCTy - clang type for struct objc_super. |
| 262 | QualType SuperCTy; |
| 263 | // SuperPtrCTy - clang type for struct objc_super *. |
| 264 | QualType SuperPtrCTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 265 | |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 266 | /// SuperTy - LLVM type for struct objc_super. |
| 267 | const llvm::StructType *SuperTy; |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 268 | /// SuperPtrTy - LLVM type for struct objc_super *. |
| 269 | const llvm::Type *SuperPtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 270 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 271 | /// PropertyTy - LLVM type for struct objc_property (struct _prop_t |
| 272 | /// in GCC parlance). |
| 273 | const llvm::StructType *PropertyTy; |
| 274 | |
| 275 | /// PropertyListTy - LLVM type for struct objc_property_list |
| 276 | /// (_prop_list_t in GCC parlance). |
| 277 | const llvm::StructType *PropertyListTy; |
| 278 | /// PropertyListPtrTy - LLVM type for struct objc_property_list*. |
| 279 | const llvm::Type *PropertyListPtrTy; |
| 280 | |
| 281 | // MethodTy - LLVM type for struct objc_method. |
| 282 | const llvm::StructType *MethodTy; |
| 283 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 284 | /// CacheTy - LLVM type for struct objc_cache. |
| 285 | const llvm::Type *CacheTy; |
| 286 | /// CachePtrTy - LLVM type for struct objc_cache *. |
| 287 | const llvm::Type *CachePtrTy; |
| 288 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 289 | llvm::Constant *getGetPropertyFn() { |
| 290 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 291 | ASTContext &Ctx = CGM.getContext(); |
| 292 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
| 293 | llvm::SmallVector<QualType,16> Params; |
| 294 | QualType IdType = Ctx.getObjCIdType(); |
| 295 | QualType SelType = Ctx.getObjCSelType(); |
| 296 | Params.push_back(IdType); |
| 297 | Params.push_back(SelType); |
| 298 | Params.push_back(Ctx.LongTy); |
| 299 | Params.push_back(Ctx.BoolTy); |
| 300 | const llvm::FunctionType *FTy = |
| 301 | Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false); |
| 302 | return CGM.CreateRuntimeFunction(FTy, "objc_getProperty"); |
| 303 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 304 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 305 | llvm::Constant *getSetPropertyFn() { |
| 306 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 307 | ASTContext &Ctx = CGM.getContext(); |
| 308 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
| 309 | llvm::SmallVector<QualType,16> Params; |
| 310 | QualType IdType = Ctx.getObjCIdType(); |
| 311 | QualType SelType = Ctx.getObjCSelType(); |
| 312 | Params.push_back(IdType); |
| 313 | Params.push_back(SelType); |
| 314 | Params.push_back(Ctx.LongTy); |
| 315 | Params.push_back(IdType); |
| 316 | Params.push_back(Ctx.BoolTy); |
| 317 | Params.push_back(Ctx.BoolTy); |
| 318 | const llvm::FunctionType *FTy = |
| 319 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
| 320 | return CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); |
| 321 | } |
| 322 | |
| 323 | llvm::Constant *getEnumerationMutationFn() { |
Daniel Dunbar | c1ab900 | 2009-07-11 20:32:50 +0000 | [diff] [blame] | 324 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 325 | ASTContext &Ctx = CGM.getContext(); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 326 | // void objc_enumerationMutation (id) |
Daniel Dunbar | c1ab900 | 2009-07-11 20:32:50 +0000 | [diff] [blame] | 327 | llvm::SmallVector<QualType,16> Params; |
Daniel Dunbar | 309a436 | 2009-07-24 07:40:24 +0000 | [diff] [blame] | 328 | Params.push_back(Ctx.getObjCIdType()); |
Daniel Dunbar | c1ab900 | 2009-07-11 20:32:50 +0000 | [diff] [blame] | 329 | const llvm::FunctionType *FTy = |
| 330 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 331 | return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation"); |
| 332 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 333 | |
| 334 | /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 335 | llvm::Constant *getGcReadWeakFn() { |
| 336 | // id objc_read_weak (id *) |
| 337 | std::vector<const llvm::Type*> Args; |
| 338 | Args.push_back(ObjectPtrTy->getPointerTo()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 339 | llvm::FunctionType *FTy = |
| 340 | VMContext.getFunctionType(ObjectPtrTy, Args, false); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 341 | return CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
| 342 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 343 | |
| 344 | /// GcAssignWeakFn -- LLVM objc_assign_weak function. |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 345 | llvm::Constant *getGcAssignWeakFn() { |
| 346 | // id objc_assign_weak (id, id *) |
| 347 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 348 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 349 | llvm::FunctionType *FTy = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 350 | VMContext.getFunctionType(ObjectPtrTy, Args, false); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 351 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 352 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 353 | |
| 354 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 355 | llvm::Constant *getGcAssignGlobalFn() { |
| 356 | // id objc_assign_global(id, id *) |
| 357 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 358 | Args.push_back(ObjectPtrTy->getPointerTo()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 359 | llvm::FunctionType *FTy = |
| 360 | VMContext.getFunctionType(ObjectPtrTy, Args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 361 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 362 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 363 | |
| 364 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 365 | llvm::Constant *getGcAssignIvarFn() { |
| 366 | // id objc_assign_ivar(id, id *) |
| 367 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 368 | Args.push_back(ObjectPtrTy->getPointerTo()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 369 | llvm::FunctionType *FTy = |
| 370 | VMContext.getFunctionType(ObjectPtrTy, Args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 371 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 372 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 373 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 374 | /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function. |
| 375 | llvm::Constant *GcMemmoveCollectableFn() { |
| 376 | // void *objc_memmove_collectable(void *dst, const void *src, size_t size) |
| 377 | std::vector<const llvm::Type*> Args(1, Int8PtrTy); |
| 378 | Args.push_back(Int8PtrTy); |
| 379 | Args.push_back(LongTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 380 | llvm::FunctionType *FTy = VMContext.getFunctionType(Int8PtrTy, Args, false); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 381 | return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable"); |
| 382 | } |
| 383 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 384 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 385 | llvm::Constant *getGcAssignStrongCastFn() { |
| 386 | // id objc_assign_global(id, id *) |
| 387 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 388 | Args.push_back(ObjectPtrTy->getPointerTo()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 389 | llvm::FunctionType *FTy = |
| 390 | VMContext.getFunctionType(ObjectPtrTy, Args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 391 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); |
| 392 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 393 | |
| 394 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 395 | llvm::Constant *getExceptionThrowFn() { |
| 396 | // void objc_exception_throw(id) |
| 397 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 398 | llvm::FunctionType *FTy = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 399 | VMContext.getFunctionType(llvm::Type::VoidTy, Args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 400 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
| 401 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 402 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 403 | /// SyncEnterFn - LLVM object_sync_enter function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 404 | llvm::Constant *getSyncEnterFn() { |
| 405 | // void objc_sync_enter (id) |
| 406 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 407 | llvm::FunctionType *FTy = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 408 | VMContext.getFunctionType(llvm::Type::VoidTy, Args, false); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 409 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 410 | } |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 411 | |
| 412 | /// SyncExitFn - LLVM object_sync_exit function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 413 | llvm::Constant *getSyncExitFn() { |
| 414 | // void objc_sync_exit (id) |
| 415 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 416 | llvm::FunctionType *FTy = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 417 | VMContext.getFunctionType(llvm::Type::VoidTy, Args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 418 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
| 419 | } |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 420 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 421 | llvm::Constant *getSendFn(bool IsSuper) const { |
| 422 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFn(); |
| 423 | } |
| 424 | |
| 425 | llvm::Constant *getSendFn2(bool IsSuper) const { |
| 426 | return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn(); |
| 427 | } |
| 428 | |
| 429 | llvm::Constant *getSendStretFn(bool IsSuper) const { |
| 430 | return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn(); |
| 431 | } |
| 432 | |
| 433 | llvm::Constant *getSendStretFn2(bool IsSuper) const { |
| 434 | return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn(); |
| 435 | } |
| 436 | |
| 437 | llvm::Constant *getSendFpretFn(bool IsSuper) const { |
| 438 | return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn(); |
| 439 | } |
| 440 | |
| 441 | llvm::Constant *getSendFpretFn2(bool IsSuper) const { |
| 442 | return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn(); |
| 443 | } |
| 444 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 445 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
| 446 | ~ObjCCommonTypesHelper(){} |
| 447 | }; |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 448 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 449 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 450 | /// construction of varies types used during ObjC generation. |
| 451 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 452 | public: |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 453 | /// SymtabTy - LLVM type for struct objc_symtab. |
| 454 | const llvm::StructType *SymtabTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 455 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
| 456 | const llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 457 | /// ModuleTy - LLVM type for struct objc_module. |
| 458 | const llvm::StructType *ModuleTy; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 459 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 460 | /// ProtocolTy - LLVM type for struct objc_protocol. |
| 461 | const llvm::StructType *ProtocolTy; |
| 462 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
| 463 | const llvm::Type *ProtocolPtrTy; |
| 464 | /// ProtocolExtensionTy - LLVM type for struct |
| 465 | /// objc_protocol_extension. |
| 466 | const llvm::StructType *ProtocolExtensionTy; |
| 467 | /// ProtocolExtensionTy - LLVM type for struct |
| 468 | /// objc_protocol_extension *. |
| 469 | const llvm::Type *ProtocolExtensionPtrTy; |
| 470 | /// MethodDescriptionTy - LLVM type for struct |
| 471 | /// objc_method_description. |
| 472 | const llvm::StructType *MethodDescriptionTy; |
| 473 | /// MethodDescriptionListTy - LLVM type for struct |
| 474 | /// objc_method_description_list. |
| 475 | const llvm::StructType *MethodDescriptionListTy; |
| 476 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 477 | /// objc_method_description_list *. |
| 478 | const llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 479 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
| 480 | const llvm::Type *ProtocolListTy; |
| 481 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
| 482 | const llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 483 | /// CategoryTy - LLVM type for struct objc_category. |
| 484 | const llvm::StructType *CategoryTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 485 | /// ClassTy - LLVM type for struct objc_class. |
| 486 | const llvm::StructType *ClassTy; |
| 487 | /// ClassPtrTy - LLVM type for struct objc_class *. |
| 488 | const llvm::Type *ClassPtrTy; |
| 489 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
| 490 | const llvm::StructType *ClassExtensionTy; |
| 491 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
| 492 | const llvm::Type *ClassExtensionPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 493 | // IvarTy - LLVM type for struct objc_ivar. |
| 494 | const llvm::StructType *IvarTy; |
| 495 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
| 496 | const llvm::Type *IvarListTy; |
| 497 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
| 498 | const llvm::Type *IvarListPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 499 | /// MethodListTy - LLVM type for struct objc_method_list. |
| 500 | const llvm::Type *MethodListTy; |
| 501 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
| 502 | const llvm::Type *MethodListPtrTy; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 503 | |
| 504 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
| 505 | const llvm::Type *ExceptionDataTy; |
| 506 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 507 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 508 | llvm::Constant *getExceptionTryEnterFn() { |
| 509 | std::vector<const llvm::Type*> Params; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 510 | Params.push_back(VMContext.getPointerTypeUnqual(ExceptionDataTy)); |
| 511 | return CGM.CreateRuntimeFunction( |
| 512 | VMContext.getFunctionType(llvm::Type::VoidTy, |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 513 | Params, false), |
| 514 | "objc_exception_try_enter"); |
| 515 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 516 | |
| 517 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 518 | llvm::Constant *getExceptionTryExitFn() { |
| 519 | std::vector<const llvm::Type*> Params; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 520 | Params.push_back(VMContext.getPointerTypeUnqual(ExceptionDataTy)); |
| 521 | return CGM.CreateRuntimeFunction( |
| 522 | VMContext.getFunctionType(llvm::Type::VoidTy, |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 523 | Params, false), |
| 524 | "objc_exception_try_exit"); |
| 525 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 526 | |
| 527 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 528 | llvm::Constant *getExceptionExtractFn() { |
| 529 | std::vector<const llvm::Type*> Params; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 530 | Params.push_back(VMContext.getPointerTypeUnqual(ExceptionDataTy)); |
| 531 | return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy, |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 532 | Params, false), |
| 533 | "objc_exception_extract"); |
| 534 | |
| 535 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 536 | |
| 537 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 538 | llvm::Constant *getExceptionMatchFn() { |
| 539 | std::vector<const llvm::Type*> Params; |
| 540 | Params.push_back(ClassPtrTy); |
| 541 | Params.push_back(ObjectPtrTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 542 | return CGM.CreateRuntimeFunction( |
| 543 | VMContext.getFunctionType(llvm::Type::Int32Ty, |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 544 | Params, false), |
| 545 | "objc_exception_match"); |
| 546 | |
| 547 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 548 | |
| 549 | /// SetJmpFn - LLVM _setjmp function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 550 | llvm::Constant *getSetJmpFn() { |
| 551 | std::vector<const llvm::Type*> Params; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 552 | Params.push_back(VMContext.getPointerTypeUnqual(llvm::Type::Int32Ty)); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 553 | return |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 554 | CGM.CreateRuntimeFunction(VMContext.getFunctionType(llvm::Type::Int32Ty, |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 555 | Params, false), |
| 556 | "_setjmp"); |
| 557 | |
| 558 | } |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 559 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 560 | public: |
| 561 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 562 | ~ObjCTypesHelper() {} |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 563 | }; |
| 564 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 565 | /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 566 | /// modern abi |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 567 | class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 568 | public: |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 569 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 570 | // MethodListnfABITy - LLVM for struct _method_list_t |
| 571 | const llvm::StructType *MethodListnfABITy; |
| 572 | |
| 573 | // MethodListnfABIPtrTy - LLVM for struct _method_list_t* |
| 574 | const llvm::Type *MethodListnfABIPtrTy; |
| 575 | |
| 576 | // ProtocolnfABITy = LLVM for struct _protocol_t |
| 577 | const llvm::StructType *ProtocolnfABITy; |
| 578 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 579 | // ProtocolnfABIPtrTy = LLVM for struct _protocol_t* |
| 580 | const llvm::Type *ProtocolnfABIPtrTy; |
| 581 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 582 | // ProtocolListnfABITy - LLVM for struct _objc_protocol_list |
| 583 | const llvm::StructType *ProtocolListnfABITy; |
| 584 | |
| 585 | // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list* |
| 586 | const llvm::Type *ProtocolListnfABIPtrTy; |
| 587 | |
| 588 | // ClassnfABITy - LLVM for struct _class_t |
| 589 | const llvm::StructType *ClassnfABITy; |
| 590 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 591 | // ClassnfABIPtrTy - LLVM for struct _class_t* |
| 592 | const llvm::Type *ClassnfABIPtrTy; |
| 593 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 594 | // IvarnfABITy - LLVM for struct _ivar_t |
| 595 | const llvm::StructType *IvarnfABITy; |
| 596 | |
| 597 | // IvarListnfABITy - LLVM for struct _ivar_list_t |
| 598 | const llvm::StructType *IvarListnfABITy; |
| 599 | |
| 600 | // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t* |
| 601 | const llvm::Type *IvarListnfABIPtrTy; |
| 602 | |
| 603 | // ClassRonfABITy - LLVM for struct _class_ro_t |
| 604 | const llvm::StructType *ClassRonfABITy; |
| 605 | |
| 606 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 607 | const llvm::Type *ImpnfABITy; |
| 608 | |
| 609 | // CategorynfABITy - LLVM for struct _category_t |
| 610 | const llvm::StructType *CategorynfABITy; |
| 611 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 612 | // New types for nonfragile abi messaging. |
| 613 | |
| 614 | // MessageRefTy - LLVM for: |
| 615 | // struct _message_ref_t { |
| 616 | // IMP messenger; |
| 617 | // SEL name; |
| 618 | // }; |
| 619 | const llvm::StructType *MessageRefTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 620 | // MessageRefCTy - clang type for struct _message_ref_t |
| 621 | QualType MessageRefCTy; |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 622 | |
| 623 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 624 | const llvm::Type *MessageRefPtrTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 625 | // MessageRefCPtrTy - clang type for struct _message_ref_t* |
| 626 | QualType MessageRefCPtrTy; |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 627 | |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 628 | // MessengerTy - Type of the messenger (shown as IMP above) |
| 629 | const llvm::FunctionType *MessengerTy; |
| 630 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 631 | // SuperMessageRefTy - LLVM for: |
| 632 | // struct _super_message_ref_t { |
| 633 | // SUPER_IMP messenger; |
| 634 | // SEL name; |
| 635 | // }; |
| 636 | const llvm::StructType *SuperMessageRefTy; |
| 637 | |
| 638 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 639 | const llvm::Type *SuperMessageRefPtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 640 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 641 | llvm::Constant *getMessageSendFixupFn() { |
| 642 | // id objc_msgSend_fixup(id, struct message_ref_t*, ...) |
| 643 | std::vector<const llvm::Type*> Params; |
| 644 | Params.push_back(ObjectPtrTy); |
| 645 | Params.push_back(MessageRefPtrTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 646 | return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 647 | Params, true), |
| 648 | "objc_msgSend_fixup"); |
| 649 | } |
| 650 | |
| 651 | llvm::Constant *getMessageSendFpretFixupFn() { |
| 652 | // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...) |
| 653 | std::vector<const llvm::Type*> Params; |
| 654 | Params.push_back(ObjectPtrTy); |
| 655 | Params.push_back(MessageRefPtrTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 656 | return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 657 | Params, true), |
| 658 | "objc_msgSend_fpret_fixup"); |
| 659 | } |
| 660 | |
| 661 | llvm::Constant *getMessageSendStretFixupFn() { |
| 662 | // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...) |
| 663 | std::vector<const llvm::Type*> Params; |
| 664 | Params.push_back(ObjectPtrTy); |
| 665 | Params.push_back(MessageRefPtrTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 666 | return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 667 | Params, true), |
| 668 | "objc_msgSend_stret_fixup"); |
| 669 | } |
| 670 | |
| 671 | llvm::Constant *getMessageSendIdFixupFn() { |
| 672 | // id objc_msgSendId_fixup(id, struct message_ref_t*, ...) |
| 673 | std::vector<const llvm::Type*> Params; |
| 674 | Params.push_back(ObjectPtrTy); |
| 675 | Params.push_back(MessageRefPtrTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 676 | return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 677 | Params, true), |
| 678 | "objc_msgSendId_fixup"); |
| 679 | } |
| 680 | |
| 681 | llvm::Constant *getMessageSendIdStretFixupFn() { |
| 682 | // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...) |
| 683 | std::vector<const llvm::Type*> Params; |
| 684 | Params.push_back(ObjectPtrTy); |
| 685 | Params.push_back(MessageRefPtrTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 686 | return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 687 | Params, true), |
| 688 | "objc_msgSendId_stret_fixup"); |
| 689 | } |
| 690 | llvm::Constant *getMessageSendSuper2FixupFn() { |
| 691 | // id objc_msgSendSuper2_fixup (struct objc_super *, |
| 692 | // struct _super_message_ref_t*, ...) |
| 693 | std::vector<const llvm::Type*> Params; |
| 694 | Params.push_back(SuperPtrTy); |
| 695 | Params.push_back(SuperMessageRefPtrTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 696 | return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 697 | Params, true), |
| 698 | "objc_msgSendSuper2_fixup"); |
| 699 | } |
| 700 | |
| 701 | llvm::Constant *getMessageSendSuper2StretFixupFn() { |
| 702 | // id objc_msgSendSuper2_stret_fixup(struct objc_super *, |
| 703 | // struct _super_message_ref_t*, ...) |
| 704 | std::vector<const llvm::Type*> Params; |
| 705 | Params.push_back(SuperPtrTy); |
| 706 | Params.push_back(SuperMessageRefPtrTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 707 | return CGM.CreateRuntimeFunction(VMContext.getFunctionType(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 708 | Params, true), |
| 709 | "objc_msgSendSuper2_stret_fixup"); |
| 710 | } |
| 711 | |
| 712 | |
| 713 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 714 | /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C |
| 715 | /// exception personality function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 716 | llvm::Value *getEHPersonalityPtr() { |
| 717 | llvm::Constant *Personality = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 718 | CGM.CreateRuntimeFunction(VMContext.getFunctionType(llvm::Type::Int32Ty, |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 719 | true), |
| 720 | "__objc_personality_v0"); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 721 | return VMContext.getConstantExprBitCast(Personality, Int8PtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 722 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 723 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 724 | llvm::Constant *getUnwindResumeOrRethrowFn() { |
| 725 | std::vector<const llvm::Type*> Params; |
| 726 | Params.push_back(Int8PtrTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 727 | return CGM.CreateRuntimeFunction( |
| 728 | VMContext.getFunctionType(llvm::Type::VoidTy, |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 729 | Params, false), |
| 730 | "_Unwind_Resume_or_Rethrow"); |
| 731 | } |
| 732 | |
| 733 | llvm::Constant *getObjCEndCatchFn() { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 734 | return CGM.CreateRuntimeFunction(VMContext.getFunctionType(llvm::Type::VoidTy, |
Chris Lattner | b59761b | 2009-07-01 04:13:52 +0000 | [diff] [blame] | 735 | false), |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 736 | "objc_end_catch"); |
| 737 | |
| 738 | } |
| 739 | |
| 740 | llvm::Constant *getObjCBeginCatchFn() { |
| 741 | std::vector<const llvm::Type*> Params; |
| 742 | Params.push_back(Int8PtrTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 743 | return CGM.CreateRuntimeFunction(VMContext.getFunctionType(Int8PtrTy, |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 744 | Params, false), |
| 745 | "objc_begin_catch"); |
| 746 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 747 | |
| 748 | const llvm::StructType *EHTypeTy; |
| 749 | const llvm::Type *EHTypePtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 750 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 751 | ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); |
| 752 | ~ObjCNonFragileABITypesHelper(){} |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 753 | }; |
| 754 | |
| 755 | class CGObjCCommonMac : public CodeGen::CGObjCRuntime { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 756 | public: |
| 757 | // FIXME - accessibility |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 758 | class GC_IVAR { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 759 | public: |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 760 | unsigned ivar_bytepos; |
| 761 | unsigned ivar_size; |
| 762 | GC_IVAR(unsigned bytepos = 0, unsigned size = 0) |
| 763 | : ivar_bytepos(bytepos), ivar_size(size) {} |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 764 | |
| 765 | // Allow sorting based on byte pos. |
| 766 | bool operator<(const GC_IVAR &b) const { |
| 767 | return ivar_bytepos < b.ivar_bytepos; |
| 768 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 769 | }; |
| 770 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 771 | class SKIP_SCAN { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 772 | public: |
| 773 | unsigned skip; |
| 774 | unsigned scan; |
| 775 | SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0) |
| 776 | : skip(_skip), scan(_scan) {} |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 777 | }; |
| 778 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 779 | protected: |
| 780 | CodeGen::CodeGenModule &CGM; |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 781 | llvm::LLVMContext &VMContext; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 782 | // FIXME! May not be needing this after all. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 783 | unsigned ObjCABI; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 784 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 785 | // gc ivar layout bitmap calculation helper caches. |
| 786 | llvm::SmallVector<GC_IVAR, 16> SkipIvars; |
| 787 | llvm::SmallVector<GC_IVAR, 16> IvarsInfo; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 788 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 789 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 790 | /// DefinedSymbols and FinishModule(). |
| 791 | std::set<IdentifierInfo*> LazySymbols; |
| 792 | |
| 793 | /// DefinedSymbols - External symbols which are defined by this |
| 794 | /// module. The symbols in this list and LazySymbols are used to add |
| 795 | /// special linker symbols which ensure that Objective-C modules are |
| 796 | /// linked properly. |
| 797 | std::set<IdentifierInfo*> DefinedSymbols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 798 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 799 | /// ClassNames - uniqued class names. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 800 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 801 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 802 | /// MethodVarNames - uniqued method variable names. |
| 803 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 804 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 805 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 806 | /// a StringMap here because have no other unique reference. |
| 807 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 808 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 809 | /// MethodDefinitions - map of methods which have been defined in |
| 810 | /// this translation unit. |
| 811 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 812 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 813 | /// PropertyNames - uniqued method variable names. |
| 814 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 815 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 816 | /// ClassReferences - uniqued class references. |
| 817 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 818 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 819 | /// SelectorReferences - uniqued selector references. |
| 820 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 821 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 822 | /// Protocols - Protocols for which an objc_protocol structure has |
| 823 | /// been emitted. Forward declarations are handled by creating an |
| 824 | /// empty structure whose initializer is filled in when/if defined. |
| 825 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 826 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 827 | /// DefinedProtocols - Protocols which have actually been |
| 828 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 829 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 830 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 831 | /// DefinedClasses - List of defined classes. |
| 832 | std::vector<llvm::GlobalValue*> DefinedClasses; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 833 | |
| 834 | /// DefinedNonLazyClasses - List of defined "non-lazy" classes. |
| 835 | std::vector<llvm::GlobalValue*> DefinedNonLazyClasses; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 836 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 837 | /// DefinedCategories - List of defined categories. |
| 838 | std::vector<llvm::GlobalValue*> DefinedCategories; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 839 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 840 | /// DefinedNonLazyCategories - List of defined "non-lazy" categories. |
| 841 | std::vector<llvm::GlobalValue*> DefinedNonLazyCategories; |
| 842 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 843 | /// GetNameForMethod - Return a name for the given method. |
| 844 | /// \param[out] NameOut - The return value. |
| 845 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 846 | const ObjCContainerDecl *CD, |
| 847 | std::string &NameOut); |
| 848 | |
| 849 | /// GetMethodVarName - Return a unique constant for the given |
| 850 | /// selector's name. The return value has type char *. |
| 851 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 852 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
| 853 | llvm::Constant *GetMethodVarName(const std::string &Name); |
| 854 | |
| 855 | /// GetMethodVarType - Return a unique constant for the given |
| 856 | /// selector's name. The return value has type char *. |
| 857 | |
| 858 | // FIXME: This is a horrible name. |
| 859 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 860 | llvm::Constant *GetMethodVarType(const FieldDecl *D); |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 861 | |
| 862 | /// GetPropertyName - Return a unique constant for the given |
| 863 | /// name. The return value has type char *. |
| 864 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
| 865 | |
| 866 | // FIXME: This can be dropped once string functions are unified. |
| 867 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 868 | const Decl *Container); |
| 869 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 870 | /// GetClassName - Return a unique constant for the given selector's |
| 871 | /// name. The return value has type char *. |
| 872 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
| 873 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 874 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 875 | /// implementation for the __strong or __weak case. |
| 876 | /// |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 877 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
| 878 | bool ForStrongLayout); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 879 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 880 | void BuildAggrIvarRecordLayout(const RecordType *RT, |
| 881 | unsigned int BytePos, bool ForStrongLayout, |
| 882 | bool &HasUnion); |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 883 | void BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 884 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 885 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 886 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 887 | unsigned int BytePos, bool ForStrongLayout, |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 888 | bool &HasUnion); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 889 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 890 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 891 | /// ivar layout bitmap. |
| 892 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 893 | const ObjCCommonTypesHelper &ObjCTypes); |
| 894 | |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 895 | /// EmitPropertyList - Emit the given property list. The return |
| 896 | /// value has type PropertyListPtrTy. |
| 897 | llvm::Constant *EmitPropertyList(const std::string &Name, |
| 898 | const Decl *Container, |
| 899 | const ObjCContainerDecl *OCD, |
| 900 | const ObjCCommonTypesHelper &ObjCTypes); |
| 901 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 902 | /// GetProtocolRef - Return a reference to the internal protocol |
| 903 | /// description, creating an empty one if it has not been |
| 904 | /// defined. The return value has type ProtocolPtrTy. |
| 905 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 906 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 907 | /// CreateMetadataVar - Create a global variable with internal |
| 908 | /// linkage for use by the Objective-C runtime. |
| 909 | /// |
| 910 | /// This is a convenience wrapper which not only creates the |
| 911 | /// variable, but also sets the section and alignment and adds the |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 912 | /// global to the "llvm.used" list. |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 913 | /// |
| 914 | /// \param Name - The variable name. |
| 915 | /// \param Init - The variable initializer; this is also used to |
| 916 | /// define the type of the variable. |
| 917 | /// \param Section - The section the variable should go into, or 0. |
| 918 | /// \param Align - The alignment for the variable, or 0. |
| 919 | /// \param AddToUsed - Whether the variable should be added to |
Daniel Dunbar | c158306 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 920 | /// "llvm.used". |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 921 | llvm::GlobalVariable *CreateMetadataVar(const std::string &Name, |
| 922 | llvm::Constant *Init, |
| 923 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 924 | unsigned Align, |
| 925 | bool AddToUsed); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 926 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 927 | CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF, |
| 928 | QualType ResultType, |
| 929 | llvm::Value *Sel, |
| 930 | llvm::Value *Arg0, |
| 931 | QualType Arg0Ty, |
| 932 | bool IsSuper, |
| 933 | const CallArgList &CallArgs, |
| 934 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 935 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 936 | public: |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 937 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : |
| 938 | CGM(cgm), VMContext(cgm.getLLVMContext()) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 939 | { } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 940 | |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 941 | virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 942 | |
| 943 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 944 | const ObjCContainerDecl *CD=0); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 945 | |
| 946 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
| 947 | |
| 948 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 949 | /// declaration, emitting it if necessary. The return value has type |
| 950 | /// ProtocolPtrTy. |
| 951 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
| 952 | |
| 953 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 954 | /// object for the given declaration, emitting it if needed. These |
| 955 | /// forward references will be filled in with empty bodies if no |
| 956 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 957 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 958 | }; |
| 959 | |
| 960 | class CGObjCMac : public CGObjCCommonMac { |
| 961 | private: |
| 962 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 963 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 964 | /// level information. |
| 965 | void EmitImageInfo(); |
| 966 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 967 | /// EmitModuleInfo - Another marker encoding module level |
| 968 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 969 | void EmitModuleInfo(); |
| 970 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 971 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 972 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 973 | llvm::Constant *EmitModuleSymbols(); |
| 974 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 975 | /// FinishModule - Write out global data structures at the end of |
| 976 | /// processing a translation unit. |
| 977 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 978 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 979 | /// EmitClassExtension - Generate the class extension structure used |
| 980 | /// to store the weak ivar layout and properties. The return value |
| 981 | /// has type ClassExtensionPtrTy. |
| 982 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 983 | |
| 984 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 985 | /// for the given class. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 986 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 987 | const ObjCInterfaceDecl *ID); |
| 988 | |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 989 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 990 | QualType ResultType, |
| 991 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 992 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 993 | QualType Arg0Ty, |
| 994 | bool IsSuper, |
| 995 | const CallArgList &CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 996 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 997 | /// EmitIvarList - Emit the ivar list for the given |
| 998 | /// implementation. If ForClass is true the list of class ivars |
| 999 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1000 | /// interface ivars will be emitted. The return value has type |
| 1001 | /// IvarListPtrTy. |
| 1002 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1003 | bool ForClass); |
| 1004 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1005 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 1006 | /// for the metaclass of the given interface. The return value has |
| 1007 | /// type ClassPtrTy. |
| 1008 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 1009 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1010 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1011 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1012 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1013 | llvm::Constant *Protocols, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1014 | const ConstantVector &Methods); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1015 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1016 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1017 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1018 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1019 | |
| 1020 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1021 | /// implementation. The return value has type MethodListPtrTy. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1022 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 1023 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1024 | const ConstantVector &Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1025 | |
| 1026 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1027 | /// method declarations. |
| 1028 | /// - TypeName: The name for the type containing the methods. |
| 1029 | /// - IsProtocol: True iff these methods are for a protocol. |
| 1030 | /// - ClassMethds: True iff these are class methods. |
| 1031 | /// - Required: When true, only "required" methods are |
| 1032 | /// listed. Similarly, when false only "optional" methods are |
| 1033 | /// listed. For classes this should always be true. |
| 1034 | /// - begin, end: The method list to output. |
| 1035 | /// |
| 1036 | /// The return value has type MethodDescriptionListPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1037 | llvm::Constant *EmitMethodDescList(const std::string &Name, |
| 1038 | const char *Section, |
| 1039 | const ConstantVector &Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1040 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1041 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1042 | /// declaration, emitting it if necessary. The return value has type |
| 1043 | /// ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1044 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1045 | |
| 1046 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1047 | /// object for the given declaration, emitting it if needed. These |
| 1048 | /// forward references will be filled in with empty bodies if no |
| 1049 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1050 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1051 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1052 | /// EmitProtocolExtension - Generate the protocol extension |
| 1053 | /// structure used to store optional instance and class methods, and |
| 1054 | /// protocol properties. The return value has type |
| 1055 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1056 | llvm::Constant * |
| 1057 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1058 | const ConstantVector &OptInstanceMethods, |
| 1059 | const ConstantVector &OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1060 | |
| 1061 | /// EmitProtocolList - Generate the list of referenced |
| 1062 | /// protocols. The return value has type ProtocolListPtrTy. |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1063 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 1064 | ObjCProtocolDecl::protocol_iterator begin, |
| 1065 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1066 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1067 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1068 | /// for the given selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1069 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1070 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1071 | public: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1072 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1073 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1074 | virtual llvm::Function *ModuleInitFunction(); |
| 1075 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1076 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1077 | QualType ResultType, |
| 1078 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1079 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1080 | bool IsClassMessage, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1081 | const CallArgList &CallArgs, |
| 1082 | const ObjCMethodDecl *Method); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1083 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1084 | virtual CodeGen::RValue |
| 1085 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1086 | QualType ResultType, |
| 1087 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1088 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1089 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1090 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1091 | bool IsClassMessage, |
| 1092 | const CallArgList &CallArgs); |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1093 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1094 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1095 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1096 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1097 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel); |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1098 | |
| 1099 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1100 | /// untyped one. |
| 1101 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1102 | const ObjCMethodDecl *Method); |
| 1103 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1104 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1105 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1106 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1107 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1108 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1109 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1110 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1111 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 1112 | virtual llvm::Constant *GetPropertySetFunction(); |
| 1113 | virtual llvm::Constant *EnumerationMutationFunction(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1114 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 1115 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1116 | const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1117 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 1118 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1119 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 1120 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1121 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 1122 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1123 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 1124 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 1125 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 1126 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1127 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 1128 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1129 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1130 | llvm::Value *dest, llvm::Value *src, |
| 1131 | unsigned long size); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 1132 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1133 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1134 | QualType ObjectTy, |
| 1135 | llvm::Value *BaseValue, |
| 1136 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1137 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1138 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1139 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1140 | const ObjCIvarDecl *Ivar); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1141 | }; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1142 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1143 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1144 | private: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1145 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1146 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 1147 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1148 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1149 | /// SuperClassReferences - uniqued super class references. |
| 1150 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; |
| 1151 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1152 | /// MetaClassReferences - uniqued meta class references. |
| 1153 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1154 | |
| 1155 | /// EHTypeReferences - uniqued class ehtype references. |
| 1156 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1157 | |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1158 | /// NonLegacyDispatchMethods - List of methods for which we do *not* generate |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1159 | /// legacy messaging dispatch. |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1160 | llvm::DenseSet<Selector> NonLegacyDispatchMethods; |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1161 | |
| 1162 | /// LegacyDispatchedSelector - Returns true if SEL is not in the list of |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1163 | /// NonLegacyDispatchMethods; false otherwise. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1164 | bool LegacyDispatchedSelector(Selector Sel); |
| 1165 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1166 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 1167 | /// processing a translation unit. |
| 1168 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1169 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 1170 | /// AddModuleClassList - Add the given list of class pointers to the |
| 1171 | /// module with the provided symbol and section names. |
| 1172 | void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container, |
| 1173 | const char *SymbolName, |
| 1174 | const char *SectionName); |
| 1175 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 1176 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 1177 | unsigned InstanceStart, |
| 1178 | unsigned InstanceSize, |
| 1179 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1180 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
| 1181 | llvm::Constant *IsAGV, |
| 1182 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 1183 | llvm::Constant *ClassRoGV, |
| 1184 | bool HiddenVisibility); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1185 | |
| 1186 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
| 1187 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 1188 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
| 1189 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1190 | /// EmitMethodList - Emit the method list for the given |
| 1191 | /// implementation. The return value has type MethodListnfABITy. |
| 1192 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 1193 | const char *Section, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 1194 | const ConstantVector &Methods); |
| 1195 | /// EmitIvarList - Emit the ivar list for the given |
| 1196 | /// implementation. If ForClass is true the list of class ivars |
| 1197 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1198 | /// interface ivars will be emitted. The return value has type |
| 1199 | /// IvarListnfABIPtrTy. |
| 1200 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 1201 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1202 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 1203 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 1204 | unsigned long int offset); |
| 1205 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1206 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1207 | /// declaration, emitting it if necessary. The return value has type |
| 1208 | /// ProtocolPtrTy. |
| 1209 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
| 1210 | |
| 1211 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1212 | /// object for the given declaration, emitting it if needed. These |
| 1213 | /// forward references will be filled in with empty bodies if no |
| 1214 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1215 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
| 1216 | |
| 1217 | /// EmitProtocolList - Generate the list of referenced |
| 1218 | /// protocols. The return value has type ProtocolListPtrTy. |
| 1219 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 1220 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1221 | ObjCProtocolDecl::protocol_iterator end); |
| 1222 | |
| 1223 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1224 | QualType ResultType, |
| 1225 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 1226 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1227 | QualType Arg0Ty, |
| 1228 | bool IsSuper, |
| 1229 | const CallArgList &CallArgs); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 1230 | |
| 1231 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1232 | /// class of the given name. |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 1233 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name); |
| 1234 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1235 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1236 | /// for the given class reference. |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1237 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1238 | const ObjCInterfaceDecl *ID); |
| 1239 | |
| 1240 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1241 | /// for the given super class reference. |
| 1242 | llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder, |
| 1243 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1244 | |
| 1245 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 1246 | /// meta-data |
| 1247 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
| 1248 | const ObjCInterfaceDecl *ID); |
| 1249 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1250 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 1251 | /// the given ivar. |
| 1252 | /// |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 1253 | llvm::GlobalVariable * ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1254 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1255 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1256 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1257 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1258 | /// for the given selector. |
| 1259 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1260 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1261 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1262 | /// interface. The return value has type EHTypePtrTy. |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1263 | llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 1264 | bool ForDefinition); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1265 | |
| 1266 | const char *getMetaclassSymbolPrefix() const { |
| 1267 | return "OBJC_METACLASS_$_"; |
| 1268 | } |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1269 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1270 | const char *getClassSymbolPrefix() const { |
| 1271 | return "OBJC_CLASS_$_"; |
| 1272 | } |
| 1273 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 1274 | void GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1275 | uint32_t &InstanceStart, |
| 1276 | uint32_t &InstanceSize); |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1277 | |
| 1278 | // Shamelessly stolen from Analysis/CFRefCount.cpp |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1279 | Selector GetNullarySelector(const char* name) const { |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1280 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1281 | return CGM.getContext().Selectors.getSelector(0, &II); |
| 1282 | } |
| 1283 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1284 | Selector GetUnarySelector(const char* name) const { |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1285 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1286 | return CGM.getContext().Selectors.getSelector(1, &II); |
| 1287 | } |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1288 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1289 | /// ImplementationIsNonLazy - Check whether the given category or |
| 1290 | /// class implementation is "non-lazy". |
Fariborz Jahanian | ecfbdcb | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 1291 | bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1292 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1293 | public: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1294 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1295 | // FIXME. All stubs for now! |
| 1296 | virtual llvm::Function *ModuleInitFunction(); |
| 1297 | |
| 1298 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1299 | QualType ResultType, |
| 1300 | Selector Sel, |
| 1301 | llvm::Value *Receiver, |
| 1302 | bool IsClassMessage, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1303 | const CallArgList &CallArgs, |
| 1304 | const ObjCMethodDecl *Method); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1305 | |
| 1306 | virtual CodeGen::RValue |
| 1307 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 1308 | QualType ResultType, |
| 1309 | Selector Sel, |
| 1310 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1311 | bool isCategoryImpl, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1312 | llvm::Value *Receiver, |
| 1313 | bool IsClassMessage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1314 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1315 | |
| 1316 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1317 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1318 | |
| 1319 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel) |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1320 | { return EmitSelector(Builder, Sel); } |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1321 | |
| 1322 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1323 | /// untyped one. |
| 1324 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1325 | const ObjCMethodDecl *Method) |
| 1326 | { return EmitSelector(Builder, Method->getSelector()); } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1327 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 1328 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1329 | |
| 1330 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1331 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 1332 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1333 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1334 | virtual llvm::Constant *GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1335 | return ObjCTypes.getGetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1336 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1337 | virtual llvm::Constant *GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1338 | return ObjCTypes.getSetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1339 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1340 | virtual llvm::Constant *EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1341 | return ObjCTypes.getEnumerationMutationFn(); |
Daniel Dunbar | 28ed084 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 1342 | } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1343 | |
| 1344 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 1345 | const Stmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1346 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 1347 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1348 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1349 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1350 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1351 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1352 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1353 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1354 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1355 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1356 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1357 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1358 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1359 | llvm::Value *dest, llvm::Value *src, |
| 1360 | unsigned long size); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1361 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1362 | QualType ObjectTy, |
| 1363 | llvm::Value *BaseValue, |
| 1364 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1365 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1366 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1367 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1368 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1369 | }; |
| 1370 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1371 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1372 | |
| 1373 | /* *** Helper Functions *** */ |
| 1374 | |
| 1375 | /// getConstantGEP() - Help routine to construct simple GEPs. |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1376 | static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext, |
| 1377 | llvm::Constant *C, |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1378 | unsigned idx0, |
| 1379 | unsigned idx1) { |
| 1380 | llvm::Value *Idxs[] = { |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 1381 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0), |
| 1382 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1383 | }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1384 | return VMContext.getConstantExprGetElementPtr(C, Idxs, 2); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1387 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 1388 | /// class has the __objc_exception__ attribute. |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1389 | static bool hasObjCExceptionAttribute(ASTContext &Context, |
| 1390 | const ObjCInterfaceDecl *OID) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1391 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1392 | return true; |
| 1393 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1394 | return hasObjCExceptionAttribute(Context, Super); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1395 | return false; |
| 1396 | } |
| 1397 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1398 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1399 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1400 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
| 1401 | ObjCTypes(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1402 | { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1403 | ObjCABI = 1; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1404 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1407 | /// GetClass - Return a reference to the class for the given interface |
| 1408 | /// decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1409 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1410 | const ObjCInterfaceDecl *ID) { |
| 1411 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
| 1414 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1415 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 1416 | return EmitSelector(Builder, Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1417 | } |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1418 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl |
| 1419 | *Method) { |
| 1420 | return EmitSelector(Builder, Method->getSelector()); |
| 1421 | } |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1422 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1423 | /// Generate a constant CFString object. |
| 1424 | /* |
| 1425 | struct __builtin_CFString { |
| 1426 | const int *isa; // point to __CFConstantStringClassReference |
| 1427 | int flags; |
| 1428 | const char *str; |
| 1429 | long length; |
| 1430 | }; |
| 1431 | */ |
| 1432 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1433 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 1434 | const ObjCStringLiteral *SL) { |
Steve Naroff | 8d4141f | 2009-04-01 13:55:36 +0000 | [diff] [blame] | 1435 | return CGM.GetAddrOfConstantCFString(SL->getString()); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | /// Generates a message send where the super is the receiver. This is |
| 1439 | /// a message send to self with special delivery semantics indicating |
| 1440 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1441 | CodeGen::RValue |
| 1442 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1443 | QualType ResultType, |
| 1444 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1445 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1446 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1447 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1448 | bool IsClassMessage, |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1449 | const CodeGen::CallArgList &CallArgs) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1450 | // Create and init a super structure; this is a (receiver, class) |
| 1451 | // pair we will pass to objc_msgSendSuper. |
| 1452 | llvm::Value *ObjCSuper = |
| 1453 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 1454 | llvm::Value *ReceiverAsObject = |
| 1455 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 1456 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 1457 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1458 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1459 | // If this is a class message the metaclass is passed as the target. |
| 1460 | llvm::Value *Target; |
| 1461 | if (IsClassMessage) { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1462 | if (isCategoryImpl) { |
| 1463 | // Message sent to 'super' in a class method defined in a category |
| 1464 | // implementation requires an odd treatment. |
| 1465 | // If we are in a class method, we must retrieve the |
| 1466 | // _metaclass_ for the current class, pointed at by |
| 1467 | // the class's "isa" pointer. The following assumes that |
| 1468 | // isa" is the first ivar in a class (which it must be). |
| 1469 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1470 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 1471 | Target = CGF.Builder.CreateLoad(Target); |
| 1472 | } |
| 1473 | else { |
| 1474 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 1475 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 1476 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 1477 | Target = Super; |
| 1478 | } |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1479 | } else { |
| 1480 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1481 | } |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1482 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 1483 | // ObjCTypes types. |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1484 | const llvm::Type *ClassTy = |
| 1485 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1486 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1487 | CGF.Builder.CreateStore(Target, |
| 1488 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1489 | return EmitLegacyMessageSend(CGF, ResultType, |
| 1490 | EmitSelector(CGF.Builder, Sel), |
| 1491 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 1492 | true, CallArgs, ObjCTypes); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1493 | } |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1494 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1495 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1496 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1497 | QualType ResultType, |
| 1498 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1499 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1500 | bool IsClassMessage, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1501 | const CallArgList &CallArgs, |
| 1502 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1503 | return EmitLegacyMessageSend(CGF, ResultType, |
| 1504 | EmitSelector(CGF.Builder, Sel), |
| 1505 | Receiver, CGF.getContext().getObjCIdType(), |
| 1506 | false, CallArgs, ObjCTypes); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1509 | CodeGen::RValue CGObjCCommonMac::EmitLegacyMessageSend( |
| 1510 | CodeGen::CodeGenFunction &CGF, |
| 1511 | QualType ResultType, |
| 1512 | llvm::Value *Sel, |
| 1513 | llvm::Value *Arg0, |
| 1514 | QualType Arg0Ty, |
| 1515 | bool IsSuper, |
| 1516 | const CallArgList &CallArgs, |
| 1517 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1518 | CallArgList ActualArgs; |
Fariborz Jahanian | d019d96 | 2009-04-24 21:07:43 +0000 | [diff] [blame] | 1519 | if (!IsSuper) |
| 1520 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1521 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1522 | ActualArgs.push_back(std::make_pair(RValue::get(Sel), |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1523 | CGF.getContext().getObjCSelType())); |
| 1524 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1525 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1526 | CodeGenTypes &Types = CGM.getTypes(); |
| 1527 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1528 | // In 64bit ABI, type must be assumed VARARG. In 32bit abi, |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1529 | // it seems not to matter. |
| 1530 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, (ObjCABI == 2)); |
| 1531 | |
| 1532 | llvm::Constant *Fn = NULL; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1533 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1534 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper) |
| 1535 | : ObjCTypes.getSendStretFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1536 | } else if (ResultType->isFloatingType()) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1537 | if (ObjCABI == 2) { |
| 1538 | if (const BuiltinType *BT = ResultType->getAsBuiltinType()) { |
| 1539 | BuiltinType::Kind k = BT->getKind(); |
| 1540 | Fn = (k == BuiltinType::LongDouble) ? ObjCTypes.getSendFpretFn2(IsSuper) |
| 1541 | : ObjCTypes.getSendFn2(IsSuper); |
Daniel Dunbar | 42f963d | 2009-06-10 04:38:50 +0000 | [diff] [blame] | 1542 | } else { |
| 1543 | Fn = ObjCTypes.getSendFn2(IsSuper); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1544 | } |
| 1545 | } |
| 1546 | else |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1547 | // FIXME. This currently matches gcc's API for x86-32. May need to change |
| 1548 | // for others if we have their API. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1549 | Fn = ObjCTypes.getSendFpretFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1550 | } else { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1551 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper) |
| 1552 | : ObjCTypes.getSendFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1553 | } |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1554 | assert(Fn && "EmitLegacyMessageSend - unknown API"); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1555 | Fn = VMContext.getConstantExprBitCast(Fn, |
| 1556 | VMContext.getPointerTypeUnqual(FTy)); |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1557 | return CGF.EmitCall(FnInfo, Fn, ActualArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1560 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1561 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1562 | // 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] | 1563 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1564 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1565 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1566 | return VMContext.getConstantExprBitCast(GetProtocolRef(PD), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1567 | ObjCTypes.ExternalProtocolPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1570 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1571 | // FIXME: We shouldn't need this, the protocol decl should contain enough |
| 1572 | // information to tell us whether this was a declaration or a definition. |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1573 | DefinedProtocols.insert(PD->getIdentifier()); |
| 1574 | |
| 1575 | // If we have generated a forward reference to this protocol, emit |
| 1576 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 1577 | // emitted. |
| 1578 | if (Protocols.count(PD->getIdentifier())) |
| 1579 | GetOrEmitProtocol(PD); |
| 1580 | } |
| 1581 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1582 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1583 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 1584 | return GetOrEmitProtocol(PD); |
| 1585 | return GetOrEmitProtocolRef(PD); |
| 1586 | } |
| 1587 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1588 | /* |
| 1589 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 1590 | struct _objc_protocol { |
| 1591 | struct _objc_protocol_extension *isa; |
| 1592 | char *protocol_name; |
| 1593 | struct _objc_protocol_list *protocol_list; |
| 1594 | struct _objc__method_prototype_list *instance_methods; |
| 1595 | struct _objc__method_prototype_list *class_methods |
| 1596 | }; |
| 1597 | |
| 1598 | See EmitProtocolExtension(). |
| 1599 | */ |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1600 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
| 1601 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1602 | |
| 1603 | // Early exit if a defining object has already been generated. |
| 1604 | if (Entry && Entry->hasInitializer()) |
| 1605 | return Entry; |
| 1606 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1607 | // 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] | 1608 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1609 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1610 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1611 | const char *ProtocolName = PD->getNameAsCString(); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1612 | |
| 1613 | // Construct method lists. |
| 1614 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1615 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1616 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1617 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1618 | ObjCMethodDecl *MD = *i; |
| 1619 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1620 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1621 | OptInstanceMethods.push_back(C); |
| 1622 | } else { |
| 1623 | InstanceMethods.push_back(C); |
| 1624 | } |
| 1625 | } |
| 1626 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1627 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1628 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1629 | ObjCMethodDecl *MD = *i; |
| 1630 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1631 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1632 | OptClassMethods.push_back(C); |
| 1633 | } else { |
| 1634 | ClassMethods.push_back(C); |
| 1635 | } |
| 1636 | } |
| 1637 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1638 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1639 | Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1640 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1641 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1642 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1643 | PD->protocol_begin(), |
| 1644 | PD->protocol_end()); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1645 | Values[3] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1646 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" |
| 1647 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1648 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1649 | InstanceMethods); |
| 1650 | Values[4] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1651 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" |
| 1652 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1653 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1654 | ClassMethods); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1655 | llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.ProtocolTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1656 | Values); |
| 1657 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1658 | if (Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1659 | // Already created, fix the linkage and update the initializer. |
| 1660 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1661 | Entry->setInitializer(Init); |
| 1662 | } else { |
| 1663 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1664 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1665 | llvm::GlobalValue::InternalLinkage, |
| 1666 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1667 | std::string("\01L_OBJC_PROTOCOL_")+ProtocolName); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1668 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1669 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1670 | // FIXME: Is this necessary? Why only for protocol? |
| 1671 | Entry->setAlignment(4); |
| 1672 | } |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 1673 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1674 | |
| 1675 | return Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1678 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1679 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1680 | |
| 1681 | if (!Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1682 | // We use the initializer as a marker of whether this is a forward |
| 1683 | // reference or not. At module finalization we add the empty |
| 1684 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1685 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1686 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1687 | llvm::GlobalValue::ExternalLinkage, |
| 1688 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1689 | "\01L_OBJC_PROTOCOL_" + PD->getNameAsString()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1690 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1691 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1692 | // FIXME: Is this necessary? Why only for protocol? |
| 1693 | Entry->setAlignment(4); |
| 1694 | } |
| 1695 | |
| 1696 | return Entry; |
| 1697 | } |
| 1698 | |
| 1699 | /* |
| 1700 | struct _objc_protocol_extension { |
| 1701 | uint32_t size; |
| 1702 | struct objc_method_description_list *optional_instance_methods; |
| 1703 | struct objc_method_description_list *optional_class_methods; |
| 1704 | struct objc_property_list *instance_properties; |
| 1705 | }; |
| 1706 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1707 | llvm::Constant * |
| 1708 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1709 | const ConstantVector &OptInstanceMethods, |
| 1710 | const ConstantVector &OptClassMethods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1711 | uint64_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1712 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1713 | std::vector<llvm::Constant*> Values(4); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 1714 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1715 | Values[1] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1716 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
| 1717 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1718 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1719 | OptInstanceMethods); |
| 1720 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1721 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" |
| 1722 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1723 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1724 | OptClassMethods); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1725 | Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + |
| 1726 | PD->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1727 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1728 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1729 | // Return null if no extension bits are used. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1730 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
| 1731 | Values[3]->isNullValue()) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 1732 | return VMContext.getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1733 | |
| 1734 | llvm::Constant *Init = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1735 | VMContext.getConstantStruct(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1736 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1737 | // No special section, but goes in llvm.used |
| 1738 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(), |
| 1739 | Init, |
| 1740 | 0, 0, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
| 1743 | /* |
| 1744 | struct objc_protocol_list { |
| 1745 | struct objc_protocol_list *next; |
| 1746 | long count; |
| 1747 | Protocol *list[]; |
| 1748 | }; |
| 1749 | */ |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1750 | llvm::Constant * |
| 1751 | CGObjCMac::EmitProtocolList(const std::string &Name, |
| 1752 | ObjCProtocolDecl::protocol_iterator begin, |
| 1753 | ObjCProtocolDecl::protocol_iterator end) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1754 | std::vector<llvm::Constant*> ProtocolRefs; |
| 1755 | |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1756 | for (; begin != end; ++begin) |
| 1757 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1758 | |
| 1759 | // Just return null for empty protocol lists |
| 1760 | if (ProtocolRefs.empty()) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 1761 | return VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1762 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1763 | // This list is null terminated. |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 1764 | ProtocolRefs.push_back(VMContext.getNullValue(ObjCTypes.ProtocolPtrTy)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1765 | |
| 1766 | std::vector<llvm::Constant*> Values(3); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1767 | // This field is only used by the runtime. |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 1768 | Values[0] = VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 1769 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1770 | ProtocolRefs.size() - 1); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1771 | Values[2] = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1772 | VMContext.getConstantArray(VMContext.getArrayType(ObjCTypes.ProtocolPtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1773 | ProtocolRefs.size()), |
| 1774 | ProtocolRefs); |
| 1775 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1776 | llvm::Constant *Init = VMContext.getConstantStruct(Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1777 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1778 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1779 | 4, false); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1780 | return VMContext.getConstantExprBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1781 | } |
| 1782 | |
| 1783 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1784 | struct _objc_property { |
| 1785 | const char * const name; |
| 1786 | const char * const attributes; |
| 1787 | }; |
| 1788 | |
| 1789 | struct _objc_property_list { |
| 1790 | uint32_t entsize; // sizeof (struct _objc_property) |
| 1791 | uint32_t prop_count; |
| 1792 | struct _objc_property[prop_count]; |
| 1793 | }; |
| 1794 | */ |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1795 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name, |
| 1796 | const Decl *Container, |
| 1797 | const ObjCContainerDecl *OCD, |
| 1798 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1799 | std::vector<llvm::Constant*> Properties, Prop(2); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1800 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(), |
| 1801 | E = OCD->prop_end(); I != E; ++I) { |
Steve Naroff | 93983f8 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 1802 | const ObjCPropertyDecl *PD = *I; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1803 | Prop[0] = GetPropertyName(PD->getIdentifier()); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1804 | Prop[1] = GetPropertyTypeString(PD, Container); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1805 | Properties.push_back(VMContext.getConstantStruct(ObjCTypes.PropertyTy, |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1806 | Prop)); |
| 1807 | } |
| 1808 | |
| 1809 | // Return null for empty list. |
| 1810 | if (Properties.empty()) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 1811 | return VMContext.getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1812 | |
| 1813 | unsigned PropertySize = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1814 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1815 | std::vector<llvm::Constant*> Values(3); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 1816 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 1817 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1818 | llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.PropertyTy, |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1819 | Properties.size()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1820 | Values[2] = VMContext.getConstantArray(AT, Properties); |
| 1821 | llvm::Constant *Init = VMContext.getConstantStruct(Values); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1822 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1823 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1824 | CreateMetadataVar(Name, Init, |
| 1825 | (ObjCABI == 2) ? "__DATA, __objc_const" : |
| 1826 | "__OBJC,__property,regular,no_dead_strip", |
| 1827 | (ObjCABI == 2) ? 8 : 4, |
| 1828 | true); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1829 | return VMContext.getConstantExprBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1830 | } |
| 1831 | |
| 1832 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1833 | struct objc_method_description_list { |
| 1834 | int count; |
| 1835 | struct objc_method_description list[]; |
| 1836 | }; |
| 1837 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1838 | llvm::Constant * |
| 1839 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 1840 | std::vector<llvm::Constant*> Desc(2); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1841 | Desc[0] = |
| 1842 | VMContext.getConstantExprBitCast(GetMethodVarName(MD->getSelector()), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1843 | ObjCTypes.SelectorPtrTy); |
| 1844 | Desc[1] = GetMethodVarType(MD); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1845 | return VMContext.getConstantStruct(ObjCTypes.MethodDescriptionTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1846 | Desc); |
| 1847 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1848 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1849 | llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name, |
| 1850 | const char *Section, |
| 1851 | const ConstantVector &Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1852 | // Return null for empty list. |
| 1853 | if (Methods.empty()) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 1854 | return VMContext.getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1855 | |
| 1856 | std::vector<llvm::Constant*> Values(2); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 1857 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1858 | llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.MethodDescriptionTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1859 | Methods.size()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1860 | Values[1] = VMContext.getConstantArray(AT, Methods); |
| 1861 | llvm::Constant *Init = VMContext.getConstantStruct(Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1862 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1863 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1864 | return VMContext.getConstantExprBitCast(GV, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1865 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1866 | } |
| 1867 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1868 | /* |
| 1869 | struct _objc_category { |
| 1870 | char *category_name; |
| 1871 | char *class_name; |
| 1872 | struct _objc_method_list *instance_methods; |
| 1873 | struct _objc_method_list *class_methods; |
| 1874 | struct _objc_protocol_list *protocols; |
| 1875 | uint32_t size; // <rdar://4585769> |
| 1876 | struct _objc_property_list *instance_properties; |
| 1877 | }; |
| 1878 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1879 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1880 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1881 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1882 | // FIXME: This is poor design, the OCD should have a pointer to the category |
| 1883 | // decl. Additionally, note that Category can be null for the @implementation |
| 1884 | // w/o an @interface case. Sema should just create one for us as it does for |
| 1885 | // @implementation so everyone else can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1886 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1887 | const ObjCCategoryDecl *Category = |
| 1888 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1889 | std::string ExtName(Interface->getNameAsString() + "_" + |
| 1890 | OCD->getNameAsString()); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1891 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1892 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1893 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1894 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1895 | // Instance methods should always be defined. |
| 1896 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1897 | } |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1898 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1899 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1900 | // Class methods should always be defined. |
| 1901 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1902 | } |
| 1903 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1904 | std::vector<llvm::Constant*> Values(7); |
| 1905 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 1906 | Values[1] = GetClassName(Interface->getIdentifier()); |
Fariborz Jahanian | 679cd7f | 2009-04-29 20:40:05 +0000 | [diff] [blame] | 1907 | LazySymbols.insert(Interface->getIdentifier()); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1908 | Values[2] = |
| 1909 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") + |
| 1910 | ExtName, |
| 1911 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1912 | InstanceMethods); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1913 | Values[3] = |
| 1914 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName, |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1915 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1916 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1917 | if (Category) { |
| 1918 | Values[4] = |
| 1919 | EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName, |
| 1920 | Category->protocol_begin(), |
| 1921 | Category->protocol_end()); |
| 1922 | } else { |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 1923 | Values[4] = VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1924 | } |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 1925 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1926 | |
| 1927 | // If there is no category @interface then there can be no properties. |
| 1928 | if (Category) { |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1929 | Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1930 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1931 | } else { |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 1932 | Values[6] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1933 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1934 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1935 | llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.CategoryTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1936 | Values); |
| 1937 | |
| 1938 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1939 | CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init, |
| 1940 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1941 | 4, true); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1942 | DefinedCategories.push_back(GV); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1945 | // FIXME: Get from somewhere? |
| 1946 | enum ClassFlags { |
| 1947 | eClassFlags_Factory = 0x00001, |
| 1948 | eClassFlags_Meta = 0x00002, |
| 1949 | // <rdr://5142207> |
| 1950 | eClassFlags_HasCXXStructors = 0x02000, |
| 1951 | eClassFlags_Hidden = 0x20000, |
| 1952 | eClassFlags_ABI2_Hidden = 0x00010, |
| 1953 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 1954 | }; |
| 1955 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1956 | /* |
| 1957 | struct _objc_class { |
| 1958 | Class isa; |
| 1959 | Class super_class; |
| 1960 | const char *name; |
| 1961 | long version; |
| 1962 | long info; |
| 1963 | long instance_size; |
| 1964 | struct _objc_ivar_list *ivars; |
| 1965 | struct _objc_method_list *methods; |
| 1966 | struct _objc_cache *cache; |
| 1967 | struct _objc_protocol_list *protocols; |
| 1968 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 1969 | const char *ivar_layout; |
| 1970 | struct _objc_class_ext *ext; |
| 1971 | }; |
| 1972 | |
| 1973 | See EmitClassExtension(); |
| 1974 | */ |
| 1975 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1976 | DefinedSymbols.insert(ID->getIdentifier()); |
| 1977 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1978 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1979 | // FIXME: Gross |
| 1980 | ObjCInterfaceDecl *Interface = |
| 1981 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1982 | llvm::Constant *Protocols = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1983 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1984 | Interface->protocol_begin(), |
| 1985 | Interface->protocol_end()); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1986 | unsigned Flags = eClassFlags_Factory; |
Daniel Dunbar | 2bebbf0 | 2009-05-03 10:46:44 +0000 | [diff] [blame] | 1987 | unsigned Size = |
| 1988 | CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1989 | |
| 1990 | // FIXME: Set CXX-structors flag. |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1991 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1992 | Flags |= eClassFlags_Hidden; |
| 1993 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1994 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1995 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1996 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1997 | // Instance methods should always be defined. |
| 1998 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1999 | } |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2000 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2001 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2002 | // Class methods should always be defined. |
| 2003 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 2004 | } |
| 2005 | |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2006 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2007 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2008 | ObjCPropertyImplDecl *PID = *i; |
| 2009 | |
| 2010 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 2011 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 2012 | |
| 2013 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 2014 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 2015 | InstanceMethods.push_back(C); |
| 2016 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 2017 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 2018 | InstanceMethods.push_back(C); |
| 2019 | } |
| 2020 | } |
| 2021 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2022 | std::vector<llvm::Constant*> Values(12); |
Daniel Dunbar | 5384b09 | 2009-05-03 08:56:52 +0000 | [diff] [blame] | 2023 | Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2024 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2025 | // Record a reference to the super class. |
| 2026 | LazySymbols.insert(Super->getIdentifier()); |
| 2027 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2028 | Values[ 1] = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2029 | VMContext.getConstantExprBitCast(GetClassName(Super->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2030 | ObjCTypes.ClassPtrTy); |
| 2031 | } else { |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 2032 | Values[ 1] = VMContext.getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2033 | } |
| 2034 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 2035 | // Version is always 0. |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 2036 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2037 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 2038 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2039 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2040 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2041 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2042 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2043 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2044 | // cache is always NULL. |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 2045 | Values[ 8] = VMContext.getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2046 | Values[ 9] = Protocols; |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 2047 | Values[10] = BuildIvarLayout(ID, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2048 | Values[11] = EmitClassExtension(ID); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2049 | llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.ClassTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2050 | Values); |
| 2051 | |
| 2052 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2053 | CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init, |
| 2054 | "__OBJC,__class,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2055 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2056 | DefinedClasses.push_back(GV); |
| 2057 | } |
| 2058 | |
| 2059 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 2060 | llvm::Constant *Protocols, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2061 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2062 | unsigned Flags = eClassFlags_Meta; |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2063 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2064 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 2065 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2066 | Flags |= eClassFlags_Hidden; |
| 2067 | |
| 2068 | std::vector<llvm::Constant*> Values(12); |
| 2069 | // The isa for the metaclass is the root of the hierarchy. |
| 2070 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 2071 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 2072 | Root = Super; |
| 2073 | Values[ 0] = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2074 | VMContext.getConstantExprBitCast(GetClassName(Root->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2075 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2076 | // The super class for the metaclass is emitted as the name of the |
| 2077 | // super class. The runtime fixes this up to point to the |
| 2078 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2079 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
| 2080 | Values[ 1] = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2081 | VMContext.getConstantExprBitCast(GetClassName(Super->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2082 | ObjCTypes.ClassPtrTy); |
| 2083 | } else { |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 2084 | Values[ 1] = VMContext.getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2085 | } |
| 2086 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 2087 | // Version is always 0. |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 2088 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2089 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 2090 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2091 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2092 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2093 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2094 | "__OBJC,__cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2095 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2096 | // cache is always NULL. |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 2097 | Values[ 8] = VMContext.getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2098 | Values[ 9] = Protocols; |
| 2099 | // ivar_layout for metaclass is always NULL. |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 2100 | Values[10] = VMContext.getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2101 | // The class extension is always unused for metaclasses. |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 2102 | Values[11] = VMContext.getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2103 | llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.ClassTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2104 | Values); |
| 2105 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2106 | std::string Name("\01L_OBJC_METACLASS_"); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2107 | Name += ID->getNameAsCString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2108 | |
| 2109 | // Check for a forward reference. |
| 2110 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 2111 | if (GV) { |
| 2112 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2113 | "Forward metaclass reference has incorrect type."); |
| 2114 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 2115 | GV->setInitializer(Init); |
| 2116 | } else { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2117 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2118 | llvm::GlobalValue::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2119 | Init, Name); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2120 | } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2121 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2122 | GV->setAlignment(4); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2123 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2124 | |
| 2125 | return GV; |
| 2126 | } |
| 2127 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2128 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2129 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2130 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2131 | // FIXME: Should we look these up somewhere other than the module. Its a bit |
| 2132 | // silly since we only generate these while processing an implementation, so |
| 2133 | // exactly one pointer would work if know when we entered/exitted an |
| 2134 | // implementation block. |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2135 | |
| 2136 | // Check for an existing forward reference. |
Fariborz Jahanian | b0d2794 | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 2137 | // Previously, metaclass with internal linkage may have been defined. |
| 2138 | // pass 'true' as 2nd argument so it is returned. |
| 2139 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) { |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2140 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2141 | "Forward metaclass reference has incorrect type."); |
| 2142 | return GV; |
| 2143 | } else { |
| 2144 | // Generate as an external reference to keep a consistent |
| 2145 | // module. This will be patched up when we emit the metaclass. |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2146 | return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2147 | llvm::GlobalValue::ExternalLinkage, |
| 2148 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2149 | Name); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2150 | } |
| 2151 | } |
| 2152 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2153 | /* |
| 2154 | struct objc_class_ext { |
| 2155 | uint32_t size; |
| 2156 | const char *weak_ivar_layout; |
| 2157 | struct _objc_property_list *properties; |
| 2158 | }; |
| 2159 | */ |
| 2160 | llvm::Constant * |
| 2161 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
| 2162 | uint64_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2163 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2164 | |
| 2165 | std::vector<llvm::Constant*> Values(3); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 2166 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 2167 | Values[1] = BuildIvarLayout(ID, false); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2168 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2169 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2170 | |
| 2171 | // Return null if no extension bits are used. |
| 2172 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 2173 | return VMContext.getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2174 | |
| 2175 | llvm::Constant *Init = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2176 | VMContext.getConstantStruct(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2177 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2178 | Init, "__OBJC,__class_ext,regular,no_dead_strip", |
| 2179 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2180 | } |
| 2181 | |
| 2182 | /* |
| 2183 | struct objc_ivar { |
| 2184 | char *ivar_name; |
| 2185 | char *ivar_type; |
| 2186 | int ivar_offset; |
| 2187 | }; |
| 2188 | |
| 2189 | struct objc_ivar_list { |
| 2190 | int ivar_count; |
| 2191 | struct objc_ivar list[count]; |
| 2192 | }; |
| 2193 | */ |
| 2194 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2195 | bool ForClass) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2196 | std::vector<llvm::Constant*> Ivars, Ivar(3); |
| 2197 | |
| 2198 | // When emitting the root class GCC emits ivar entries for the |
| 2199 | // actual class structure. It is not clear if we need to follow this |
| 2200 | // behavior; for now lets try and get away with not doing it. If so, |
| 2201 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 2202 | // for the class. |
| 2203 | if (ForClass) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 2204 | return VMContext.getNullValue(ObjCTypes.IvarListPtrTy); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2205 | |
| 2206 | ObjCInterfaceDecl *OID = |
| 2207 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2208 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 2209 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 2210 | CGM.getContext().ShallowCollectObjCIvars(OID, OIvars); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 2211 | |
| 2212 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 2213 | ObjCIvarDecl *IVD = OIvars[i]; |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 2214 | // Ignore unnamed bit-fields. |
| 2215 | if (!IVD->getDeclName()) |
| 2216 | continue; |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 2217 | Ivar[0] = GetMethodVarName(IVD->getIdentifier()); |
| 2218 | Ivar[1] = GetMethodVarType(IVD); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 2219 | Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2220 | ComputeIvarBaseOffset(CGM, OID, IVD)); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2221 | Ivars.push_back(VMContext.getConstantStruct(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2222 | } |
| 2223 | |
| 2224 | // Return null for empty list. |
| 2225 | if (Ivars.empty()) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 2226 | return VMContext.getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2227 | |
| 2228 | std::vector<llvm::Constant*> Values(2); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 2229 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2230 | llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.IvarTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2231 | Ivars.size()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2232 | Values[1] = VMContext.getConstantArray(AT, Ivars); |
| 2233 | llvm::Constant *Init = VMContext.getConstantStruct(Values); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2234 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2235 | llvm::GlobalVariable *GV; |
| 2236 | if (ForClass) |
| 2237 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(), |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2238 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
| 2239 | 4, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2240 | else |
| 2241 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" |
| 2242 | + ID->getNameAsString(), |
| 2243 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2244 | 4, true); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2245 | return VMContext.getConstantExprBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | /* |
| 2249 | struct objc_method { |
| 2250 | SEL method_name; |
| 2251 | char *method_types; |
| 2252 | void *method; |
| 2253 | }; |
| 2254 | |
| 2255 | struct objc_method_list { |
| 2256 | struct objc_method_list *obsolete; |
| 2257 | int count; |
| 2258 | struct objc_method methods_list[count]; |
| 2259 | }; |
| 2260 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2261 | |
| 2262 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 2263 | /// given method if it has been defined. The result is null if the |
| 2264 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2265 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2266 | // FIXME: Use DenseMap::lookup |
| 2267 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 2268 | if (!Fn) |
| 2269 | return 0; |
| 2270 | |
| 2271 | std::vector<llvm::Constant*> Method(3); |
| 2272 | Method[0] = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2273 | VMContext.getConstantExprBitCast(GetMethodVarName(MD->getSelector()), |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2274 | ObjCTypes.SelectorPtrTy); |
| 2275 | Method[1] = GetMethodVarType(MD); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2276 | Method[2] = VMContext.getConstantExprBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 2277 | return VMContext.getConstantStruct(ObjCTypes.MethodTy, Method); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2278 | } |
| 2279 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2280 | llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name, |
| 2281 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2282 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2283 | // Return null for empty list. |
| 2284 | if (Methods.empty()) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 2285 | return VMContext.getNullValue(ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2286 | |
| 2287 | std::vector<llvm::Constant*> Values(3); |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 2288 | Values[0] = VMContext.getNullValue(ObjCTypes.Int8PtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 2289 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2290 | llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.MethodTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2291 | Methods.size()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2292 | Values[2] = VMContext.getConstantArray(AT, Methods); |
| 2293 | llvm::Constant *Init = VMContext.getConstantStruct(Values); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2294 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2295 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2296 | return VMContext.getConstantExprBitCast(GV, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2297 | ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2298 | } |
| 2299 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 2300 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 2301 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2302 | std::string Name; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 2303 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2304 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2305 | CodeGenTypes &Types = CGM.getTypes(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2306 | const llvm::FunctionType *MethodTy = |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2307 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2308 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2309 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2310 | llvm::GlobalValue::InternalLinkage, |
| 2311 | Name, |
| 2312 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2313 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2314 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2315 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2318 | llvm::GlobalVariable * |
| 2319 | CGObjCCommonMac::CreateMetadataVar(const std::string &Name, |
| 2320 | llvm::Constant *Init, |
| 2321 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2322 | unsigned Align, |
| 2323 | bool AddToUsed) { |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2324 | const llvm::Type *Ty = Init->getType(); |
| 2325 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2326 | new llvm::GlobalVariable(CGM.getModule(), Ty, false, |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2327 | llvm::GlobalValue::InternalLinkage, Init, Name); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2328 | if (Section) |
| 2329 | GV->setSection(Section); |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2330 | if (Align) |
| 2331 | GV->setAlignment(Align); |
| 2332 | if (AddToUsed) |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2333 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2334 | return GV; |
| 2335 | } |
| 2336 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2337 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2338 | // Abuse this interface function as a place to finalize. |
| 2339 | FinishModule(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2340 | return NULL; |
| 2341 | } |
| 2342 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2343 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2344 | return ObjCTypes.getGetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2345 | } |
| 2346 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2347 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2348 | return ObjCTypes.getSetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2349 | } |
| 2350 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2351 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2352 | return ObjCTypes.getEnumerationMutationFn(); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2353 | } |
| 2354 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2355 | /* |
| 2356 | |
| 2357 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 2358 | -- |
| 2359 | |
| 2360 | The basic framework for a @try-catch-finally is as follows: |
| 2361 | { |
| 2362 | objc_exception_data d; |
| 2363 | id _rethrow = null; |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2364 | bool _call_try_exit = true; |
| 2365 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2366 | objc_exception_try_enter(&d); |
| 2367 | if (!setjmp(d.jmp_buf)) { |
| 2368 | ... try body ... |
| 2369 | } else { |
| 2370 | // exception path |
| 2371 | id _caught = objc_exception_extract(&d); |
| 2372 | |
| 2373 | // enter new try scope for handlers |
| 2374 | if (!setjmp(d.jmp_buf)) { |
| 2375 | ... match exception and execute catch blocks ... |
| 2376 | |
| 2377 | // fell off end, rethrow. |
| 2378 | _rethrow = _caught; |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2379 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2380 | } else { |
| 2381 | // exception in catch block |
| 2382 | _rethrow = objc_exception_extract(&d); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2383 | _call_try_exit = false; |
| 2384 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2385 | } |
| 2386 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2387 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2388 | |
| 2389 | finally: |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2390 | if (_call_try_exit) |
| 2391 | objc_exception_try_exit(&d); |
| 2392 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2393 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2394 | ... dispatch to finally destination ... |
| 2395 | |
| 2396 | finally_rethrow: |
| 2397 | objc_exception_throw(_rethrow); |
| 2398 | |
| 2399 | finally_end: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | This framework differs slightly from the one gcc uses, in that gcc |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2403 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 2404 | and if the object should be rethrown. This breaks in the face of |
| 2405 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2406 | |
| 2407 | We specialize this framework for a few particular circumstances: |
| 2408 | |
| 2409 | - If there are no catch blocks, then we avoid emitting the second |
| 2410 | exception handling context. |
| 2411 | |
| 2412 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 2413 | e)) we avoid emitting the code to rethrow an uncaught exception. |
| 2414 | |
| 2415 | - FIXME: If there is no @finally block we can do a few more |
| 2416 | simplifications. |
| 2417 | |
| 2418 | Rethrows and Jumps-Through-Finally |
| 2419 | -- |
| 2420 | |
| 2421 | Support for implicit rethrows and jumping through the finally block is |
| 2422 | handled by storing the current exception-handling context in |
| 2423 | ObjCEHStack. |
| 2424 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2425 | In order to implement proper @finally semantics, we support one basic |
| 2426 | mechanism for jumping through the finally block to an arbitrary |
| 2427 | destination. Constructs which generate exits from a @try or @catch |
| 2428 | block use this mechanism to implement the proper semantics by chaining |
| 2429 | jumps, as necessary. |
| 2430 | |
| 2431 | This mechanism works like the one used for indirect goto: we |
| 2432 | arbitrarily assign an ID to each destination and store the ID for the |
| 2433 | destination in a variable prior to entering the finally block. At the |
| 2434 | end of the finally block we simply create a switch to the proper |
| 2435 | destination. |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2436 | |
| 2437 | Code gen for @synchronized(expr) stmt; |
| 2438 | Effectively generating code for: |
| 2439 | objc_sync_enter(expr); |
| 2440 | @try stmt @finally { objc_sync_exit(expr); } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2441 | */ |
| 2442 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2443 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 2444 | const Stmt &S) { |
| 2445 | bool isTry = isa<ObjCAtTryStmt>(S); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2446 | // Create various blocks we refer to for handling @finally. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2447 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2448 | llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit"); |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2449 | llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit"); |
| 2450 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
| 2451 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2452 | |
| 2453 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 2454 | // evaluation of the expression must occur before we enter the |
| 2455 | // @synchronized. We can safely avoid a temp here because jumps into |
| 2456 | // @synchronized are illegal & this will dominate uses. |
| 2457 | llvm::Value *SyncArg = 0; |
| 2458 | if (!isTry) { |
| 2459 | SyncArg = |
| 2460 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 2461 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 2462 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2463 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2464 | |
| 2465 | // Push an EH context entry, used for handling rethrows and jumps |
| 2466 | // through finally. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2467 | CGF.PushCleanupBlock(FinallyBlock); |
| 2468 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2469 | CGF.ObjCEHValueStack.push_back(0); |
| 2470 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2471 | // Allocate memory for the exception data and rethrow pointer. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2472 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 2473 | "exceptiondata.ptr"); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2474 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy, |
| 2475 | "_rethrow"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2476 | llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty, |
| 2477 | "_call_try_exit"); |
Owen Anderson | 4cd1608 | 2009-07-21 18:06:41 +0000 | [diff] [blame] | 2478 | CGF.Builder.CreateStore(VMContext.getTrue(), CallTryExitPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2479 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2480 | // Enter a new try block and call setjmp. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2481 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2482 | llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0, |
| 2483 | "jmpbufarray"); |
| 2484 | JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2485 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2486 | JmpBufPtr, "result"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2487 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2488 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 2489 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2490 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2491 | TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2492 | |
| 2493 | // Emit the @try block. |
| 2494 | CGF.EmitBlock(TryBlock); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2495 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 2496 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2497 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2498 | |
| 2499 | // Emit the "exception in @try" block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2500 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2501 | |
| 2502 | // Retrieve the exception object. We may emit multiple blocks but |
| 2503 | // nothing can cross this so the value is already in SSA form. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2504 | llvm::Value *Caught = |
| 2505 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2506 | ExceptionData, "caught"); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2507 | CGF.ObjCEHValueStack.back() = Caught; |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2508 | if (!isTry) |
| 2509 | { |
| 2510 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Owen Anderson | 4cd1608 | 2009-07-21 18:06:41 +0000 | [diff] [blame] | 2511 | CGF.Builder.CreateStore(VMContext.getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2512 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2513 | } |
| 2514 | else if (const ObjCAtCatchStmt* CatchStmt = |
| 2515 | cast<ObjCAtTryStmt>(S).getCatchStmts()) |
| 2516 | { |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2517 | // Enter a new exception try block (in case a @catch block throws |
| 2518 | // an exception). |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2519 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2520 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2521 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2522 | JmpBufPtr, "result"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2523 | llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2524 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2525 | llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch"); |
| 2526 | llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler"); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2527 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2528 | |
| 2529 | CGF.EmitBlock(CatchBlock); |
| 2530 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2531 | // Handle catch list. As a special case we check if everything is |
| 2532 | // matched and avoid generating code for falling off the end if |
| 2533 | // so. |
| 2534 | bool AllMatched = false; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2535 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2536 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2537 | |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2538 | const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2539 | const ObjCObjectPointerType *OPT = 0; |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2540 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2541 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2542 | if (!CatchParam) { |
| 2543 | AllMatched = true; |
| 2544 | } else { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2545 | OPT = CatchParam->getType()->getAsObjCObjectPointerType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2546 | |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 2547 | // catch(id e) always matches. |
| 2548 | // FIXME: For the time being we also match id<X>; this should |
| 2549 | // be rejected by Sema instead. |
Eli Friedman | 818e96f | 2009-07-11 00:57:02 +0000 | [diff] [blame] | 2550 | if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType())) |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2551 | AllMatched = true; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2552 | } |
| 2553 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2554 | if (AllMatched) { |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2555 | if (CatchParam) { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2556 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2557 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2558 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2559 | } |
Anders Carlsson | 1452f55 | 2008-09-11 08:21:54 +0000 | [diff] [blame] | 2560 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2561 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2562 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2563 | break; |
| 2564 | } |
| 2565 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2566 | assert(OPT && "Unexpected non-object pointer type in @catch"); |
| 2567 | QualType T = OPT->getPointeeType(); |
Anders Carlsson | 4b7ff6e | 2008-09-11 06:35:14 +0000 | [diff] [blame] | 2568 | const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2569 | assert(ObjCType && "Catch parameter must have Objective-C type!"); |
| 2570 | |
| 2571 | // Check if the @catch block matches the exception object. |
| 2572 | llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl()); |
| 2573 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2574 | llvm::Value *Match = |
| 2575 | CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(), |
| 2576 | Class, Caught, "match"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2577 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2578 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2579 | |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2580 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2581 | MatchedBlock, NextCatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2582 | |
| 2583 | // Emit the @catch block. |
| 2584 | CGF.EmitBlock(MatchedBlock); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2585 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2586 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2587 | |
| 2588 | llvm::Value *Tmp = |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2589 | CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()), |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2590 | "tmp"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2591 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2592 | |
| 2593 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2594 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2595 | |
| 2596 | CGF.EmitBlock(NextCatchBlock); |
| 2597 | } |
| 2598 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2599 | if (!AllMatched) { |
| 2600 | // None of the handlers caught the exception, so store it to be |
| 2601 | // rethrown at the end of the @finally block. |
| 2602 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2603 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2604 | } |
| 2605 | |
| 2606 | // Emit the exception handler for the @catch blocks. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2607 | CGF.EmitBlock(CatchHandler); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2608 | CGF.Builder.CreateStore( |
| 2609 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2610 | ExceptionData), |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2611 | RethrowPtr); |
Owen Anderson | 4cd1608 | 2009-07-21 18:06:41 +0000 | [diff] [blame] | 2612 | CGF.Builder.CreateStore(VMContext.getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2613 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2614 | } else { |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2615 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Owen Anderson | 4cd1608 | 2009-07-21 18:06:41 +0000 | [diff] [blame] | 2616 | CGF.Builder.CreateStore(VMContext.getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2617 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2618 | } |
| 2619 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2620 | // Pop the exception-handling stack entry. It is important to do |
| 2621 | // this now, because the code in the @finally block is not in this |
| 2622 | // context. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2623 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 2624 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2625 | CGF.ObjCEHValueStack.pop_back(); |
| 2626 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2627 | // Emit the @finally block. |
| 2628 | CGF.EmitBlock(FinallyBlock); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2629 | llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp"); |
| 2630 | |
| 2631 | CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit); |
| 2632 | |
| 2633 | CGF.EmitBlock(FinallyExit); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2634 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2635 | |
| 2636 | CGF.EmitBlock(FinallyNoExit); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2637 | if (isTry) { |
| 2638 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 2639 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 2640 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2641 | } else { |
| 2642 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 2643 | // @synchronized. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2644 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg); |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 2645 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2646 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2647 | // Emit the switch block |
| 2648 | if (Info.SwitchBlock) |
| 2649 | CGF.EmitBlock(Info.SwitchBlock); |
| 2650 | if (Info.EndBlock) |
| 2651 | CGF.EmitBlock(Info.EndBlock); |
| 2652 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2653 | CGF.EmitBlock(FinallyRethrow); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2654 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2655 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2656 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2657 | |
| 2658 | CGF.EmitBlock(FinallyEnd); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2659 | } |
| 2660 | |
| 2661 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2662 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2663 | llvm::Value *ExceptionAsObject; |
| 2664 | |
| 2665 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 2666 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
| 2667 | ExceptionAsObject = |
| 2668 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 2669 | } else { |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2670 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2671 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2672 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2673 | } |
| 2674 | |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2675 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2676 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2677 | |
| 2678 | // Clear the insertion point to indicate we are in unreachable code. |
| 2679 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2680 | } |
| 2681 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2682 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2683 | /// object: objc_read_weak (id *src) |
| 2684 | /// |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2685 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2686 | llvm::Value *AddrWeakObj) |
| 2687 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2688 | const llvm::Type* DestTy = |
| 2689 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2690 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2691 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2692 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2693 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2694 | return read_weak; |
| 2695 | } |
| 2696 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2697 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 2698 | /// objc_assign_weak (id src, id *dst) |
| 2699 | /// |
| 2700 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 2701 | llvm::Value *src, llvm::Value *dst) |
| 2702 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2703 | const llvm::Type * SrcTy = src->getType(); |
| 2704 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2705 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2706 | assert(Size <= 8 && "does not support size > 8"); |
| 2707 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2708 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2709 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2710 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2711 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2712 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 2713 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2714 | src, dst, "weakassign"); |
| 2715 | return; |
| 2716 | } |
| 2717 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2718 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 2719 | /// objc_assign_global (id src, id *dst) |
| 2720 | /// |
| 2721 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 2722 | llvm::Value *src, llvm::Value *dst) |
| 2723 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2724 | const llvm::Type * SrcTy = src->getType(); |
| 2725 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2726 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2727 | assert(Size <= 8 && "does not support size > 8"); |
| 2728 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2729 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2730 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2731 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2732 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2733 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2734 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2735 | src, dst, "globalassign"); |
| 2736 | return; |
| 2737 | } |
| 2738 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2739 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 2740 | /// objc_assign_ivar (id src, id *dst) |
| 2741 | /// |
| 2742 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 2743 | llvm::Value *src, llvm::Value *dst) |
| 2744 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2745 | const llvm::Type * SrcTy = src->getType(); |
| 2746 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2747 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2748 | assert(Size <= 8 && "does not support size > 8"); |
| 2749 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2750 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2751 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2752 | } |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2753 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2754 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2755 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(), |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2756 | src, dst, "assignivar"); |
| 2757 | return; |
| 2758 | } |
| 2759 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2760 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 2761 | /// objc_assign_strongCast (id src, id *dst) |
| 2762 | /// |
| 2763 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 2764 | llvm::Value *src, llvm::Value *dst) |
| 2765 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2766 | const llvm::Type * SrcTy = src->getType(); |
| 2767 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2768 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2769 | assert(Size <= 8 && "does not support size > 8"); |
| 2770 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2771 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2772 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2773 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2774 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2775 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2776 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2777 | src, dst, "weakassign"); |
| 2778 | return; |
| 2779 | } |
| 2780 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2781 | void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 2782 | llvm::Value *DestPtr, |
| 2783 | llvm::Value *SrcPtr, |
| 2784 | unsigned long size) { |
| 2785 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 2786 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 2787 | llvm::Value *N = llvm::ConstantInt::get(ObjCTypes.LongTy, size); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2788 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), |
| 2789 | DestPtr, SrcPtr, N); |
| 2790 | return; |
| 2791 | } |
| 2792 | |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2793 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 2794 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2795 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 2796 | QualType ObjectTy, |
| 2797 | llvm::Value *BaseValue, |
| 2798 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2799 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 2800 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2801 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 2802 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2803 | } |
| 2804 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2805 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 2806 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2807 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2808 | uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 2809 | return llvm::ConstantInt::get( |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2810 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 2811 | Offset); |
| 2812 | } |
| 2813 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2814 | /* *** Private Interface *** */ |
| 2815 | |
| 2816 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 2817 | /// level information. |
| 2818 | /// |
| 2819 | /// See: <rdr://4810609&4810587&4810587> |
| 2820 | /// struct IMAGE_INFO { |
| 2821 | /// unsigned version; |
| 2822 | /// unsigned flags; |
| 2823 | /// }; |
| 2824 | enum ImageInfoFlags { |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2825 | eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what |
| 2826 | // this implies. |
| 2827 | eImageInfo_GarbageCollected = (1 << 1), |
| 2828 | eImageInfo_GCOnly = (1 << 2), |
| 2829 | eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set. |
| 2830 | |
| 2831 | // A flag indicating that the module has no instances of an |
| 2832 | // @synthesize of a superclass variable. <rdar://problem/6803242> |
| 2833 | eImageInfo_CorrectedSynthesize = (1 << 4) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2834 | }; |
| 2835 | |
| 2836 | void CGObjCMac::EmitImageInfo() { |
| 2837 | unsigned version = 0; // Version is unused? |
| 2838 | unsigned flags = 0; |
| 2839 | |
| 2840 | // FIXME: Fix and continue? |
| 2841 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 2842 | flags |= eImageInfo_GarbageCollected; |
| 2843 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 2844 | flags |= eImageInfo_GCOnly; |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2845 | |
| 2846 | // We never allow @synthesize of a superclass property. |
| 2847 | flags |= eImageInfo_CorrectedSynthesize; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2848 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2849 | // Emitted as int[2]; |
| 2850 | llvm::Constant *values[2] = { |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 2851 | llvm::ConstantInt::get(llvm::Type::Int32Ty, version), |
| 2852 | llvm::ConstantInt::get(llvm::Type::Int32Ty, flags) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2853 | }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2854 | llvm::ArrayType *AT = VMContext.getArrayType(llvm::Type::Int32Ty, 2); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2855 | |
| 2856 | const char *Section; |
| 2857 | if (ObjCABI == 1) |
| 2858 | Section = "__OBJC, __image_info,regular"; |
| 2859 | else |
| 2860 | Section = "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2861 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2862 | CreateMetadataVar("\01L_OBJC_IMAGE_INFO", |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2863 | VMContext.getConstantArray(AT, values, 2), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2864 | Section, |
| 2865 | 0, |
| 2866 | true); |
| 2867 | GV->setConstant(true); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2868 | } |
| 2869 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2870 | |
| 2871 | // struct objc_module { |
| 2872 | // unsigned long version; |
| 2873 | // unsigned long size; |
| 2874 | // const char *name; |
| 2875 | // Symtab symtab; |
| 2876 | // }; |
| 2877 | |
| 2878 | // FIXME: Get from somewhere |
| 2879 | static const int ModuleVersion = 7; |
| 2880 | |
| 2881 | void CGObjCMac::EmitModuleInfo() { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2882 | uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2883 | |
| 2884 | std::vector<llvm::Constant*> Values(4); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 2885 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion); |
| 2886 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2887 | // This used to be the filename, now it is unused. <rdr://4327263> |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2888 | Values[2] = GetClassName(&CGM.getContext().Idents.get("")); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2889 | Values[3] = EmitModuleSymbols(); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2890 | CreateMetadataVar("\01L_OBJC_MODULES", |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2891 | VMContext.getConstantStruct(ObjCTypes.ModuleTy, Values), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2892 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2893 | 4, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2894 | } |
| 2895 | |
| 2896 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2897 | unsigned NumClasses = DefinedClasses.size(); |
| 2898 | unsigned NumCategories = DefinedCategories.size(); |
| 2899 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2900 | // Return null if no symbols were defined. |
| 2901 | if (!NumClasses && !NumCategories) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 2902 | return VMContext.getNullValue(ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2903 | |
| 2904 | std::vector<llvm::Constant*> Values(5); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 2905 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 2906 | Values[1] = VMContext.getNullValue(ObjCTypes.SelectorPtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 2907 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 2908 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2909 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2910 | // The runtime expects exactly the list of defined classes followed |
| 2911 | // by the list of defined categories, in a single array. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2912 | std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2913 | for (unsigned i=0; i<NumClasses; i++) |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2914 | Symbols[i] = VMContext.getConstantExprBitCast(DefinedClasses[i], |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2915 | ObjCTypes.Int8PtrTy); |
| 2916 | for (unsigned i=0; i<NumCategories; i++) |
| 2917 | Symbols[NumClasses + i] = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2918 | VMContext.getConstantExprBitCast(DefinedCategories[i], |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2919 | ObjCTypes.Int8PtrTy); |
| 2920 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2921 | Values[4] = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2922 | VMContext.getConstantArray(VMContext.getArrayType(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2923 | NumClasses + NumCategories), |
| 2924 | Symbols); |
| 2925 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2926 | llvm::Constant *Init = VMContext.getConstantStruct(Values); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2927 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2928 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2929 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 2930 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2931 | 4, true); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2932 | return VMContext.getConstantExprBitCast(GV, ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2933 | } |
| 2934 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2935 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2936 | const ObjCInterfaceDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2937 | LazySymbols.insert(ID->getIdentifier()); |
| 2938 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2939 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 2940 | |
| 2941 | if (!Entry) { |
| 2942 | llvm::Constant *Casted = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2943 | VMContext.getConstantExprBitCast(GetClassName(ID->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2944 | ObjCTypes.ClassPtrTy); |
| 2945 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2946 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 2947 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2948 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2949 | } |
| 2950 | |
| 2951 | return Builder.CreateLoad(Entry, false, "tmp"); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2952 | } |
| 2953 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2954 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2955 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 2956 | |
| 2957 | if (!Entry) { |
| 2958 | llvm::Constant *Casted = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2959 | VMContext.getConstantExprBitCast(GetMethodVarName(Sel), |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2960 | ObjCTypes.SelectorPtrTy); |
| 2961 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2962 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 2963 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2964 | 4, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2965 | } |
| 2966 | |
| 2967 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 2968 | } |
| 2969 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 2970 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2971 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2972 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2973 | if (!Entry) |
| 2974 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2975 | VMContext.getConstantArray(Ident->getName()), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2976 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 2977 | 1, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2978 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2979 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2980 | } |
| 2981 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 2982 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 2983 | /// ivar layout bitmap. |
| 2984 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
| 2985 | const ObjCCommonTypesHelper &ObjCTypes) { |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 2986 | return VMContext.getNullValue(ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 2989 | static QualType::GCAttrTypes GetGCAttrTypeForType(ASTContext &Ctx, |
| 2990 | QualType FQT) { |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 2991 | if (FQT.isObjCGCStrong()) |
| 2992 | return QualType::Strong; |
| 2993 | |
| 2994 | if (FQT.isObjCGCWeak()) |
| 2995 | return QualType::Weak; |
| 2996 | |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 2997 | if (FQT->isObjCObjectPointerType()) |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 2998 | return QualType::Strong; |
| 2999 | |
Ted Kremenek | 35366a6 | 2009-07-17 17:50:17 +0000 | [diff] [blame] | 3000 | if (const PointerType *PT = FQT->getAsPointerType()) |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3001 | return GetGCAttrTypeForType(Ctx, PT->getPointeeType()); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3002 | |
| 3003 | return QualType::GCNone; |
| 3004 | } |
| 3005 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3006 | void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT, |
| 3007 | unsigned int BytePos, |
| 3008 | bool ForStrongLayout, |
| 3009 | bool &HasUnion) { |
| 3010 | const RecordDecl *RD = RT->getDecl(); |
| 3011 | // FIXME - Use iterator. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3012 | llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end()); |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3013 | const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0)); |
| 3014 | const llvm::StructLayout *RecLayout = |
| 3015 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); |
| 3016 | |
| 3017 | BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos, |
| 3018 | ForStrongLayout, HasUnion); |
| 3019 | } |
| 3020 | |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 3021 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3022 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3023 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3024 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3025 | unsigned int BytePos, bool ForStrongLayout, |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3026 | bool &HasUnion) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3027 | bool IsUnion = (RD && RD->isUnion()); |
| 3028 | uint64_t MaxUnionIvarSize = 0; |
| 3029 | uint64_t MaxSkippedUnionIvarSize = 0; |
| 3030 | FieldDecl *MaxField = 0; |
| 3031 | FieldDecl *MaxSkippedField = 0; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3032 | FieldDecl *LastFieldBitfield = 0; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3033 | uint64_t MaxFieldOffset = 0; |
| 3034 | uint64_t MaxSkippedFieldOffset = 0; |
| 3035 | uint64_t LastBitfieldOffset = 0; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3036 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3037 | if (RecFields.empty()) |
| 3038 | return; |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3039 | unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0); |
| 3040 | unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth(); |
| 3041 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3042 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3043 | FieldDecl *Field = RecFields[i]; |
Daniel Dunbar | e05cc98 | 2009-05-03 23:35:23 +0000 | [diff] [blame] | 3044 | uint64_t FieldOffset; |
Anders Carlsson | fc514ab | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 3045 | if (RD) { |
| 3046 | if (Field->isBitField()) { |
| 3047 | CodeGenTypes::BitFieldInfo Info = CGM.getTypes().getBitFieldInfo(Field); |
| 3048 | FieldOffset = Layout->getElementOffset(Info.FieldNo); |
| 3049 | } else |
| 3050 | FieldOffset = |
| 3051 | Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); |
| 3052 | } else |
Daniel Dunbar | e05cc98 | 2009-05-03 23:35:23 +0000 | [diff] [blame] | 3053 | FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)); |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3054 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3055 | // Skip over unnamed or bitfields |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3056 | if (!Field->getIdentifier() || Field->isBitField()) { |
| 3057 | LastFieldBitfield = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3058 | LastBitfieldOffset = FieldOffset; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3059 | continue; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3060 | } |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3061 | |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3062 | LastFieldBitfield = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3063 | QualType FQT = Field->getType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3064 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3065 | if (FQT->isUnionType()) |
| 3066 | HasUnion = true; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3067 | |
Ted Kremenek | 35366a6 | 2009-07-17 17:50:17 +0000 | [diff] [blame] | 3068 | BuildAggrIvarRecordLayout(FQT->getAsRecordType(), |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3069 | BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3070 | ForStrongLayout, HasUnion); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3071 | continue; |
| 3072 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3073 | |
| 3074 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3075 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3076 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3077 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3078 | assert(CArray && "only array with known element size is supported"); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3079 | FQT = CArray->getElementType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3080 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 3081 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3082 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3083 | ElCount *= CArray->getSize().getZExtValue(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3084 | FQT = CArray->getElementType(); |
| 3085 | } |
| 3086 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3087 | assert(!FQT->isUnionType() && |
| 3088 | "layout for array of unions not supported"); |
| 3089 | if (FQT->isRecordType()) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3090 | int OldIndex = IvarsInfo.size() - 1; |
| 3091 | int OldSkIndex = SkipIvars.size() -1; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3092 | |
Ted Kremenek | 35366a6 | 2009-07-17 17:50:17 +0000 | [diff] [blame] | 3093 | const RecordType *RT = FQT->getAsRecordType(); |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3094 | BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3095 | ForStrongLayout, HasUnion); |
| 3096 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3097 | // Replicate layout information for each array element. Note that |
| 3098 | // one element is already done. |
| 3099 | uint64_t ElIx = 1; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3100 | for (int FirstIndex = IvarsInfo.size() - 1, |
| 3101 | FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3102 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3103 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 3104 | IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx, |
| 3105 | IvarsInfo[i].ivar_size)); |
| 3106 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) |
| 3107 | SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx, |
| 3108 | SkipIvars[i].ivar_size)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3109 | } |
| 3110 | continue; |
| 3111 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3112 | } |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3113 | // At this point, we are done with Record/Union and array there of. |
| 3114 | // For other arrays we are down to its element type. |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3115 | QualType::GCAttrTypes GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3116 | |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3117 | unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType()); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3118 | if ((ForStrongLayout && GCAttr == QualType::Strong) |
| 3119 | || (!ForStrongLayout && GCAttr == QualType::Weak)) { |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3120 | if (IsUnion) { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3121 | uint64_t UnionIvarSize = FieldSize / WordSizeInBits; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3122 | if (UnionIvarSize > MaxUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3123 | MaxUnionIvarSize = UnionIvarSize; |
| 3124 | MaxField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3125 | MaxFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3126 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3127 | } else { |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3128 | IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3129 | FieldSize / WordSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3130 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3131 | } else if ((ForStrongLayout && |
| 3132 | (GCAttr == QualType::GCNone || GCAttr == QualType::Weak)) |
| 3133 | || (!ForStrongLayout && GCAttr != QualType::Weak)) { |
| 3134 | if (IsUnion) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3135 | // FIXME: Why the asymmetry? We divide by word size in bits on other |
| 3136 | // side. |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3137 | uint64_t UnionIvarSize = FieldSize; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3138 | if (UnionIvarSize > MaxSkippedUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3139 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 3140 | MaxSkippedField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3141 | MaxSkippedFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3142 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3143 | } else { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3144 | // FIXME: Why the asymmetry, we divide by byte size in bits here? |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3145 | SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3146 | FieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3147 | } |
| 3148 | } |
| 3149 | } |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3150 | |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3151 | if (LastFieldBitfield) { |
| 3152 | // Last field was a bitfield. Must update skip info. |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3153 | Expr *BitWidth = LastFieldBitfield->getBitWidth(); |
| 3154 | uint64_t BitFieldSize = |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 3155 | BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue(); |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3156 | GC_IVAR skivar; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3157 | skivar.ivar_bytepos = BytePos + LastBitfieldOffset; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3158 | skivar.ivar_size = (BitFieldSize / ByteSizeInBits) |
| 3159 | + ((BitFieldSize % ByteSizeInBits) != 0); |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3160 | SkipIvars.push_back(skivar); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3161 | } |
| 3162 | |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3163 | if (MaxField) |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3164 | IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3165 | MaxUnionIvarSize)); |
| 3166 | if (MaxSkippedField) |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3167 | SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3168 | MaxSkippedUnionIvarSize)); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3169 | } |
| 3170 | |
| 3171 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 3172 | /// implementation for the __strong or __weak case. |
| 3173 | /// The layout map displays which words in ivar list must be skipped |
| 3174 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 3175 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
| 3176 | /// of words to skip and right nibble is count of words to scan. So, each |
| 3177 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
| 3178 | /// represented by a 0x00 byte which also ends the string. |
| 3179 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 3180 | /// - id, Class |
| 3181 | /// - object * |
| 3182 | /// - __strong anything |
| 3183 | /// |
| 3184 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 3185 | /// - __weak anything |
| 3186 | /// |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3187 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3188 | const ObjCImplementationDecl *OMD, |
| 3189 | bool ForStrongLayout) { |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3190 | bool hasUnion = false; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3191 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3192 | unsigned int WordsToScan, WordsToSkip; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3193 | const llvm::Type *PtrTy = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3194 | if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 3195 | return VMContext.getNullValue(PtrTy); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3196 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3197 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3198 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3199 | CGM.getContext().CollectObjCIvars(OI, RecFields); |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 3200 | |
Daniel Dunbar | 3715328 | 2009-05-04 04:10:48 +0000 | [diff] [blame] | 3201 | // Add this implementations synthesized ivars. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 3202 | llvm::SmallVector<ObjCIvarDecl*, 16> Ivars; |
| 3203 | CGM.getContext().CollectSynthesizedIvars(OI, Ivars); |
| 3204 | for (unsigned k = 0, e = Ivars.size(); k != e; ++k) |
| 3205 | RecFields.push_back(cast<FieldDecl>(Ivars[k])); |
| 3206 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3207 | if (RecFields.empty()) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 3208 | return VMContext.getNullValue(PtrTy); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3209 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3210 | SkipIvars.clear(); |
| 3211 | IvarsInfo.clear(); |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 3212 | |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 3213 | BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion); |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3214 | if (IvarsInfo.empty()) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 3215 | return VMContext.getNullValue(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3216 | |
| 3217 | // Sort on byte position in case we encounterred a union nested in |
| 3218 | // the ivar list. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3219 | if (hasUnion && !IvarsInfo.empty()) |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 3220 | std::sort(IvarsInfo.begin(), IvarsInfo.end()); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3221 | if (hasUnion && !SkipIvars.empty()) |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 3222 | std::sort(SkipIvars.begin(), SkipIvars.end()); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3223 | |
| 3224 | // Build the string of skip/scan nibbles |
Fariborz Jahanian | 8c2f2d1 | 2009-04-24 17:15:27 +0000 | [diff] [blame] | 3225 | llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3226 | unsigned int WordSize = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3227 | CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3228 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 3229 | WordsToSkip = 0; |
| 3230 | WordsToScan = IvarsInfo[0].ivar_size; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3231 | } else { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3232 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 3233 | WordsToScan = IvarsInfo[0].ivar_size; |
| 3234 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3235 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3236 | unsigned int TailPrevGCObjC = |
| 3237 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3238 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3239 | // consecutive 'scanned' object pointers. |
| 3240 | WordsToScan += IvarsInfo[i].ivar_size; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3241 | } else { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3242 | // Skip over 'gc'able object pointer which lay over each other. |
| 3243 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 3244 | continue; |
| 3245 | // Must skip over 1 or more words. We save current skip/scan values |
| 3246 | // and start a new pair. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3247 | SKIP_SCAN SkScan; |
| 3248 | SkScan.skip = WordsToSkip; |
| 3249 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3250 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3251 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3252 | // Skip the hole. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3253 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 3254 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3255 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3256 | WordsToSkip = 0; |
| 3257 | WordsToScan = IvarsInfo[i].ivar_size; |
| 3258 | } |
| 3259 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3260 | if (WordsToScan > 0) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3261 | SKIP_SCAN SkScan; |
| 3262 | SkScan.skip = WordsToSkip; |
| 3263 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3264 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3265 | } |
| 3266 | |
| 3267 | bool BytesSkipped = false; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3268 | if (!SkipIvars.empty()) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3269 | unsigned int LastIndex = SkipIvars.size()-1; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3270 | int LastByteSkipped = |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3271 | SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size; |
| 3272 | LastIndex = IvarsInfo.size()-1; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3273 | int LastByteScanned = |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3274 | IvarsInfo[LastIndex].ivar_bytepos + |
| 3275 | IvarsInfo[LastIndex].ivar_size * WordSize; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3276 | BytesSkipped = (LastByteSkipped > LastByteScanned); |
| 3277 | // Compute number of bytes to skip at the tail end of the last ivar scanned. |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3278 | if (BytesSkipped) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3279 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3280 | SKIP_SCAN SkScan; |
| 3281 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
| 3282 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3283 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3284 | } |
| 3285 | } |
| 3286 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 3287 | // as 0xMN. |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3288 | int SkipScan = SkipScanIvars.size()-1; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3289 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3290 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 3291 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 3292 | // 0xM0 followed by 0x0N detected. |
| 3293 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 3294 | for (int j = i+1; j < SkipScan; j++) |
| 3295 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 3296 | --SkipScan; |
| 3297 | } |
| 3298 | } |
| 3299 | |
| 3300 | // Generate the string. |
| 3301 | std::string BitMap; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3302 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3303 | unsigned char byte; |
| 3304 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 3305 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 3306 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 3307 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
| 3308 | |
| 3309 | if (skip_small > 0 || skip_big > 0) |
| 3310 | BytesSkipped = true; |
| 3311 | // first skip big. |
| 3312 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 3313 | BitMap += (unsigned char)(0xf0); |
| 3314 | |
| 3315 | // next (skip small, scan) |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3316 | if (skip_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3317 | byte = skip_small << 4; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3318 | if (scan_big > 0) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3319 | byte |= 0xf; |
| 3320 | --scan_big; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3321 | } else if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3322 | byte |= scan_small; |
| 3323 | scan_small = 0; |
| 3324 | } |
| 3325 | BitMap += byte; |
| 3326 | } |
| 3327 | // next scan big |
| 3328 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 3329 | BitMap += (unsigned char)(0x0f); |
| 3330 | // last scan small |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3331 | if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3332 | byte = scan_small; |
| 3333 | BitMap += byte; |
| 3334 | } |
| 3335 | } |
| 3336 | // null terminate string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3337 | unsigned char zero = 0; |
| 3338 | BitMap += zero; |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3339 | |
| 3340 | if (CGM.getLangOptions().ObjCGCBitmapPrint) { |
| 3341 | printf("\n%s ivar layout for class '%s': ", |
| 3342 | ForStrongLayout ? "strong" : "weak", |
| 3343 | OMD->getClassInterface()->getNameAsCString()); |
| 3344 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
| 3345 | for (unsigned i = 0; i < BitMap.size(); i++) |
| 3346 | if (!(s[i] & 0xf0)) |
| 3347 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3348 | else |
| 3349 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3350 | printf("\n"); |
| 3351 | } |
| 3352 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3353 | // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as |
| 3354 | // final layout. |
| 3355 | if (ForStrongLayout && !BytesSkipped) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 3356 | return VMContext.getNullValue(PtrTy); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3357 | llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3358 | VMContext.getConstantArray(BitMap.c_str()), |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3359 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3360 | 1, true); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3361 | return getConstantGEP(VMContext, Entry, 0, 0); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3362 | } |
| 3363 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3364 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3365 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 3366 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3367 | // FIXME: Avoid std::string copying. |
| 3368 | if (!Entry) |
| 3369 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3370 | VMContext.getConstantArray(Sel.getAsString()), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3371 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3372 | 1, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3373 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3374 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3375 | } |
| 3376 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3377 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3378 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3379 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 3380 | } |
| 3381 | |
| 3382 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3383 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3384 | return GetMethodVarName(&CGM.getContext().Idents.get(Name)); |
| 3385 | } |
| 3386 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 3387 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3388 | std::string TypeStr; |
| 3389 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 3390 | |
| 3391 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3392 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3393 | if (!Entry) |
| 3394 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3395 | VMContext.getConstantArray(TypeStr), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3396 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3397 | 1, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3398 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3399 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3400 | } |
| 3401 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3402 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3403 | std::string TypeStr; |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3404 | CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D), |
| 3405 | TypeStr); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3406 | |
| 3407 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 3408 | |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3409 | if (!Entry) |
| 3410 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3411 | VMContext.getConstantArray(TypeStr), |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3412 | "__TEXT,__cstring,cstring_literals", |
| 3413 | 1, true); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3414 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3415 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3416 | } |
| 3417 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3418 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3419 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3420 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
| 3421 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3422 | if (!Entry) |
| 3423 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3424 | VMContext.getConstantArray(Ident->getName()), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3425 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3426 | 1, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3427 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3428 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3429 | } |
| 3430 | |
| 3431 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3432 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3433 | llvm::Constant * |
| 3434 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 3435 | const Decl *Container) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3436 | std::string TypeStr; |
| 3437 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3438 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 3439 | } |
| 3440 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3441 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
| 3442 | const ObjCContainerDecl *CD, |
| 3443 | std::string &NameOut) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 3444 | NameOut = '\01'; |
| 3445 | NameOut += (D->isInstanceMethod() ? '-' : '+'); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3446 | NameOut += '['; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 3447 | assert (CD && "Missing container decl in GetNameForMethod"); |
| 3448 | NameOut += CD->getNameAsString(); |
Fariborz Jahanian | 1e9aef3 | 2009-04-16 18:34:20 +0000 | [diff] [blame] | 3449 | if (const ObjCCategoryImplDecl *CID = |
| 3450 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) { |
| 3451 | NameOut += '('; |
| 3452 | NameOut += CID->getNameAsString(); |
| 3453 | NameOut+= ')'; |
| 3454 | } |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3455 | NameOut += ' '; |
| 3456 | NameOut += D->getSelector().getAsString(); |
| 3457 | NameOut += ']'; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3458 | } |
| 3459 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3460 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3461 | EmitModuleInfo(); |
| 3462 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3463 | // Emit the dummy bodies for any protocols which were referenced but |
| 3464 | // never defined. |
| 3465 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 3466 | I = Protocols.begin(), e = Protocols.end(); I != e; ++I) { |
| 3467 | if (I->second->hasInitializer()) |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3468 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3469 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3470 | std::vector<llvm::Constant*> Values(5); |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 3471 | Values[0] = VMContext.getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 3472 | Values[1] = GetClassName(I->first); |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 3473 | Values[2] = VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3474 | Values[3] = Values[4] = |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 3475 | VMContext.getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 3476 | I->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 3477 | I->second->setInitializer(VMContext.getConstantStruct(ObjCTypes.ProtocolTy, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3478 | Values)); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 3479 | CGM.AddUsedGlobal(I->second); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3480 | } |
| 3481 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3482 | // Add assembler directives to add lazy undefined symbol references |
| 3483 | // for classes which are referenced but not defined. This is |
| 3484 | // important for correct linker interaction. |
| 3485 | |
| 3486 | // FIXME: Uh, this isn't particularly portable. |
| 3487 | std::stringstream s; |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3488 | |
| 3489 | if (!CGM.getModule().getModuleInlineAsm().empty()) |
| 3490 | s << "\n"; |
| 3491 | |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 3492 | for (std::set<IdentifierInfo*>::iterator I = LazySymbols.begin(), |
| 3493 | e = LazySymbols.end(); I != e; ++I) { |
| 3494 | s << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n"; |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3495 | } |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 3496 | for (std::set<IdentifierInfo*>::iterator I = DefinedSymbols.begin(), |
| 3497 | e = DefinedSymbols.end(); I != e; ++I) { |
| 3498 | s << "\t.objc_class_name_" << (*I)->getName() << "=0\n" |
| 3499 | << "\t.globl .objc_class_name_" << (*I)->getName() << "\n"; |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3500 | } |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3501 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3502 | CGM.getModule().appendModuleInlineAsm(s.str()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3503 | } |
| 3504 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3505 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3506 | : CGObjCCommonMac(cgm), |
| 3507 | ObjCTypes(cgm) |
| 3508 | { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3509 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3510 | ObjCABI = 2; |
| 3511 | } |
| 3512 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3513 | /* *** */ |
| 3514 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3515 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3516 | : VMContext(cgm.getLLVMContext()), CGM(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3517 | { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3518 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3519 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3520 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3521 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3522 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3523 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3524 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3525 | Int8PtrTy = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3526 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3527 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3528 | PtrObjectPtrTy = VMContext.getPointerTypeUnqual(ObjectPtrTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3529 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3530 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3531 | // FIXME: It would be nice to unify this with the opaque type, so that the IR |
| 3532 | // comes out a bit cleaner. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3533 | const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3534 | ExternalProtocolPtrTy = VMContext.getPointerTypeUnqual(T); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3535 | |
| 3536 | // I'm not sure I like this. The implicit coordination is a bit |
| 3537 | // gross. We should solve this in a reasonable fashion because this |
| 3538 | // is a pretty common task (match some runtime data structure with |
| 3539 | // an LLVM data structure). |
| 3540 | |
| 3541 | // FIXME: This is leaked. |
| 3542 | // FIXME: Merge with rewriter code? |
| 3543 | |
| 3544 | // struct _objc_super { |
| 3545 | // id self; |
| 3546 | // Class cls; |
| 3547 | // } |
| 3548 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3549 | SourceLocation(), |
| 3550 | &Ctx.Idents.get("_objc_super")); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3551 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3552 | Ctx.getObjCIdType(), 0, false)); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3553 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3554 | Ctx.getObjCClassType(), 0, false)); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3555 | RD->completeDefinition(Ctx); |
| 3556 | |
| 3557 | SuperCTy = Ctx.getTagDeclType(RD); |
| 3558 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
| 3559 | |
| 3560 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3561 | SuperPtrTy = VMContext.getPointerTypeUnqual(SuperTy); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3562 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3563 | // struct _prop_t { |
| 3564 | // char *name; |
| 3565 | // char *attributes; |
| 3566 | // } |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3567 | PropertyTy = VMContext.getStructType(Int8PtrTy, Int8PtrTy, NULL); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3568 | CGM.getModule().addTypeName("struct._prop_t", |
| 3569 | PropertyTy); |
| 3570 | |
| 3571 | // struct _prop_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3572 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3573 | // uint32_t count_of_properties; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3574 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3575 | // } |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3576 | PropertyListTy = VMContext.getStructType(IntTy, |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3577 | IntTy, |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3578 | VMContext.getArrayType(PropertyTy, 0), |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3579 | NULL); |
| 3580 | CGM.getModule().addTypeName("struct._prop_list_t", |
| 3581 | PropertyListTy); |
| 3582 | // struct _prop_list_t * |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3583 | PropertyListPtrTy = VMContext.getPointerTypeUnqual(PropertyListTy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3584 | |
| 3585 | // struct _objc_method { |
| 3586 | // SEL _cmd; |
| 3587 | // char *method_type; |
| 3588 | // char *_imp; |
| 3589 | // } |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3590 | MethodTy = VMContext.getStructType(SelectorPtrTy, |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3591 | Int8PtrTy, |
| 3592 | Int8PtrTy, |
| 3593 | NULL); |
| 3594 | CGM.getModule().addTypeName("struct._objc_method", MethodTy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3595 | |
| 3596 | // struct _objc_cache * |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3597 | CacheTy = VMContext.getOpaqueType(); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3598 | CGM.getModule().addTypeName("struct._objc_cache", CacheTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3599 | CachePtrTy = VMContext.getPointerTypeUnqual(CacheTy); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3600 | } |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3601 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3602 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3603 | : ObjCCommonTypesHelper(cgm) |
| 3604 | { |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3605 | // struct _objc_method_description { |
| 3606 | // SEL name; |
| 3607 | // char *types; |
| 3608 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3609 | MethodDescriptionTy = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3610 | VMContext.getStructType(SelectorPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3611 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3612 | NULL); |
| 3613 | CGM.getModule().addTypeName("struct._objc_method_description", |
| 3614 | MethodDescriptionTy); |
| 3615 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3616 | // struct _objc_method_description_list { |
| 3617 | // int count; |
| 3618 | // struct _objc_method_description[1]; |
| 3619 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3620 | MethodDescriptionListTy = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3621 | VMContext.getStructType(IntTy, |
| 3622 | VMContext.getArrayType(MethodDescriptionTy, 0), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3623 | NULL); |
| 3624 | CGM.getModule().addTypeName("struct._objc_method_description_list", |
| 3625 | MethodDescriptionListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3626 | |
| 3627 | // struct _objc_method_description_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3628 | MethodDescriptionListPtrTy = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3629 | VMContext.getPointerTypeUnqual(MethodDescriptionListTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3630 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3631 | // Protocol description structures |
| 3632 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3633 | // struct _objc_protocol_extension { |
| 3634 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 3635 | // struct _objc_method_description_list *optional_instance_methods; |
| 3636 | // struct _objc_method_description_list *optional_class_methods; |
| 3637 | // struct _objc_property_list *instance_properties; |
| 3638 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3639 | ProtocolExtensionTy = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3640 | VMContext.getStructType(IntTy, |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3641 | MethodDescriptionListPtrTy, |
| 3642 | MethodDescriptionListPtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3643 | PropertyListPtrTy, |
| 3644 | NULL); |
| 3645 | CGM.getModule().addTypeName("struct._objc_protocol_extension", |
| 3646 | ProtocolExtensionTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3647 | |
| 3648 | // struct _objc_protocol_extension * |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3649 | ProtocolExtensionPtrTy = VMContext.getPointerTypeUnqual(ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3650 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3651 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3652 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3653 | llvm::PATypeHolder ProtocolTyHolder = VMContext.getOpaqueType(); |
| 3654 | llvm::PATypeHolder ProtocolListTyHolder = VMContext.getOpaqueType(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3655 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3656 | const llvm::Type *T = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3657 | VMContext.getStructType(VMContext.getPointerTypeUnqual(ProtocolListTyHolder), |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3658 | LongTy, |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3659 | VMContext.getArrayType(ProtocolTyHolder, 0), |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3660 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3661 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T); |
| 3662 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3663 | // struct _objc_protocol { |
| 3664 | // struct _objc_protocol_extension *isa; |
| 3665 | // char *protocol_name; |
| 3666 | // struct _objc_protocol **_objc_protocol_list; |
| 3667 | // struct _objc_method_description_list *instance_methods; |
| 3668 | // struct _objc_method_description_list *class_methods; |
| 3669 | // } |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3670 | T = VMContext.getStructType(ProtocolExtensionPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3671 | Int8PtrTy, |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3672 | VMContext.getPointerTypeUnqual(ProtocolListTyHolder), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3673 | MethodDescriptionListPtrTy, |
| 3674 | MethodDescriptionListPtrTy, |
| 3675 | NULL); |
| 3676 | cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T); |
| 3677 | |
| 3678 | ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get()); |
| 3679 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3680 | ProtocolListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3681 | // struct _objc_protocol_list * |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3682 | ProtocolListPtrTy = VMContext.getPointerTypeUnqual(ProtocolListTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3683 | |
| 3684 | ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get()); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3685 | CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3686 | ProtocolPtrTy = VMContext.getPointerTypeUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3687 | |
| 3688 | // Class description structures |
| 3689 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3690 | // struct _objc_ivar { |
| 3691 | // char *ivar_name; |
| 3692 | // char *ivar_type; |
| 3693 | // int ivar_offset; |
| 3694 | // } |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3695 | IvarTy = VMContext.getStructType(Int8PtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3696 | Int8PtrTy, |
| 3697 | IntTy, |
| 3698 | NULL); |
| 3699 | CGM.getModule().addTypeName("struct._objc_ivar", IvarTy); |
| 3700 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3701 | // struct _objc_ivar_list * |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3702 | IvarListTy = VMContext.getOpaqueType(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3703 | CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3704 | IvarListPtrTy = VMContext.getPointerTypeUnqual(IvarListTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3705 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3706 | // struct _objc_method_list * |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3707 | MethodListTy = VMContext.getOpaqueType(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3708 | CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3709 | MethodListPtrTy = VMContext.getPointerTypeUnqual(MethodListTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3710 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3711 | // struct _objc_class_extension * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3712 | ClassExtensionTy = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3713 | VMContext.getStructType(IntTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3714 | Int8PtrTy, |
| 3715 | PropertyListPtrTy, |
| 3716 | NULL); |
| 3717 | CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3718 | ClassExtensionPtrTy = VMContext.getPointerTypeUnqual(ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3719 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3720 | llvm::PATypeHolder ClassTyHolder = VMContext.getOpaqueType(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3721 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3722 | // struct _objc_class { |
| 3723 | // Class isa; |
| 3724 | // Class super_class; |
| 3725 | // char *name; |
| 3726 | // long version; |
| 3727 | // long info; |
| 3728 | // long instance_size; |
| 3729 | // struct _objc_ivar_list *ivars; |
| 3730 | // struct _objc_method_list *methods; |
| 3731 | // struct _objc_cache *cache; |
| 3732 | // struct _objc_protocol_list *protocols; |
| 3733 | // char *ivar_layout; |
| 3734 | // struct _objc_class_ext *ext; |
| 3735 | // }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3736 | T = VMContext.getStructType(VMContext.getPointerTypeUnqual(ClassTyHolder), |
| 3737 | VMContext.getPointerTypeUnqual(ClassTyHolder), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3738 | Int8PtrTy, |
| 3739 | LongTy, |
| 3740 | LongTy, |
| 3741 | LongTy, |
| 3742 | IvarListPtrTy, |
| 3743 | MethodListPtrTy, |
| 3744 | CachePtrTy, |
| 3745 | ProtocolListPtrTy, |
| 3746 | Int8PtrTy, |
| 3747 | ClassExtensionPtrTy, |
| 3748 | NULL); |
| 3749 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T); |
| 3750 | |
| 3751 | ClassTy = cast<llvm::StructType>(ClassTyHolder.get()); |
| 3752 | CGM.getModule().addTypeName("struct._objc_class", ClassTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3753 | ClassPtrTy = VMContext.getPointerTypeUnqual(ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3754 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3755 | // struct _objc_category { |
| 3756 | // char *category_name; |
| 3757 | // char *class_name; |
| 3758 | // struct _objc_method_list *instance_method; |
| 3759 | // struct _objc_method_list *class_method; |
| 3760 | // uint32_t size; // sizeof(struct _objc_category) |
| 3761 | // struct _objc_property_list *instance_properties;// category's @property |
| 3762 | // } |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3763 | CategoryTy = VMContext.getStructType(Int8PtrTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3764 | Int8PtrTy, |
| 3765 | MethodListPtrTy, |
| 3766 | MethodListPtrTy, |
| 3767 | ProtocolListPtrTy, |
| 3768 | IntTy, |
| 3769 | PropertyListPtrTy, |
| 3770 | NULL); |
| 3771 | CGM.getModule().addTypeName("struct._objc_category", CategoryTy); |
| 3772 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3773 | // Global metadata structures |
| 3774 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3775 | // struct _objc_symtab { |
| 3776 | // long sel_ref_cnt; |
| 3777 | // SEL *refs; |
| 3778 | // short cls_def_cnt; |
| 3779 | // short cat_def_cnt; |
| 3780 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 3781 | // } |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3782 | SymtabTy = VMContext.getStructType(LongTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3783 | SelectorPtrTy, |
| 3784 | ShortTy, |
| 3785 | ShortTy, |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3786 | VMContext.getArrayType(Int8PtrTy, 0), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3787 | NULL); |
| 3788 | CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3789 | SymtabPtrTy = VMContext.getPointerTypeUnqual(SymtabTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3790 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3791 | // struct _objc_module { |
| 3792 | // long version; |
| 3793 | // long size; // sizeof(struct _objc_module) |
| 3794 | // char *name; |
| 3795 | // struct _objc_symtab* symtab; |
| 3796 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3797 | ModuleTy = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3798 | VMContext.getStructType(LongTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3799 | LongTy, |
| 3800 | Int8PtrTy, |
| 3801 | SymtabPtrTy, |
| 3802 | NULL); |
| 3803 | CGM.getModule().addTypeName("struct._objc_module", ModuleTy); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3804 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 3805 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3806 | // FIXME: This is the size of the setjmp buffer and should be target |
| 3807 | // specific. 18 is what's used on 32-bit X86. |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3808 | uint64_t SetJmpBufferSize = 18; |
| 3809 | |
| 3810 | // Exceptions |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3811 | const llvm::Type *StackPtrTy = VMContext.getArrayType( |
| 3812 | VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty), 4); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3813 | |
| 3814 | ExceptionDataTy = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3815 | VMContext.getStructType(VMContext.getArrayType(llvm::Type::Int32Ty, |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3816 | SetJmpBufferSize), |
| 3817 | StackPtrTy, NULL); |
| 3818 | CGM.getModule().addTypeName("struct._objc_exception_data", |
| 3819 | ExceptionDataTy); |
| 3820 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3821 | } |
| 3822 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3823 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3824 | : ObjCCommonTypesHelper(cgm) |
| 3825 | { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3826 | // struct _method_list_t { |
| 3827 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 3828 | // uint32_t method_count; |
| 3829 | // struct _objc_method method_list[method_count]; |
| 3830 | // } |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3831 | MethodListnfABITy = VMContext.getStructType(IntTy, |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3832 | IntTy, |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3833 | VMContext.getArrayType(MethodTy, 0), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3834 | NULL); |
| 3835 | CGM.getModule().addTypeName("struct.__method_list_t", |
| 3836 | MethodListnfABITy); |
| 3837 | // struct method_list_t * |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3838 | MethodListnfABIPtrTy = VMContext.getPointerTypeUnqual(MethodListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3839 | |
| 3840 | // struct _protocol_t { |
| 3841 | // id isa; // NULL |
| 3842 | // const char * const protocol_name; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3843 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3844 | // const struct method_list_t * const instance_methods; |
| 3845 | // const struct method_list_t * const class_methods; |
| 3846 | // const struct method_list_t *optionalInstanceMethods; |
| 3847 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3848 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3849 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 3850 | // const uint32_t flags; // = 0 |
| 3851 | // } |
| 3852 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3853 | // Holder for struct _protocol_list_t * |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3854 | llvm::PATypeHolder ProtocolListTyHolder = VMContext.getOpaqueType(); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3855 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3856 | ProtocolnfABITy = VMContext.getStructType(ObjectPtrTy, |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3857 | Int8PtrTy, |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3858 | VMContext.getPointerTypeUnqual( |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3859 | ProtocolListTyHolder), |
| 3860 | MethodListnfABIPtrTy, |
| 3861 | MethodListnfABIPtrTy, |
| 3862 | MethodListnfABIPtrTy, |
| 3863 | MethodListnfABIPtrTy, |
| 3864 | PropertyListPtrTy, |
| 3865 | IntTy, |
| 3866 | IntTy, |
| 3867 | NULL); |
| 3868 | CGM.getModule().addTypeName("struct._protocol_t", |
| 3869 | ProtocolnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3870 | |
| 3871 | // struct _protocol_t* |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3872 | ProtocolnfABIPtrTy = VMContext.getPointerTypeUnqual(ProtocolnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3873 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3874 | // struct _protocol_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3875 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3876 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3877 | // } |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3878 | ProtocolListnfABITy = VMContext.getStructType(LongTy, |
| 3879 | VMContext.getArrayType( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3880 | ProtocolnfABIPtrTy, 0), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3881 | NULL); |
| 3882 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3883 | ProtocolListnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3884 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo( |
| 3885 | ProtocolListnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3886 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3887 | // struct _objc_protocol_list* |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3888 | ProtocolListnfABIPtrTy = VMContext.getPointerTypeUnqual(ProtocolListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3889 | |
| 3890 | // struct _ivar_t { |
| 3891 | // unsigned long int *offset; // pointer to ivar offset location |
| 3892 | // char *name; |
| 3893 | // char *type; |
| 3894 | // uint32_t alignment; |
| 3895 | // uint32_t size; |
| 3896 | // } |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3897 | IvarnfABITy = VMContext.getStructType(VMContext.getPointerTypeUnqual(LongTy), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3898 | Int8PtrTy, |
| 3899 | Int8PtrTy, |
| 3900 | IntTy, |
| 3901 | IntTy, |
| 3902 | NULL); |
| 3903 | CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy); |
| 3904 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3905 | // struct _ivar_list_t { |
| 3906 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 3907 | // uint32 count; |
| 3908 | // struct _iver_t list[count]; |
| 3909 | // } |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3910 | IvarListnfABITy = VMContext.getStructType(IntTy, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3911 | IntTy, |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3912 | VMContext.getArrayType( |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3913 | IvarnfABITy, 0), |
| 3914 | NULL); |
| 3915 | CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy); |
| 3916 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3917 | IvarListnfABIPtrTy = VMContext.getPointerTypeUnqual(IvarListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3918 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3919 | // struct _class_ro_t { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3920 | // uint32_t const flags; |
| 3921 | // uint32_t const instanceStart; |
| 3922 | // uint32_t const instanceSize; |
| 3923 | // uint32_t const reserved; // only when building for 64bit targets |
| 3924 | // const uint8_t * const ivarLayout; |
| 3925 | // const char *const name; |
| 3926 | // const struct _method_list_t * const baseMethods; |
| 3927 | // const struct _objc_protocol_list *const baseProtocols; |
| 3928 | // const struct _ivar_list_t *const ivars; |
| 3929 | // const uint8_t * const weakIvarLayout; |
| 3930 | // const struct _prop_list_t * const properties; |
| 3931 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3932 | |
| 3933 | // FIXME. Add 'reserved' field in 64bit abi mode! |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3934 | ClassRonfABITy = VMContext.getStructType(IntTy, |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3935 | IntTy, |
| 3936 | IntTy, |
| 3937 | Int8PtrTy, |
| 3938 | Int8PtrTy, |
| 3939 | MethodListnfABIPtrTy, |
| 3940 | ProtocolListnfABIPtrTy, |
| 3941 | IvarListnfABIPtrTy, |
| 3942 | Int8PtrTy, |
| 3943 | PropertyListPtrTy, |
| 3944 | NULL); |
| 3945 | CGM.getModule().addTypeName("struct._class_ro_t", |
| 3946 | ClassRonfABITy); |
| 3947 | |
| 3948 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 3949 | std::vector<const llvm::Type*> Params; |
| 3950 | Params.push_back(ObjectPtrTy); |
| 3951 | Params.push_back(SelectorPtrTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3952 | ImpnfABITy = VMContext.getPointerTypeUnqual( |
| 3953 | VMContext.getFunctionType(ObjectPtrTy, Params, false)); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3954 | |
| 3955 | // struct _class_t { |
| 3956 | // struct _class_t *isa; |
| 3957 | // struct _class_t * const superclass; |
| 3958 | // void *cache; |
| 3959 | // IMP *vtable; |
| 3960 | // struct class_ro_t *ro; |
| 3961 | // } |
| 3962 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3963 | llvm::PATypeHolder ClassTyHolder = VMContext.getOpaqueType(); |
| 3964 | ClassnfABITy = |
| 3965 | VMContext.getStructType(VMContext.getPointerTypeUnqual(ClassTyHolder), |
| 3966 | VMContext.getPointerTypeUnqual(ClassTyHolder), |
| 3967 | CachePtrTy, |
| 3968 | VMContext.getPointerTypeUnqual(ImpnfABITy), |
| 3969 | VMContext.getPointerTypeUnqual(ClassRonfABITy), |
| 3970 | NULL); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3971 | CGM.getModule().addTypeName("struct._class_t", ClassnfABITy); |
| 3972 | |
| 3973 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo( |
| 3974 | ClassnfABITy); |
| 3975 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3976 | // LLVM for struct _class_t * |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3977 | ClassnfABIPtrTy = VMContext.getPointerTypeUnqual(ClassnfABITy); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3978 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3979 | // struct _category_t { |
| 3980 | // const char * const name; |
| 3981 | // struct _class_t *const cls; |
| 3982 | // const struct _method_list_t * const instance_methods; |
| 3983 | // const struct _method_list_t * const class_methods; |
| 3984 | // const struct _protocol_list_t * const protocols; |
| 3985 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3986 | // } |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3987 | CategorynfABITy = VMContext.getStructType(Int8PtrTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3988 | ClassnfABIPtrTy, |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3989 | MethodListnfABIPtrTy, |
| 3990 | MethodListnfABIPtrTy, |
| 3991 | ProtocolListnfABIPtrTy, |
| 3992 | PropertyListPtrTy, |
| 3993 | NULL); |
| 3994 | CGM.getModule().addTypeName("struct._category_t", CategorynfABITy); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3995 | |
| 3996 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3997 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3998 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3999 | |
| 4000 | // MessageRefTy - LLVM for: |
| 4001 | // struct _message_ref_t { |
| 4002 | // IMP messenger; |
| 4003 | // SEL name; |
| 4004 | // }; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4005 | |
| 4006 | // First the clang type for struct _message_ref_t |
| 4007 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 4008 | SourceLocation(), |
| 4009 | &Ctx.Idents.get("_message_ref_t")); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4010 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4011 | Ctx.VoidPtrTy, 0, false)); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4012 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4013 | Ctx.getObjCSelType(), 0, false)); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4014 | RD->completeDefinition(Ctx); |
| 4015 | |
| 4016 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 4017 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 4018 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4019 | |
| 4020 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4021 | MessageRefPtrTy = VMContext.getPointerTypeUnqual(MessageRefTy); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4022 | |
| 4023 | // SuperMessageRefTy - LLVM for: |
| 4024 | // struct _super_message_ref_t { |
| 4025 | // SUPER_IMP messenger; |
| 4026 | // SEL name; |
| 4027 | // }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4028 | SuperMessageRefTy = VMContext.getStructType(ImpnfABITy, |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4029 | SelectorPtrTy, |
| 4030 | NULL); |
| 4031 | CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy); |
| 4032 | |
| 4033 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4034 | SuperMessageRefPtrTy = VMContext.getPointerTypeUnqual(SuperMessageRefTy); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4035 | |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 4036 | |
| 4037 | // struct objc_typeinfo { |
| 4038 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 4039 | // const char* name; // c++ typeinfo string |
| 4040 | // Class cls; |
| 4041 | // }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4042 | EHTypeTy = VMContext.getStructType(VMContext.getPointerTypeUnqual(Int8PtrTy), |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 4043 | Int8PtrTy, |
| 4044 | ClassnfABIPtrTy, |
| 4045 | NULL); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 4046 | CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4047 | EHTypePtrTy = VMContext.getPointerTypeUnqual(EHTypeTy); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4048 | } |
| 4049 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4050 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
| 4051 | FinishNonFragileABIModule(); |
| 4052 | |
| 4053 | return NULL; |
| 4054 | } |
| 4055 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4056 | void CGObjCNonFragileABIMac::AddModuleClassList(const |
| 4057 | std::vector<llvm::GlobalValue*> |
| 4058 | &Container, |
| 4059 | const char *SymbolName, |
| 4060 | const char *SectionName) { |
| 4061 | unsigned NumClasses = Container.size(); |
| 4062 | |
| 4063 | if (!NumClasses) |
| 4064 | return; |
| 4065 | |
| 4066 | std::vector<llvm::Constant*> Symbols(NumClasses); |
| 4067 | for (unsigned i=0; i<NumClasses; i++) |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4068 | Symbols[i] = VMContext.getConstantExprBitCast(Container[i], |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4069 | ObjCTypes.Int8PtrTy); |
| 4070 | llvm::Constant* Init = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4071 | VMContext.getConstantArray(VMContext.getArrayType(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4072 | NumClasses), |
| 4073 | Symbols); |
| 4074 | |
| 4075 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4076 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4077 | llvm::GlobalValue::InternalLinkage, |
| 4078 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4079 | SymbolName); |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4080 | GV->setAlignment(8); |
| 4081 | GV->setSection(SectionName); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4082 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4083 | } |
| 4084 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4085 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 4086 | // nonfragile abi has no module definition. |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4087 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4088 | // Build list of all implemented class addresses in array |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4089 | // L_OBJC_LABEL_CLASS_$. |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4090 | AddModuleClassList(DefinedClasses, |
| 4091 | "\01L_OBJC_LABEL_CLASS_$", |
| 4092 | "__DATA, __objc_classlist, regular, no_dead_strip"); |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4093 | AddModuleClassList(DefinedNonLazyClasses, |
| 4094 | "\01L_OBJC_LABEL_NONLAZY_CLASS_$", |
| 4095 | "__DATA, __objc_nlclslist, regular, no_dead_strip"); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4096 | |
| 4097 | // Build list of all implemented category addresses in array |
| 4098 | // L_OBJC_LABEL_CATEGORY_$. |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4099 | AddModuleClassList(DefinedCategories, |
| 4100 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 4101 | "__DATA, __objc_catlist, regular, no_dead_strip"); |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4102 | AddModuleClassList(DefinedNonLazyCategories, |
| 4103 | "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$", |
| 4104 | "__DATA, __objc_nlcatlist, regular, no_dead_strip"); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4105 | |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4106 | // static int L_OBJC_IMAGE_INFO[2] = { 0, flags }; |
| 4107 | // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0 |
| 4108 | std::vector<llvm::Constant*> Values(2); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 4109 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0); |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4110 | unsigned int flags = 0; |
Fariborz Jahanian | 66a5c2c | 2009-02-24 23:34:44 +0000 | [diff] [blame] | 4111 | // FIXME: Fix and continue? |
| 4112 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 4113 | flags |= eImageInfo_GarbageCollected; |
| 4114 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 4115 | flags |= eImageInfo_GCOnly; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 4116 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4117 | llvm::Constant* Init = VMContext.getConstantArray( |
| 4118 | VMContext.getArrayType(ObjCTypes.IntTy, 2), |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4119 | Values); |
| 4120 | llvm::GlobalVariable *IMGV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4121 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4122 | llvm::GlobalValue::InternalLinkage, |
| 4123 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4124 | "\01L_OBJC_IMAGE_INFO"); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4125 | IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); |
Daniel Dunbar | 325f758 | 2009-04-23 08:03:21 +0000 | [diff] [blame] | 4126 | IMGV->setConstant(true); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4127 | CGM.AddUsedGlobal(IMGV); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4128 | } |
| 4129 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4130 | /// LegacyDispatchedSelector - Returns true if SEL is not in the list of |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4131 | /// NonLegacyDispatchMethods; false otherwise. What this means is that |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4132 | /// except for the 19 selectors in the list, we generate 32bit-style |
| 4133 | /// message dispatch call for all the rest. |
| 4134 | /// |
| 4135 | bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) { |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4136 | if (NonLegacyDispatchMethods.empty()) { |
| 4137 | NonLegacyDispatchMethods.insert(GetNullarySelector("alloc")); |
| 4138 | NonLegacyDispatchMethods.insert(GetNullarySelector("class")); |
| 4139 | NonLegacyDispatchMethods.insert(GetNullarySelector("self")); |
| 4140 | NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped")); |
| 4141 | NonLegacyDispatchMethods.insert(GetNullarySelector("length")); |
| 4142 | NonLegacyDispatchMethods.insert(GetNullarySelector("count")); |
| 4143 | NonLegacyDispatchMethods.insert(GetNullarySelector("retain")); |
| 4144 | NonLegacyDispatchMethods.insert(GetNullarySelector("release")); |
| 4145 | NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease")); |
| 4146 | NonLegacyDispatchMethods.insert(GetNullarySelector("hash")); |
| 4147 | |
| 4148 | NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone")); |
| 4149 | NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass")); |
| 4150 | NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector")); |
| 4151 | NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey")); |
| 4152 | NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex")); |
| 4153 | NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString")); |
| 4154 | NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual")); |
| 4155 | NonLegacyDispatchMethods.insert(GetUnarySelector("addObject")); |
Fariborz Jahanian | be53be4 | 2009-05-13 16:19:02 +0000 | [diff] [blame] | 4156 | // "countByEnumeratingWithState:objects:count" |
| 4157 | IdentifierInfo *KeyIdents[] = { |
| 4158 | &CGM.getContext().Idents.get("countByEnumeratingWithState"), |
| 4159 | &CGM.getContext().Idents.get("objects"), |
| 4160 | &CGM.getContext().Idents.get("count") |
| 4161 | }; |
| 4162 | NonLegacyDispatchMethods.insert( |
| 4163 | CGM.getContext().Selectors.getSelector(3, KeyIdents)); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4164 | } |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4165 | return (NonLegacyDispatchMethods.count(Sel) == 0); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4166 | } |
| 4167 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4168 | // Metadata flags |
| 4169 | enum MetaDataDlags { |
| 4170 | CLS = 0x0, |
| 4171 | CLS_META = 0x1, |
| 4172 | CLS_ROOT = 0x2, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4173 | OBJC2_CLS_HIDDEN = 0x10, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4174 | CLS_EXCEPTION = 0x20 |
| 4175 | }; |
| 4176 | /// BuildClassRoTInitializer - generate meta-data for: |
| 4177 | /// struct _class_ro_t { |
| 4178 | /// uint32_t const flags; |
| 4179 | /// uint32_t const instanceStart; |
| 4180 | /// uint32_t const instanceSize; |
| 4181 | /// uint32_t const reserved; // only when building for 64bit targets |
| 4182 | /// const uint8_t * const ivarLayout; |
| 4183 | /// const char *const name; |
| 4184 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4185 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4186 | /// const struct _ivar_list_t *const ivars; |
| 4187 | /// const uint8_t * const weakIvarLayout; |
| 4188 | /// const struct _prop_list_t * const properties; |
| 4189 | /// } |
| 4190 | /// |
| 4191 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
| 4192 | unsigned flags, |
| 4193 | unsigned InstanceStart, |
| 4194 | unsigned InstanceSize, |
| 4195 | const ObjCImplementationDecl *ID) { |
| 4196 | std::string ClassName = ID->getNameAsString(); |
| 4197 | std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets! |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 4198 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 4199 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 4200 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4201 | // FIXME. For 64bit targets add 0 here. |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 4202 | Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4203 | : BuildIvarLayout(ID, true); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4204 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4205 | // const struct _method_list_t * const baseMethods; |
| 4206 | std::vector<llvm::Constant*> Methods; |
| 4207 | std::string MethodListName("\01l_OBJC_$_"); |
| 4208 | if (flags & CLS_META) { |
| 4209 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4210 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4211 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4212 | // Class methods should always be defined. |
| 4213 | Methods.push_back(GetMethodConstant(*i)); |
| 4214 | } |
| 4215 | } else { |
| 4216 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4217 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4218 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4219 | // Instance methods should always be defined. |
| 4220 | Methods.push_back(GetMethodConstant(*i)); |
| 4221 | } |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4222 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4223 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4224 | ObjCPropertyImplDecl *PID = *i; |
| 4225 | |
| 4226 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 4227 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 4228 | |
| 4229 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 4230 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4231 | Methods.push_back(C); |
| 4232 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 4233 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4234 | Methods.push_back(C); |
| 4235 | } |
| 4236 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4237 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4238 | Values[ 5] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4239 | "__DATA, __objc_const", Methods); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4240 | |
| 4241 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4242 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
| 4243 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
| 4244 | + OID->getNameAsString(), |
| 4245 | OID->protocol_begin(), |
| 4246 | OID->protocol_end()); |
| 4247 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4248 | if (flags & CLS_META) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 4249 | Values[ 7] = VMContext.getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4250 | else |
| 4251 | Values[ 7] = EmitIvarList(ID); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 4252 | Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4253 | : BuildIvarLayout(ID, false); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4254 | if (flags & CLS_META) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 4255 | Values[ 9] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4256 | else |
| 4257 | Values[ 9] = |
| 4258 | EmitPropertyList( |
| 4259 | "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
| 4260 | ID, ID->getClassInterface(), ObjCTypes); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4261 | llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.ClassRonfABITy, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4262 | Values); |
| 4263 | llvm::GlobalVariable *CLASS_RO_GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4264 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4265 | llvm::GlobalValue::InternalLinkage, |
| 4266 | Init, |
| 4267 | (flags & CLS_META) ? |
| 4268 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4269 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4270 | CLASS_RO_GV->setAlignment( |
| 4271 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4272 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4273 | return CLASS_RO_GV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4274 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4275 | } |
| 4276 | |
| 4277 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 4278 | /// for the given ClassName for: |
| 4279 | /// struct _class_t { |
| 4280 | /// struct _class_t *isa; |
| 4281 | /// struct _class_t * const superclass; |
| 4282 | /// void *cache; |
| 4283 | /// IMP *vtable; |
| 4284 | /// struct class_ro_t *ro; |
| 4285 | /// } |
| 4286 | /// |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4287 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
| 4288 | std::string &ClassName, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4289 | llvm::Constant *IsAGV, |
| 4290 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4291 | llvm::Constant *ClassRoGV, |
| 4292 | bool HiddenVisibility) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4293 | std::vector<llvm::Constant*> Values(5); |
| 4294 | Values[0] = IsAGV; |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4295 | Values[1] = SuperClassGV |
| 4296 | ? SuperClassGV |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 4297 | : VMContext.getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4298 | Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar |
| 4299 | Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar |
| 4300 | Values[4] = ClassRoGV; // &CLASS_RO_GV |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4301 | llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.ClassnfABITy, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4302 | Values); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4303 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 4304 | GV->setInitializer(Init); |
Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 4305 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4306 | GV->setAlignment( |
| 4307 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4308 | if (HiddenVisibility) |
| 4309 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4310 | return GV; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4311 | } |
| 4312 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4313 | bool |
Fariborz Jahanian | ecfbdcb | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 4314 | CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4315 | return OD->getClassMethod(GetNullarySelector("load")) != 0; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4316 | } |
| 4317 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4318 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4319 | uint32_t &InstanceStart, |
| 4320 | uint32_t &InstanceSize) { |
Daniel Dunbar | b4c79e0 | 2009-05-04 21:26:30 +0000 | [diff] [blame] | 4321 | const ASTRecordLayout &RL = |
| 4322 | CGM.getContext().getASTObjCImplementationLayout(OID); |
| 4323 | |
Daniel Dunbar | 6e8575b | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 4324 | // InstanceSize is really instance end. |
Anders Carlsson | 243a685 | 2009-07-18 21:26:44 +0000 | [diff] [blame] | 4325 | InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8; |
Daniel Dunbar | 6e8575b | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 4326 | |
| 4327 | // If there are no fields, the start is the same as the end. |
| 4328 | if (!RL.getFieldCount()) |
| 4329 | InstanceStart = InstanceSize; |
| 4330 | else |
| 4331 | InstanceStart = RL.getFieldOffset(0) / 8; |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4332 | } |
| 4333 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4334 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 4335 | std::string ClassName = ID->getNameAsString(); |
| 4336 | if (!ObjCEmptyCacheVar) { |
| 4337 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4338 | CGM.getModule(), |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4339 | ObjCTypes.CacheTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4340 | false, |
| 4341 | llvm::GlobalValue::ExternalLinkage, |
| 4342 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4343 | "_objc_empty_cache"); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4344 | |
| 4345 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4346 | CGM.getModule(), |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4347 | ObjCTypes.ImpnfABITy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4348 | false, |
| 4349 | llvm::GlobalValue::ExternalLinkage, |
| 4350 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4351 | "_objc_empty_vtable"); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4352 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4353 | assert(ID->getClassInterface() && |
| 4354 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 4355 | // FIXME: Is this correct (that meta class size is never computed)? |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4356 | uint32_t InstanceStart = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4357 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4358 | uint32_t InstanceSize = InstanceStart; |
| 4359 | uint32_t flags = CLS_META; |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4360 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 4361 | std::string ObjCClassName(getClassSymbolPrefix()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4362 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4363 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4364 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4365 | bool classIsHidden = |
| 4366 | CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4367 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4368 | flags |= OBJC2_CLS_HIDDEN; |
| 4369 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4370 | // class is root |
| 4371 | flags |= CLS_ROOT; |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4372 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4373 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4374 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4375 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4376 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 4377 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 4378 | Root = Super; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4379 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4380 | // work on super class metadata symbol. |
| 4381 | std::string SuperClassName = |
| 4382 | ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4383 | SuperClassGV = GetClassGlobal(SuperClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4384 | } |
| 4385 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 4386 | InstanceStart, |
| 4387 | InstanceSize,ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4388 | std::string TClassName = ObjCMetaClassName + ClassName; |
| 4389 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4390 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 4391 | classIsHidden); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4392 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4393 | // Metadata for the class |
| 4394 | flags = CLS; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4395 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4396 | flags |= OBJC2_CLS_HIDDEN; |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4397 | |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 4398 | if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface())) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4399 | flags |= CLS_EXCEPTION; |
| 4400 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4401 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4402 | flags |= CLS_ROOT; |
| 4403 | SuperClassGV = 0; |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 4404 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4405 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4406 | std::string RootClassName = |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4407 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4408 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4409 | } |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4410 | GetClassSizeInfo(ID, InstanceStart, InstanceSize); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4411 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4412 | InstanceStart, |
| 4413 | InstanceSize, |
| 4414 | ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4415 | |
| 4416 | TClassName = ObjCClassName + ClassName; |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4417 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4418 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 4419 | classIsHidden); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4420 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4421 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4422 | // Determine if this class is also "non-lazy". |
| 4423 | if (ImplementationIsNonLazy(ID)) |
| 4424 | DefinedNonLazyClasses.push_back(ClassMD); |
| 4425 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4426 | // Force the definition of the EHType if necessary. |
| 4427 | if (flags & CLS_EXCEPTION) |
| 4428 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4429 | } |
| 4430 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4431 | /// GenerateProtocolRef - This routine is called to generate code for |
| 4432 | /// a protocol reference expression; as in: |
| 4433 | /// @code |
| 4434 | /// @protocol(Proto1); |
| 4435 | /// @endcode |
| 4436 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 4437 | /// which will hold address of the protocol meta-data. |
| 4438 | /// |
| 4439 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
| 4440 | const ObjCProtocolDecl *PD) { |
| 4441 | |
Fariborz Jahanian | 960cd06 | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 4442 | // This routine is called for @protocol only. So, we must build definition |
| 4443 | // of protocol's meta-data (not a reference to it!) |
| 4444 | // |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4445 | llvm::Constant *Init = |
| 4446 | VMContext.getConstantExprBitCast(GetOrEmitProtocol(PD), |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4447 | ObjCTypes.ExternalProtocolPtrTy); |
| 4448 | |
| 4449 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
| 4450 | ProtocolName += PD->getNameAsCString(); |
| 4451 | |
| 4452 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 4453 | if (PTGV) |
| 4454 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4455 | PTGV = new llvm::GlobalVariable( |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4456 | CGM.getModule(), |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4457 | Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4458 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4459 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4460 | ProtocolName); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4461 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 4462 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4463 | CGM.AddUsedGlobal(PTGV); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4464 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4465 | } |
| 4466 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4467 | /// GenerateCategory - Build metadata for a category implementation. |
| 4468 | /// struct _category_t { |
| 4469 | /// const char * const name; |
| 4470 | /// struct _class_t *const cls; |
| 4471 | /// const struct _method_list_t * const instance_methods; |
| 4472 | /// const struct _method_list_t * const class_methods; |
| 4473 | /// const struct _protocol_list_t * const protocols; |
| 4474 | /// const struct _prop_list_t * const properties; |
| 4475 | /// } |
| 4476 | /// |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4477 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4478 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4479 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
| 4480 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4481 | "_$_" + OCD->getNameAsString()); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4482 | std::string ExtClassName(getClassSymbolPrefix() + |
| 4483 | Interface->getNameAsString()); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4484 | |
| 4485 | std::vector<llvm::Constant*> Values(6); |
| 4486 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 4487 | // meta-class entry symbol |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4488 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4489 | Values[1] = ClassGV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4490 | std::vector<llvm::Constant*> Methods; |
| 4491 | std::string MethodListName(Prefix); |
| 4492 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
| 4493 | "_$_" + OCD->getNameAsString(); |
| 4494 | |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4495 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4496 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4497 | // Instance methods should always be defined. |
| 4498 | Methods.push_back(GetMethodConstant(*i)); |
| 4499 | } |
| 4500 | |
| 4501 | Values[2] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4502 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4503 | Methods); |
| 4504 | |
| 4505 | MethodListName = Prefix; |
| 4506 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 4507 | OCD->getNameAsString(); |
| 4508 | Methods.clear(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4509 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4510 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4511 | // Class methods should always be defined. |
| 4512 | Methods.push_back(GetMethodConstant(*i)); |
| 4513 | } |
| 4514 | |
| 4515 | Values[3] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4516 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4517 | Methods); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4518 | const ObjCCategoryDecl *Category = |
| 4519 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4520 | if (Category) { |
| 4521 | std::string ExtName(Interface->getNameAsString() + "_$_" + |
| 4522 | OCD->getNameAsString()); |
| 4523 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
| 4524 | + Interface->getNameAsString() + "_$_" |
| 4525 | + Category->getNameAsString(), |
| 4526 | Category->protocol_begin(), |
| 4527 | Category->protocol_end()); |
| 4528 | Values[5] = |
| 4529 | EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
| 4530 | OCD, Category, ObjCTypes); |
| 4531 | } |
| 4532 | else { |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 4533 | Values[4] = VMContext.getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4534 | Values[5] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4535 | } |
| 4536 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4537 | llvm::Constant *Init = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4538 | VMContext.getConstantStruct(ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4539 | Values); |
| 4540 | llvm::GlobalVariable *GCATV |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4541 | = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4542 | false, |
| 4543 | llvm::GlobalValue::InternalLinkage, |
| 4544 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4545 | ExtCatName); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4546 | GCATV->setAlignment( |
| 4547 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4548 | GCATV->setSection("__DATA, __objc_const"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4549 | CGM.AddUsedGlobal(GCATV); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4550 | DefinedCategories.push_back(GCATV); |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4551 | |
| 4552 | // Determine if this category is also "non-lazy". |
| 4553 | if (ImplementationIsNonLazy(OCD)) |
| 4554 | DefinedNonLazyCategories.push_back(GCATV); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4555 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4556 | |
| 4557 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 4558 | /// given method if it has been defined. The result is null if the |
| 4559 | /// method has not been defined. The return value has type MethodPtrTy. |
| 4560 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
| 4561 | const ObjCMethodDecl *MD) { |
| 4562 | // FIXME: Use DenseMap::lookup |
| 4563 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 4564 | if (!Fn) |
| 4565 | return 0; |
| 4566 | |
| 4567 | std::vector<llvm::Constant*> Method(3); |
| 4568 | Method[0] = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4569 | VMContext.getConstantExprBitCast(GetMethodVarName(MD->getSelector()), |
| 4570 | ObjCTypes.SelectorPtrTy); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4571 | Method[1] = GetMethodVarType(MD); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4572 | Method[2] = VMContext.getConstantExprBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 4573 | return VMContext.getConstantStruct(ObjCTypes.MethodTy, Method); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4574 | } |
| 4575 | |
| 4576 | /// EmitMethodList - Build meta-data for method declarations |
| 4577 | /// struct _method_list_t { |
| 4578 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 4579 | /// uint32_t method_count; |
| 4580 | /// struct _objc_method method_list[method_count]; |
| 4581 | /// } |
| 4582 | /// |
| 4583 | llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList( |
| 4584 | const std::string &Name, |
| 4585 | const char *Section, |
| 4586 | const ConstantVector &Methods) { |
| 4587 | // Return null for empty list. |
| 4588 | if (Methods.empty()) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 4589 | return VMContext.getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4590 | |
| 4591 | std::vector<llvm::Constant*> Values(3); |
| 4592 | // sizeof(struct _objc_method) |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4593 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 4594 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4595 | // method_count |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 4596 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4597 | llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.MethodTy, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4598 | Methods.size()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4599 | Values[2] = VMContext.getConstantArray(AT, Methods); |
| 4600 | llvm::Constant *Init = VMContext.getConstantStruct(Values); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4601 | |
| 4602 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4603 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4604 | llvm::GlobalValue::InternalLinkage, |
| 4605 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4606 | Name); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4607 | GV->setAlignment( |
| 4608 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4609 | GV->setSection(Section); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4610 | CGM.AddUsedGlobal(GV); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4611 | return VMContext.getConstantExprBitCast(GV, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4612 | ObjCTypes.MethodListnfABIPtrTy); |
| 4613 | } |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4614 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4615 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 4616 | /// the given ivar. |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4617 | llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 4618 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4619 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | a81419d | 2009-05-05 00:36:57 +0000 | [diff] [blame] | 4620 | // FIXME: We shouldn't need to do this lookup. |
| 4621 | unsigned Index; |
| 4622 | const ObjCInterfaceDecl *Container = |
| 4623 | FindIvarInterface(CGM.getContext(), ID, Ivar, Index); |
| 4624 | assert(Container && "Unable to find ivar container!"); |
| 4625 | std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() + |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4626 | '.' + Ivar->getNameAsString(); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4627 | llvm::GlobalVariable *IvarOffsetGV = |
| 4628 | CGM.getModule().getGlobalVariable(Name); |
| 4629 | if (!IvarOffsetGV) |
| 4630 | IvarOffsetGV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4631 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4632 | false, |
| 4633 | llvm::GlobalValue::ExternalLinkage, |
| 4634 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4635 | Name); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4636 | return IvarOffsetGV; |
| 4637 | } |
| 4638 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4639 | llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar( |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4640 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4641 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4642 | unsigned long int Offset) { |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4643 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 4644 | IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy, |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4645 | Offset)); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4646 | IvarOffsetGV->setAlignment( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4647 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy)); |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4648 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 4649 | // FIXME: This matches gcc, but shouldn't the visibility be set on the use as |
| 4650 | // well (i.e., in ObjCIvarOffsetVariable). |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4651 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 4652 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
| 4653 | CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden) |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4654 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4655 | else |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4656 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4657 | IvarOffsetGV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4658 | return IvarOffsetGV; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4659 | } |
| 4660 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4661 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 4662 | /// implementation. The return value has type |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4663 | /// IvarListnfABIPtrTy. |
| 4664 | /// struct _ivar_t { |
| 4665 | /// unsigned long int *offset; // pointer to ivar offset location |
| 4666 | /// char *name; |
| 4667 | /// char *type; |
| 4668 | /// uint32_t alignment; |
| 4669 | /// uint32_t size; |
| 4670 | /// } |
| 4671 | /// struct _ivar_list_t { |
| 4672 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 4673 | /// uint32 count; |
| 4674 | /// struct _iver_t list[count]; |
| 4675 | /// } |
| 4676 | /// |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4677 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4678 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
| 4679 | const ObjCImplementationDecl *ID) { |
| 4680 | |
| 4681 | std::vector<llvm::Constant*> Ivars, Ivar(5); |
| 4682 | |
| 4683 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4684 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
| 4685 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4686 | // FIXME. Consolidate this with similar code in GenerateClass. |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 4687 | |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4688 | // Collect declared and synthesized ivars in a small vector. |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4689 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 4690 | CGM.getContext().ShallowCollectObjCIvars(OID, OIvars); |
Fariborz Jahanian | 99eee36 | 2009-04-01 19:37:34 +0000 | [diff] [blame] | 4691 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4692 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 4693 | ObjCIvarDecl *IVD = OIvars[i]; |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 4694 | // Ignore unnamed bit-fields. |
| 4695 | if (!IVD->getDeclName()) |
| 4696 | continue; |
Daniel Dunbar | 3eec8aa | 2009-04-20 05:53:40 +0000 | [diff] [blame] | 4697 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD, |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4698 | ComputeIvarBaseOffset(CGM, ID, IVD)); |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4699 | Ivar[1] = GetMethodVarName(IVD->getIdentifier()); |
| 4700 | Ivar[2] = GetMethodVarType(IVD); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4701 | const llvm::Type *FieldTy = |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4702 | CGM.getTypes().ConvertTypeForMem(IVD->getType()); |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4703 | unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4704 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4705 | IVD->getType().getTypePtr()) >> 3; |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4706 | Align = llvm::Log2_32(Align); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 4707 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4708 | // NOTE. Size of a bitfield does not match gcc's, because of the |
| 4709 | // way bitfields are treated special in each. But I am told that |
| 4710 | // 'size' for bitfield ivars is ignored by the runtime so it does |
| 4711 | // not matter. If it matters, there is enough info to get the |
| 4712 | // bitfield right! |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 4713 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4714 | Ivars.push_back(VMContext.getConstantStruct(ObjCTypes.IvarnfABITy, Ivar)); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4715 | } |
| 4716 | // Return null for empty list. |
| 4717 | if (Ivars.empty()) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 4718 | return VMContext.getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4719 | std::vector<llvm::Constant*> Values(3); |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4720 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 4721 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4722 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4723 | llvm::ArrayType *AT = VMContext.getArrayType(ObjCTypes.IvarnfABITy, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4724 | Ivars.size()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4725 | Values[2] = VMContext.getConstantArray(AT, Ivars); |
| 4726 | llvm::Constant *Init = VMContext.getConstantStruct(Values); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4727 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 4728 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4729 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4730 | llvm::GlobalValue::InternalLinkage, |
| 4731 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4732 | Prefix + OID->getNameAsString()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4733 | GV->setAlignment( |
| 4734 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4735 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4736 | |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4737 | CGM.AddUsedGlobal(GV); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4738 | return VMContext.getConstantExprBitCast(GV, ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4739 | } |
| 4740 | |
| 4741 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
| 4742 | const ObjCProtocolDecl *PD) { |
| 4743 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4744 | |
| 4745 | if (!Entry) { |
| 4746 | // We use the initializer as a marker of whether this is a forward |
| 4747 | // reference or not. At module finalization we add the empty |
| 4748 | // contents for protocols which were referenced but never defined. |
| 4749 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4750 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4751 | llvm::GlobalValue::ExternalLinkage, |
| 4752 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4753 | "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString()); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4754 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4755 | } |
| 4756 | |
| 4757 | return Entry; |
| 4758 | } |
| 4759 | |
| 4760 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 4761 | /// @code |
| 4762 | /// struct _protocol_t { |
| 4763 | /// id isa; // NULL |
| 4764 | /// const char * const protocol_name; |
| 4765 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 4766 | /// const struct method_list_t * const instance_methods; |
| 4767 | /// const struct method_list_t * const class_methods; |
| 4768 | /// const struct method_list_t *optionalInstanceMethods; |
| 4769 | /// const struct method_list_t *optionalClassMethods; |
| 4770 | /// const struct _prop_list_t * properties; |
| 4771 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 4772 | /// const uint32_t flags; // = 0 |
| 4773 | /// } |
| 4774 | /// @endcode |
| 4775 | /// |
| 4776 | |
| 4777 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
| 4778 | const ObjCProtocolDecl *PD) { |
| 4779 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4780 | |
| 4781 | // Early exit if a defining object has already been generated. |
| 4782 | if (Entry && Entry->hasInitializer()) |
| 4783 | return Entry; |
| 4784 | |
| 4785 | const char *ProtocolName = PD->getNameAsCString(); |
| 4786 | |
| 4787 | // Construct method lists. |
| 4788 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 4789 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4790 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4791 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4792 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4793 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4794 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4795 | OptInstanceMethods.push_back(C); |
| 4796 | } else { |
| 4797 | InstanceMethods.push_back(C); |
| 4798 | } |
| 4799 | } |
| 4800 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4801 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4802 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4803 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4804 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4805 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4806 | OptClassMethods.push_back(C); |
| 4807 | } else { |
| 4808 | ClassMethods.push_back(C); |
| 4809 | } |
| 4810 | } |
| 4811 | |
| 4812 | std::vector<llvm::Constant*> Values(10); |
| 4813 | // isa is NULL |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 4814 | Values[0] = VMContext.getNullValue(ObjCTypes.ObjectPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4815 | Values[1] = GetClassName(PD->getIdentifier()); |
| 4816 | Values[2] = EmitProtocolList( |
| 4817 | "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(), |
| 4818 | PD->protocol_begin(), |
| 4819 | PD->protocol_end()); |
| 4820 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4821 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4822 | + PD->getNameAsString(), |
| 4823 | "__DATA, __objc_const", |
| 4824 | InstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4825 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4826 | + PD->getNameAsString(), |
| 4827 | "__DATA, __objc_const", |
| 4828 | ClassMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4829 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4830 | + PD->getNameAsString(), |
| 4831 | "__DATA, __objc_const", |
| 4832 | OptInstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4833 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4834 | + PD->getNameAsString(), |
| 4835 | "__DATA, __objc_const", |
| 4836 | OptClassMethods); |
| 4837 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(), |
| 4838 | 0, PD, ObjCTypes); |
| 4839 | uint32_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4840 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 4841 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 4842 | Values[9] = VMContext.getNullValue(ObjCTypes.IntTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4843 | llvm::Constant *Init = VMContext.getConstantStruct(ObjCTypes.ProtocolnfABITy, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4844 | Values); |
| 4845 | |
| 4846 | if (Entry) { |
| 4847 | // Already created, fix the linkage and update the initializer. |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4848 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4849 | Entry->setInitializer(Init); |
| 4850 | } else { |
| 4851 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4852 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4853 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4854 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4855 | std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4856 | Entry->setAlignment( |
| 4857 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4858 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4859 | } |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4860 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4861 | CGM.AddUsedGlobal(Entry); |
| 4862 | |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4863 | // Use this protocol meta-data to build protocol list table in section |
| 4864 | // __DATA, __objc_protolist |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4865 | llvm::GlobalVariable *PTGV = new llvm::GlobalVariable( |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4866 | CGM.getModule(), |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4867 | ObjCTypes.ProtocolnfABIPtrTy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4868 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4869 | Entry, |
| 4870 | std::string("\01l_OBJC_LABEL_PROTOCOL_$_") |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4871 | +ProtocolName); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4872 | PTGV->setAlignment( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4873 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 4874 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4875 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4876 | CGM.AddUsedGlobal(PTGV); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4877 | return Entry; |
| 4878 | } |
| 4879 | |
| 4880 | /// EmitProtocolList - Generate protocol list meta-data: |
| 4881 | /// @code |
| 4882 | /// struct _protocol_list_t { |
| 4883 | /// long protocol_count; // Note, this is 32/64 bit |
| 4884 | /// struct _protocol_t[protocol_count]; |
| 4885 | /// } |
| 4886 | /// @endcode |
| 4887 | /// |
| 4888 | llvm::Constant * |
| 4889 | CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name, |
| 4890 | ObjCProtocolDecl::protocol_iterator begin, |
| 4891 | ObjCProtocolDecl::protocol_iterator end) { |
| 4892 | std::vector<llvm::Constant*> ProtocolRefs; |
| 4893 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4894 | // Just return null for empty protocol lists |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4895 | if (begin == end) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 4896 | return VMContext.getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4897 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4898 | // FIXME: We shouldn't need to do this lookup here, should we? |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4899 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
| 4900 | if (GV) |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4901 | return VMContext.getConstantExprBitCast(GV, |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4902 | ObjCTypes.ProtocolListnfABIPtrTy); |
| 4903 | |
| 4904 | for (; begin != end; ++begin) |
| 4905 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 4906 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4907 | // This list is null terminated. |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 4908 | ProtocolRefs.push_back(VMContext.getNullValue( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4909 | ObjCTypes.ProtocolnfABIPtrTy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4910 | |
| 4911 | std::vector<llvm::Constant*> Values(2); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4912 | Values[0] = |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 4913 | llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4914 | Values[1] = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4915 | VMContext.getConstantArray( |
| 4916 | VMContext.getArrayType(ObjCTypes.ProtocolnfABIPtrTy, |
| 4917 | ProtocolRefs.size()), |
| 4918 | ProtocolRefs); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4919 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4920 | llvm::Constant *Init = VMContext.getConstantStruct(Values); |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4921 | GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4922 | llvm::GlobalValue::InternalLinkage, |
| 4923 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4924 | Name); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4925 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4926 | GV->setAlignment( |
| 4927 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4928 | CGM.AddUsedGlobal(GV); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4929 | return VMContext.getConstantExprBitCast(GV, |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4930 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4931 | } |
| 4932 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4933 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 4934 | /// struct _objc_method { |
| 4935 | /// SEL _cmd; |
| 4936 | /// char *method_type; |
| 4937 | /// char *_imp; |
| 4938 | /// } |
| 4939 | |
| 4940 | llvm::Constant * |
| 4941 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 4942 | std::vector<llvm::Constant*> Desc(3); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4943 | Desc[0] = |
| 4944 | VMContext.getConstantExprBitCast(GetMethodVarName(MD->getSelector()), |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4945 | ObjCTypes.SelectorPtrTy); |
| 4946 | Desc[1] = GetMethodVarType(MD); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4947 | // Protocol methods have no implementation. So, this entry is always NULL. |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 4948 | Desc[2] = VMContext.getNullValue(ObjCTypes.Int8PtrTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4949 | return VMContext.getConstantStruct(ObjCTypes.MethodTy, Desc); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4950 | } |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4951 | |
| 4952 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 4953 | /// This code gen. amounts to generating code for: |
| 4954 | /// @code |
| 4955 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 4956 | /// @encode |
| 4957 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4958 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4959 | CodeGen::CodeGenFunction &CGF, |
| 4960 | QualType ObjectTy, |
| 4961 | llvm::Value *BaseValue, |
| 4962 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4963 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 4964 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4965 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 4966 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4967 | } |
| 4968 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4969 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
| 4970 | CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 4971 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4972 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 4973 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar), |
| 4974 | false, "ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4975 | } |
| 4976 | |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4977 | CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend( |
| 4978 | CodeGen::CodeGenFunction &CGF, |
| 4979 | QualType ResultType, |
| 4980 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4981 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4982 | QualType Arg0Ty, |
| 4983 | bool IsSuper, |
| 4984 | const CallArgList &CallArgs) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 4985 | // FIXME. Even though IsSuper is passes. This function doese not handle calls |
| 4986 | // to 'super' receivers. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4987 | CodeGenTypes &Types = CGM.getTypes(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4988 | llvm::Value *Arg0 = Receiver; |
| 4989 | if (!IsSuper) |
| 4990 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4991 | |
| 4992 | // Find the message function name. |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 4993 | // FIXME. This is too much work to get the ABI-specific result type needed to |
| 4994 | // find the message name. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4995 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, |
| 4996 | llvm::SmallVector<QualType, 16>()); |
Fariborz Jahanian | 70b51c7 | 2009-04-30 23:08:58 +0000 | [diff] [blame] | 4997 | llvm::Constant *Fn = 0; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4998 | std::string Name("\01l_"); |
| 4999 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5000 | #if 0 |
| 5001 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5002 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5003 | Fn = ObjCTypes.getMessageSendIdStretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5004 | // FIXME. Is there a better way of getting these names. |
| 5005 | // They are available in RuntimeFunctions vector pair. |
| 5006 | Name += "objc_msgSendId_stret_fixup"; |
| 5007 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5008 | else |
| 5009 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5010 | if (IsSuper) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5011 | Fn = ObjCTypes.getMessageSendSuper2StretFixupFn(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5012 | Name += "objc_msgSendSuper2_stret_fixup"; |
| 5013 | } |
| 5014 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5015 | { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5016 | Fn = ObjCTypes.getMessageSendStretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5017 | Name += "objc_msgSend_stret_fixup"; |
| 5018 | } |
| 5019 | } |
Fariborz Jahanian | 5b2bad0 | 2009-04-30 16:31:11 +0000 | [diff] [blame] | 5020 | else if (!IsSuper && ResultType->isFloatingType()) { |
Daniel Dunbar | c0183e8 | 2009-06-26 18:32:06 +0000 | [diff] [blame] | 5021 | if (ResultType->isSpecificBuiltinType(BuiltinType::LongDouble)) { |
| 5022 | Fn = ObjCTypes.getMessageSendFpretFixupFn(); |
| 5023 | Name += "objc_msgSend_fpret_fixup"; |
| 5024 | } |
| 5025 | else { |
| 5026 | Fn = ObjCTypes.getMessageSendFixupFn(); |
| 5027 | Name += "objc_msgSend_fixup"; |
Fariborz Jahanian | 5b2bad0 | 2009-04-30 16:31:11 +0000 | [diff] [blame] | 5028 | } |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5029 | } |
| 5030 | else { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5031 | #if 0 |
| 5032 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5033 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5034 | Fn = ObjCTypes.getMessageSendIdFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5035 | Name += "objc_msgSendId_fixup"; |
| 5036 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5037 | else |
| 5038 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5039 | if (IsSuper) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5040 | Fn = ObjCTypes.getMessageSendSuper2FixupFn(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5041 | Name += "objc_msgSendSuper2_fixup"; |
| 5042 | } |
| 5043 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5044 | { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5045 | Fn = ObjCTypes.getMessageSendFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5046 | Name += "objc_msgSend_fixup"; |
| 5047 | } |
| 5048 | } |
Fariborz Jahanian | 70b51c7 | 2009-04-30 23:08:58 +0000 | [diff] [blame] | 5049 | assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5050 | Name += '_'; |
| 5051 | std::string SelName(Sel.getAsString()); |
| 5052 | // Replace all ':' in selector name with '_' ouch! |
| 5053 | for(unsigned i = 0; i < SelName.size(); i++) |
| 5054 | if (SelName[i] == ':') |
| 5055 | SelName[i] = '_'; |
| 5056 | Name += SelName; |
| 5057 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5058 | if (!GV) { |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5059 | // Build message ref table entry. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5060 | std::vector<llvm::Constant*> Values(2); |
| 5061 | Values[0] = Fn; |
| 5062 | Values[1] = GetMethodVarName(Sel); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5063 | llvm::Constant *Init = VMContext.getConstantStruct(Values); |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5064 | GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 5065 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5066 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5067 | Name); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5068 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | f59c1a6 | 2009-04-15 19:04:46 +0000 | [diff] [blame] | 5069 | GV->setAlignment(16); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5070 | GV->setSection("__DATA, __objc_msgrefs, coalesced"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5071 | } |
| 5072 | llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5073 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5074 | CallArgList ActualArgs; |
| 5075 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 5076 | ActualArgs.push_back(std::make_pair(RValue::get(Arg1), |
| 5077 | ObjCTypes.MessageRefCPtrTy)); |
| 5078 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5079 | const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs); |
| 5080 | llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0); |
| 5081 | Callee = CGF.Builder.CreateLoad(Callee); |
Fariborz Jahanian | 3ab75bd | 2009-02-14 21:25:36 +0000 | [diff] [blame] | 5082 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5083 | Callee = CGF.Builder.CreateBitCast(Callee, |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5084 | VMContext.getPointerTypeUnqual(FTy)); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5085 | return CGF.EmitCall(FnInfo1, Callee, ActualArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5086 | } |
| 5087 | |
| 5088 | /// Generate code for a message send expression in the nonfragile abi. |
| 5089 | CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend( |
| 5090 | CodeGen::CodeGenFunction &CGF, |
| 5091 | QualType ResultType, |
| 5092 | Selector Sel, |
| 5093 | llvm::Value *Receiver, |
| 5094 | bool IsClassMessage, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 5095 | const CallArgList &CallArgs, |
| 5096 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5097 | return LegacyDispatchedSelector(Sel) |
| 5098 | ? EmitLegacyMessageSend(CGF, ResultType, EmitSelector(CGF.Builder, Sel), |
| 5099 | Receiver, CGF.getContext().getObjCIdType(), |
| 5100 | false, CallArgs, ObjCTypes) |
| 5101 | : EmitMessageSend(CGF, ResultType, Sel, |
| 5102 | Receiver, CGF.getContext().getObjCIdType(), |
| 5103 | false, CallArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5104 | } |
| 5105 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5106 | llvm::GlobalVariable * |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5107 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5108 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5109 | |
Daniel Dunbar | dfff230 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 5110 | if (!GV) { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5111 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy, |
| 5112 | false, llvm::GlobalValue::ExternalLinkage, |
| 5113 | 0, Name); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5114 | } |
| 5115 | |
| 5116 | return GV; |
| 5117 | } |
| 5118 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5119 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5120 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5121 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 5122 | |
| 5123 | if (!Entry) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5124 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5125 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5126 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5127 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
| 5128 | false, llvm::GlobalValue::InternalLinkage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5129 | ClassGV, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5130 | "\01L_OBJC_CLASSLIST_REFERENCES_$_"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5131 | Entry->setAlignment( |
| 5132 | CGM.getTargetData().getPrefTypeAlignment( |
| 5133 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5134 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5135 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5136 | } |
| 5137 | |
| 5138 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5139 | } |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5140 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5141 | llvm::Value * |
| 5142 | CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder, |
| 5143 | const ObjCInterfaceDecl *ID) { |
| 5144 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
| 5145 | |
| 5146 | if (!Entry) { |
| 5147 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5148 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
| 5149 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5150 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
| 5151 | false, llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5152 | ClassGV, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5153 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5154 | Entry->setAlignment( |
| 5155 | CGM.getTargetData().getPrefTypeAlignment( |
| 5156 | ObjCTypes.ClassnfABIPtrTy)); |
| 5157 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5158 | CGM.AddUsedGlobal(Entry); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5159 | } |
| 5160 | |
| 5161 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5162 | } |
| 5163 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5164 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 5165 | /// meta-data |
| 5166 | /// |
| 5167 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 5168 | const ObjCInterfaceDecl *ID) { |
| 5169 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 5170 | if (Entry) |
| 5171 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5172 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5173 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5174 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5175 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5176 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5177 | llvm::GlobalValue::InternalLinkage, |
| 5178 | MetaClassGV, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5179 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5180 | Entry->setAlignment( |
| 5181 | CGM.getTargetData().getPrefTypeAlignment( |
| 5182 | ObjCTypes.ClassnfABIPtrTy)); |
| 5183 | |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5184 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5185 | CGM.AddUsedGlobal(Entry); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5186 | |
| 5187 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5188 | } |
| 5189 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5190 | /// GetClass - Return a reference to the class for the given interface |
| 5191 | /// decl. |
| 5192 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 5193 | const ObjCInterfaceDecl *ID) { |
| 5194 | return EmitClassRef(Builder, ID); |
| 5195 | } |
| 5196 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5197 | /// Generates a message send where the super is the receiver. This is |
| 5198 | /// a message send to self with special delivery semantics indicating |
| 5199 | /// which class's method should be called. |
| 5200 | CodeGen::RValue |
| 5201 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 5202 | QualType ResultType, |
| 5203 | Selector Sel, |
| 5204 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5205 | bool isCategoryImpl, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5206 | llvm::Value *Receiver, |
| 5207 | bool IsClassMessage, |
| 5208 | const CodeGen::CallArgList &CallArgs) { |
| 5209 | // ... |
| 5210 | // Create and init a super structure; this is a (receiver, class) |
| 5211 | // pair we will pass to objc_msgSendSuper. |
| 5212 | llvm::Value *ObjCSuper = |
| 5213 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 5214 | |
| 5215 | llvm::Value *ReceiverAsObject = |
| 5216 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 5217 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 5218 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
| 5219 | |
| 5220 | // 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] | 5221 | llvm::Value *Target; |
| 5222 | if (IsClassMessage) { |
| 5223 | if (isCategoryImpl) { |
| 5224 | // Message sent to "super' in a class method defined in |
| 5225 | // a category implementation. |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5226 | Target = EmitClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5227 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 5228 | Target = CGF.Builder.CreateLoad(Target); |
| 5229 | } |
| 5230 | else |
| 5231 | Target = EmitMetaClassRef(CGF.Builder, Class); |
| 5232 | } |
| 5233 | else |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5234 | Target = EmitSuperClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5235 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5236 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 5237 | // ObjCTypes types. |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5238 | const llvm::Type *ClassTy = |
| 5239 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 5240 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 5241 | CGF.Builder.CreateStore(Target, |
| 5242 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 5243 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5244 | return (LegacyDispatchedSelector(Sel)) |
| 5245 | ? EmitLegacyMessageSend(CGF, ResultType,EmitSelector(CGF.Builder, Sel), |
| 5246 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 5247 | true, CallArgs, |
| 5248 | ObjCTypes) |
| 5249 | : EmitMessageSend(CGF, ResultType, Sel, |
| 5250 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 5251 | true, CallArgs); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5252 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5253 | |
| 5254 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
| 5255 | Selector Sel) { |
| 5256 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 5257 | |
| 5258 | if (!Entry) { |
| 5259 | llvm::Constant *Casted = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5260 | VMContext.getConstantExprBitCast(GetMethodVarName(Sel), |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5261 | ObjCTypes.SelectorPtrTy); |
| 5262 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5263 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false, |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5264 | llvm::GlobalValue::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5265 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5266 | Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5267 | CGM.AddUsedGlobal(Entry); |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5268 | } |
| 5269 | |
| 5270 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5271 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5272 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 5273 | /// objc_assign_ivar (id src, id *dst) |
| 5274 | /// |
| 5275 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 5276 | llvm::Value *src, llvm::Value *dst) |
| 5277 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5278 | const llvm::Type * SrcTy = src->getType(); |
| 5279 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5280 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5281 | assert(Size <= 8 && "does not support size > 8"); |
| 5282 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5283 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5284 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5285 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5286 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5287 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5288 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5289 | src, dst, "assignivar"); |
| 5290 | return; |
| 5291 | } |
| 5292 | |
| 5293 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 5294 | /// objc_assign_strongCast (id src, id *dst) |
| 5295 | /// |
| 5296 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
| 5297 | CodeGen::CodeGenFunction &CGF, |
| 5298 | llvm::Value *src, llvm::Value *dst) |
| 5299 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5300 | const llvm::Type * SrcTy = src->getType(); |
| 5301 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5302 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5303 | assert(Size <= 8 && "does not support size > 8"); |
| 5304 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5305 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5306 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5307 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5308 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5309 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5310 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5311 | src, dst, "weakassign"); |
| 5312 | return; |
| 5313 | } |
| 5314 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 5315 | void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable( |
| 5316 | CodeGen::CodeGenFunction &CGF, |
| 5317 | llvm::Value *DestPtr, |
| 5318 | llvm::Value *SrcPtr, |
| 5319 | unsigned long size) { |
| 5320 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 5321 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 5322 | llvm::Value *N = llvm::ConstantInt::get(ObjCTypes.LongTy, size); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 5323 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), |
| 5324 | DestPtr, SrcPtr, N); |
| 5325 | return; |
| 5326 | } |
| 5327 | |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5328 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 5329 | /// object: objc_read_weak (id *src) |
| 5330 | /// |
| 5331 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
| 5332 | CodeGen::CodeGenFunction &CGF, |
| 5333 | llvm::Value *AddrWeakObj) |
| 5334 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5335 | const llvm::Type* DestTy = |
| 5336 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5337 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 5338 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5339 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5340 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5341 | return read_weak; |
| 5342 | } |
| 5343 | |
| 5344 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 5345 | /// objc_assign_weak (id src, id *dst) |
| 5346 | /// |
| 5347 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 5348 | llvm::Value *src, llvm::Value *dst) |
| 5349 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5350 | const llvm::Type * SrcTy = src->getType(); |
| 5351 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5352 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5353 | assert(Size <= 8 && "does not support size > 8"); |
| 5354 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5355 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5356 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5357 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5358 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5359 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 5360 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5361 | src, dst, "weakassign"); |
| 5362 | return; |
| 5363 | } |
| 5364 | |
| 5365 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 5366 | /// objc_assign_global (id src, id *dst) |
| 5367 | /// |
| 5368 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 5369 | llvm::Value *src, llvm::Value *dst) |
| 5370 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5371 | const llvm::Type * SrcTy = src->getType(); |
| 5372 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5373 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5374 | assert(Size <= 8 && "does not support size > 8"); |
| 5375 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5376 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5377 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5378 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5379 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5380 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5381 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5382 | src, dst, "globalassign"); |
| 5383 | return; |
| 5384 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5385 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5386 | void |
| 5387 | CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 5388 | const Stmt &S) { |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5389 | bool isTry = isa<ObjCAtTryStmt>(S); |
| 5390 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 5391 | llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5392 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5393 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5394 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5395 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
| 5396 | |
| 5397 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 5398 | // evaluation of the expression must occur before we enter the |
| 5399 | // @synchronized. We can safely avoid a temp here because jumps into |
| 5400 | // @synchronized are illegal & this will dominate uses. |
| 5401 | llvm::Value *SyncArg = 0; |
| 5402 | if (!isTry) { |
| 5403 | SyncArg = |
| 5404 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 5405 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5406 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5407 | } |
| 5408 | |
| 5409 | // Push an EH context entry, used for handling rethrows and jumps |
| 5410 | // through finally. |
| 5411 | CGF.PushCleanupBlock(FinallyBlock); |
| 5412 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5413 | CGF.setInvokeDest(TryHandler); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5414 | |
| 5415 | CGF.EmitBlock(TryBlock); |
| 5416 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 5417 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
| 5418 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5419 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5420 | // Emit the exception handler. |
| 5421 | |
| 5422 | CGF.EmitBlock(TryHandler); |
| 5423 | |
| 5424 | llvm::Value *llvm_eh_exception = |
| 5425 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
| 5426 | llvm::Value *llvm_eh_selector_i64 = |
| 5427 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64); |
| 5428 | llvm::Value *llvm_eh_typeid_for_i64 = |
| 5429 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64); |
| 5430 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5431 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow"); |
| 5432 | |
| 5433 | llvm::SmallVector<llvm::Value*, 8> SelectorArgs; |
| 5434 | SelectorArgs.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5435 | SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5436 | |
| 5437 | // Construct the lists of (type, catch body) to handle. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5438 | llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5439 | bool HasCatchAll = false; |
| 5440 | if (isTry) { |
| 5441 | if (const ObjCAtCatchStmt* CatchStmt = |
| 5442 | cast<ObjCAtTryStmt>(S).getCatchStmts()) { |
| 5443 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5444 | const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5445 | Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody())); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5446 | |
| 5447 | // catch(...) always matches. |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5448 | if (!CatchDecl) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5449 | // Use i8* null here to signal this is a catch all, not a cleanup. |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 5450 | llvm::Value *Null = VMContext.getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5451 | SelectorArgs.push_back(Null); |
| 5452 | HasCatchAll = true; |
| 5453 | break; |
| 5454 | } |
| 5455 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5456 | if (CatchDecl->getType()->isObjCIdType() || |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5457 | CatchDecl->getType()->isObjCQualifiedIdType()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5458 | llvm::Value *IDEHType = |
| 5459 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 5460 | if (!IDEHType) |
| 5461 | IDEHType = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5462 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, |
| 5463 | false, |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5464 | llvm::GlobalValue::ExternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5465 | 0, "OBJC_EHTYPE_id"); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5466 | SelectorArgs.push_back(IDEHType); |
| 5467 | HasCatchAll = true; |
| 5468 | break; |
| 5469 | } |
| 5470 | |
| 5471 | // All other types should be Objective-C interface pointer types. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5472 | const ObjCObjectPointerType *PT = |
| 5473 | CatchDecl->getType()->getAsObjCObjectPointerType(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5474 | assert(PT && "Invalid @catch type."); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5475 | const ObjCInterfaceType *IT = PT->getInterfaceType(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5476 | assert(IT && "Invalid @catch type."); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5477 | llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5478 | SelectorArgs.push_back(EHType); |
| 5479 | } |
| 5480 | } |
| 5481 | } |
| 5482 | |
| 5483 | // We use a cleanup unless there was already a catch all. |
| 5484 | if (!HasCatchAll) { |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 5485 | SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5486 | Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5487 | } |
| 5488 | |
| 5489 | llvm::Value *Selector = |
| 5490 | CGF.Builder.CreateCall(llvm_eh_selector_i64, |
| 5491 | SelectorArgs.begin(), SelectorArgs.end(), |
| 5492 | "selector"); |
| 5493 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5494 | const ParmVarDecl *CatchParam = Handlers[i].first; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5495 | const Stmt *CatchBody = Handlers[i].second; |
| 5496 | |
| 5497 | llvm::BasicBlock *Next = 0; |
| 5498 | |
| 5499 | // The last handler always matches. |
| 5500 | if (i + 1 != e) { |
| 5501 | assert(CatchParam && "Only last handler can be a catch all."); |
| 5502 | |
| 5503 | llvm::BasicBlock *Match = CGF.createBasicBlock("match"); |
| 5504 | Next = CGF.createBasicBlock("catch.next"); |
| 5505 | llvm::Value *Id = |
| 5506 | CGF.Builder.CreateCall(llvm_eh_typeid_for_i64, |
| 5507 | CGF.Builder.CreateBitCast(SelectorArgs[i+2], |
| 5508 | ObjCTypes.Int8PtrTy)); |
| 5509 | CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id), |
| 5510 | Match, Next); |
| 5511 | |
| 5512 | CGF.EmitBlock(Match); |
| 5513 | } |
| 5514 | |
| 5515 | if (CatchBody) { |
| 5516 | llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end"); |
| 5517 | llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler"); |
| 5518 | |
| 5519 | // Cleanups must call objc_end_catch. |
| 5520 | // |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5521 | // FIXME: It seems incorrect for objc_begin_catch to be inside this |
| 5522 | // context, but this matches gcc. |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5523 | CGF.PushCleanupBlock(MatchEnd); |
| 5524 | CGF.setInvokeDest(MatchHandler); |
| 5525 | |
| 5526 | llvm::Value *ExcObject = |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5527 | CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5528 | |
| 5529 | // Bind the catch parameter if it exists. |
| 5530 | if (CatchParam) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5531 | ExcObject = |
| 5532 | CGF.Builder.CreateBitCast(ExcObject, |
| 5533 | CGF.ConvertType(CatchParam->getType())); |
| 5534 | // CatchParam is a ParmVarDecl because of the grammar |
| 5535 | // construction used to handle this, but for codegen purposes |
| 5536 | // we treat this as a local decl. |
| 5537 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
| 5538 | CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5539 | } |
| 5540 | |
| 5541 | CGF.ObjCEHValueStack.push_back(ExcObject); |
| 5542 | CGF.EmitStmt(CatchBody); |
| 5543 | CGF.ObjCEHValueStack.pop_back(); |
| 5544 | |
| 5545 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5546 | |
| 5547 | CGF.EmitBlock(MatchHandler); |
| 5548 | |
| 5549 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5550 | // We are required to emit this call to satisfy LLVM, even |
| 5551 | // though we don't use the result. |
| 5552 | llvm::SmallVector<llvm::Value*, 8> Args; |
| 5553 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5554 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 5555 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5556 | 0)); |
| 5557 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5558 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5559 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5560 | |
| 5561 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5562 | |
| 5563 | CGF.EmitBlock(MatchEnd); |
| 5564 | |
| 5565 | // Unfortunately, we also have to generate another EH frame here |
| 5566 | // in case this throws. |
| 5567 | llvm::BasicBlock *MatchEndHandler = |
| 5568 | CGF.createBasicBlock("match.end.handler"); |
| 5569 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5570 | CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5571 | Cont, MatchEndHandler, |
| 5572 | Args.begin(), Args.begin()); |
| 5573 | |
| 5574 | CGF.EmitBlock(Cont); |
| 5575 | if (Info.SwitchBlock) |
| 5576 | CGF.EmitBlock(Info.SwitchBlock); |
| 5577 | if (Info.EndBlock) |
| 5578 | CGF.EmitBlock(Info.EndBlock); |
| 5579 | |
| 5580 | CGF.EmitBlock(MatchEndHandler); |
| 5581 | Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5582 | // We are required to emit this call to satisfy LLVM, even |
| 5583 | // though we don't use the result. |
| 5584 | Args.clear(); |
| 5585 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5586 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 5587 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5588 | 0)); |
| 5589 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5590 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5591 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5592 | |
| 5593 | if (Next) |
| 5594 | CGF.EmitBlock(Next); |
| 5595 | } else { |
| 5596 | assert(!Next && "catchup should be last handler."); |
| 5597 | |
| 5598 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5599 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5600 | } |
| 5601 | } |
| 5602 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5603 | // Pop the cleanup entry, the @finally is outside this cleanup |
| 5604 | // scope. |
| 5605 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5606 | CGF.setInvokeDest(PrevLandingPad); |
| 5607 | |
| 5608 | CGF.EmitBlock(FinallyBlock); |
| 5609 | |
| 5610 | if (isTry) { |
| 5611 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 5612 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 5613 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 5614 | } else { |
| 5615 | // Emit 'objc_sync_exit(expr)' as finally's sole statement for |
| 5616 | // @synchronized. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5617 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5618 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5619 | |
| 5620 | if (Info.SwitchBlock) |
| 5621 | CGF.EmitBlock(Info.SwitchBlock); |
| 5622 | if (Info.EndBlock) |
| 5623 | CGF.EmitBlock(Info.EndBlock); |
| 5624 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5625 | // Branch around the rethrow code. |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5626 | CGF.EmitBranch(FinallyEnd); |
| 5627 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5628 | CGF.EmitBlock(FinallyRethrow); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5629 | CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5630 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5631 | CGF.Builder.CreateUnreachable(); |
| 5632 | |
| 5633 | CGF.EmitBlock(FinallyEnd); |
| 5634 | } |
| 5635 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5636 | /// EmitThrowStmt - Generate code for a throw statement. |
| 5637 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 5638 | const ObjCAtThrowStmt &S) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5639 | llvm::Value *Exception; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5640 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5641 | Exception = CGF.EmitScalarExpr(ThrowExpr); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5642 | } else { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5643 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
| 5644 | "Unexpected rethrow outside @catch block."); |
| 5645 | Exception = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5646 | } |
| 5647 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5648 | llvm::Value *ExceptionAsObject = |
| 5649 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 5650 | llvm::BasicBlock *InvokeDest = CGF.getInvokeDest(); |
| 5651 | if (InvokeDest) { |
| 5652 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5653 | CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5654 | Cont, InvokeDest, |
| 5655 | &ExceptionAsObject, &ExceptionAsObject + 1); |
| 5656 | CGF.EmitBlock(Cont); |
| 5657 | } else |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5658 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5659 | CGF.Builder.CreateUnreachable(); |
| 5660 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5661 | // Clear the insertion point to indicate we are in unreachable code. |
| 5662 | CGF.Builder.ClearInsertionPoint(); |
| 5663 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5664 | |
| 5665 | llvm::Value * |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5666 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 5667 | bool ForDefinition) { |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5668 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5669 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5670 | // If we don't need a definition, return the entry if found or check |
| 5671 | // if we use an external reference. |
| 5672 | if (!ForDefinition) { |
| 5673 | if (Entry) |
| 5674 | return Entry; |
Daniel Dunbar | 7e075cb | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 5675 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5676 | // If this type (or a super class) has the __objc_exception__ |
| 5677 | // attribute, emit an external reference. |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 5678 | if (hasObjCExceptionAttribute(CGM.getContext(), ID)) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5679 | return Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5680 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5681 | llvm::GlobalValue::ExternalLinkage, |
| 5682 | 0, |
| 5683 | (std::string("OBJC_EHTYPE_$_") + |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5684 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5685 | } |
| 5686 | |
| 5687 | // Otherwise we need to either make a new entry or fill in the |
| 5688 | // initializer. |
| 5689 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5690 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5691 | std::string VTableName = "objc_ehtype_vtable"; |
| 5692 | llvm::GlobalVariable *VTableGV = |
| 5693 | CGM.getModule().getGlobalVariable(VTableName); |
| 5694 | if (!VTableGV) |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5695 | VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy, |
| 5696 | false, |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5697 | llvm::GlobalValue::ExternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5698 | 0, VTableName); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5699 | |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame^] | 5700 | llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5701 | |
| 5702 | std::vector<llvm::Constant*> Values(3); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5703 | Values[0] = VMContext.getConstantExprGetElementPtr(VTableGV, &VTableIdx, 1); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5704 | Values[1] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5705 | Values[2] = GetClassGlobal(ClassName); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5706 | llvm::Constant *Init = |
| 5707 | VMContext.getConstantStruct(ObjCTypes.EHTypeTy, Values); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5708 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5709 | if (Entry) { |
| 5710 | Entry->setInitializer(Init); |
| 5711 | } else { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5712 | Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5713 | llvm::GlobalValue::WeakAnyLinkage, |
| 5714 | Init, |
| 5715 | (std::string("OBJC_EHTYPE_$_") + |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5716 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5717 | } |
| 5718 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 5719 | if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden) |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5720 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5721 | Entry->setAlignment(8); |
| 5722 | |
| 5723 | if (ForDefinition) { |
| 5724 | Entry->setSection("__DATA,__objc_const"); |
| 5725 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 5726 | } else { |
| 5727 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 5728 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5729 | |
| 5730 | return Entry; |
| 5731 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5732 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5733 | /* *** */ |
| 5734 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5735 | CodeGen::CGObjCRuntime * |
| 5736 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 5737 | return new CGObjCMac(CGM); |
| 5738 | } |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5739 | |
| 5740 | CodeGen::CGObjCRuntime * |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5741 | CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5742 | return new CGObjCNonFragileABIMac(CGM); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5743 | } |