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" |
Daniel Dunbar | 2bebbf0 | 2009-05-03 10:46:44 +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) |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 104 | llvm::Type *I8Ptr = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 105 | QualType IvarTy = Ivar->getType(); |
| 106 | const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 107 | llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 108 | V = CGF.Builder.CreateGEP(V, Offset, "add.ptr"); |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 109 | V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy)); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 110 | |
| 111 | if (Ivar->isBitField()) { |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 112 | // We need to compute the bit offset for the bit-field, the offset |
| 113 | // is to the byte. Note, there is a subtle invariant here: we can |
| 114 | // only call this routine on non-sythesized ivars but we may be |
| 115 | // called for synthesized ivars. However, a synthesized ivar can |
| 116 | // never be a bit-field so this is safe. |
| 117 | uint64_t BitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar) % 8; |
| 118 | |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 119 | uint64_t BitFieldSize = |
| 120 | Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue(); |
| 121 | return LValue::MakeBitfield(V, BitOffset, BitFieldSize, |
Daniel Dunbar | e38df86 | 2009-05-03 07:52:00 +0000 | [diff] [blame] | 122 | IvarTy->isSignedIntegerType(), |
| 123 | IvarTy.getCVRQualifiers()|CVRQualifiers); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 126 | LValue LV = LValue::MakeAddr(V, IvarTy.getCVRQualifiers()|CVRQualifiers, |
| 127 | CGF.CGM.getContext().getObjCGCAttrKind(IvarTy)); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 128 | LValue::SetObjCIvar(LV, true); |
| 129 | return LV; |
| 130 | } |
| 131 | |
| 132 | /// |
| 133 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 134 | namespace { |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 135 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 136 | typedef std::vector<llvm::Constant*> ConstantVector; |
| 137 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 138 | // FIXME: We should find a nicer way to make the labels for metadata, string |
| 139 | // concatenation is lame. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 140 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 141 | class ObjCCommonTypesHelper { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 142 | private: |
| 143 | llvm::Constant *getMessageSendFn() const { |
| 144 | // id objc_msgSend (id, SEL, ...) |
| 145 | std::vector<const llvm::Type*> Params; |
| 146 | Params.push_back(ObjectPtrTy); |
| 147 | Params.push_back(SelectorPtrTy); |
| 148 | return |
| 149 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 150 | Params, true), |
| 151 | "objc_msgSend"); |
| 152 | } |
| 153 | |
| 154 | llvm::Constant *getMessageSendStretFn() const { |
| 155 | // id objc_msgSend_stret (id, SEL, ...) |
| 156 | std::vector<const llvm::Type*> Params; |
| 157 | Params.push_back(ObjectPtrTy); |
| 158 | Params.push_back(SelectorPtrTy); |
| 159 | return |
| 160 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 161 | Params, true), |
| 162 | "objc_msgSend_stret"); |
| 163 | |
| 164 | } |
| 165 | |
| 166 | llvm::Constant *getMessageSendFpretFn() const { |
| 167 | // FIXME: This should be long double on x86_64? |
| 168 | // [double | long double] objc_msgSend_fpret(id self, SEL op, ...) |
| 169 | std::vector<const llvm::Type*> Params; |
| 170 | Params.push_back(ObjectPtrTy); |
| 171 | Params.push_back(SelectorPtrTy); |
| 172 | return |
| 173 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy, |
| 174 | Params, |
| 175 | true), |
| 176 | "objc_msgSend_fpret"); |
| 177 | |
| 178 | } |
| 179 | |
| 180 | llvm::Constant *getMessageSendSuperFn() const { |
| 181 | // id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
| 182 | const char *SuperName = "objc_msgSendSuper"; |
| 183 | std::vector<const llvm::Type*> Params; |
| 184 | Params.push_back(SuperPtrTy); |
| 185 | Params.push_back(SelectorPtrTy); |
| 186 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 187 | Params, true), |
| 188 | SuperName); |
| 189 | } |
| 190 | |
| 191 | llvm::Constant *getMessageSendSuperFn2() const { |
| 192 | // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...) |
| 193 | const char *SuperName = "objc_msgSendSuper2"; |
| 194 | std::vector<const llvm::Type*> Params; |
| 195 | Params.push_back(SuperPtrTy); |
| 196 | Params.push_back(SelectorPtrTy); |
| 197 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 198 | Params, true), |
| 199 | SuperName); |
| 200 | } |
| 201 | |
| 202 | llvm::Constant *getMessageSendSuperStretFn() const { |
| 203 | // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super, |
| 204 | // SEL op, ...) |
| 205 | std::vector<const llvm::Type*> Params; |
| 206 | Params.push_back(Int8PtrTy); |
| 207 | Params.push_back(SuperPtrTy); |
| 208 | Params.push_back(SelectorPtrTy); |
| 209 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 210 | Params, true), |
| 211 | "objc_msgSendSuper_stret"); |
| 212 | } |
| 213 | |
| 214 | llvm::Constant *getMessageSendSuperStretFn2() const { |
| 215 | // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super, |
| 216 | // SEL op, ...) |
| 217 | std::vector<const llvm::Type*> Params; |
| 218 | Params.push_back(Int8PtrTy); |
| 219 | Params.push_back(SuperPtrTy); |
| 220 | Params.push_back(SelectorPtrTy); |
| 221 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 222 | Params, true), |
| 223 | "objc_msgSendSuper2_stret"); |
| 224 | } |
| 225 | |
| 226 | llvm::Constant *getMessageSendSuperFpretFn() const { |
| 227 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 228 | return getMessageSendSuperFn(); |
| 229 | } |
| 230 | |
| 231 | llvm::Constant *getMessageSendSuperFpretFn2() const { |
| 232 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 233 | return getMessageSendSuperFn2(); |
| 234 | } |
| 235 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 236 | protected: |
| 237 | CodeGen::CodeGenModule &CGM; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 238 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 239 | public: |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 240 | const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 241 | const llvm::Type *Int8PtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 242 | |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 243 | /// ObjectPtrTy - LLVM type for object handles (typeof(id)) |
| 244 | const llvm::Type *ObjectPtrTy; |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 245 | |
| 246 | /// PtrObjectPtrTy - LLVM type for id * |
| 247 | const llvm::Type *PtrObjectPtrTy; |
| 248 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 249 | /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL)) |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 250 | const llvm::Type *SelectorPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 251 | /// ProtocolPtrTy - LLVM type for external protocol handles |
| 252 | /// (typeof(Protocol)) |
| 253 | const llvm::Type *ExternalProtocolPtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 254 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 255 | // SuperCTy - clang type for struct objc_super. |
| 256 | QualType SuperCTy; |
| 257 | // SuperPtrCTy - clang type for struct objc_super *. |
| 258 | QualType SuperPtrCTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 259 | |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 260 | /// SuperTy - LLVM type for struct objc_super. |
| 261 | const llvm::StructType *SuperTy; |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 262 | /// SuperPtrTy - LLVM type for struct objc_super *. |
| 263 | const llvm::Type *SuperPtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 264 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 265 | /// PropertyTy - LLVM type for struct objc_property (struct _prop_t |
| 266 | /// in GCC parlance). |
| 267 | const llvm::StructType *PropertyTy; |
| 268 | |
| 269 | /// PropertyListTy - LLVM type for struct objc_property_list |
| 270 | /// (_prop_list_t in GCC parlance). |
| 271 | const llvm::StructType *PropertyListTy; |
| 272 | /// PropertyListPtrTy - LLVM type for struct objc_property_list*. |
| 273 | const llvm::Type *PropertyListPtrTy; |
| 274 | |
| 275 | // MethodTy - LLVM type for struct objc_method. |
| 276 | const llvm::StructType *MethodTy; |
| 277 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 278 | /// CacheTy - LLVM type for struct objc_cache. |
| 279 | const llvm::Type *CacheTy; |
| 280 | /// CachePtrTy - LLVM type for struct objc_cache *. |
| 281 | const llvm::Type *CachePtrTy; |
| 282 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 283 | llvm::Constant *getGetPropertyFn() { |
| 284 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 285 | ASTContext &Ctx = CGM.getContext(); |
| 286 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
| 287 | llvm::SmallVector<QualType,16> Params; |
| 288 | QualType IdType = Ctx.getObjCIdType(); |
| 289 | QualType SelType = Ctx.getObjCSelType(); |
| 290 | Params.push_back(IdType); |
| 291 | Params.push_back(SelType); |
| 292 | Params.push_back(Ctx.LongTy); |
| 293 | Params.push_back(Ctx.BoolTy); |
| 294 | const llvm::FunctionType *FTy = |
| 295 | Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false); |
| 296 | return CGM.CreateRuntimeFunction(FTy, "objc_getProperty"); |
| 297 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 298 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 299 | llvm::Constant *getSetPropertyFn() { |
| 300 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 301 | ASTContext &Ctx = CGM.getContext(); |
| 302 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
| 303 | llvm::SmallVector<QualType,16> Params; |
| 304 | QualType IdType = Ctx.getObjCIdType(); |
| 305 | QualType SelType = Ctx.getObjCSelType(); |
| 306 | Params.push_back(IdType); |
| 307 | Params.push_back(SelType); |
| 308 | Params.push_back(Ctx.LongTy); |
| 309 | Params.push_back(IdType); |
| 310 | Params.push_back(Ctx.BoolTy); |
| 311 | Params.push_back(Ctx.BoolTy); |
| 312 | const llvm::FunctionType *FTy = |
| 313 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
| 314 | return CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); |
| 315 | } |
| 316 | |
| 317 | llvm::Constant *getEnumerationMutationFn() { |
Daniel Dunbar | c1ab900 | 2009-07-11 20:32:50 +0000 | [diff] [blame] | 318 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 319 | ASTContext &Ctx = CGM.getContext(); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 320 | // void objc_enumerationMutation (id) |
Daniel Dunbar | c1ab900 | 2009-07-11 20:32:50 +0000 | [diff] [blame] | 321 | llvm::SmallVector<QualType,16> Params; |
| 322 | QualType IdType = Ctx.getObjCIdType(); |
| 323 | Params.push_back(IdType); |
| 324 | const llvm::FunctionType *FTy = |
| 325 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 326 | return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation"); |
| 327 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 328 | |
| 329 | /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 330 | llvm::Constant *getGcReadWeakFn() { |
| 331 | // id objc_read_weak (id *) |
| 332 | std::vector<const llvm::Type*> Args; |
| 333 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 334 | llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 335 | return CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
| 336 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 337 | |
| 338 | /// GcAssignWeakFn -- LLVM objc_assign_weak function. |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 339 | llvm::Constant *getGcAssignWeakFn() { |
| 340 | // id objc_assign_weak (id, id *) |
| 341 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 342 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 343 | llvm::FunctionType *FTy = |
| 344 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 345 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 346 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 347 | |
| 348 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 349 | llvm::Constant *getGcAssignGlobalFn() { |
| 350 | // id objc_assign_global(id, id *) |
| 351 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 352 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 353 | llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 354 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 355 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 356 | |
| 357 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 358 | llvm::Constant *getGcAssignIvarFn() { |
| 359 | // id objc_assign_ivar(id, id *) |
| 360 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 361 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 362 | llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 363 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 364 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 365 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 366 | /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function. |
| 367 | llvm::Constant *GcMemmoveCollectableFn() { |
| 368 | // void *objc_memmove_collectable(void *dst, const void *src, size_t size) |
| 369 | std::vector<const llvm::Type*> Args(1, Int8PtrTy); |
| 370 | Args.push_back(Int8PtrTy); |
| 371 | Args.push_back(LongTy); |
| 372 | llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false); |
| 373 | return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable"); |
| 374 | } |
| 375 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 376 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 377 | llvm::Constant *getGcAssignStrongCastFn() { |
| 378 | // id objc_assign_global(id, id *) |
| 379 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 380 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 381 | llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 382 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); |
| 383 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 384 | |
| 385 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 386 | llvm::Constant *getExceptionThrowFn() { |
| 387 | // void objc_exception_throw(id) |
| 388 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 389 | llvm::FunctionType *FTy = |
| 390 | llvm::FunctionType::get(llvm::Type::VoidTy, Args, false); |
| 391 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
| 392 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 393 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 394 | /// SyncEnterFn - LLVM object_sync_enter function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 395 | llvm::Constant *getSyncEnterFn() { |
| 396 | // void objc_sync_enter (id) |
| 397 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 398 | llvm::FunctionType *FTy = |
| 399 | llvm::FunctionType::get(llvm::Type::VoidTy, Args, false); |
| 400 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 401 | } |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 402 | |
| 403 | /// SyncExitFn - LLVM object_sync_exit function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 404 | llvm::Constant *getSyncExitFn() { |
| 405 | // void objc_sync_exit (id) |
| 406 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 407 | llvm::FunctionType *FTy = |
| 408 | llvm::FunctionType::get(llvm::Type::VoidTy, Args, false); |
| 409 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
| 410 | } |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 411 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 412 | llvm::Constant *getSendFn(bool IsSuper) const { |
| 413 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFn(); |
| 414 | } |
| 415 | |
| 416 | llvm::Constant *getSendFn2(bool IsSuper) const { |
| 417 | return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn(); |
| 418 | } |
| 419 | |
| 420 | llvm::Constant *getSendStretFn(bool IsSuper) const { |
| 421 | return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn(); |
| 422 | } |
| 423 | |
| 424 | llvm::Constant *getSendStretFn2(bool IsSuper) const { |
| 425 | return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn(); |
| 426 | } |
| 427 | |
| 428 | llvm::Constant *getSendFpretFn(bool IsSuper) const { |
| 429 | return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn(); |
| 430 | } |
| 431 | |
| 432 | llvm::Constant *getSendFpretFn2(bool IsSuper) const { |
| 433 | return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn(); |
| 434 | } |
| 435 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 436 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
| 437 | ~ObjCCommonTypesHelper(){} |
| 438 | }; |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 439 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 440 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 441 | /// construction of varies types used during ObjC generation. |
| 442 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 443 | public: |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 444 | /// SymtabTy - LLVM type for struct objc_symtab. |
| 445 | const llvm::StructType *SymtabTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 446 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
| 447 | const llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 448 | /// ModuleTy - LLVM type for struct objc_module. |
| 449 | const llvm::StructType *ModuleTy; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 450 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 451 | /// ProtocolTy - LLVM type for struct objc_protocol. |
| 452 | const llvm::StructType *ProtocolTy; |
| 453 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
| 454 | const llvm::Type *ProtocolPtrTy; |
| 455 | /// ProtocolExtensionTy - LLVM type for struct |
| 456 | /// objc_protocol_extension. |
| 457 | const llvm::StructType *ProtocolExtensionTy; |
| 458 | /// ProtocolExtensionTy - LLVM type for struct |
| 459 | /// objc_protocol_extension *. |
| 460 | const llvm::Type *ProtocolExtensionPtrTy; |
| 461 | /// MethodDescriptionTy - LLVM type for struct |
| 462 | /// objc_method_description. |
| 463 | const llvm::StructType *MethodDescriptionTy; |
| 464 | /// MethodDescriptionListTy - LLVM type for struct |
| 465 | /// objc_method_description_list. |
| 466 | const llvm::StructType *MethodDescriptionListTy; |
| 467 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 468 | /// objc_method_description_list *. |
| 469 | const llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 470 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
| 471 | const llvm::Type *ProtocolListTy; |
| 472 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
| 473 | const llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 474 | /// CategoryTy - LLVM type for struct objc_category. |
| 475 | const llvm::StructType *CategoryTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 476 | /// ClassTy - LLVM type for struct objc_class. |
| 477 | const llvm::StructType *ClassTy; |
| 478 | /// ClassPtrTy - LLVM type for struct objc_class *. |
| 479 | const llvm::Type *ClassPtrTy; |
| 480 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
| 481 | const llvm::StructType *ClassExtensionTy; |
| 482 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
| 483 | const llvm::Type *ClassExtensionPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 484 | // IvarTy - LLVM type for struct objc_ivar. |
| 485 | const llvm::StructType *IvarTy; |
| 486 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
| 487 | const llvm::Type *IvarListTy; |
| 488 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
| 489 | const llvm::Type *IvarListPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 490 | /// MethodListTy - LLVM type for struct objc_method_list. |
| 491 | const llvm::Type *MethodListTy; |
| 492 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
| 493 | const llvm::Type *MethodListPtrTy; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 494 | |
| 495 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
| 496 | const llvm::Type *ExceptionDataTy; |
| 497 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 498 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 499 | llvm::Constant *getExceptionTryEnterFn() { |
| 500 | std::vector<const llvm::Type*> Params; |
| 501 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 502 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 503 | Params, false), |
| 504 | "objc_exception_try_enter"); |
| 505 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 506 | |
| 507 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 508 | llvm::Constant *getExceptionTryExitFn() { |
| 509 | std::vector<const llvm::Type*> Params; |
| 510 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 511 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 512 | Params, false), |
| 513 | "objc_exception_try_exit"); |
| 514 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 515 | |
| 516 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 517 | llvm::Constant *getExceptionExtractFn() { |
| 518 | std::vector<const llvm::Type*> Params; |
| 519 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 520 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 521 | Params, false), |
| 522 | "objc_exception_extract"); |
| 523 | |
| 524 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 525 | |
| 526 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 527 | llvm::Constant *getExceptionMatchFn() { |
| 528 | std::vector<const llvm::Type*> Params; |
| 529 | Params.push_back(ClassPtrTy); |
| 530 | Params.push_back(ObjectPtrTy); |
| 531 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 532 | Params, false), |
| 533 | "objc_exception_match"); |
| 534 | |
| 535 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 536 | |
| 537 | /// SetJmpFn - LLVM _setjmp function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 538 | llvm::Constant *getSetJmpFn() { |
| 539 | std::vector<const llvm::Type*> Params; |
| 540 | Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty)); |
| 541 | return |
| 542 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 543 | Params, false), |
| 544 | "_setjmp"); |
| 545 | |
| 546 | } |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 547 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 548 | public: |
| 549 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 550 | ~ObjCTypesHelper() {} |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 551 | }; |
| 552 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 553 | /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 554 | /// modern abi |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 555 | class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 556 | public: |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 557 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 558 | // MethodListnfABITy - LLVM for struct _method_list_t |
| 559 | const llvm::StructType *MethodListnfABITy; |
| 560 | |
| 561 | // MethodListnfABIPtrTy - LLVM for struct _method_list_t* |
| 562 | const llvm::Type *MethodListnfABIPtrTy; |
| 563 | |
| 564 | // ProtocolnfABITy = LLVM for struct _protocol_t |
| 565 | const llvm::StructType *ProtocolnfABITy; |
| 566 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 567 | // ProtocolnfABIPtrTy = LLVM for struct _protocol_t* |
| 568 | const llvm::Type *ProtocolnfABIPtrTy; |
| 569 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 570 | // ProtocolListnfABITy - LLVM for struct _objc_protocol_list |
| 571 | const llvm::StructType *ProtocolListnfABITy; |
| 572 | |
| 573 | // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list* |
| 574 | const llvm::Type *ProtocolListnfABIPtrTy; |
| 575 | |
| 576 | // ClassnfABITy - LLVM for struct _class_t |
| 577 | const llvm::StructType *ClassnfABITy; |
| 578 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 579 | // ClassnfABIPtrTy - LLVM for struct _class_t* |
| 580 | const llvm::Type *ClassnfABIPtrTy; |
| 581 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 582 | // IvarnfABITy - LLVM for struct _ivar_t |
| 583 | const llvm::StructType *IvarnfABITy; |
| 584 | |
| 585 | // IvarListnfABITy - LLVM for struct _ivar_list_t |
| 586 | const llvm::StructType *IvarListnfABITy; |
| 587 | |
| 588 | // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t* |
| 589 | const llvm::Type *IvarListnfABIPtrTy; |
| 590 | |
| 591 | // ClassRonfABITy - LLVM for struct _class_ro_t |
| 592 | const llvm::StructType *ClassRonfABITy; |
| 593 | |
| 594 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 595 | const llvm::Type *ImpnfABITy; |
| 596 | |
| 597 | // CategorynfABITy - LLVM for struct _category_t |
| 598 | const llvm::StructType *CategorynfABITy; |
| 599 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 600 | // New types for nonfragile abi messaging. |
| 601 | |
| 602 | // MessageRefTy - LLVM for: |
| 603 | // struct _message_ref_t { |
| 604 | // IMP messenger; |
| 605 | // SEL name; |
| 606 | // }; |
| 607 | const llvm::StructType *MessageRefTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 608 | // MessageRefCTy - clang type for struct _message_ref_t |
| 609 | QualType MessageRefCTy; |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 610 | |
| 611 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 612 | const llvm::Type *MessageRefPtrTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 613 | // MessageRefCPtrTy - clang type for struct _message_ref_t* |
| 614 | QualType MessageRefCPtrTy; |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 615 | |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 616 | // MessengerTy - Type of the messenger (shown as IMP above) |
| 617 | const llvm::FunctionType *MessengerTy; |
| 618 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 619 | // SuperMessageRefTy - LLVM for: |
| 620 | // struct _super_message_ref_t { |
| 621 | // SUPER_IMP messenger; |
| 622 | // SEL name; |
| 623 | // }; |
| 624 | const llvm::StructType *SuperMessageRefTy; |
| 625 | |
| 626 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 627 | const llvm::Type *SuperMessageRefPtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 628 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 629 | llvm::Constant *getMessageSendFixupFn() { |
| 630 | // id objc_msgSend_fixup(id, struct message_ref_t*, ...) |
| 631 | std::vector<const llvm::Type*> Params; |
| 632 | Params.push_back(ObjectPtrTy); |
| 633 | Params.push_back(MessageRefPtrTy); |
| 634 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 635 | Params, true), |
| 636 | "objc_msgSend_fixup"); |
| 637 | } |
| 638 | |
| 639 | llvm::Constant *getMessageSendFpretFixupFn() { |
| 640 | // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...) |
| 641 | std::vector<const llvm::Type*> Params; |
| 642 | Params.push_back(ObjectPtrTy); |
| 643 | Params.push_back(MessageRefPtrTy); |
| 644 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 645 | Params, true), |
| 646 | "objc_msgSend_fpret_fixup"); |
| 647 | } |
| 648 | |
| 649 | llvm::Constant *getMessageSendStretFixupFn() { |
| 650 | // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...) |
| 651 | std::vector<const llvm::Type*> Params; |
| 652 | Params.push_back(ObjectPtrTy); |
| 653 | Params.push_back(MessageRefPtrTy); |
| 654 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 655 | Params, true), |
| 656 | "objc_msgSend_stret_fixup"); |
| 657 | } |
| 658 | |
| 659 | llvm::Constant *getMessageSendIdFixupFn() { |
| 660 | // id objc_msgSendId_fixup(id, struct message_ref_t*, ...) |
| 661 | std::vector<const llvm::Type*> Params; |
| 662 | Params.push_back(ObjectPtrTy); |
| 663 | Params.push_back(MessageRefPtrTy); |
| 664 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 665 | Params, true), |
| 666 | "objc_msgSendId_fixup"); |
| 667 | } |
| 668 | |
| 669 | llvm::Constant *getMessageSendIdStretFixupFn() { |
| 670 | // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...) |
| 671 | std::vector<const llvm::Type*> Params; |
| 672 | Params.push_back(ObjectPtrTy); |
| 673 | Params.push_back(MessageRefPtrTy); |
| 674 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 675 | Params, true), |
| 676 | "objc_msgSendId_stret_fixup"); |
| 677 | } |
| 678 | llvm::Constant *getMessageSendSuper2FixupFn() { |
| 679 | // id objc_msgSendSuper2_fixup (struct objc_super *, |
| 680 | // struct _super_message_ref_t*, ...) |
| 681 | std::vector<const llvm::Type*> Params; |
| 682 | Params.push_back(SuperPtrTy); |
| 683 | Params.push_back(SuperMessageRefPtrTy); |
| 684 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 685 | Params, true), |
| 686 | "objc_msgSendSuper2_fixup"); |
| 687 | } |
| 688 | |
| 689 | llvm::Constant *getMessageSendSuper2StretFixupFn() { |
| 690 | // id objc_msgSendSuper2_stret_fixup(struct objc_super *, |
| 691 | // struct _super_message_ref_t*, ...) |
| 692 | std::vector<const llvm::Type*> Params; |
| 693 | Params.push_back(SuperPtrTy); |
| 694 | Params.push_back(SuperMessageRefPtrTy); |
| 695 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 696 | Params, true), |
| 697 | "objc_msgSendSuper2_stret_fixup"); |
| 698 | } |
| 699 | |
| 700 | |
| 701 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 702 | /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C |
| 703 | /// exception personality function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 704 | llvm::Value *getEHPersonalityPtr() { |
| 705 | llvm::Constant *Personality = |
| 706 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 707 | true), |
| 708 | "__objc_personality_v0"); |
| 709 | return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy); |
| 710 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 711 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 712 | llvm::Constant *getUnwindResumeOrRethrowFn() { |
| 713 | std::vector<const llvm::Type*> Params; |
| 714 | Params.push_back(Int8PtrTy); |
| 715 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 716 | Params, false), |
| 717 | "_Unwind_Resume_or_Rethrow"); |
| 718 | } |
| 719 | |
| 720 | llvm::Constant *getObjCEndCatchFn() { |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 721 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
Chris Lattner | b59761b | 2009-07-01 04:13:52 +0000 | [diff] [blame] | 722 | false), |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 723 | "objc_end_catch"); |
| 724 | |
| 725 | } |
| 726 | |
| 727 | llvm::Constant *getObjCBeginCatchFn() { |
| 728 | std::vector<const llvm::Type*> Params; |
| 729 | Params.push_back(Int8PtrTy); |
| 730 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy, |
| 731 | Params, false), |
| 732 | "objc_begin_catch"); |
| 733 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 734 | |
| 735 | const llvm::StructType *EHTypeTy; |
| 736 | const llvm::Type *EHTypePtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 737 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 738 | ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); |
| 739 | ~ObjCNonFragileABITypesHelper(){} |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 740 | }; |
| 741 | |
| 742 | class CGObjCCommonMac : public CodeGen::CGObjCRuntime { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 743 | public: |
| 744 | // FIXME - accessibility |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 745 | class GC_IVAR { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 746 | public: |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 747 | unsigned ivar_bytepos; |
| 748 | unsigned ivar_size; |
| 749 | GC_IVAR(unsigned bytepos = 0, unsigned size = 0) |
| 750 | : ivar_bytepos(bytepos), ivar_size(size) {} |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 751 | |
| 752 | // Allow sorting based on byte pos. |
| 753 | bool operator<(const GC_IVAR &b) const { |
| 754 | return ivar_bytepos < b.ivar_bytepos; |
| 755 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 756 | }; |
| 757 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 758 | class SKIP_SCAN { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 759 | public: |
| 760 | unsigned skip; |
| 761 | unsigned scan; |
| 762 | SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0) |
| 763 | : skip(_skip), scan(_scan) {} |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 764 | }; |
| 765 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 766 | protected: |
| 767 | CodeGen::CodeGenModule &CGM; |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 768 | llvm::LLVMContext &VMContext; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 769 | // FIXME! May not be needing this after all. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 770 | unsigned ObjCABI; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 771 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 772 | // gc ivar layout bitmap calculation helper caches. |
| 773 | llvm::SmallVector<GC_IVAR, 16> SkipIvars; |
| 774 | llvm::SmallVector<GC_IVAR, 16> IvarsInfo; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 775 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 776 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 777 | /// DefinedSymbols and FinishModule(). |
| 778 | std::set<IdentifierInfo*> LazySymbols; |
| 779 | |
| 780 | /// DefinedSymbols - External symbols which are defined by this |
| 781 | /// module. The symbols in this list and LazySymbols are used to add |
| 782 | /// special linker symbols which ensure that Objective-C modules are |
| 783 | /// linked properly. |
| 784 | std::set<IdentifierInfo*> DefinedSymbols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 785 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 786 | /// ClassNames - uniqued class names. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 787 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 788 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 789 | /// MethodVarNames - uniqued method variable names. |
| 790 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 791 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 792 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 793 | /// a StringMap here because have no other unique reference. |
| 794 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 795 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 796 | /// MethodDefinitions - map of methods which have been defined in |
| 797 | /// this translation unit. |
| 798 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 799 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 800 | /// PropertyNames - uniqued method variable names. |
| 801 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 802 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 803 | /// ClassReferences - uniqued class references. |
| 804 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 805 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 806 | /// SelectorReferences - uniqued selector references. |
| 807 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 808 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 809 | /// Protocols - Protocols for which an objc_protocol structure has |
| 810 | /// been emitted. Forward declarations are handled by creating an |
| 811 | /// empty structure whose initializer is filled in when/if defined. |
| 812 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 813 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 814 | /// DefinedProtocols - Protocols which have actually been |
| 815 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 816 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 817 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 818 | /// DefinedClasses - List of defined classes. |
| 819 | std::vector<llvm::GlobalValue*> DefinedClasses; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 820 | |
| 821 | /// DefinedNonLazyClasses - List of defined "non-lazy" classes. |
| 822 | std::vector<llvm::GlobalValue*> DefinedNonLazyClasses; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 823 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 824 | /// DefinedCategories - List of defined categories. |
| 825 | std::vector<llvm::GlobalValue*> DefinedCategories; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 826 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 827 | /// DefinedNonLazyCategories - List of defined "non-lazy" categories. |
| 828 | std::vector<llvm::GlobalValue*> DefinedNonLazyCategories; |
| 829 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 830 | /// UsedGlobals - List of globals to pack into the llvm.used metadata |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 831 | /// to prevent them from being clobbered. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 832 | std::vector<llvm::GlobalVariable*> UsedGlobals; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 833 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 834 | /// GetNameForMethod - Return a name for the given method. |
| 835 | /// \param[out] NameOut - The return value. |
| 836 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 837 | const ObjCContainerDecl *CD, |
| 838 | std::string &NameOut); |
| 839 | |
| 840 | /// GetMethodVarName - Return a unique constant for the given |
| 841 | /// selector's name. The return value has type char *. |
| 842 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 843 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
| 844 | llvm::Constant *GetMethodVarName(const std::string &Name); |
| 845 | |
| 846 | /// GetMethodVarType - Return a unique constant for the given |
| 847 | /// selector's name. The return value has type char *. |
| 848 | |
| 849 | // FIXME: This is a horrible name. |
| 850 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 851 | llvm::Constant *GetMethodVarType(const FieldDecl *D); |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 852 | |
| 853 | /// GetPropertyName - Return a unique constant for the given |
| 854 | /// name. The return value has type char *. |
| 855 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
| 856 | |
| 857 | // FIXME: This can be dropped once string functions are unified. |
| 858 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 859 | const Decl *Container); |
| 860 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 861 | /// GetClassName - Return a unique constant for the given selector's |
| 862 | /// name. The return value has type char *. |
| 863 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
| 864 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 865 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 866 | /// implementation for the __strong or __weak case. |
| 867 | /// |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 868 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
| 869 | bool ForStrongLayout); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 870 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 871 | void BuildAggrIvarRecordLayout(const RecordType *RT, |
| 872 | unsigned int BytePos, bool ForStrongLayout, |
| 873 | bool &HasUnion); |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 874 | void BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 875 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 876 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 877 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 878 | unsigned int BytePos, bool ForStrongLayout, |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 879 | bool &HasUnion); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 880 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 881 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 882 | /// ivar layout bitmap. |
| 883 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 884 | const ObjCCommonTypesHelper &ObjCTypes); |
| 885 | |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 886 | /// EmitPropertyList - Emit the given property list. The return |
| 887 | /// value has type PropertyListPtrTy. |
| 888 | llvm::Constant *EmitPropertyList(const std::string &Name, |
| 889 | const Decl *Container, |
| 890 | const ObjCContainerDecl *OCD, |
| 891 | const ObjCCommonTypesHelper &ObjCTypes); |
| 892 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 893 | /// GetProtocolRef - Return a reference to the internal protocol |
| 894 | /// description, creating an empty one if it has not been |
| 895 | /// defined. The return value has type ProtocolPtrTy. |
| 896 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 897 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 898 | /// CreateMetadataVar - Create a global variable with internal |
| 899 | /// linkage for use by the Objective-C runtime. |
| 900 | /// |
| 901 | /// This is a convenience wrapper which not only creates the |
| 902 | /// variable, but also sets the section and alignment and adds the |
| 903 | /// global to the UsedGlobals list. |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 904 | /// |
| 905 | /// \param Name - The variable name. |
| 906 | /// \param Init - The variable initializer; this is also used to |
| 907 | /// define the type of the variable. |
| 908 | /// \param Section - The section the variable should go into, or 0. |
| 909 | /// \param Align - The alignment for the variable, or 0. |
| 910 | /// \param AddToUsed - Whether the variable should be added to |
Daniel Dunbar | c158306 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 911 | /// "llvm.used". |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 912 | llvm::GlobalVariable *CreateMetadataVar(const std::string &Name, |
| 913 | llvm::Constant *Init, |
| 914 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 915 | unsigned Align, |
| 916 | bool AddToUsed); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 917 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 918 | CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF, |
| 919 | QualType ResultType, |
| 920 | llvm::Value *Sel, |
| 921 | llvm::Value *Arg0, |
| 922 | QualType Arg0Ty, |
| 923 | bool IsSuper, |
| 924 | const CallArgList &CallArgs, |
| 925 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 926 | |
Fariborz Jahanian | c38e9af | 2009-06-23 21:47:46 +0000 | [diff] [blame] | 927 | virtual void MergeMetadataGlobals(std::vector<llvm::Constant*> &UsedArray); |
| 928 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 929 | public: |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 930 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : |
| 931 | CGM(cgm), VMContext(cgm.getLLVMContext()) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 932 | { } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 933 | |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 934 | virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 935 | |
| 936 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 937 | const ObjCContainerDecl *CD=0); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 938 | |
| 939 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
| 940 | |
| 941 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 942 | /// declaration, emitting it if necessary. The return value has type |
| 943 | /// ProtocolPtrTy. |
| 944 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
| 945 | |
| 946 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 947 | /// object for the given declaration, emitting it if needed. These |
| 948 | /// forward references will be filled in with empty bodies if no |
| 949 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 950 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 951 | }; |
| 952 | |
| 953 | class CGObjCMac : public CGObjCCommonMac { |
| 954 | private: |
| 955 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 956 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 957 | /// level information. |
| 958 | void EmitImageInfo(); |
| 959 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 960 | /// EmitModuleInfo - Another marker encoding module level |
| 961 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 962 | void EmitModuleInfo(); |
| 963 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 964 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 965 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 966 | llvm::Constant *EmitModuleSymbols(); |
| 967 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 968 | /// FinishModule - Write out global data structures at the end of |
| 969 | /// processing a translation unit. |
| 970 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 971 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 972 | /// EmitClassExtension - Generate the class extension structure used |
| 973 | /// to store the weak ivar layout and properties. The return value |
| 974 | /// has type ClassExtensionPtrTy. |
| 975 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 976 | |
| 977 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 978 | /// for the given class. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 979 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 980 | const ObjCInterfaceDecl *ID); |
| 981 | |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 982 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 983 | QualType ResultType, |
| 984 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 985 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 986 | QualType Arg0Ty, |
| 987 | bool IsSuper, |
| 988 | const CallArgList &CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 989 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 990 | /// EmitIvarList - Emit the ivar list for the given |
| 991 | /// implementation. If ForClass is true the list of class ivars |
| 992 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 993 | /// interface ivars will be emitted. The return value has type |
| 994 | /// IvarListPtrTy. |
| 995 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 996 | bool ForClass); |
| 997 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 998 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 999 | /// for the metaclass of the given interface. The return value has |
| 1000 | /// type ClassPtrTy. |
| 1001 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 1002 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1003 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1004 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1005 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1006 | llvm::Constant *Protocols, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1007 | const ConstantVector &Methods); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1008 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1009 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1010 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1011 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1012 | |
| 1013 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1014 | /// implementation. The return value has type MethodListPtrTy. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1015 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 1016 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1017 | const ConstantVector &Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1018 | |
| 1019 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1020 | /// method declarations. |
| 1021 | /// - TypeName: The name for the type containing the methods. |
| 1022 | /// - IsProtocol: True iff these methods are for a protocol. |
| 1023 | /// - ClassMethds: True iff these are class methods. |
| 1024 | /// - Required: When true, only "required" methods are |
| 1025 | /// listed. Similarly, when false only "optional" methods are |
| 1026 | /// listed. For classes this should always be true. |
| 1027 | /// - begin, end: The method list to output. |
| 1028 | /// |
| 1029 | /// The return value has type MethodDescriptionListPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1030 | llvm::Constant *EmitMethodDescList(const std::string &Name, |
| 1031 | const char *Section, |
| 1032 | const ConstantVector &Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1033 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1034 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1035 | /// declaration, emitting it if necessary. The return value has type |
| 1036 | /// ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1037 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1038 | |
| 1039 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1040 | /// object for the given declaration, emitting it if needed. These |
| 1041 | /// forward references will be filled in with empty bodies if no |
| 1042 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1043 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1044 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1045 | /// EmitProtocolExtension - Generate the protocol extension |
| 1046 | /// structure used to store optional instance and class methods, and |
| 1047 | /// protocol properties. The return value has type |
| 1048 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1049 | llvm::Constant * |
| 1050 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1051 | const ConstantVector &OptInstanceMethods, |
| 1052 | const ConstantVector &OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1053 | |
| 1054 | /// EmitProtocolList - Generate the list of referenced |
| 1055 | /// protocols. The return value has type ProtocolListPtrTy. |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1056 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 1057 | ObjCProtocolDecl::protocol_iterator begin, |
| 1058 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1059 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1060 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1061 | /// for the given selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1062 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1063 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1064 | public: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1065 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1066 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1067 | virtual llvm::Function *ModuleInitFunction(); |
| 1068 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1069 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1070 | QualType ResultType, |
| 1071 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1072 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1073 | bool IsClassMessage, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1074 | const CallArgList &CallArgs, |
| 1075 | const ObjCMethodDecl *Method); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1076 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1077 | virtual CodeGen::RValue |
| 1078 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1079 | QualType ResultType, |
| 1080 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1081 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1082 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1083 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1084 | bool IsClassMessage, |
| 1085 | const CallArgList &CallArgs); |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1086 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1087 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1088 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1089 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1090 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel); |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1091 | |
| 1092 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1093 | /// untyped one. |
| 1094 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1095 | const ObjCMethodDecl *Method); |
| 1096 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1097 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1098 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1099 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1100 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1101 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1102 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1103 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1104 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 1105 | virtual llvm::Constant *GetPropertySetFunction(); |
| 1106 | virtual llvm::Constant *EnumerationMutationFunction(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1107 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 1108 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1109 | const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1110 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 1111 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1112 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 1113 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1114 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 1115 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1116 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 1117 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 1118 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 1119 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1120 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 1121 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1122 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1123 | llvm::Value *dest, llvm::Value *src, |
| 1124 | unsigned long size); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 1125 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1126 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1127 | QualType ObjectTy, |
| 1128 | llvm::Value *BaseValue, |
| 1129 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1130 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1131 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1132 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1133 | const ObjCIvarDecl *Ivar); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1134 | }; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1135 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1136 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1137 | private: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1138 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1139 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 1140 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1141 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1142 | /// SuperClassReferences - uniqued super class references. |
| 1143 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; |
| 1144 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1145 | /// MetaClassReferences - uniqued meta class references. |
| 1146 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1147 | |
| 1148 | /// EHTypeReferences - uniqued class ehtype references. |
| 1149 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1150 | |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1151 | /// NonLegacyDispatchMethods - List of methods for which we do *not* generate |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1152 | /// legacy messaging dispatch. |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1153 | llvm::DenseSet<Selector> NonLegacyDispatchMethods; |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1154 | |
| 1155 | /// LegacyDispatchedSelector - Returns true if SEL is not in the list of |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1156 | /// NonLegacyDispatchMethods; false otherwise. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1157 | bool LegacyDispatchedSelector(Selector Sel); |
| 1158 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1159 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 1160 | /// processing a translation unit. |
| 1161 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1162 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 1163 | /// AddModuleClassList - Add the given list of class pointers to the |
| 1164 | /// module with the provided symbol and section names. |
| 1165 | void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container, |
| 1166 | const char *SymbolName, |
| 1167 | const char *SectionName); |
| 1168 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 1169 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 1170 | unsigned InstanceStart, |
| 1171 | unsigned InstanceSize, |
| 1172 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1173 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
| 1174 | llvm::Constant *IsAGV, |
| 1175 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 1176 | llvm::Constant *ClassRoGV, |
| 1177 | bool HiddenVisibility); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1178 | |
| 1179 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
| 1180 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 1181 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
| 1182 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1183 | /// EmitMethodList - Emit the method list for the given |
| 1184 | /// implementation. The return value has type MethodListnfABITy. |
| 1185 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 1186 | const char *Section, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 1187 | const ConstantVector &Methods); |
| 1188 | /// EmitIvarList - Emit the ivar list for the given |
| 1189 | /// implementation. If ForClass is true the list of class ivars |
| 1190 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1191 | /// interface ivars will be emitted. The return value has type |
| 1192 | /// IvarListnfABIPtrTy. |
| 1193 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 1194 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1195 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 1196 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 1197 | unsigned long int offset); |
| 1198 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1199 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1200 | /// declaration, emitting it if necessary. The return value has type |
| 1201 | /// ProtocolPtrTy. |
| 1202 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
| 1203 | |
| 1204 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1205 | /// object for the given declaration, emitting it if needed. These |
| 1206 | /// forward references will be filled in with empty bodies if no |
| 1207 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1208 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
| 1209 | |
| 1210 | /// EmitProtocolList - Generate the list of referenced |
| 1211 | /// protocols. The return value has type ProtocolListPtrTy. |
| 1212 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 1213 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1214 | ObjCProtocolDecl::protocol_iterator end); |
| 1215 | |
| 1216 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1217 | QualType ResultType, |
| 1218 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 1219 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1220 | QualType Arg0Ty, |
| 1221 | bool IsSuper, |
| 1222 | const CallArgList &CallArgs); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 1223 | |
| 1224 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1225 | /// class of the given name. |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 1226 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name); |
| 1227 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1228 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1229 | /// for the given class reference. |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1230 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1231 | const ObjCInterfaceDecl *ID); |
| 1232 | |
| 1233 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1234 | /// for the given super class reference. |
| 1235 | llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder, |
| 1236 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1237 | |
| 1238 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 1239 | /// meta-data |
| 1240 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
| 1241 | const ObjCInterfaceDecl *ID); |
| 1242 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1243 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 1244 | /// the given ivar. |
| 1245 | /// |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 1246 | llvm::GlobalVariable * ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1247 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1248 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1249 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1250 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1251 | /// for the given selector. |
| 1252 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1253 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1254 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1255 | /// interface. The return value has type EHTypePtrTy. |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1256 | llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 1257 | bool ForDefinition); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1258 | |
| 1259 | const char *getMetaclassSymbolPrefix() const { |
| 1260 | return "OBJC_METACLASS_$_"; |
| 1261 | } |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1262 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1263 | const char *getClassSymbolPrefix() const { |
| 1264 | return "OBJC_CLASS_$_"; |
| 1265 | } |
| 1266 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 1267 | void GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1268 | uint32_t &InstanceStart, |
| 1269 | uint32_t &InstanceSize); |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1270 | |
| 1271 | // Shamelessly stolen from Analysis/CFRefCount.cpp |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1272 | Selector GetNullarySelector(const char* name) const { |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1273 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1274 | return CGM.getContext().Selectors.getSelector(0, &II); |
| 1275 | } |
| 1276 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1277 | Selector GetUnarySelector(const char* name) const { |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1278 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1279 | return CGM.getContext().Selectors.getSelector(1, &II); |
| 1280 | } |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1281 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1282 | /// ImplementationIsNonLazy - Check whether the given category or |
| 1283 | /// class implementation is "non-lazy". |
Fariborz Jahanian | ecfbdcb | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 1284 | bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1285 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1286 | public: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1287 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1288 | // FIXME. All stubs for now! |
| 1289 | virtual llvm::Function *ModuleInitFunction(); |
| 1290 | |
| 1291 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1292 | QualType ResultType, |
| 1293 | Selector Sel, |
| 1294 | llvm::Value *Receiver, |
| 1295 | bool IsClassMessage, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1296 | const CallArgList &CallArgs, |
| 1297 | const ObjCMethodDecl *Method); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1298 | |
| 1299 | virtual CodeGen::RValue |
| 1300 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 1301 | QualType ResultType, |
| 1302 | Selector Sel, |
| 1303 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1304 | bool isCategoryImpl, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1305 | llvm::Value *Receiver, |
| 1306 | bool IsClassMessage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1307 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1308 | |
| 1309 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1310 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1311 | |
| 1312 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel) |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1313 | { return EmitSelector(Builder, Sel); } |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1314 | |
| 1315 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1316 | /// untyped one. |
| 1317 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1318 | const ObjCMethodDecl *Method) |
| 1319 | { return EmitSelector(Builder, Method->getSelector()); } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1320 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 1321 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1322 | |
| 1323 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1324 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 1325 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1326 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1327 | virtual llvm::Constant *GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1328 | return ObjCTypes.getGetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1329 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1330 | virtual llvm::Constant *GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1331 | return ObjCTypes.getSetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1332 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1333 | virtual llvm::Constant *EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1334 | return ObjCTypes.getEnumerationMutationFn(); |
Daniel Dunbar | 28ed084 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 1335 | } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1336 | |
| 1337 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 1338 | const Stmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1339 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 1340 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1341 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1342 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1343 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1344 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1345 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1346 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1347 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1348 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1349 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1350 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1351 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1352 | llvm::Value *dest, llvm::Value *src, |
| 1353 | unsigned long size); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1354 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1355 | QualType ObjectTy, |
| 1356 | llvm::Value *BaseValue, |
| 1357 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1358 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1359 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1360 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1361 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1362 | }; |
| 1363 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1364 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1365 | |
| 1366 | /* *** Helper Functions *** */ |
| 1367 | |
| 1368 | /// getConstantGEP() - Help routine to construct simple GEPs. |
| 1369 | static llvm::Constant *getConstantGEP(llvm::Constant *C, |
| 1370 | unsigned idx0, |
| 1371 | unsigned idx1) { |
| 1372 | llvm::Value *Idxs[] = { |
| 1373 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0), |
| 1374 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1) |
| 1375 | }; |
| 1376 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2); |
| 1377 | } |
| 1378 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1379 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 1380 | /// class has the __objc_exception__ attribute. |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1381 | static bool hasObjCExceptionAttribute(ASTContext &Context, |
| 1382 | const ObjCInterfaceDecl *OID) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1383 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1384 | return true; |
| 1385 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1386 | return hasObjCExceptionAttribute(Context, Super); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1387 | return false; |
| 1388 | } |
| 1389 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1390 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1391 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1392 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
| 1393 | ObjCTypes(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1394 | { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1395 | ObjCABI = 1; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1396 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1399 | /// GetClass - Return a reference to the class for the given interface |
| 1400 | /// decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1401 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1402 | const ObjCInterfaceDecl *ID) { |
| 1403 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1407 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 1408 | return EmitSelector(Builder, Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1409 | } |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1410 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl |
| 1411 | *Method) { |
| 1412 | return EmitSelector(Builder, Method->getSelector()); |
| 1413 | } |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1414 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1415 | /// Generate a constant CFString object. |
| 1416 | /* |
| 1417 | struct __builtin_CFString { |
| 1418 | const int *isa; // point to __CFConstantStringClassReference |
| 1419 | int flags; |
| 1420 | const char *str; |
| 1421 | long length; |
| 1422 | }; |
| 1423 | */ |
| 1424 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1425 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 1426 | const ObjCStringLiteral *SL) { |
Steve Naroff | 8d4141f | 2009-04-01 13:55:36 +0000 | [diff] [blame] | 1427 | return CGM.GetAddrOfConstantCFString(SL->getString()); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | /// Generates a message send where the super is the receiver. This is |
| 1431 | /// a message send to self with special delivery semantics indicating |
| 1432 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1433 | CodeGen::RValue |
| 1434 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1435 | QualType ResultType, |
| 1436 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1437 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1438 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1439 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1440 | bool IsClassMessage, |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1441 | const CodeGen::CallArgList &CallArgs) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1442 | // Create and init a super structure; this is a (receiver, class) |
| 1443 | // pair we will pass to objc_msgSendSuper. |
| 1444 | llvm::Value *ObjCSuper = |
| 1445 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 1446 | llvm::Value *ReceiverAsObject = |
| 1447 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 1448 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 1449 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1450 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1451 | // If this is a class message the metaclass is passed as the target. |
| 1452 | llvm::Value *Target; |
| 1453 | if (IsClassMessage) { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1454 | if (isCategoryImpl) { |
| 1455 | // Message sent to 'super' in a class method defined in a category |
| 1456 | // implementation requires an odd treatment. |
| 1457 | // If we are in a class method, we must retrieve the |
| 1458 | // _metaclass_ for the current class, pointed at by |
| 1459 | // the class's "isa" pointer. The following assumes that |
| 1460 | // isa" is the first ivar in a class (which it must be). |
| 1461 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1462 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 1463 | Target = CGF.Builder.CreateLoad(Target); |
| 1464 | } |
| 1465 | else { |
| 1466 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 1467 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 1468 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 1469 | Target = Super; |
| 1470 | } |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1471 | } else { |
| 1472 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1473 | } |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1474 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 1475 | // ObjCTypes types. |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1476 | const llvm::Type *ClassTy = |
| 1477 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1478 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1479 | CGF.Builder.CreateStore(Target, |
| 1480 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1481 | return EmitLegacyMessageSend(CGF, ResultType, |
| 1482 | EmitSelector(CGF.Builder, Sel), |
| 1483 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 1484 | true, CallArgs, ObjCTypes); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1485 | } |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1486 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1487 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1488 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1489 | QualType ResultType, |
| 1490 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1491 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1492 | bool IsClassMessage, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1493 | const CallArgList &CallArgs, |
| 1494 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1495 | return EmitLegacyMessageSend(CGF, ResultType, |
| 1496 | EmitSelector(CGF.Builder, Sel), |
| 1497 | Receiver, CGF.getContext().getObjCIdType(), |
| 1498 | false, CallArgs, ObjCTypes); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1501 | CodeGen::RValue CGObjCCommonMac::EmitLegacyMessageSend( |
| 1502 | CodeGen::CodeGenFunction &CGF, |
| 1503 | QualType ResultType, |
| 1504 | llvm::Value *Sel, |
| 1505 | llvm::Value *Arg0, |
| 1506 | QualType Arg0Ty, |
| 1507 | bool IsSuper, |
| 1508 | const CallArgList &CallArgs, |
| 1509 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1510 | CallArgList ActualArgs; |
Fariborz Jahanian | d019d96 | 2009-04-24 21:07:43 +0000 | [diff] [blame] | 1511 | if (!IsSuper) |
| 1512 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1513 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1514 | ActualArgs.push_back(std::make_pair(RValue::get(Sel), |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1515 | CGF.getContext().getObjCSelType())); |
| 1516 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1517 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1518 | CodeGenTypes &Types = CGM.getTypes(); |
| 1519 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1520 | // In 64bit ABI, type must be assumed VARARG. In 32bit abi, |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1521 | // it seems not to matter. |
| 1522 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, (ObjCABI == 2)); |
| 1523 | |
| 1524 | llvm::Constant *Fn = NULL; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1525 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1526 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper) |
| 1527 | : ObjCTypes.getSendStretFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1528 | } else if (ResultType->isFloatingType()) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1529 | if (ObjCABI == 2) { |
| 1530 | if (const BuiltinType *BT = ResultType->getAsBuiltinType()) { |
| 1531 | BuiltinType::Kind k = BT->getKind(); |
| 1532 | Fn = (k == BuiltinType::LongDouble) ? ObjCTypes.getSendFpretFn2(IsSuper) |
| 1533 | : ObjCTypes.getSendFn2(IsSuper); |
Daniel Dunbar | 42f963d | 2009-06-10 04:38:50 +0000 | [diff] [blame] | 1534 | } else { |
| 1535 | Fn = ObjCTypes.getSendFn2(IsSuper); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1536 | } |
| 1537 | } |
| 1538 | else |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1539 | // FIXME. This currently matches gcc's API for x86-32. May need to change |
| 1540 | // for others if we have their API. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1541 | Fn = ObjCTypes.getSendFpretFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1542 | } else { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1543 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper) |
| 1544 | : ObjCTypes.getSendFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1545 | } |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1546 | assert(Fn && "EmitLegacyMessageSend - unknown API"); |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 1547 | Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy)); |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1548 | return CGF.EmitCall(FnInfo, Fn, ActualArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1551 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1552 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1553 | // 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] | 1554 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1555 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1556 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1557 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
| 1558 | ObjCTypes.ExternalProtocolPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1561 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1562 | // FIXME: We shouldn't need this, the protocol decl should contain enough |
| 1563 | // information to tell us whether this was a declaration or a definition. |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1564 | DefinedProtocols.insert(PD->getIdentifier()); |
| 1565 | |
| 1566 | // If we have generated a forward reference to this protocol, emit |
| 1567 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 1568 | // emitted. |
| 1569 | if (Protocols.count(PD->getIdentifier())) |
| 1570 | GetOrEmitProtocol(PD); |
| 1571 | } |
| 1572 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1573 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1574 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 1575 | return GetOrEmitProtocol(PD); |
| 1576 | return GetOrEmitProtocolRef(PD); |
| 1577 | } |
| 1578 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1579 | /* |
| 1580 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 1581 | struct _objc_protocol { |
| 1582 | struct _objc_protocol_extension *isa; |
| 1583 | char *protocol_name; |
| 1584 | struct _objc_protocol_list *protocol_list; |
| 1585 | struct _objc__method_prototype_list *instance_methods; |
| 1586 | struct _objc__method_prototype_list *class_methods |
| 1587 | }; |
| 1588 | |
| 1589 | See EmitProtocolExtension(). |
| 1590 | */ |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1591 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
| 1592 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1593 | |
| 1594 | // Early exit if a defining object has already been generated. |
| 1595 | if (Entry && Entry->hasInitializer()) |
| 1596 | return Entry; |
| 1597 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1598 | // 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] | 1599 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1600 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1601 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1602 | const char *ProtocolName = PD->getNameAsCString(); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1603 | |
| 1604 | // Construct method lists. |
| 1605 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1606 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1607 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1608 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1609 | ObjCMethodDecl *MD = *i; |
| 1610 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1611 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1612 | OptInstanceMethods.push_back(C); |
| 1613 | } else { |
| 1614 | InstanceMethods.push_back(C); |
| 1615 | } |
| 1616 | } |
| 1617 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1618 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1619 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1620 | ObjCMethodDecl *MD = *i; |
| 1621 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1622 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1623 | OptClassMethods.push_back(C); |
| 1624 | } else { |
| 1625 | ClassMethods.push_back(C); |
| 1626 | } |
| 1627 | } |
| 1628 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1629 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1630 | Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1631 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1632 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1633 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1634 | PD->protocol_begin(), |
| 1635 | PD->protocol_end()); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1636 | Values[3] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1637 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" |
| 1638 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1639 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1640 | InstanceMethods); |
| 1641 | Values[4] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1642 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" |
| 1643 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1644 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1645 | ClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1646 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 1647 | Values); |
| 1648 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1649 | if (Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1650 | // Already created, fix the linkage and update the initializer. |
| 1651 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1652 | Entry->setInitializer(Init); |
| 1653 | } else { |
| 1654 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1655 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1656 | llvm::GlobalValue::InternalLinkage, |
| 1657 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1658 | std::string("\01L_OBJC_PROTOCOL_")+ProtocolName); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1659 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1660 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1661 | UsedGlobals.push_back(Entry); |
| 1662 | // FIXME: Is this necessary? Why only for protocol? |
| 1663 | Entry->setAlignment(4); |
| 1664 | } |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1665 | |
| 1666 | return Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1669 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1670 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1671 | |
| 1672 | if (!Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1673 | // We use the initializer as a marker of whether this is a forward |
| 1674 | // reference or not. At module finalization we add the empty |
| 1675 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1676 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1677 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1678 | llvm::GlobalValue::ExternalLinkage, |
| 1679 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1680 | "\01L_OBJC_PROTOCOL_" + PD->getNameAsString()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1681 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1682 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1683 | UsedGlobals.push_back(Entry); |
| 1684 | // FIXME: Is this necessary? Why only for protocol? |
| 1685 | Entry->setAlignment(4); |
| 1686 | } |
| 1687 | |
| 1688 | return Entry; |
| 1689 | } |
| 1690 | |
| 1691 | /* |
| 1692 | struct _objc_protocol_extension { |
| 1693 | uint32_t size; |
| 1694 | struct objc_method_description_list *optional_instance_methods; |
| 1695 | struct objc_method_description_list *optional_class_methods; |
| 1696 | struct objc_property_list *instance_properties; |
| 1697 | }; |
| 1698 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1699 | llvm::Constant * |
| 1700 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1701 | const ConstantVector &OptInstanceMethods, |
| 1702 | const ConstantVector &OptClassMethods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1703 | uint64_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1704 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1705 | std::vector<llvm::Constant*> Values(4); |
| 1706 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1707 | Values[1] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1708 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
| 1709 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1710 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1711 | OptInstanceMethods); |
| 1712 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1713 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" |
| 1714 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1715 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1716 | OptClassMethods); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1717 | Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + |
| 1718 | PD->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1719 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1720 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1721 | // Return null if no extension bits are used. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1722 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
| 1723 | Values[3]->isNullValue()) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 1724 | return VMContext.getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1725 | |
| 1726 | llvm::Constant *Init = |
| 1727 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1728 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1729 | // No special section, but goes in llvm.used |
| 1730 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(), |
| 1731 | Init, |
| 1732 | 0, 0, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1733 | } |
| 1734 | |
| 1735 | /* |
| 1736 | struct objc_protocol_list { |
| 1737 | struct objc_protocol_list *next; |
| 1738 | long count; |
| 1739 | Protocol *list[]; |
| 1740 | }; |
| 1741 | */ |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1742 | llvm::Constant * |
| 1743 | CGObjCMac::EmitProtocolList(const std::string &Name, |
| 1744 | ObjCProtocolDecl::protocol_iterator begin, |
| 1745 | ObjCProtocolDecl::protocol_iterator end) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1746 | std::vector<llvm::Constant*> ProtocolRefs; |
| 1747 | |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1748 | for (; begin != end; ++begin) |
| 1749 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1750 | |
| 1751 | // Just return null for empty protocol lists |
| 1752 | if (ProtocolRefs.empty()) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 1753 | return VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1754 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1755 | // This list is null terminated. |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 1756 | ProtocolRefs.push_back(VMContext.getNullValue(ObjCTypes.ProtocolPtrTy)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1757 | |
| 1758 | std::vector<llvm::Constant*> Values(3); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1759 | // This field is only used by the runtime. |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 1760 | Values[0] = VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1761 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 1762 | Values[2] = |
| 1763 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 1764 | ProtocolRefs.size()), |
| 1765 | ProtocolRefs); |
| 1766 | |
| 1767 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1768 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1769 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1770 | 4, false); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1771 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
| 1772 | } |
| 1773 | |
| 1774 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1775 | struct _objc_property { |
| 1776 | const char * const name; |
| 1777 | const char * const attributes; |
| 1778 | }; |
| 1779 | |
| 1780 | struct _objc_property_list { |
| 1781 | uint32_t entsize; // sizeof (struct _objc_property) |
| 1782 | uint32_t prop_count; |
| 1783 | struct _objc_property[prop_count]; |
| 1784 | }; |
| 1785 | */ |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1786 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name, |
| 1787 | const Decl *Container, |
| 1788 | const ObjCContainerDecl *OCD, |
| 1789 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1790 | std::vector<llvm::Constant*> Properties, Prop(2); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1791 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(), |
| 1792 | E = OCD->prop_end(); I != E; ++I) { |
Steve Naroff | 93983f8 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 1793 | const ObjCPropertyDecl *PD = *I; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1794 | Prop[0] = GetPropertyName(PD->getIdentifier()); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1795 | Prop[1] = GetPropertyTypeString(PD, Container); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1796 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
| 1797 | Prop)); |
| 1798 | } |
| 1799 | |
| 1800 | // Return null for empty list. |
| 1801 | if (Properties.empty()) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 1802 | return VMContext.getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1803 | |
| 1804 | unsigned PropertySize = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1805 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1806 | std::vector<llvm::Constant*> Values(3); |
| 1807 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 1808 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
| 1809 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
| 1810 | Properties.size()); |
| 1811 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
| 1812 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1813 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1814 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1815 | CreateMetadataVar(Name, Init, |
| 1816 | (ObjCABI == 2) ? "__DATA, __objc_const" : |
| 1817 | "__OBJC,__property,regular,no_dead_strip", |
| 1818 | (ObjCABI == 2) ? 8 : 4, |
| 1819 | true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1820 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1821 | } |
| 1822 | |
| 1823 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1824 | struct objc_method_description_list { |
| 1825 | int count; |
| 1826 | struct objc_method_description list[]; |
| 1827 | }; |
| 1828 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1829 | llvm::Constant * |
| 1830 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 1831 | std::vector<llvm::Constant*> Desc(2); |
| 1832 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1833 | ObjCTypes.SelectorPtrTy); |
| 1834 | Desc[1] = GetMethodVarType(MD); |
| 1835 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
| 1836 | Desc); |
| 1837 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1838 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1839 | llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name, |
| 1840 | const char *Section, |
| 1841 | const ConstantVector &Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1842 | // Return null for empty list. |
| 1843 | if (Methods.empty()) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 1844 | return VMContext.getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1845 | |
| 1846 | std::vector<llvm::Constant*> Values(2); |
| 1847 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 1848 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
| 1849 | Methods.size()); |
| 1850 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
| 1851 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1852 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1853 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1854 | return llvm::ConstantExpr::getBitCast(GV, |
| 1855 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1858 | /* |
| 1859 | struct _objc_category { |
| 1860 | char *category_name; |
| 1861 | char *class_name; |
| 1862 | struct _objc_method_list *instance_methods; |
| 1863 | struct _objc_method_list *class_methods; |
| 1864 | struct _objc_protocol_list *protocols; |
| 1865 | uint32_t size; // <rdar://4585769> |
| 1866 | struct _objc_property_list *instance_properties; |
| 1867 | }; |
| 1868 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1869 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1870 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1871 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1872 | // FIXME: This is poor design, the OCD should have a pointer to the category |
| 1873 | // decl. Additionally, note that Category can be null for the @implementation |
| 1874 | // w/o an @interface case. Sema should just create one for us as it does for |
| 1875 | // @implementation so everyone else can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1876 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1877 | const ObjCCategoryDecl *Category = |
| 1878 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1879 | std::string ExtName(Interface->getNameAsString() + "_" + |
| 1880 | OCD->getNameAsString()); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1881 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1882 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1883 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1884 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1885 | // Instance methods should always be defined. |
| 1886 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1887 | } |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1888 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1889 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1890 | // Class methods should always be defined. |
| 1891 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1892 | } |
| 1893 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1894 | std::vector<llvm::Constant*> Values(7); |
| 1895 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 1896 | Values[1] = GetClassName(Interface->getIdentifier()); |
Fariborz Jahanian | 679cd7f | 2009-04-29 20:40:05 +0000 | [diff] [blame] | 1897 | LazySymbols.insert(Interface->getIdentifier()); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1898 | Values[2] = |
| 1899 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") + |
| 1900 | ExtName, |
| 1901 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1902 | InstanceMethods); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1903 | Values[3] = |
| 1904 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName, |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1905 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1906 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1907 | if (Category) { |
| 1908 | Values[4] = |
| 1909 | EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName, |
| 1910 | Category->protocol_begin(), |
| 1911 | Category->protocol_end()); |
| 1912 | } else { |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 1913 | Values[4] = VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1914 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1915 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1916 | |
| 1917 | // If there is no category @interface then there can be no properties. |
| 1918 | if (Category) { |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1919 | Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1920 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1921 | } else { |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 1922 | Values[6] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1923 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1924 | |
| 1925 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
| 1926 | Values); |
| 1927 | |
| 1928 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1929 | CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init, |
| 1930 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1931 | 4, true); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1932 | DefinedCategories.push_back(GV); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1935 | // FIXME: Get from somewhere? |
| 1936 | enum ClassFlags { |
| 1937 | eClassFlags_Factory = 0x00001, |
| 1938 | eClassFlags_Meta = 0x00002, |
| 1939 | // <rdr://5142207> |
| 1940 | eClassFlags_HasCXXStructors = 0x02000, |
| 1941 | eClassFlags_Hidden = 0x20000, |
| 1942 | eClassFlags_ABI2_Hidden = 0x00010, |
| 1943 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 1944 | }; |
| 1945 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1946 | /* |
| 1947 | struct _objc_class { |
| 1948 | Class isa; |
| 1949 | Class super_class; |
| 1950 | const char *name; |
| 1951 | long version; |
| 1952 | long info; |
| 1953 | long instance_size; |
| 1954 | struct _objc_ivar_list *ivars; |
| 1955 | struct _objc_method_list *methods; |
| 1956 | struct _objc_cache *cache; |
| 1957 | struct _objc_protocol_list *protocols; |
| 1958 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 1959 | const char *ivar_layout; |
| 1960 | struct _objc_class_ext *ext; |
| 1961 | }; |
| 1962 | |
| 1963 | See EmitClassExtension(); |
| 1964 | */ |
| 1965 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1966 | DefinedSymbols.insert(ID->getIdentifier()); |
| 1967 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1968 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1969 | // FIXME: Gross |
| 1970 | ObjCInterfaceDecl *Interface = |
| 1971 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1972 | llvm::Constant *Protocols = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1973 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1974 | Interface->protocol_begin(), |
| 1975 | Interface->protocol_end()); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1976 | unsigned Flags = eClassFlags_Factory; |
Daniel Dunbar | 2bebbf0 | 2009-05-03 10:46:44 +0000 | [diff] [blame] | 1977 | unsigned Size = |
| 1978 | CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1979 | |
| 1980 | // FIXME: Set CXX-structors flag. |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1981 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1982 | Flags |= eClassFlags_Hidden; |
| 1983 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1984 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1985 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1986 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1987 | // Instance methods should always be defined. |
| 1988 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1989 | } |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1990 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1991 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1992 | // Class methods should always be defined. |
| 1993 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1994 | } |
| 1995 | |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1996 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1997 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1998 | ObjCPropertyImplDecl *PID = *i; |
| 1999 | |
| 2000 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 2001 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 2002 | |
| 2003 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 2004 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 2005 | InstanceMethods.push_back(C); |
| 2006 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 2007 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 2008 | InstanceMethods.push_back(C); |
| 2009 | } |
| 2010 | } |
| 2011 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2012 | std::vector<llvm::Constant*> Values(12); |
Daniel Dunbar | 5384b09 | 2009-05-03 08:56:52 +0000 | [diff] [blame] | 2013 | Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2014 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2015 | // Record a reference to the super class. |
| 2016 | LazySymbols.insert(Super->getIdentifier()); |
| 2017 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2018 | Values[ 1] = |
| 2019 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 2020 | ObjCTypes.ClassPtrTy); |
| 2021 | } else { |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 2022 | Values[ 1] = VMContext.getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2023 | } |
| 2024 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 2025 | // Version is always 0. |
| 2026 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2027 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 2028 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2029 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2030 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2031 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2032 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2033 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2034 | // cache is always NULL. |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 2035 | Values[ 8] = VMContext.getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2036 | Values[ 9] = Protocols; |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 2037 | Values[10] = BuildIvarLayout(ID, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2038 | Values[11] = EmitClassExtension(ID); |
| 2039 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 2040 | Values); |
| 2041 | |
| 2042 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2043 | CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init, |
| 2044 | "__OBJC,__class,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2045 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2046 | DefinedClasses.push_back(GV); |
| 2047 | } |
| 2048 | |
| 2049 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 2050 | llvm::Constant *Protocols, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2051 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2052 | unsigned Flags = eClassFlags_Meta; |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2053 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2054 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 2055 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2056 | Flags |= eClassFlags_Hidden; |
| 2057 | |
| 2058 | std::vector<llvm::Constant*> Values(12); |
| 2059 | // The isa for the metaclass is the root of the hierarchy. |
| 2060 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 2061 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 2062 | Root = Super; |
| 2063 | Values[ 0] = |
| 2064 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
| 2065 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2066 | // The super class for the metaclass is emitted as the name of the |
| 2067 | // super class. The runtime fixes this up to point to the |
| 2068 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2069 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
| 2070 | Values[ 1] = |
| 2071 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 2072 | ObjCTypes.ClassPtrTy); |
| 2073 | } else { |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 2074 | Values[ 1] = VMContext.getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2075 | } |
| 2076 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 2077 | // Version is always 0. |
| 2078 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2079 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 2080 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2081 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2082 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2083 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2084 | "__OBJC,__cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2085 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2086 | // cache is always NULL. |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 2087 | Values[ 8] = VMContext.getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2088 | Values[ 9] = Protocols; |
| 2089 | // ivar_layout for metaclass is always NULL. |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 2090 | Values[10] = VMContext.getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2091 | // The class extension is always unused for metaclasses. |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 2092 | Values[11] = VMContext.getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2093 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 2094 | Values); |
| 2095 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2096 | std::string Name("\01L_OBJC_METACLASS_"); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2097 | Name += ID->getNameAsCString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2098 | |
| 2099 | // Check for a forward reference. |
| 2100 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 2101 | if (GV) { |
| 2102 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2103 | "Forward metaclass reference has incorrect type."); |
| 2104 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 2105 | GV->setInitializer(Init); |
| 2106 | } else { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2107 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2108 | llvm::GlobalValue::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2109 | Init, Name); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2110 | } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2111 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2112 | GV->setAlignment(4); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2113 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2114 | |
| 2115 | return GV; |
| 2116 | } |
| 2117 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2118 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2119 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2120 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2121 | // FIXME: Should we look these up somewhere other than the module. Its a bit |
| 2122 | // silly since we only generate these while processing an implementation, so |
| 2123 | // exactly one pointer would work if know when we entered/exitted an |
| 2124 | // implementation block. |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2125 | |
| 2126 | // Check for an existing forward reference. |
Fariborz Jahanian | b0d2794 | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 2127 | // Previously, metaclass with internal linkage may have been defined. |
| 2128 | // pass 'true' as 2nd argument so it is returned. |
| 2129 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) { |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2130 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2131 | "Forward metaclass reference has incorrect type."); |
| 2132 | return GV; |
| 2133 | } else { |
| 2134 | // Generate as an external reference to keep a consistent |
| 2135 | // module. This will be patched up when we emit the metaclass. |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2136 | return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2137 | llvm::GlobalValue::ExternalLinkage, |
| 2138 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2139 | Name); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2140 | } |
| 2141 | } |
| 2142 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2143 | /* |
| 2144 | struct objc_class_ext { |
| 2145 | uint32_t size; |
| 2146 | const char *weak_ivar_layout; |
| 2147 | struct _objc_property_list *properties; |
| 2148 | }; |
| 2149 | */ |
| 2150 | llvm::Constant * |
| 2151 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
| 2152 | uint64_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2153 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2154 | |
| 2155 | std::vector<llvm::Constant*> Values(3); |
| 2156 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 2157 | Values[1] = BuildIvarLayout(ID, false); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2158 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2159 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2160 | |
| 2161 | // Return null if no extension bits are used. |
| 2162 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 2163 | return VMContext.getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2164 | |
| 2165 | llvm::Constant *Init = |
| 2166 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2167 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2168 | Init, "__OBJC,__class_ext,regular,no_dead_strip", |
| 2169 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2170 | } |
| 2171 | |
| 2172 | /* |
| 2173 | struct objc_ivar { |
| 2174 | char *ivar_name; |
| 2175 | char *ivar_type; |
| 2176 | int ivar_offset; |
| 2177 | }; |
| 2178 | |
| 2179 | struct objc_ivar_list { |
| 2180 | int ivar_count; |
| 2181 | struct objc_ivar list[count]; |
| 2182 | }; |
| 2183 | */ |
| 2184 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2185 | bool ForClass) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2186 | std::vector<llvm::Constant*> Ivars, Ivar(3); |
| 2187 | |
| 2188 | // When emitting the root class GCC emits ivar entries for the |
| 2189 | // actual class structure. It is not clear if we need to follow this |
| 2190 | // behavior; for now lets try and get away with not doing it. If so, |
| 2191 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 2192 | // for the class. |
| 2193 | if (ForClass) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 2194 | return VMContext.getNullValue(ObjCTypes.IvarListPtrTy); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2195 | |
| 2196 | ObjCInterfaceDecl *OID = |
| 2197 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2198 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 2199 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 2200 | CGM.getContext().ShallowCollectObjCIvars(OID, OIvars); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 2201 | |
| 2202 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 2203 | ObjCIvarDecl *IVD = OIvars[i]; |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 2204 | // Ignore unnamed bit-fields. |
| 2205 | if (!IVD->getDeclName()) |
| 2206 | continue; |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 2207 | Ivar[0] = GetMethodVarName(IVD->getIdentifier()); |
| 2208 | Ivar[1] = GetMethodVarType(IVD); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 2209 | Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2210 | ComputeIvarBaseOffset(CGM, OID, IVD)); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2211 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2212 | } |
| 2213 | |
| 2214 | // Return null for empty list. |
| 2215 | if (Ivars.empty()) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 2216 | return VMContext.getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2217 | |
| 2218 | std::vector<llvm::Constant*> Values(2); |
| 2219 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 2220 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
| 2221 | Ivars.size()); |
| 2222 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
| 2223 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2224 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2225 | llvm::GlobalVariable *GV; |
| 2226 | if (ForClass) |
| 2227 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(), |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2228 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
| 2229 | 4, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2230 | else |
| 2231 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" |
| 2232 | + ID->getNameAsString(), |
| 2233 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2234 | 4, true); |
| 2235 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2236 | } |
| 2237 | |
| 2238 | /* |
| 2239 | struct objc_method { |
| 2240 | SEL method_name; |
| 2241 | char *method_types; |
| 2242 | void *method; |
| 2243 | }; |
| 2244 | |
| 2245 | struct objc_method_list { |
| 2246 | struct objc_method_list *obsolete; |
| 2247 | int count; |
| 2248 | struct objc_method methods_list[count]; |
| 2249 | }; |
| 2250 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2251 | |
| 2252 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 2253 | /// given method if it has been defined. The result is null if the |
| 2254 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2255 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2256 | // FIXME: Use DenseMap::lookup |
| 2257 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 2258 | if (!Fn) |
| 2259 | return 0; |
| 2260 | |
| 2261 | std::vector<llvm::Constant*> Method(3); |
| 2262 | Method[0] = |
| 2263 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 2264 | ObjCTypes.SelectorPtrTy); |
| 2265 | Method[1] = GetMethodVarType(MD); |
| 2266 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 2267 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 2268 | } |
| 2269 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2270 | llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name, |
| 2271 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2272 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2273 | // Return null for empty list. |
| 2274 | if (Methods.empty()) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 2275 | return VMContext.getNullValue(ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2276 | |
| 2277 | std::vector<llvm::Constant*> Values(3); |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 2278 | Values[0] = VMContext.getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2279 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 2280 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 2281 | Methods.size()); |
| 2282 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 2283 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2284 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2285 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2286 | return llvm::ConstantExpr::getBitCast(GV, |
| 2287 | ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 2290 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 2291 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2292 | std::string Name; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 2293 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2294 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2295 | CodeGenTypes &Types = CGM.getTypes(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2296 | const llvm::FunctionType *MethodTy = |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2297 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2298 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2299 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2300 | llvm::GlobalValue::InternalLinkage, |
| 2301 | Name, |
| 2302 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2303 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2304 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2305 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2306 | } |
| 2307 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2308 | llvm::GlobalVariable * |
| 2309 | CGObjCCommonMac::CreateMetadataVar(const std::string &Name, |
| 2310 | llvm::Constant *Init, |
| 2311 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2312 | unsigned Align, |
| 2313 | bool AddToUsed) { |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2314 | const llvm::Type *Ty = Init->getType(); |
| 2315 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2316 | new llvm::GlobalVariable(CGM.getModule(), Ty, false, |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2317 | llvm::GlobalValue::InternalLinkage, |
| 2318 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2319 | Name); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2320 | if (Section) |
| 2321 | GV->setSection(Section); |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2322 | if (Align) |
| 2323 | GV->setAlignment(Align); |
| 2324 | if (AddToUsed) |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2325 | UsedGlobals.push_back(GV); |
| 2326 | return GV; |
| 2327 | } |
| 2328 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2329 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2330 | // Abuse this interface function as a place to finalize. |
| 2331 | FinishModule(); |
| 2332 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2333 | return NULL; |
| 2334 | } |
| 2335 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2336 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2337 | return ObjCTypes.getGetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2340 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2341 | return ObjCTypes.getSetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2344 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2345 | return ObjCTypes.getEnumerationMutationFn(); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2346 | } |
| 2347 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2348 | /* |
| 2349 | |
| 2350 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 2351 | -- |
| 2352 | |
| 2353 | The basic framework for a @try-catch-finally is as follows: |
| 2354 | { |
| 2355 | objc_exception_data d; |
| 2356 | id _rethrow = null; |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2357 | bool _call_try_exit = true; |
| 2358 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2359 | objc_exception_try_enter(&d); |
| 2360 | if (!setjmp(d.jmp_buf)) { |
| 2361 | ... try body ... |
| 2362 | } else { |
| 2363 | // exception path |
| 2364 | id _caught = objc_exception_extract(&d); |
| 2365 | |
| 2366 | // enter new try scope for handlers |
| 2367 | if (!setjmp(d.jmp_buf)) { |
| 2368 | ... match exception and execute catch blocks ... |
| 2369 | |
| 2370 | // fell off end, rethrow. |
| 2371 | _rethrow = _caught; |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2372 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2373 | } else { |
| 2374 | // exception in catch block |
| 2375 | _rethrow = objc_exception_extract(&d); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2376 | _call_try_exit = false; |
| 2377 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2378 | } |
| 2379 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2380 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2381 | |
| 2382 | finally: |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2383 | if (_call_try_exit) |
| 2384 | objc_exception_try_exit(&d); |
| 2385 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2386 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2387 | ... dispatch to finally destination ... |
| 2388 | |
| 2389 | finally_rethrow: |
| 2390 | objc_exception_throw(_rethrow); |
| 2391 | |
| 2392 | finally_end: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2393 | } |
| 2394 | |
| 2395 | This framework differs slightly from the one gcc uses, in that gcc |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2396 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 2397 | and if the object should be rethrown. This breaks in the face of |
| 2398 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2399 | |
| 2400 | We specialize this framework for a few particular circumstances: |
| 2401 | |
| 2402 | - If there are no catch blocks, then we avoid emitting the second |
| 2403 | exception handling context. |
| 2404 | |
| 2405 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 2406 | e)) we avoid emitting the code to rethrow an uncaught exception. |
| 2407 | |
| 2408 | - FIXME: If there is no @finally block we can do a few more |
| 2409 | simplifications. |
| 2410 | |
| 2411 | Rethrows and Jumps-Through-Finally |
| 2412 | -- |
| 2413 | |
| 2414 | Support for implicit rethrows and jumping through the finally block is |
| 2415 | handled by storing the current exception-handling context in |
| 2416 | ObjCEHStack. |
| 2417 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2418 | In order to implement proper @finally semantics, we support one basic |
| 2419 | mechanism for jumping through the finally block to an arbitrary |
| 2420 | destination. Constructs which generate exits from a @try or @catch |
| 2421 | block use this mechanism to implement the proper semantics by chaining |
| 2422 | jumps, as necessary. |
| 2423 | |
| 2424 | This mechanism works like the one used for indirect goto: we |
| 2425 | arbitrarily assign an ID to each destination and store the ID for the |
| 2426 | destination in a variable prior to entering the finally block. At the |
| 2427 | end of the finally block we simply create a switch to the proper |
| 2428 | destination. |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2429 | |
| 2430 | Code gen for @synchronized(expr) stmt; |
| 2431 | Effectively generating code for: |
| 2432 | objc_sync_enter(expr); |
| 2433 | @try stmt @finally { objc_sync_exit(expr); } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2434 | */ |
| 2435 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2436 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 2437 | const Stmt &S) { |
| 2438 | bool isTry = isa<ObjCAtTryStmt>(S); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2439 | // Create various blocks we refer to for handling @finally. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2440 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2441 | llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit"); |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2442 | llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit"); |
| 2443 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
| 2444 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2445 | |
| 2446 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 2447 | // evaluation of the expression must occur before we enter the |
| 2448 | // @synchronized. We can safely avoid a temp here because jumps into |
| 2449 | // @synchronized are illegal & this will dominate uses. |
| 2450 | llvm::Value *SyncArg = 0; |
| 2451 | if (!isTry) { |
| 2452 | SyncArg = |
| 2453 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 2454 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 2455 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2456 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2457 | |
| 2458 | // Push an EH context entry, used for handling rethrows and jumps |
| 2459 | // through finally. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2460 | CGF.PushCleanupBlock(FinallyBlock); |
| 2461 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2462 | CGF.ObjCEHValueStack.push_back(0); |
| 2463 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2464 | // Allocate memory for the exception data and rethrow pointer. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2465 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 2466 | "exceptiondata.ptr"); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2467 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy, |
| 2468 | "_rethrow"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2469 | llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty, |
| 2470 | "_call_try_exit"); |
| 2471 | CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr); |
| 2472 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2473 | // Enter a new try block and call setjmp. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2474 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2475 | llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0, |
| 2476 | "jmpbufarray"); |
| 2477 | JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2478 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2479 | JmpBufPtr, "result"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2480 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2481 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 2482 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2483 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2484 | TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2485 | |
| 2486 | // Emit the @try block. |
| 2487 | CGF.EmitBlock(TryBlock); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2488 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 2489 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2490 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2491 | |
| 2492 | // Emit the "exception in @try" block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2493 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2494 | |
| 2495 | // Retrieve the exception object. We may emit multiple blocks but |
| 2496 | // nothing can cross this so the value is already in SSA form. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2497 | llvm::Value *Caught = |
| 2498 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2499 | ExceptionData, "caught"); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2500 | CGF.ObjCEHValueStack.back() = Caught; |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2501 | if (!isTry) |
| 2502 | { |
| 2503 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2504 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2505 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2506 | } |
| 2507 | else if (const ObjCAtCatchStmt* CatchStmt = |
| 2508 | cast<ObjCAtTryStmt>(S).getCatchStmts()) |
| 2509 | { |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2510 | // Enter a new exception try block (in case a @catch block throws |
| 2511 | // an exception). |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2512 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2513 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2514 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2515 | JmpBufPtr, "result"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2516 | llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2517 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2518 | llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch"); |
| 2519 | llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler"); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2520 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2521 | |
| 2522 | CGF.EmitBlock(CatchBlock); |
| 2523 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2524 | // Handle catch list. As a special case we check if everything is |
| 2525 | // matched and avoid generating code for falling off the end if |
| 2526 | // so. |
| 2527 | bool AllMatched = false; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2528 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2529 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2530 | |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2531 | const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2532 | const ObjCObjectPointerType *OPT = 0; |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2533 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2534 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2535 | if (!CatchParam) { |
| 2536 | AllMatched = true; |
| 2537 | } else { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2538 | OPT = CatchParam->getType()->getAsObjCObjectPointerType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2539 | |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 2540 | // catch(id e) always matches. |
| 2541 | // FIXME: For the time being we also match id<X>; this should |
| 2542 | // be rejected by Sema instead. |
Eli Friedman | 818e96f | 2009-07-11 00:57:02 +0000 | [diff] [blame] | 2543 | if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType())) |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2544 | AllMatched = true; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2545 | } |
| 2546 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2547 | if (AllMatched) { |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2548 | if (CatchParam) { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2549 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2550 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2551 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2552 | } |
Anders Carlsson | 1452f55 | 2008-09-11 08:21:54 +0000 | [diff] [blame] | 2553 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2554 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2555 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2556 | break; |
| 2557 | } |
| 2558 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2559 | assert(OPT && "Unexpected non-object pointer type in @catch"); |
| 2560 | QualType T = OPT->getPointeeType(); |
Anders Carlsson | 4b7ff6e | 2008-09-11 06:35:14 +0000 | [diff] [blame] | 2561 | const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2562 | assert(ObjCType && "Catch parameter must have Objective-C type!"); |
| 2563 | |
| 2564 | // Check if the @catch block matches the exception object. |
| 2565 | llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl()); |
| 2566 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2567 | llvm::Value *Match = |
| 2568 | CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(), |
| 2569 | Class, Caught, "match"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2570 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2571 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2572 | |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2573 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2574 | MatchedBlock, NextCatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2575 | |
| 2576 | // Emit the @catch block. |
| 2577 | CGF.EmitBlock(MatchedBlock); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2578 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2579 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2580 | |
| 2581 | llvm::Value *Tmp = |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2582 | CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()), |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2583 | "tmp"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2584 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2585 | |
| 2586 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2587 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2588 | |
| 2589 | CGF.EmitBlock(NextCatchBlock); |
| 2590 | } |
| 2591 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2592 | if (!AllMatched) { |
| 2593 | // None of the handlers caught the exception, so store it to be |
| 2594 | // rethrown at the end of the @finally block. |
| 2595 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2596 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2597 | } |
| 2598 | |
| 2599 | // Emit the exception handler for the @catch blocks. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2600 | CGF.EmitBlock(CatchHandler); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2601 | CGF.Builder.CreateStore( |
| 2602 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2603 | ExceptionData), |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2604 | RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2605 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2606 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2607 | } else { |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2608 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2609 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2610 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2611 | } |
| 2612 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2613 | // Pop the exception-handling stack entry. It is important to do |
| 2614 | // this now, because the code in the @finally block is not in this |
| 2615 | // context. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2616 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 2617 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2618 | CGF.ObjCEHValueStack.pop_back(); |
| 2619 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2620 | // Emit the @finally block. |
| 2621 | CGF.EmitBlock(FinallyBlock); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2622 | llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp"); |
| 2623 | |
| 2624 | CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit); |
| 2625 | |
| 2626 | CGF.EmitBlock(FinallyExit); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2627 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2628 | |
| 2629 | CGF.EmitBlock(FinallyNoExit); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2630 | if (isTry) { |
| 2631 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 2632 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 2633 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2634 | } else { |
| 2635 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 2636 | // @synchronized. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2637 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg); |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 2638 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2639 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2640 | // Emit the switch block |
| 2641 | if (Info.SwitchBlock) |
| 2642 | CGF.EmitBlock(Info.SwitchBlock); |
| 2643 | if (Info.EndBlock) |
| 2644 | CGF.EmitBlock(Info.EndBlock); |
| 2645 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2646 | CGF.EmitBlock(FinallyRethrow); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2647 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2648 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2649 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2650 | |
| 2651 | CGF.EmitBlock(FinallyEnd); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2655 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2656 | llvm::Value *ExceptionAsObject; |
| 2657 | |
| 2658 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 2659 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
| 2660 | ExceptionAsObject = |
| 2661 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 2662 | } else { |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2663 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2664 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2665 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2668 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2669 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2670 | |
| 2671 | // Clear the insertion point to indicate we are in unreachable code. |
| 2672 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2673 | } |
| 2674 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2675 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2676 | /// object: objc_read_weak (id *src) |
| 2677 | /// |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2678 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2679 | llvm::Value *AddrWeakObj) |
| 2680 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2681 | const llvm::Type* DestTy = |
| 2682 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2683 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2684 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2685 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2686 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2687 | return read_weak; |
| 2688 | } |
| 2689 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2690 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 2691 | /// objc_assign_weak (id src, id *dst) |
| 2692 | /// |
| 2693 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 2694 | llvm::Value *src, llvm::Value *dst) |
| 2695 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2696 | const llvm::Type * SrcTy = src->getType(); |
| 2697 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2698 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2699 | assert(Size <= 8 && "does not support size > 8"); |
| 2700 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2701 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2702 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2703 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2704 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2705 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 2706 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2707 | src, dst, "weakassign"); |
| 2708 | return; |
| 2709 | } |
| 2710 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2711 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 2712 | /// objc_assign_global (id src, id *dst) |
| 2713 | /// |
| 2714 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 2715 | llvm::Value *src, llvm::Value *dst) |
| 2716 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2717 | const llvm::Type * SrcTy = src->getType(); |
| 2718 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2719 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2720 | assert(Size <= 8 && "does not support size > 8"); |
| 2721 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2722 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2723 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2724 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2725 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2726 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2727 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2728 | src, dst, "globalassign"); |
| 2729 | return; |
| 2730 | } |
| 2731 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2732 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 2733 | /// objc_assign_ivar (id src, id *dst) |
| 2734 | /// |
| 2735 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 2736 | llvm::Value *src, llvm::Value *dst) |
| 2737 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2738 | const llvm::Type * SrcTy = src->getType(); |
| 2739 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2740 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2741 | assert(Size <= 8 && "does not support size > 8"); |
| 2742 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2743 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2744 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2745 | } |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2746 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2747 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2748 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(), |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2749 | src, dst, "assignivar"); |
| 2750 | return; |
| 2751 | } |
| 2752 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2753 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 2754 | /// objc_assign_strongCast (id src, id *dst) |
| 2755 | /// |
| 2756 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 2757 | llvm::Value *src, llvm::Value *dst) |
| 2758 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2759 | const llvm::Type * SrcTy = src->getType(); |
| 2760 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2761 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2762 | assert(Size <= 8 && "does not support size > 8"); |
| 2763 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2764 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2765 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2766 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2767 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2768 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2769 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2770 | src, dst, "weakassign"); |
| 2771 | return; |
| 2772 | } |
| 2773 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2774 | void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 2775 | llvm::Value *DestPtr, |
| 2776 | llvm::Value *SrcPtr, |
| 2777 | unsigned long size) { |
| 2778 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 2779 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
| 2780 | llvm::Value *N = llvm::ConstantInt::get(ObjCTypes.LongTy, size); |
| 2781 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), |
| 2782 | DestPtr, SrcPtr, N); |
| 2783 | return; |
| 2784 | } |
| 2785 | |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2786 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 2787 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2788 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 2789 | QualType ObjectTy, |
| 2790 | llvm::Value *BaseValue, |
| 2791 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2792 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 2793 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2794 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 2795 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2798 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 2799 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2800 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2801 | uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2802 | return llvm::ConstantInt::get( |
| 2803 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 2804 | Offset); |
| 2805 | } |
| 2806 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2807 | /* *** Private Interface *** */ |
| 2808 | |
| 2809 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 2810 | /// level information. |
| 2811 | /// |
| 2812 | /// See: <rdr://4810609&4810587&4810587> |
| 2813 | /// struct IMAGE_INFO { |
| 2814 | /// unsigned version; |
| 2815 | /// unsigned flags; |
| 2816 | /// }; |
| 2817 | enum ImageInfoFlags { |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2818 | eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what |
| 2819 | // this implies. |
| 2820 | eImageInfo_GarbageCollected = (1 << 1), |
| 2821 | eImageInfo_GCOnly = (1 << 2), |
| 2822 | eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set. |
| 2823 | |
| 2824 | // A flag indicating that the module has no instances of an |
| 2825 | // @synthesize of a superclass variable. <rdar://problem/6803242> |
| 2826 | eImageInfo_CorrectedSynthesize = (1 << 4) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2827 | }; |
| 2828 | |
| 2829 | void CGObjCMac::EmitImageInfo() { |
| 2830 | unsigned version = 0; // Version is unused? |
| 2831 | unsigned flags = 0; |
| 2832 | |
| 2833 | // FIXME: Fix and continue? |
| 2834 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 2835 | flags |= eImageInfo_GarbageCollected; |
| 2836 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 2837 | flags |= eImageInfo_GCOnly; |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2838 | |
| 2839 | // We never allow @synthesize of a superclass property. |
| 2840 | flags |= eImageInfo_CorrectedSynthesize; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2841 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2842 | // Emitted as int[2]; |
| 2843 | llvm::Constant *values[2] = { |
| 2844 | llvm::ConstantInt::get(llvm::Type::Int32Ty, version), |
| 2845 | llvm::ConstantInt::get(llvm::Type::Int32Ty, flags) |
| 2846 | }; |
| 2847 | llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2848 | |
| 2849 | const char *Section; |
| 2850 | if (ObjCABI == 1) |
| 2851 | Section = "__OBJC, __image_info,regular"; |
| 2852 | else |
| 2853 | Section = "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2854 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2855 | CreateMetadataVar("\01L_OBJC_IMAGE_INFO", |
| 2856 | llvm::ConstantArray::get(AT, values, 2), |
| 2857 | Section, |
| 2858 | 0, |
| 2859 | true); |
| 2860 | GV->setConstant(true); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2863 | |
| 2864 | // struct objc_module { |
| 2865 | // unsigned long version; |
| 2866 | // unsigned long size; |
| 2867 | // const char *name; |
| 2868 | // Symtab symtab; |
| 2869 | // }; |
| 2870 | |
| 2871 | // FIXME: Get from somewhere |
| 2872 | static const int ModuleVersion = 7; |
| 2873 | |
| 2874 | void CGObjCMac::EmitModuleInfo() { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2875 | uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2876 | |
| 2877 | std::vector<llvm::Constant*> Values(4); |
| 2878 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion); |
| 2879 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2880 | // This used to be the filename, now it is unused. <rdr://4327263> |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2881 | Values[2] = GetClassName(&CGM.getContext().Idents.get("")); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2882 | Values[3] = EmitModuleSymbols(); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2883 | CreateMetadataVar("\01L_OBJC_MODULES", |
| 2884 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
| 2885 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2886 | 4, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
| 2889 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2890 | unsigned NumClasses = DefinedClasses.size(); |
| 2891 | unsigned NumCategories = DefinedCategories.size(); |
| 2892 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2893 | // Return null if no symbols were defined. |
| 2894 | if (!NumClasses && !NumCategories) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 2895 | return VMContext.getNullValue(ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2896 | |
| 2897 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2898 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 2899 | Values[1] = VMContext.getNullValue(ObjCTypes.SelectorPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2900 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 2901 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
| 2902 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2903 | // The runtime expects exactly the list of defined classes followed |
| 2904 | // by the list of defined categories, in a single array. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2905 | std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2906 | for (unsigned i=0; i<NumClasses; i++) |
| 2907 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 2908 | ObjCTypes.Int8PtrTy); |
| 2909 | for (unsigned i=0; i<NumCategories; i++) |
| 2910 | Symbols[NumClasses + i] = |
| 2911 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 2912 | ObjCTypes.Int8PtrTy); |
| 2913 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2914 | Values[4] = |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2915 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2916 | NumClasses + NumCategories), |
| 2917 | Symbols); |
| 2918 | |
| 2919 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2920 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2921 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2922 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 2923 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2924 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2925 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
| 2926 | } |
| 2927 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2928 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2929 | const ObjCInterfaceDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2930 | LazySymbols.insert(ID->getIdentifier()); |
| 2931 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2932 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 2933 | |
| 2934 | if (!Entry) { |
| 2935 | llvm::Constant *Casted = |
| 2936 | llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()), |
| 2937 | ObjCTypes.ClassPtrTy); |
| 2938 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2939 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 2940 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2941 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2942 | } |
| 2943 | |
| 2944 | return Builder.CreateLoad(Entry, false, "tmp"); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2945 | } |
| 2946 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2947 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2948 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 2949 | |
| 2950 | if (!Entry) { |
| 2951 | llvm::Constant *Casted = |
| 2952 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 2953 | ObjCTypes.SelectorPtrTy); |
| 2954 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2955 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 2956 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2957 | 4, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2958 | } |
| 2959 | |
| 2960 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 2961 | } |
| 2962 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 2963 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2964 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2965 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2966 | if (!Entry) |
| 2967 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 2968 | llvm::ConstantArray::get(Ident->getName()), |
| 2969 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 2970 | 1, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2971 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2972 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2973 | } |
| 2974 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 2975 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 2976 | /// ivar layout bitmap. |
| 2977 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
| 2978 | const ObjCCommonTypesHelper &ObjCTypes) { |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 2979 | return VMContext.getNullValue(ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 2980 | } |
| 2981 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 2982 | static QualType::GCAttrTypes GetGCAttrTypeForType(ASTContext &Ctx, |
| 2983 | QualType FQT) { |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 2984 | if (FQT.isObjCGCStrong()) |
| 2985 | return QualType::Strong; |
| 2986 | |
| 2987 | if (FQT.isObjCGCWeak()) |
| 2988 | return QualType::Weak; |
| 2989 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 2990 | if (Ctx.isObjCObjectPointerType(FQT)) |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 2991 | return QualType::Strong; |
| 2992 | |
| 2993 | if (const PointerType *PT = FQT->getAsPointerType()) |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 2994 | return GetGCAttrTypeForType(Ctx, PT->getPointeeType()); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 2995 | |
| 2996 | return QualType::GCNone; |
| 2997 | } |
| 2998 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 2999 | void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT, |
| 3000 | unsigned int BytePos, |
| 3001 | bool ForStrongLayout, |
| 3002 | bool &HasUnion) { |
| 3003 | const RecordDecl *RD = RT->getDecl(); |
| 3004 | // FIXME - Use iterator. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3005 | llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end()); |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3006 | const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0)); |
| 3007 | const llvm::StructLayout *RecLayout = |
| 3008 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); |
| 3009 | |
| 3010 | BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos, |
| 3011 | ForStrongLayout, HasUnion); |
| 3012 | } |
| 3013 | |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 3014 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3015 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3016 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3017 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3018 | unsigned int BytePos, bool ForStrongLayout, |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3019 | bool &HasUnion) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3020 | bool IsUnion = (RD && RD->isUnion()); |
| 3021 | uint64_t MaxUnionIvarSize = 0; |
| 3022 | uint64_t MaxSkippedUnionIvarSize = 0; |
| 3023 | FieldDecl *MaxField = 0; |
| 3024 | FieldDecl *MaxSkippedField = 0; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3025 | FieldDecl *LastFieldBitfield = 0; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3026 | uint64_t MaxFieldOffset = 0; |
| 3027 | uint64_t MaxSkippedFieldOffset = 0; |
| 3028 | uint64_t LastBitfieldOffset = 0; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3029 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3030 | if (RecFields.empty()) |
| 3031 | return; |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3032 | unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0); |
| 3033 | unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth(); |
| 3034 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3035 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3036 | FieldDecl *Field = RecFields[i]; |
Daniel Dunbar | e05cc98 | 2009-05-03 23:35:23 +0000 | [diff] [blame] | 3037 | uint64_t FieldOffset; |
| 3038 | if (RD) |
| 3039 | FieldOffset = |
| 3040 | Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); |
| 3041 | else |
| 3042 | FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)); |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3043 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3044 | // Skip over unnamed or bitfields |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3045 | if (!Field->getIdentifier() || Field->isBitField()) { |
| 3046 | LastFieldBitfield = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3047 | LastBitfieldOffset = FieldOffset; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3048 | continue; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3049 | } |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3050 | |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3051 | LastFieldBitfield = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3052 | QualType FQT = Field->getType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3053 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3054 | if (FQT->isUnionType()) |
| 3055 | HasUnion = true; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3056 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3057 | BuildAggrIvarRecordLayout(FQT->getAsRecordType(), |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3058 | BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3059 | ForStrongLayout, HasUnion); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3060 | continue; |
| 3061 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3062 | |
| 3063 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3064 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3065 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3066 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3067 | assert(CArray && "only array with known element size is supported"); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3068 | FQT = CArray->getElementType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3069 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 3070 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3071 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3072 | ElCount *= CArray->getSize().getZExtValue(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3073 | FQT = CArray->getElementType(); |
| 3074 | } |
| 3075 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3076 | assert(!FQT->isUnionType() && |
| 3077 | "layout for array of unions not supported"); |
| 3078 | if (FQT->isRecordType()) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3079 | int OldIndex = IvarsInfo.size() - 1; |
| 3080 | int OldSkIndex = SkipIvars.size() -1; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3081 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3082 | const RecordType *RT = FQT->getAsRecordType(); |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3083 | BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3084 | ForStrongLayout, HasUnion); |
| 3085 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3086 | // Replicate layout information for each array element. Note that |
| 3087 | // one element is already done. |
| 3088 | uint64_t ElIx = 1; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3089 | for (int FirstIndex = IvarsInfo.size() - 1, |
| 3090 | FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3091 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3092 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 3093 | IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx, |
| 3094 | IvarsInfo[i].ivar_size)); |
| 3095 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) |
| 3096 | SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx, |
| 3097 | SkipIvars[i].ivar_size)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3098 | } |
| 3099 | continue; |
| 3100 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3101 | } |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3102 | // At this point, we are done with Record/Union and array there of. |
| 3103 | // For other arrays we are down to its element type. |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3104 | QualType::GCAttrTypes GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3105 | |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3106 | unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType()); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3107 | if ((ForStrongLayout && GCAttr == QualType::Strong) |
| 3108 | || (!ForStrongLayout && GCAttr == QualType::Weak)) { |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3109 | if (IsUnion) { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3110 | uint64_t UnionIvarSize = FieldSize / WordSizeInBits; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3111 | if (UnionIvarSize > MaxUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3112 | MaxUnionIvarSize = UnionIvarSize; |
| 3113 | MaxField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3114 | MaxFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3115 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3116 | } else { |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3117 | IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3118 | FieldSize / WordSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3119 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3120 | } else if ((ForStrongLayout && |
| 3121 | (GCAttr == QualType::GCNone || GCAttr == QualType::Weak)) |
| 3122 | || (!ForStrongLayout && GCAttr != QualType::Weak)) { |
| 3123 | if (IsUnion) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3124 | // FIXME: Why the asymmetry? We divide by word size in bits on other |
| 3125 | // side. |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3126 | uint64_t UnionIvarSize = FieldSize; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3127 | if (UnionIvarSize > MaxSkippedUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3128 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 3129 | MaxSkippedField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3130 | MaxSkippedFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3131 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3132 | } else { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3133 | // FIXME: Why the asymmetry, we divide by byte size in bits here? |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3134 | SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3135 | FieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3136 | } |
| 3137 | } |
| 3138 | } |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3139 | |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3140 | if (LastFieldBitfield) { |
| 3141 | // Last field was a bitfield. Must update skip info. |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3142 | Expr *BitWidth = LastFieldBitfield->getBitWidth(); |
| 3143 | uint64_t BitFieldSize = |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 3144 | BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue(); |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3145 | GC_IVAR skivar; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3146 | skivar.ivar_bytepos = BytePos + LastBitfieldOffset; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3147 | skivar.ivar_size = (BitFieldSize / ByteSizeInBits) |
| 3148 | + ((BitFieldSize % ByteSizeInBits) != 0); |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3149 | SkipIvars.push_back(skivar); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3150 | } |
| 3151 | |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3152 | if (MaxField) |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3153 | IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3154 | MaxUnionIvarSize)); |
| 3155 | if (MaxSkippedField) |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3156 | SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3157 | MaxSkippedUnionIvarSize)); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3158 | } |
| 3159 | |
| 3160 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 3161 | /// implementation for the __strong or __weak case. |
| 3162 | /// The layout map displays which words in ivar list must be skipped |
| 3163 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 3164 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
| 3165 | /// of words to skip and right nibble is count of words to scan. So, each |
| 3166 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
| 3167 | /// represented by a 0x00 byte which also ends the string. |
| 3168 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 3169 | /// - id, Class |
| 3170 | /// - object * |
| 3171 | /// - __strong anything |
| 3172 | /// |
| 3173 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 3174 | /// - __weak anything |
| 3175 | /// |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3176 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3177 | const ObjCImplementationDecl *OMD, |
| 3178 | bool ForStrongLayout) { |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3179 | bool hasUnion = false; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3180 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3181 | unsigned int WordsToScan, WordsToSkip; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3182 | const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 3183 | if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 3184 | return VMContext.getNullValue(PtrTy); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3185 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3186 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3187 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3188 | CGM.getContext().CollectObjCIvars(OI, RecFields); |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 3189 | |
Daniel Dunbar | 3715328 | 2009-05-04 04:10:48 +0000 | [diff] [blame] | 3190 | // Add this implementations synthesized ivars. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 3191 | llvm::SmallVector<ObjCIvarDecl*, 16> Ivars; |
| 3192 | CGM.getContext().CollectSynthesizedIvars(OI, Ivars); |
| 3193 | for (unsigned k = 0, e = Ivars.size(); k != e; ++k) |
| 3194 | RecFields.push_back(cast<FieldDecl>(Ivars[k])); |
| 3195 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3196 | if (RecFields.empty()) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 3197 | return VMContext.getNullValue(PtrTy); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3198 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3199 | SkipIvars.clear(); |
| 3200 | IvarsInfo.clear(); |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 3201 | |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 3202 | BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion); |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3203 | if (IvarsInfo.empty()) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 3204 | return VMContext.getNullValue(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3205 | |
| 3206 | // Sort on byte position in case we encounterred a union nested in |
| 3207 | // the ivar list. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3208 | if (hasUnion && !IvarsInfo.empty()) |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 3209 | std::sort(IvarsInfo.begin(), IvarsInfo.end()); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3210 | if (hasUnion && !SkipIvars.empty()) |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 3211 | std::sort(SkipIvars.begin(), SkipIvars.end()); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3212 | |
| 3213 | // Build the string of skip/scan nibbles |
Fariborz Jahanian | 8c2f2d1 | 2009-04-24 17:15:27 +0000 | [diff] [blame] | 3214 | llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3215 | unsigned int WordSize = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3216 | CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3217 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 3218 | WordsToSkip = 0; |
| 3219 | WordsToScan = IvarsInfo[0].ivar_size; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3220 | } else { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3221 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 3222 | WordsToScan = IvarsInfo[0].ivar_size; |
| 3223 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3224 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3225 | unsigned int TailPrevGCObjC = |
| 3226 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3227 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3228 | // consecutive 'scanned' object pointers. |
| 3229 | WordsToScan += IvarsInfo[i].ivar_size; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3230 | } else { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3231 | // Skip over 'gc'able object pointer which lay over each other. |
| 3232 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 3233 | continue; |
| 3234 | // Must skip over 1 or more words. We save current skip/scan values |
| 3235 | // and start a new pair. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3236 | SKIP_SCAN SkScan; |
| 3237 | SkScan.skip = WordsToSkip; |
| 3238 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3239 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3240 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3241 | // Skip the hole. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3242 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 3243 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3244 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3245 | WordsToSkip = 0; |
| 3246 | WordsToScan = IvarsInfo[i].ivar_size; |
| 3247 | } |
| 3248 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3249 | if (WordsToScan > 0) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3250 | SKIP_SCAN SkScan; |
| 3251 | SkScan.skip = WordsToSkip; |
| 3252 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3253 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3254 | } |
| 3255 | |
| 3256 | bool BytesSkipped = false; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3257 | if (!SkipIvars.empty()) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3258 | unsigned int LastIndex = SkipIvars.size()-1; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3259 | int LastByteSkipped = |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3260 | SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size; |
| 3261 | LastIndex = IvarsInfo.size()-1; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3262 | int LastByteScanned = |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3263 | IvarsInfo[LastIndex].ivar_bytepos + |
| 3264 | IvarsInfo[LastIndex].ivar_size * WordSize; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3265 | BytesSkipped = (LastByteSkipped > LastByteScanned); |
| 3266 | // 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] | 3267 | if (BytesSkipped) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3268 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3269 | SKIP_SCAN SkScan; |
| 3270 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
| 3271 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3272 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3273 | } |
| 3274 | } |
| 3275 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 3276 | // as 0xMN. |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3277 | int SkipScan = SkipScanIvars.size()-1; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3278 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3279 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 3280 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 3281 | // 0xM0 followed by 0x0N detected. |
| 3282 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 3283 | for (int j = i+1; j < SkipScan; j++) |
| 3284 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 3285 | --SkipScan; |
| 3286 | } |
| 3287 | } |
| 3288 | |
| 3289 | // Generate the string. |
| 3290 | std::string BitMap; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3291 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3292 | unsigned char byte; |
| 3293 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 3294 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 3295 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 3296 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
| 3297 | |
| 3298 | if (skip_small > 0 || skip_big > 0) |
| 3299 | BytesSkipped = true; |
| 3300 | // first skip big. |
| 3301 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 3302 | BitMap += (unsigned char)(0xf0); |
| 3303 | |
| 3304 | // next (skip small, scan) |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3305 | if (skip_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3306 | byte = skip_small << 4; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3307 | if (scan_big > 0) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3308 | byte |= 0xf; |
| 3309 | --scan_big; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3310 | } else if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3311 | byte |= scan_small; |
| 3312 | scan_small = 0; |
| 3313 | } |
| 3314 | BitMap += byte; |
| 3315 | } |
| 3316 | // next scan big |
| 3317 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 3318 | BitMap += (unsigned char)(0x0f); |
| 3319 | // last scan small |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3320 | if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3321 | byte = scan_small; |
| 3322 | BitMap += byte; |
| 3323 | } |
| 3324 | } |
| 3325 | // null terminate string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3326 | unsigned char zero = 0; |
| 3327 | BitMap += zero; |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3328 | |
| 3329 | if (CGM.getLangOptions().ObjCGCBitmapPrint) { |
| 3330 | printf("\n%s ivar layout for class '%s': ", |
| 3331 | ForStrongLayout ? "strong" : "weak", |
| 3332 | OMD->getClassInterface()->getNameAsCString()); |
| 3333 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
| 3334 | for (unsigned i = 0; i < BitMap.size(); i++) |
| 3335 | if (!(s[i] & 0xf0)) |
| 3336 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3337 | else |
| 3338 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3339 | printf("\n"); |
| 3340 | } |
| 3341 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3342 | // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as |
| 3343 | // final layout. |
| 3344 | if (ForStrongLayout && !BytesSkipped) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 3345 | return VMContext.getNullValue(PtrTy); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3346 | llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 3347 | llvm::ConstantArray::get(BitMap.c_str()), |
| 3348 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3349 | 1, true); |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3350 | return getConstantGEP(Entry, 0, 0); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3351 | } |
| 3352 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3353 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3354 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 3355 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3356 | // FIXME: Avoid std::string copying. |
| 3357 | if (!Entry) |
| 3358 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
| 3359 | llvm::ConstantArray::get(Sel.getAsString()), |
| 3360 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3361 | 1, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3362 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3363 | return getConstantGEP(Entry, 0, 0); |
| 3364 | } |
| 3365 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3366 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3367 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3368 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 3369 | } |
| 3370 | |
| 3371 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3372 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3373 | return GetMethodVarName(&CGM.getContext().Idents.get(Name)); |
| 3374 | } |
| 3375 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 3376 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3377 | std::string TypeStr; |
| 3378 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 3379 | |
| 3380 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3381 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3382 | if (!Entry) |
| 3383 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3384 | llvm::ConstantArray::get(TypeStr), |
| 3385 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3386 | 1, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3387 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3388 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3389 | } |
| 3390 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3391 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3392 | std::string TypeStr; |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3393 | CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D), |
| 3394 | TypeStr); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3395 | |
| 3396 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 3397 | |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3398 | if (!Entry) |
| 3399 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3400 | llvm::ConstantArray::get(TypeStr), |
| 3401 | "__TEXT,__cstring,cstring_literals", |
| 3402 | 1, true); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3403 | |
| 3404 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3405 | } |
| 3406 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3407 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3408 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3409 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
| 3410 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3411 | if (!Entry) |
| 3412 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
| 3413 | llvm::ConstantArray::get(Ident->getName()), |
| 3414 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3415 | 1, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3416 | |
| 3417 | return getConstantGEP(Entry, 0, 0); |
| 3418 | } |
| 3419 | |
| 3420 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3421 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3422 | llvm::Constant * |
| 3423 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 3424 | const Decl *Container) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3425 | std::string TypeStr; |
| 3426 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3427 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 3428 | } |
| 3429 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3430 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
| 3431 | const ObjCContainerDecl *CD, |
| 3432 | std::string &NameOut) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 3433 | NameOut = '\01'; |
| 3434 | NameOut += (D->isInstanceMethod() ? '-' : '+'); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3435 | NameOut += '['; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 3436 | assert (CD && "Missing container decl in GetNameForMethod"); |
| 3437 | NameOut += CD->getNameAsString(); |
Fariborz Jahanian | 1e9aef3 | 2009-04-16 18:34:20 +0000 | [diff] [blame] | 3438 | if (const ObjCCategoryImplDecl *CID = |
| 3439 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) { |
| 3440 | NameOut += '('; |
| 3441 | NameOut += CID->getNameAsString(); |
| 3442 | NameOut+= ')'; |
| 3443 | } |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3444 | NameOut += ' '; |
| 3445 | NameOut += D->getSelector().getAsString(); |
| 3446 | NameOut += ']'; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3447 | } |
| 3448 | |
Fariborz Jahanian | c38e9af | 2009-06-23 21:47:46 +0000 | [diff] [blame] | 3449 | void CGObjCCommonMac::MergeMetadataGlobals( |
| 3450 | std::vector<llvm::Constant*> &UsedArray) { |
| 3451 | llvm::Type *i8PTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 3452 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
| 3453 | e = UsedGlobals.end(); i != e; ++i) { |
| 3454 | UsedArray.push_back(llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(*i), |
| 3455 | i8PTy)); |
| 3456 | } |
| 3457 | } |
| 3458 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3459 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3460 | EmitModuleInfo(); |
| 3461 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3462 | // Emit the dummy bodies for any protocols which were referenced but |
| 3463 | // never defined. |
| 3464 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
| 3465 | i = Protocols.begin(), e = Protocols.end(); i != e; ++i) { |
| 3466 | if (i->second->hasInitializer()) |
| 3467 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3468 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3469 | std::vector<llvm::Constant*> Values(5); |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 3470 | Values[0] = VMContext.getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3471 | Values[1] = GetClassName(i->first); |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 3472 | Values[2] = VMContext.getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3473 | Values[3] = Values[4] = |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 3474 | VMContext.getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3475 | i->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 3476 | i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 3477 | Values)); |
| 3478 | } |
| 3479 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3480 | // Add assembler directives to add lazy undefined symbol references |
| 3481 | // for classes which are referenced but not defined. This is |
| 3482 | // important for correct linker interaction. |
| 3483 | |
| 3484 | // FIXME: Uh, this isn't particularly portable. |
| 3485 | std::stringstream s; |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3486 | |
| 3487 | if (!CGM.getModule().getModuleInlineAsm().empty()) |
| 3488 | s << "\n"; |
| 3489 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3490 | for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(), |
| 3491 | e = LazySymbols.end(); i != e; ++i) { |
| 3492 | s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n"; |
| 3493 | } |
| 3494 | for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(), |
| 3495 | e = DefinedSymbols.end(); i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3496 | s << "\t.objc_class_name_" << (*i)->getName() << "=0\n" |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3497 | << "\t.globl .objc_class_name_" << (*i)->getName() << "\n"; |
| 3498 | } |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3499 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3500 | CGM.getModule().appendModuleInlineAsm(s.str()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3501 | } |
| 3502 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3503 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3504 | : CGObjCCommonMac(cgm), |
| 3505 | ObjCTypes(cgm) |
| 3506 | { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3507 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3508 | ObjCABI = 2; |
| 3509 | } |
| 3510 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3511 | /* *** */ |
| 3512 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3513 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3514 | : CGM(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3515 | { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3516 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3517 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3518 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3519 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3520 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3521 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3522 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3523 | Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 3524 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3525 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 3526 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3527 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3528 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3529 | // FIXME: It would be nice to unify this with the opaque type, so that the IR |
| 3530 | // comes out a bit cleaner. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3531 | const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 3532 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3533 | |
| 3534 | // I'm not sure I like this. The implicit coordination is a bit |
| 3535 | // gross. We should solve this in a reasonable fashion because this |
| 3536 | // is a pretty common task (match some runtime data structure with |
| 3537 | // an LLVM data structure). |
| 3538 | |
| 3539 | // FIXME: This is leaked. |
| 3540 | // FIXME: Merge with rewriter code? |
| 3541 | |
| 3542 | // struct _objc_super { |
| 3543 | // id self; |
| 3544 | // Class cls; |
| 3545 | // } |
| 3546 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3547 | SourceLocation(), |
| 3548 | &Ctx.Idents.get("_objc_super")); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3549 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3550 | Ctx.getObjCIdType(), 0, false)); |
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.getObjCClassType(), 0, false)); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3553 | RD->completeDefinition(Ctx); |
| 3554 | |
| 3555 | SuperCTy = Ctx.getTagDeclType(RD); |
| 3556 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
| 3557 | |
| 3558 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3559 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 3560 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3561 | // struct _prop_t { |
| 3562 | // char *name; |
| 3563 | // char *attributes; |
| 3564 | // } |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 3565 | PropertyTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, NULL); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3566 | CGM.getModule().addTypeName("struct._prop_t", |
| 3567 | PropertyTy); |
| 3568 | |
| 3569 | // struct _prop_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3570 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3571 | // uint32_t count_of_properties; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3572 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3573 | // } |
| 3574 | PropertyListTy = llvm::StructType::get(IntTy, |
| 3575 | IntTy, |
| 3576 | llvm::ArrayType::get(PropertyTy, 0), |
| 3577 | NULL); |
| 3578 | CGM.getModule().addTypeName("struct._prop_list_t", |
| 3579 | PropertyListTy); |
| 3580 | // struct _prop_list_t * |
| 3581 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
| 3582 | |
| 3583 | // struct _objc_method { |
| 3584 | // SEL _cmd; |
| 3585 | // char *method_type; |
| 3586 | // char *_imp; |
| 3587 | // } |
| 3588 | MethodTy = llvm::StructType::get(SelectorPtrTy, |
| 3589 | Int8PtrTy, |
| 3590 | Int8PtrTy, |
| 3591 | NULL); |
| 3592 | CGM.getModule().addTypeName("struct._objc_method", MethodTy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3593 | |
| 3594 | // struct _objc_cache * |
| 3595 | CacheTy = llvm::OpaqueType::get(); |
| 3596 | CGM.getModule().addTypeName("struct._objc_cache", CacheTy); |
| 3597 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3598 | } |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3599 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3600 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3601 | : ObjCCommonTypesHelper(cgm) |
| 3602 | { |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3603 | // struct _objc_method_description { |
| 3604 | // SEL name; |
| 3605 | // char *types; |
| 3606 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3607 | MethodDescriptionTy = |
| 3608 | llvm::StructType::get(SelectorPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3609 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3610 | NULL); |
| 3611 | CGM.getModule().addTypeName("struct._objc_method_description", |
| 3612 | MethodDescriptionTy); |
| 3613 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3614 | // struct _objc_method_description_list { |
| 3615 | // int count; |
| 3616 | // struct _objc_method_description[1]; |
| 3617 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3618 | MethodDescriptionListTy = |
| 3619 | llvm::StructType::get(IntTy, |
| 3620 | llvm::ArrayType::get(MethodDescriptionTy, 0), |
| 3621 | NULL); |
| 3622 | CGM.getModule().addTypeName("struct._objc_method_description_list", |
| 3623 | MethodDescriptionListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3624 | |
| 3625 | // struct _objc_method_description_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3626 | MethodDescriptionListPtrTy = |
| 3627 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
| 3628 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3629 | // Protocol description structures |
| 3630 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3631 | // struct _objc_protocol_extension { |
| 3632 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 3633 | // struct _objc_method_description_list *optional_instance_methods; |
| 3634 | // struct _objc_method_description_list *optional_class_methods; |
| 3635 | // struct _objc_property_list *instance_properties; |
| 3636 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3637 | ProtocolExtensionTy = |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3638 | llvm::StructType::get(IntTy, |
| 3639 | MethodDescriptionListPtrTy, |
| 3640 | MethodDescriptionListPtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3641 | PropertyListPtrTy, |
| 3642 | NULL); |
| 3643 | CGM.getModule().addTypeName("struct._objc_protocol_extension", |
| 3644 | ProtocolExtensionTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3645 | |
| 3646 | // struct _objc_protocol_extension * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3647 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
| 3648 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3649 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3650 | |
| 3651 | llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(); |
| 3652 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3653 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3654 | const llvm::Type *T = |
| 3655 | llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3656 | LongTy, |
| 3657 | llvm::ArrayType::get(ProtocolTyHolder, 0), |
| 3658 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3659 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T); |
| 3660 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3661 | // struct _objc_protocol { |
| 3662 | // struct _objc_protocol_extension *isa; |
| 3663 | // char *protocol_name; |
| 3664 | // struct _objc_protocol **_objc_protocol_list; |
| 3665 | // struct _objc_method_description_list *instance_methods; |
| 3666 | // struct _objc_method_description_list *class_methods; |
| 3667 | // } |
| 3668 | T = llvm::StructType::get(ProtocolExtensionPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3669 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3670 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3671 | MethodDescriptionListPtrTy, |
| 3672 | MethodDescriptionListPtrTy, |
| 3673 | NULL); |
| 3674 | cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T); |
| 3675 | |
| 3676 | ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get()); |
| 3677 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3678 | ProtocolListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3679 | // struct _objc_protocol_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3680 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
| 3681 | |
| 3682 | ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get()); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3683 | CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3684 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3685 | |
| 3686 | // Class description structures |
| 3687 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3688 | // struct _objc_ivar { |
| 3689 | // char *ivar_name; |
| 3690 | // char *ivar_type; |
| 3691 | // int ivar_offset; |
| 3692 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3693 | IvarTy = llvm::StructType::get(Int8PtrTy, |
| 3694 | Int8PtrTy, |
| 3695 | IntTy, |
| 3696 | NULL); |
| 3697 | CGM.getModule().addTypeName("struct._objc_ivar", IvarTy); |
| 3698 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3699 | // struct _objc_ivar_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3700 | IvarListTy = llvm::OpaqueType::get(); |
| 3701 | CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy); |
| 3702 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
| 3703 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3704 | // struct _objc_method_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3705 | MethodListTy = llvm::OpaqueType::get(); |
| 3706 | CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy); |
| 3707 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
| 3708 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3709 | // struct _objc_class_extension * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3710 | ClassExtensionTy = |
| 3711 | llvm::StructType::get(IntTy, |
| 3712 | Int8PtrTy, |
| 3713 | PropertyListPtrTy, |
| 3714 | NULL); |
| 3715 | CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy); |
| 3716 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
| 3717 | |
| 3718 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3719 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3720 | // struct _objc_class { |
| 3721 | // Class isa; |
| 3722 | // Class super_class; |
| 3723 | // char *name; |
| 3724 | // long version; |
| 3725 | // long info; |
| 3726 | // long instance_size; |
| 3727 | // struct _objc_ivar_list *ivars; |
| 3728 | // struct _objc_method_list *methods; |
| 3729 | // struct _objc_cache *cache; |
| 3730 | // struct _objc_protocol_list *protocols; |
| 3731 | // char *ivar_layout; |
| 3732 | // struct _objc_class_ext *ext; |
| 3733 | // }; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3734 | T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3735 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3736 | Int8PtrTy, |
| 3737 | LongTy, |
| 3738 | LongTy, |
| 3739 | LongTy, |
| 3740 | IvarListPtrTy, |
| 3741 | MethodListPtrTy, |
| 3742 | CachePtrTy, |
| 3743 | ProtocolListPtrTy, |
| 3744 | Int8PtrTy, |
| 3745 | ClassExtensionPtrTy, |
| 3746 | NULL); |
| 3747 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T); |
| 3748 | |
| 3749 | ClassTy = cast<llvm::StructType>(ClassTyHolder.get()); |
| 3750 | CGM.getModule().addTypeName("struct._objc_class", ClassTy); |
| 3751 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
| 3752 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3753 | // struct _objc_category { |
| 3754 | // char *category_name; |
| 3755 | // char *class_name; |
| 3756 | // struct _objc_method_list *instance_method; |
| 3757 | // struct _objc_method_list *class_method; |
| 3758 | // uint32_t size; // sizeof(struct _objc_category) |
| 3759 | // struct _objc_property_list *instance_properties;// category's @property |
| 3760 | // } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3761 | CategoryTy = llvm::StructType::get(Int8PtrTy, |
| 3762 | Int8PtrTy, |
| 3763 | MethodListPtrTy, |
| 3764 | MethodListPtrTy, |
| 3765 | ProtocolListPtrTy, |
| 3766 | IntTy, |
| 3767 | PropertyListPtrTy, |
| 3768 | NULL); |
| 3769 | CGM.getModule().addTypeName("struct._objc_category", CategoryTy); |
| 3770 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3771 | // Global metadata structures |
| 3772 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3773 | // struct _objc_symtab { |
| 3774 | // long sel_ref_cnt; |
| 3775 | // SEL *refs; |
| 3776 | // short cls_def_cnt; |
| 3777 | // short cat_def_cnt; |
| 3778 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 3779 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3780 | SymtabTy = llvm::StructType::get(LongTy, |
| 3781 | SelectorPtrTy, |
| 3782 | ShortTy, |
| 3783 | ShortTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3784 | llvm::ArrayType::get(Int8PtrTy, 0), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3785 | NULL); |
| 3786 | CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy); |
| 3787 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
| 3788 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3789 | // struct _objc_module { |
| 3790 | // long version; |
| 3791 | // long size; // sizeof(struct _objc_module) |
| 3792 | // char *name; |
| 3793 | // struct _objc_symtab* symtab; |
| 3794 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3795 | ModuleTy = |
| 3796 | llvm::StructType::get(LongTy, |
| 3797 | LongTy, |
| 3798 | Int8PtrTy, |
| 3799 | SymtabPtrTy, |
| 3800 | NULL); |
| 3801 | CGM.getModule().addTypeName("struct._objc_module", ModuleTy); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3802 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 3803 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3804 | // FIXME: This is the size of the setjmp buffer and should be target |
| 3805 | // specific. 18 is what's used on 32-bit X86. |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3806 | uint64_t SetJmpBufferSize = 18; |
| 3807 | |
| 3808 | // Exceptions |
| 3809 | const llvm::Type *StackPtrTy = |
Daniel Dunbar | 1000491 | 2008-09-27 06:32:25 +0000 | [diff] [blame] | 3810 | llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3811 | |
| 3812 | ExceptionDataTy = |
| 3813 | llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty, |
| 3814 | SetJmpBufferSize), |
| 3815 | StackPtrTy, NULL); |
| 3816 | CGM.getModule().addTypeName("struct._objc_exception_data", |
| 3817 | ExceptionDataTy); |
| 3818 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3819 | } |
| 3820 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3821 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3822 | : ObjCCommonTypesHelper(cgm) |
| 3823 | { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3824 | // struct _method_list_t { |
| 3825 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 3826 | // uint32_t method_count; |
| 3827 | // struct _objc_method method_list[method_count]; |
| 3828 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3829 | MethodListnfABITy = llvm::StructType::get(IntTy, |
| 3830 | IntTy, |
| 3831 | llvm::ArrayType::get(MethodTy, 0), |
| 3832 | NULL); |
| 3833 | CGM.getModule().addTypeName("struct.__method_list_t", |
| 3834 | MethodListnfABITy); |
| 3835 | // struct method_list_t * |
| 3836 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3837 | |
| 3838 | // struct _protocol_t { |
| 3839 | // id isa; // NULL |
| 3840 | // const char * const protocol_name; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3841 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3842 | // const struct method_list_t * const instance_methods; |
| 3843 | // const struct method_list_t * const class_methods; |
| 3844 | // const struct method_list_t *optionalInstanceMethods; |
| 3845 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3846 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3847 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 3848 | // const uint32_t flags; // = 0 |
| 3849 | // } |
| 3850 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3851 | // Holder for struct _protocol_list_t * |
| 3852 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3853 | |
| 3854 | ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy, |
| 3855 | Int8PtrTy, |
| 3856 | llvm::PointerType::getUnqual( |
| 3857 | ProtocolListTyHolder), |
| 3858 | MethodListnfABIPtrTy, |
| 3859 | MethodListnfABIPtrTy, |
| 3860 | MethodListnfABIPtrTy, |
| 3861 | MethodListnfABIPtrTy, |
| 3862 | PropertyListPtrTy, |
| 3863 | IntTy, |
| 3864 | IntTy, |
| 3865 | NULL); |
| 3866 | CGM.getModule().addTypeName("struct._protocol_t", |
| 3867 | ProtocolnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3868 | |
| 3869 | // struct _protocol_t* |
| 3870 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3871 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3872 | // struct _protocol_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3873 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3874 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3875 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3876 | ProtocolListnfABITy = llvm::StructType::get(LongTy, |
| 3877 | llvm::ArrayType::get( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3878 | ProtocolnfABIPtrTy, 0), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3879 | NULL); |
| 3880 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3881 | ProtocolListnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3882 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo( |
| 3883 | ProtocolListnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3884 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3885 | // struct _objc_protocol_list* |
| 3886 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3887 | |
| 3888 | // struct _ivar_t { |
| 3889 | // unsigned long int *offset; // pointer to ivar offset location |
| 3890 | // char *name; |
| 3891 | // char *type; |
| 3892 | // uint32_t alignment; |
| 3893 | // uint32_t size; |
| 3894 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3895 | IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy), |
| 3896 | Int8PtrTy, |
| 3897 | Int8PtrTy, |
| 3898 | IntTy, |
| 3899 | IntTy, |
| 3900 | NULL); |
| 3901 | CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy); |
| 3902 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3903 | // struct _ivar_list_t { |
| 3904 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 3905 | // uint32 count; |
| 3906 | // struct _iver_t list[count]; |
| 3907 | // } |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3908 | IvarListnfABITy = llvm::StructType::get(IntTy, |
| 3909 | IntTy, |
| 3910 | llvm::ArrayType::get( |
| 3911 | IvarnfABITy, 0), |
| 3912 | NULL); |
| 3913 | CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy); |
| 3914 | |
| 3915 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3916 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3917 | // struct _class_ro_t { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3918 | // uint32_t const flags; |
| 3919 | // uint32_t const instanceStart; |
| 3920 | // uint32_t const instanceSize; |
| 3921 | // uint32_t const reserved; // only when building for 64bit targets |
| 3922 | // const uint8_t * const ivarLayout; |
| 3923 | // const char *const name; |
| 3924 | // const struct _method_list_t * const baseMethods; |
| 3925 | // const struct _objc_protocol_list *const baseProtocols; |
| 3926 | // const struct _ivar_list_t *const ivars; |
| 3927 | // const uint8_t * const weakIvarLayout; |
| 3928 | // const struct _prop_list_t * const properties; |
| 3929 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3930 | |
| 3931 | // FIXME. Add 'reserved' field in 64bit abi mode! |
| 3932 | ClassRonfABITy = llvm::StructType::get(IntTy, |
| 3933 | IntTy, |
| 3934 | IntTy, |
| 3935 | Int8PtrTy, |
| 3936 | Int8PtrTy, |
| 3937 | MethodListnfABIPtrTy, |
| 3938 | ProtocolListnfABIPtrTy, |
| 3939 | IvarListnfABIPtrTy, |
| 3940 | Int8PtrTy, |
| 3941 | PropertyListPtrTy, |
| 3942 | NULL); |
| 3943 | CGM.getModule().addTypeName("struct._class_ro_t", |
| 3944 | ClassRonfABITy); |
| 3945 | |
| 3946 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 3947 | std::vector<const llvm::Type*> Params; |
| 3948 | Params.push_back(ObjectPtrTy); |
| 3949 | Params.push_back(SelectorPtrTy); |
| 3950 | ImpnfABITy = llvm::PointerType::getUnqual( |
| 3951 | llvm::FunctionType::get(ObjectPtrTy, Params, false)); |
| 3952 | |
| 3953 | // struct _class_t { |
| 3954 | // struct _class_t *isa; |
| 3955 | // struct _class_t * const superclass; |
| 3956 | // void *cache; |
| 3957 | // IMP *vtable; |
| 3958 | // struct class_ro_t *ro; |
| 3959 | // } |
| 3960 | |
| 3961 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3962 | ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3963 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3964 | CachePtrTy, |
| 3965 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 3966 | llvm::PointerType::getUnqual( |
| 3967 | ClassRonfABITy), |
| 3968 | NULL); |
| 3969 | CGM.getModule().addTypeName("struct._class_t", ClassnfABITy); |
| 3970 | |
| 3971 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo( |
| 3972 | ClassnfABITy); |
| 3973 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3974 | // LLVM for struct _class_t * |
| 3975 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
| 3976 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3977 | // struct _category_t { |
| 3978 | // const char * const name; |
| 3979 | // struct _class_t *const cls; |
| 3980 | // const struct _method_list_t * const instance_methods; |
| 3981 | // const struct _method_list_t * const class_methods; |
| 3982 | // const struct _protocol_list_t * const protocols; |
| 3983 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3984 | // } |
| 3985 | CategorynfABITy = llvm::StructType::get(Int8PtrTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3986 | ClassnfABIPtrTy, |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3987 | MethodListnfABIPtrTy, |
| 3988 | MethodListnfABIPtrTy, |
| 3989 | ProtocolListnfABIPtrTy, |
| 3990 | PropertyListPtrTy, |
| 3991 | NULL); |
| 3992 | CGM.getModule().addTypeName("struct._category_t", CategorynfABITy); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3993 | |
| 3994 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3995 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3996 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3997 | |
| 3998 | // MessageRefTy - LLVM for: |
| 3999 | // struct _message_ref_t { |
| 4000 | // IMP messenger; |
| 4001 | // SEL name; |
| 4002 | // }; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4003 | |
| 4004 | // First the clang type for struct _message_ref_t |
| 4005 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 4006 | SourceLocation(), |
| 4007 | &Ctx.Idents.get("_message_ref_t")); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4008 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4009 | Ctx.VoidPtrTy, 0, false)); |
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.getObjCSelType(), 0, false)); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4012 | RD->completeDefinition(Ctx); |
| 4013 | |
| 4014 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 4015 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 4016 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4017 | |
| 4018 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 4019 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
| 4020 | |
| 4021 | // SuperMessageRefTy - LLVM for: |
| 4022 | // struct _super_message_ref_t { |
| 4023 | // SUPER_IMP messenger; |
| 4024 | // SEL name; |
| 4025 | // }; |
| 4026 | SuperMessageRefTy = llvm::StructType::get(ImpnfABITy, |
| 4027 | SelectorPtrTy, |
| 4028 | NULL); |
| 4029 | CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy); |
| 4030 | |
| 4031 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 4032 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
| 4033 | |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 4034 | |
| 4035 | // struct objc_typeinfo { |
| 4036 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 4037 | // const char* name; // c++ typeinfo string |
| 4038 | // Class cls; |
| 4039 | // }; |
| 4040 | EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy), |
| 4041 | Int8PtrTy, |
| 4042 | ClassnfABIPtrTy, |
| 4043 | NULL); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 4044 | CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 4045 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4046 | } |
| 4047 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4048 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
| 4049 | FinishNonFragileABIModule(); |
| 4050 | |
| 4051 | return NULL; |
| 4052 | } |
| 4053 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4054 | void CGObjCNonFragileABIMac::AddModuleClassList(const |
| 4055 | std::vector<llvm::GlobalValue*> |
| 4056 | &Container, |
| 4057 | const char *SymbolName, |
| 4058 | const char *SectionName) { |
| 4059 | unsigned NumClasses = Container.size(); |
| 4060 | |
| 4061 | if (!NumClasses) |
| 4062 | return; |
| 4063 | |
| 4064 | std::vector<llvm::Constant*> Symbols(NumClasses); |
| 4065 | for (unsigned i=0; i<NumClasses; i++) |
| 4066 | Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i], |
| 4067 | ObjCTypes.Int8PtrTy); |
| 4068 | llvm::Constant* Init = |
| 4069 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 4070 | NumClasses), |
| 4071 | Symbols); |
| 4072 | |
| 4073 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4074 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4075 | llvm::GlobalValue::InternalLinkage, |
| 4076 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4077 | SymbolName); |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4078 | GV->setAlignment(8); |
| 4079 | GV->setSection(SectionName); |
| 4080 | UsedGlobals.push_back(GV); |
| 4081 | } |
| 4082 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4083 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 4084 | // nonfragile abi has no module definition. |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4085 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4086 | // Build list of all implemented class addresses in array |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4087 | // L_OBJC_LABEL_CLASS_$. |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4088 | AddModuleClassList(DefinedClasses, |
| 4089 | "\01L_OBJC_LABEL_CLASS_$", |
| 4090 | "__DATA, __objc_classlist, regular, no_dead_strip"); |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4091 | AddModuleClassList(DefinedNonLazyClasses, |
| 4092 | "\01L_OBJC_LABEL_NONLAZY_CLASS_$", |
| 4093 | "__DATA, __objc_nlclslist, regular, no_dead_strip"); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4094 | |
| 4095 | // Build list of all implemented category addresses in array |
| 4096 | // L_OBJC_LABEL_CATEGORY_$. |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4097 | AddModuleClassList(DefinedCategories, |
| 4098 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 4099 | "__DATA, __objc_catlist, regular, no_dead_strip"); |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4100 | AddModuleClassList(DefinedNonLazyCategories, |
| 4101 | "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$", |
| 4102 | "__DATA, __objc_nlcatlist, regular, no_dead_strip"); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4103 | |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4104 | // static int L_OBJC_IMAGE_INFO[2] = { 0, flags }; |
| 4105 | // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0 |
| 4106 | std::vector<llvm::Constant*> Values(2); |
| 4107 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0); |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4108 | unsigned int flags = 0; |
Fariborz Jahanian | 66a5c2c | 2009-02-24 23:34:44 +0000 | [diff] [blame] | 4109 | // FIXME: Fix and continue? |
| 4110 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 4111 | flags |= eImageInfo_GarbageCollected; |
| 4112 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 4113 | flags |= eImageInfo_GCOnly; |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4114 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4115 | llvm::Constant* Init = llvm::ConstantArray::get( |
| 4116 | llvm::ArrayType::get(ObjCTypes.IntTy, 2), |
| 4117 | Values); |
| 4118 | llvm::GlobalVariable *IMGV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4119 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4120 | llvm::GlobalValue::InternalLinkage, |
| 4121 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4122 | "\01L_OBJC_IMAGE_INFO"); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4123 | IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); |
Daniel Dunbar | 325f758 | 2009-04-23 08:03:21 +0000 | [diff] [blame] | 4124 | IMGV->setConstant(true); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4125 | UsedGlobals.push_back(IMGV); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4126 | } |
| 4127 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4128 | /// LegacyDispatchedSelector - Returns true if SEL is not in the list of |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4129 | /// NonLegacyDispatchMethods; false otherwise. What this means is that |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4130 | /// except for the 19 selectors in the list, we generate 32bit-style |
| 4131 | /// message dispatch call for all the rest. |
| 4132 | /// |
| 4133 | bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) { |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4134 | if (NonLegacyDispatchMethods.empty()) { |
| 4135 | NonLegacyDispatchMethods.insert(GetNullarySelector("alloc")); |
| 4136 | NonLegacyDispatchMethods.insert(GetNullarySelector("class")); |
| 4137 | NonLegacyDispatchMethods.insert(GetNullarySelector("self")); |
| 4138 | NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped")); |
| 4139 | NonLegacyDispatchMethods.insert(GetNullarySelector("length")); |
| 4140 | NonLegacyDispatchMethods.insert(GetNullarySelector("count")); |
| 4141 | NonLegacyDispatchMethods.insert(GetNullarySelector("retain")); |
| 4142 | NonLegacyDispatchMethods.insert(GetNullarySelector("release")); |
| 4143 | NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease")); |
| 4144 | NonLegacyDispatchMethods.insert(GetNullarySelector("hash")); |
| 4145 | |
| 4146 | NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone")); |
| 4147 | NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass")); |
| 4148 | NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector")); |
| 4149 | NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey")); |
| 4150 | NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex")); |
| 4151 | NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString")); |
| 4152 | NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual")); |
| 4153 | NonLegacyDispatchMethods.insert(GetUnarySelector("addObject")); |
Fariborz Jahanian | be53be4 | 2009-05-13 16:19:02 +0000 | [diff] [blame] | 4154 | // "countByEnumeratingWithState:objects:count" |
| 4155 | IdentifierInfo *KeyIdents[] = { |
| 4156 | &CGM.getContext().Idents.get("countByEnumeratingWithState"), |
| 4157 | &CGM.getContext().Idents.get("objects"), |
| 4158 | &CGM.getContext().Idents.get("count") |
| 4159 | }; |
| 4160 | NonLegacyDispatchMethods.insert( |
| 4161 | CGM.getContext().Selectors.getSelector(3, KeyIdents)); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4162 | } |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4163 | return (NonLegacyDispatchMethods.count(Sel) == 0); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4164 | } |
| 4165 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4166 | // Metadata flags |
| 4167 | enum MetaDataDlags { |
| 4168 | CLS = 0x0, |
| 4169 | CLS_META = 0x1, |
| 4170 | CLS_ROOT = 0x2, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4171 | OBJC2_CLS_HIDDEN = 0x10, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4172 | CLS_EXCEPTION = 0x20 |
| 4173 | }; |
| 4174 | /// BuildClassRoTInitializer - generate meta-data for: |
| 4175 | /// struct _class_ro_t { |
| 4176 | /// uint32_t const flags; |
| 4177 | /// uint32_t const instanceStart; |
| 4178 | /// uint32_t const instanceSize; |
| 4179 | /// uint32_t const reserved; // only when building for 64bit targets |
| 4180 | /// const uint8_t * const ivarLayout; |
| 4181 | /// const char *const name; |
| 4182 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4183 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4184 | /// const struct _ivar_list_t *const ivars; |
| 4185 | /// const uint8_t * const weakIvarLayout; |
| 4186 | /// const struct _prop_list_t * const properties; |
| 4187 | /// } |
| 4188 | /// |
| 4189 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
| 4190 | unsigned flags, |
| 4191 | unsigned InstanceStart, |
| 4192 | unsigned InstanceSize, |
| 4193 | const ObjCImplementationDecl *ID) { |
| 4194 | std::string ClassName = ID->getNameAsString(); |
| 4195 | std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets! |
| 4196 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 4197 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 4198 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
| 4199 | // FIXME. For 64bit targets add 0 here. |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 4200 | Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4201 | : BuildIvarLayout(ID, true); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4202 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4203 | // const struct _method_list_t * const baseMethods; |
| 4204 | std::vector<llvm::Constant*> Methods; |
| 4205 | std::string MethodListName("\01l_OBJC_$_"); |
| 4206 | if (flags & CLS_META) { |
| 4207 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4208 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4209 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4210 | // Class methods should always be defined. |
| 4211 | Methods.push_back(GetMethodConstant(*i)); |
| 4212 | } |
| 4213 | } else { |
| 4214 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4215 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4216 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4217 | // Instance methods should always be defined. |
| 4218 | Methods.push_back(GetMethodConstant(*i)); |
| 4219 | } |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4220 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4221 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4222 | ObjCPropertyImplDecl *PID = *i; |
| 4223 | |
| 4224 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 4225 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 4226 | |
| 4227 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 4228 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4229 | Methods.push_back(C); |
| 4230 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 4231 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4232 | Methods.push_back(C); |
| 4233 | } |
| 4234 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4235 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4236 | Values[ 5] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4237 | "__DATA, __objc_const", Methods); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4238 | |
| 4239 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4240 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
| 4241 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
| 4242 | + OID->getNameAsString(), |
| 4243 | OID->protocol_begin(), |
| 4244 | OID->protocol_end()); |
| 4245 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4246 | if (flags & CLS_META) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 4247 | Values[ 7] = VMContext.getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4248 | else |
| 4249 | Values[ 7] = EmitIvarList(ID); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 4250 | Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4251 | : BuildIvarLayout(ID, false); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4252 | if (flags & CLS_META) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 4253 | Values[ 9] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4254 | else |
| 4255 | Values[ 9] = |
| 4256 | EmitPropertyList( |
| 4257 | "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
| 4258 | ID, ID->getClassInterface(), ObjCTypes); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4259 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
| 4260 | Values); |
| 4261 | llvm::GlobalVariable *CLASS_RO_GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4262 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4263 | llvm::GlobalValue::InternalLinkage, |
| 4264 | Init, |
| 4265 | (flags & CLS_META) ? |
| 4266 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4267 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4268 | CLASS_RO_GV->setAlignment( |
| 4269 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4270 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4271 | return CLASS_RO_GV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4272 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4273 | } |
| 4274 | |
| 4275 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 4276 | /// for the given ClassName for: |
| 4277 | /// struct _class_t { |
| 4278 | /// struct _class_t *isa; |
| 4279 | /// struct _class_t * const superclass; |
| 4280 | /// void *cache; |
| 4281 | /// IMP *vtable; |
| 4282 | /// struct class_ro_t *ro; |
| 4283 | /// } |
| 4284 | /// |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4285 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
| 4286 | std::string &ClassName, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4287 | llvm::Constant *IsAGV, |
| 4288 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4289 | llvm::Constant *ClassRoGV, |
| 4290 | bool HiddenVisibility) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4291 | std::vector<llvm::Constant*> Values(5); |
| 4292 | Values[0] = IsAGV; |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4293 | Values[1] = SuperClassGV |
| 4294 | ? SuperClassGV |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 4295 | : VMContext.getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4296 | Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar |
| 4297 | Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar |
| 4298 | Values[4] = ClassRoGV; // &CLASS_RO_GV |
| 4299 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
| 4300 | Values); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4301 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 4302 | GV->setInitializer(Init); |
Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 4303 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4304 | GV->setAlignment( |
| 4305 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4306 | if (HiddenVisibility) |
| 4307 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4308 | return GV; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4309 | } |
| 4310 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4311 | bool |
Fariborz Jahanian | ecfbdcb | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 4312 | CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4313 | return OD->getClassMethod(GetNullarySelector("load")) != 0; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4314 | } |
| 4315 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4316 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4317 | uint32_t &InstanceStart, |
| 4318 | uint32_t &InstanceSize) { |
Daniel Dunbar | b4c79e0 | 2009-05-04 21:26:30 +0000 | [diff] [blame] | 4319 | const ASTRecordLayout &RL = |
| 4320 | CGM.getContext().getASTObjCImplementationLayout(OID); |
| 4321 | |
Daniel Dunbar | 6e8575b | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 4322 | // InstanceSize is really instance end. |
Daniel Dunbar | b4c79e0 | 2009-05-04 21:26:30 +0000 | [diff] [blame] | 4323 | InstanceSize = llvm::RoundUpToAlignment(RL.getNextOffset(), 8) / 8; |
Daniel Dunbar | 6e8575b | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 4324 | |
| 4325 | // If there are no fields, the start is the same as the end. |
| 4326 | if (!RL.getFieldCount()) |
| 4327 | InstanceStart = InstanceSize; |
| 4328 | else |
| 4329 | InstanceStart = RL.getFieldOffset(0) / 8; |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4330 | } |
| 4331 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4332 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 4333 | std::string ClassName = ID->getNameAsString(); |
| 4334 | if (!ObjCEmptyCacheVar) { |
| 4335 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4336 | CGM.getModule(), |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4337 | ObjCTypes.CacheTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4338 | false, |
| 4339 | llvm::GlobalValue::ExternalLinkage, |
| 4340 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4341 | "_objc_empty_cache"); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4342 | |
| 4343 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4344 | CGM.getModule(), |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4345 | ObjCTypes.ImpnfABITy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4346 | false, |
| 4347 | llvm::GlobalValue::ExternalLinkage, |
| 4348 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4349 | "_objc_empty_vtable"); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4350 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4351 | assert(ID->getClassInterface() && |
| 4352 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 4353 | // FIXME: Is this correct (that meta class size is never computed)? |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4354 | uint32_t InstanceStart = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4355 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4356 | uint32_t InstanceSize = InstanceStart; |
| 4357 | uint32_t flags = CLS_META; |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4358 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 4359 | std::string ObjCClassName(getClassSymbolPrefix()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4360 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4361 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4362 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4363 | bool classIsHidden = |
| 4364 | CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4365 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4366 | flags |= OBJC2_CLS_HIDDEN; |
| 4367 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4368 | // class is root |
| 4369 | flags |= CLS_ROOT; |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4370 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4371 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4372 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4373 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4374 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 4375 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 4376 | Root = Super; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4377 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4378 | // work on super class metadata symbol. |
| 4379 | std::string SuperClassName = |
| 4380 | ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4381 | SuperClassGV = GetClassGlobal(SuperClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4382 | } |
| 4383 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 4384 | InstanceStart, |
| 4385 | InstanceSize,ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4386 | std::string TClassName = ObjCMetaClassName + ClassName; |
| 4387 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4388 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 4389 | classIsHidden); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4390 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4391 | // Metadata for the class |
| 4392 | flags = CLS; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4393 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4394 | flags |= OBJC2_CLS_HIDDEN; |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4395 | |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 4396 | if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface())) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4397 | flags |= CLS_EXCEPTION; |
| 4398 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4399 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4400 | flags |= CLS_ROOT; |
| 4401 | SuperClassGV = 0; |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 4402 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4403 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4404 | std::string RootClassName = |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4405 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4406 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4407 | } |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4408 | GetClassSizeInfo(ID, InstanceStart, InstanceSize); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4409 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4410 | InstanceStart, |
| 4411 | InstanceSize, |
| 4412 | ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4413 | |
| 4414 | TClassName = ObjCClassName + ClassName; |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4415 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4416 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 4417 | classIsHidden); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4418 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4419 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4420 | // Determine if this class is also "non-lazy". |
| 4421 | if (ImplementationIsNonLazy(ID)) |
| 4422 | DefinedNonLazyClasses.push_back(ClassMD); |
| 4423 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4424 | // Force the definition of the EHType if necessary. |
| 4425 | if (flags & CLS_EXCEPTION) |
| 4426 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4427 | } |
| 4428 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4429 | /// GenerateProtocolRef - This routine is called to generate code for |
| 4430 | /// a protocol reference expression; as in: |
| 4431 | /// @code |
| 4432 | /// @protocol(Proto1); |
| 4433 | /// @endcode |
| 4434 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 4435 | /// which will hold address of the protocol meta-data. |
| 4436 | /// |
| 4437 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
| 4438 | const ObjCProtocolDecl *PD) { |
| 4439 | |
Fariborz Jahanian | 960cd06 | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 4440 | // This routine is called for @protocol only. So, we must build definition |
| 4441 | // of protocol's meta-data (not a reference to it!) |
| 4442 | // |
| 4443 | llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4444 | ObjCTypes.ExternalProtocolPtrTy); |
| 4445 | |
| 4446 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
| 4447 | ProtocolName += PD->getNameAsCString(); |
| 4448 | |
| 4449 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 4450 | if (PTGV) |
| 4451 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4452 | PTGV = new llvm::GlobalVariable( |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4453 | CGM.getModule(), |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4454 | Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4455 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4456 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4457 | ProtocolName); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4458 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 4459 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4460 | UsedGlobals.push_back(PTGV); |
| 4461 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4462 | } |
| 4463 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4464 | /// GenerateCategory - Build metadata for a category implementation. |
| 4465 | /// struct _category_t { |
| 4466 | /// const char * const name; |
| 4467 | /// struct _class_t *const cls; |
| 4468 | /// const struct _method_list_t * const instance_methods; |
| 4469 | /// const struct _method_list_t * const class_methods; |
| 4470 | /// const struct _protocol_list_t * const protocols; |
| 4471 | /// const struct _prop_list_t * const properties; |
| 4472 | /// } |
| 4473 | /// |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4474 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4475 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4476 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
| 4477 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4478 | "_$_" + OCD->getNameAsString()); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4479 | std::string ExtClassName(getClassSymbolPrefix() + |
| 4480 | Interface->getNameAsString()); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4481 | |
| 4482 | std::vector<llvm::Constant*> Values(6); |
| 4483 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 4484 | // meta-class entry symbol |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4485 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4486 | Values[1] = ClassGV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4487 | std::vector<llvm::Constant*> Methods; |
| 4488 | std::string MethodListName(Prefix); |
| 4489 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
| 4490 | "_$_" + OCD->getNameAsString(); |
| 4491 | |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4492 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4493 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4494 | // Instance methods should always be defined. |
| 4495 | Methods.push_back(GetMethodConstant(*i)); |
| 4496 | } |
| 4497 | |
| 4498 | Values[2] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4499 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4500 | Methods); |
| 4501 | |
| 4502 | MethodListName = Prefix; |
| 4503 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 4504 | OCD->getNameAsString(); |
| 4505 | Methods.clear(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4506 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4507 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4508 | // Class methods should always be defined. |
| 4509 | Methods.push_back(GetMethodConstant(*i)); |
| 4510 | } |
| 4511 | |
| 4512 | Values[3] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4513 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4514 | Methods); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4515 | const ObjCCategoryDecl *Category = |
| 4516 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4517 | if (Category) { |
| 4518 | std::string ExtName(Interface->getNameAsString() + "_$_" + |
| 4519 | OCD->getNameAsString()); |
| 4520 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
| 4521 | + Interface->getNameAsString() + "_$_" |
| 4522 | + Category->getNameAsString(), |
| 4523 | Category->protocol_begin(), |
| 4524 | Category->protocol_end()); |
| 4525 | Values[5] = |
| 4526 | EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
| 4527 | OCD, Category, ObjCTypes); |
| 4528 | } |
| 4529 | else { |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 4530 | Values[4] = VMContext.getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4531 | Values[5] = VMContext.getNullValue(ObjCTypes.PropertyListPtrTy); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4532 | } |
| 4533 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4534 | llvm::Constant *Init = |
| 4535 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
| 4536 | Values); |
| 4537 | llvm::GlobalVariable *GCATV |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4538 | = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4539 | false, |
| 4540 | llvm::GlobalValue::InternalLinkage, |
| 4541 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4542 | ExtCatName); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4543 | GCATV->setAlignment( |
| 4544 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4545 | GCATV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4546 | UsedGlobals.push_back(GCATV); |
| 4547 | DefinedCategories.push_back(GCATV); |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4548 | |
| 4549 | // Determine if this category is also "non-lazy". |
| 4550 | if (ImplementationIsNonLazy(OCD)) |
| 4551 | DefinedNonLazyCategories.push_back(GCATV); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4552 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4553 | |
| 4554 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 4555 | /// given method if it has been defined. The result is null if the |
| 4556 | /// method has not been defined. The return value has type MethodPtrTy. |
| 4557 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
| 4558 | const ObjCMethodDecl *MD) { |
| 4559 | // FIXME: Use DenseMap::lookup |
| 4560 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 4561 | if (!Fn) |
| 4562 | return 0; |
| 4563 | |
| 4564 | std::vector<llvm::Constant*> Method(3); |
| 4565 | Method[0] = |
| 4566 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4567 | ObjCTypes.SelectorPtrTy); |
| 4568 | Method[1] = GetMethodVarType(MD); |
| 4569 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 4570 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 4571 | } |
| 4572 | |
| 4573 | /// EmitMethodList - Build meta-data for method declarations |
| 4574 | /// struct _method_list_t { |
| 4575 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 4576 | /// uint32_t method_count; |
| 4577 | /// struct _objc_method method_list[method_count]; |
| 4578 | /// } |
| 4579 | /// |
| 4580 | llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList( |
| 4581 | const std::string &Name, |
| 4582 | const char *Section, |
| 4583 | const ConstantVector &Methods) { |
| 4584 | // Return null for empty list. |
| 4585 | if (Methods.empty()) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 4586 | return VMContext.getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4587 | |
| 4588 | std::vector<llvm::Constant*> Values(3); |
| 4589 | // sizeof(struct _objc_method) |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4590 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4591 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4592 | // method_count |
| 4593 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 4594 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 4595 | Methods.size()); |
| 4596 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 4597 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4598 | |
| 4599 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4600 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4601 | llvm::GlobalValue::InternalLinkage, |
| 4602 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4603 | Name); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4604 | GV->setAlignment( |
| 4605 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4606 | GV->setSection(Section); |
| 4607 | UsedGlobals.push_back(GV); |
| 4608 | return llvm::ConstantExpr::getBitCast(GV, |
| 4609 | ObjCTypes.MethodListnfABIPtrTy); |
| 4610 | } |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4611 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4612 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 4613 | /// the given ivar. |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4614 | llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 4615 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4616 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | a81419d | 2009-05-05 00:36:57 +0000 | [diff] [blame] | 4617 | // FIXME: We shouldn't need to do this lookup. |
| 4618 | unsigned Index; |
| 4619 | const ObjCInterfaceDecl *Container = |
| 4620 | FindIvarInterface(CGM.getContext(), ID, Ivar, Index); |
| 4621 | assert(Container && "Unable to find ivar container!"); |
| 4622 | std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() + |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4623 | '.' + Ivar->getNameAsString(); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4624 | llvm::GlobalVariable *IvarOffsetGV = |
| 4625 | CGM.getModule().getGlobalVariable(Name); |
| 4626 | if (!IvarOffsetGV) |
| 4627 | IvarOffsetGV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4628 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4629 | false, |
| 4630 | llvm::GlobalValue::ExternalLinkage, |
| 4631 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4632 | Name); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4633 | return IvarOffsetGV; |
| 4634 | } |
| 4635 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4636 | llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar( |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4637 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4638 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4639 | unsigned long int Offset) { |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4640 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
| 4641 | IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy, |
| 4642 | Offset)); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4643 | IvarOffsetGV->setAlignment( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4644 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy)); |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4645 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 4646 | // FIXME: This matches gcc, but shouldn't the visibility be set on the use as |
| 4647 | // well (i.e., in ObjCIvarOffsetVariable). |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4648 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 4649 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
| 4650 | CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden) |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4651 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4652 | else |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4653 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4654 | IvarOffsetGV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4655 | return IvarOffsetGV; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4656 | } |
| 4657 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4658 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 4659 | /// implementation. The return value has type |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4660 | /// IvarListnfABIPtrTy. |
| 4661 | /// struct _ivar_t { |
| 4662 | /// unsigned long int *offset; // pointer to ivar offset location |
| 4663 | /// char *name; |
| 4664 | /// char *type; |
| 4665 | /// uint32_t alignment; |
| 4666 | /// uint32_t size; |
| 4667 | /// } |
| 4668 | /// struct _ivar_list_t { |
| 4669 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 4670 | /// uint32 count; |
| 4671 | /// struct _iver_t list[count]; |
| 4672 | /// } |
| 4673 | /// |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4674 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4675 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
| 4676 | const ObjCImplementationDecl *ID) { |
| 4677 | |
| 4678 | std::vector<llvm::Constant*> Ivars, Ivar(5); |
| 4679 | |
| 4680 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4681 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
| 4682 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4683 | // FIXME. Consolidate this with similar code in GenerateClass. |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 4684 | |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4685 | // Collect declared and synthesized ivars in a small vector. |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4686 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 4687 | CGM.getContext().ShallowCollectObjCIvars(OID, OIvars); |
Fariborz Jahanian | 99eee36 | 2009-04-01 19:37:34 +0000 | [diff] [blame] | 4688 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4689 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 4690 | ObjCIvarDecl *IVD = OIvars[i]; |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 4691 | // Ignore unnamed bit-fields. |
| 4692 | if (!IVD->getDeclName()) |
| 4693 | continue; |
Daniel Dunbar | 3eec8aa | 2009-04-20 05:53:40 +0000 | [diff] [blame] | 4694 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD, |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4695 | ComputeIvarBaseOffset(CGM, ID, IVD)); |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4696 | Ivar[1] = GetMethodVarName(IVD->getIdentifier()); |
| 4697 | Ivar[2] = GetMethodVarType(IVD); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4698 | const llvm::Type *FieldTy = |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4699 | CGM.getTypes().ConvertTypeForMem(IVD->getType()); |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4700 | unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4701 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4702 | IVD->getType().getTypePtr()) >> 3; |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4703 | Align = llvm::Log2_32(Align); |
| 4704 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4705 | // NOTE. Size of a bitfield does not match gcc's, because of the |
| 4706 | // way bitfields are treated special in each. But I am told that |
| 4707 | // 'size' for bitfield ivars is ignored by the runtime so it does |
| 4708 | // not matter. If it matters, there is enough info to get the |
| 4709 | // bitfield right! |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4710 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4711 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
| 4712 | } |
| 4713 | // Return null for empty list. |
| 4714 | if (Ivars.empty()) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 4715 | return VMContext.getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4716 | std::vector<llvm::Constant*> Values(3); |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4717 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4718 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4719 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 4720 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
| 4721 | Ivars.size()); |
| 4722 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
| 4723 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4724 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 4725 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4726 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4727 | llvm::GlobalValue::InternalLinkage, |
| 4728 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4729 | Prefix + OID->getNameAsString()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4730 | GV->setAlignment( |
| 4731 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4732 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4733 | |
| 4734 | UsedGlobals.push_back(GV); |
| 4735 | return llvm::ConstantExpr::getBitCast(GV, |
| 4736 | ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4737 | } |
| 4738 | |
| 4739 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
| 4740 | const ObjCProtocolDecl *PD) { |
| 4741 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4742 | |
| 4743 | if (!Entry) { |
| 4744 | // We use the initializer as a marker of whether this is a forward |
| 4745 | // reference or not. At module finalization we add the empty |
| 4746 | // contents for protocols which were referenced but never defined. |
| 4747 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4748 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4749 | llvm::GlobalValue::ExternalLinkage, |
| 4750 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4751 | "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString()); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4752 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 4753 | UsedGlobals.push_back(Entry); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4754 | } |
| 4755 | |
| 4756 | return Entry; |
| 4757 | } |
| 4758 | |
| 4759 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 4760 | /// @code |
| 4761 | /// struct _protocol_t { |
| 4762 | /// id isa; // NULL |
| 4763 | /// const char * const protocol_name; |
| 4764 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 4765 | /// const struct method_list_t * const instance_methods; |
| 4766 | /// const struct method_list_t * const class_methods; |
| 4767 | /// const struct method_list_t *optionalInstanceMethods; |
| 4768 | /// const struct method_list_t *optionalClassMethods; |
| 4769 | /// const struct _prop_list_t * properties; |
| 4770 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 4771 | /// const uint32_t flags; // = 0 |
| 4772 | /// } |
| 4773 | /// @endcode |
| 4774 | /// |
| 4775 | |
| 4776 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
| 4777 | const ObjCProtocolDecl *PD) { |
| 4778 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4779 | |
| 4780 | // Early exit if a defining object has already been generated. |
| 4781 | if (Entry && Entry->hasInitializer()) |
| 4782 | return Entry; |
| 4783 | |
| 4784 | const char *ProtocolName = PD->getNameAsCString(); |
| 4785 | |
| 4786 | // Construct method lists. |
| 4787 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 4788 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4789 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4790 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4791 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4792 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4793 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4794 | OptInstanceMethods.push_back(C); |
| 4795 | } else { |
| 4796 | InstanceMethods.push_back(C); |
| 4797 | } |
| 4798 | } |
| 4799 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4800 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4801 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4802 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4803 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4804 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4805 | OptClassMethods.push_back(C); |
| 4806 | } else { |
| 4807 | ClassMethods.push_back(C); |
| 4808 | } |
| 4809 | } |
| 4810 | |
| 4811 | std::vector<llvm::Constant*> Values(10); |
| 4812 | // isa is NULL |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 4813 | Values[0] = VMContext.getNullValue(ObjCTypes.ObjectPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4814 | Values[1] = GetClassName(PD->getIdentifier()); |
| 4815 | Values[2] = EmitProtocolList( |
| 4816 | "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(), |
| 4817 | PD->protocol_begin(), |
| 4818 | PD->protocol_end()); |
| 4819 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4820 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4821 | + PD->getNameAsString(), |
| 4822 | "__DATA, __objc_const", |
| 4823 | InstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4824 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4825 | + PD->getNameAsString(), |
| 4826 | "__DATA, __objc_const", |
| 4827 | ClassMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4828 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4829 | + PD->getNameAsString(), |
| 4830 | "__DATA, __objc_const", |
| 4831 | OptInstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4832 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4833 | + PD->getNameAsString(), |
| 4834 | "__DATA, __objc_const", |
| 4835 | OptClassMethods); |
| 4836 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(), |
| 4837 | 0, PD, ObjCTypes); |
| 4838 | uint32_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4839 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4840 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 4841 | Values[9] = VMContext.getNullValue(ObjCTypes.IntTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4842 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
| 4843 | Values); |
| 4844 | |
| 4845 | if (Entry) { |
| 4846 | // Already created, fix the linkage and update the initializer. |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4847 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4848 | Entry->setInitializer(Init); |
| 4849 | } else { |
| 4850 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4851 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4852 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4853 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4854 | std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4855 | Entry->setAlignment( |
| 4856 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4857 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4858 | } |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4859 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4860 | |
| 4861 | // Use this protocol meta-data to build protocol list table in section |
| 4862 | // __DATA, __objc_protolist |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4863 | llvm::GlobalVariable *PTGV = new llvm::GlobalVariable( |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4864 | CGM.getModule(), |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4865 | ObjCTypes.ProtocolnfABIPtrTy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4866 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4867 | Entry, |
| 4868 | std::string("\01l_OBJC_LABEL_PROTOCOL_$_") |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4869 | +ProtocolName); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4870 | PTGV->setAlignment( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4871 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 4872 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4873 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4874 | UsedGlobals.push_back(PTGV); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4875 | return Entry; |
| 4876 | } |
| 4877 | |
| 4878 | /// EmitProtocolList - Generate protocol list meta-data: |
| 4879 | /// @code |
| 4880 | /// struct _protocol_list_t { |
| 4881 | /// long protocol_count; // Note, this is 32/64 bit |
| 4882 | /// struct _protocol_t[protocol_count]; |
| 4883 | /// } |
| 4884 | /// @endcode |
| 4885 | /// |
| 4886 | llvm::Constant * |
| 4887 | CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name, |
| 4888 | ObjCProtocolDecl::protocol_iterator begin, |
| 4889 | ObjCProtocolDecl::protocol_iterator end) { |
| 4890 | std::vector<llvm::Constant*> ProtocolRefs; |
| 4891 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4892 | // Just return null for empty protocol lists |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4893 | if (begin == end) |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 4894 | return VMContext.getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4895 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4896 | // FIXME: We shouldn't need to do this lookup here, should we? |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4897 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
| 4898 | if (GV) |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4899 | return llvm::ConstantExpr::getBitCast(GV, |
| 4900 | ObjCTypes.ProtocolListnfABIPtrTy); |
| 4901 | |
| 4902 | for (; begin != end; ++begin) |
| 4903 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 4904 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4905 | // This list is null terminated. |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 4906 | ProtocolRefs.push_back(VMContext.getNullValue( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4907 | ObjCTypes.ProtocolnfABIPtrTy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4908 | |
| 4909 | std::vector<llvm::Constant*> Values(2); |
| 4910 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 4911 | Values[1] = |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4912 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4913 | ProtocolRefs.size()), |
| 4914 | ProtocolRefs); |
| 4915 | |
| 4916 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4917 | GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4918 | llvm::GlobalValue::InternalLinkage, |
| 4919 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4920 | Name); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4921 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4922 | GV->setAlignment( |
| 4923 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4924 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4925 | return llvm::ConstantExpr::getBitCast(GV, |
| 4926 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4927 | } |
| 4928 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4929 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 4930 | /// struct _objc_method { |
| 4931 | /// SEL _cmd; |
| 4932 | /// char *method_type; |
| 4933 | /// char *_imp; |
| 4934 | /// } |
| 4935 | |
| 4936 | llvm::Constant * |
| 4937 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 4938 | std::vector<llvm::Constant*> Desc(3); |
| 4939 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4940 | ObjCTypes.SelectorPtrTy); |
| 4941 | Desc[1] = GetMethodVarType(MD); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4942 | // Protocol methods have no implementation. So, this entry is always NULL. |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame^] | 4943 | Desc[2] = VMContext.getNullValue(ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4944 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
| 4945 | } |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4946 | |
| 4947 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 4948 | /// This code gen. amounts to generating code for: |
| 4949 | /// @code |
| 4950 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 4951 | /// @encode |
| 4952 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4953 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4954 | CodeGen::CodeGenFunction &CGF, |
| 4955 | QualType ObjectTy, |
| 4956 | llvm::Value *BaseValue, |
| 4957 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4958 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 4959 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4960 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 4961 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4962 | } |
| 4963 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4964 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
| 4965 | CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 4966 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4967 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 4968 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar), |
| 4969 | false, "ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4970 | } |
| 4971 | |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4972 | CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend( |
| 4973 | CodeGen::CodeGenFunction &CGF, |
| 4974 | QualType ResultType, |
| 4975 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4976 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4977 | QualType Arg0Ty, |
| 4978 | bool IsSuper, |
| 4979 | const CallArgList &CallArgs) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 4980 | // FIXME. Even though IsSuper is passes. This function doese not handle calls |
| 4981 | // to 'super' receivers. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4982 | CodeGenTypes &Types = CGM.getTypes(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4983 | llvm::Value *Arg0 = Receiver; |
| 4984 | if (!IsSuper) |
| 4985 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4986 | |
| 4987 | // Find the message function name. |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 4988 | // FIXME. This is too much work to get the ABI-specific result type needed to |
| 4989 | // find the message name. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4990 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, |
| 4991 | llvm::SmallVector<QualType, 16>()); |
Fariborz Jahanian | 70b51c7 | 2009-04-30 23:08:58 +0000 | [diff] [blame] | 4992 | llvm::Constant *Fn = 0; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4993 | std::string Name("\01l_"); |
| 4994 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4995 | #if 0 |
| 4996 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4997 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4998 | Fn = ObjCTypes.getMessageSendIdStretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4999 | // FIXME. Is there a better way of getting these names. |
| 5000 | // They are available in RuntimeFunctions vector pair. |
| 5001 | Name += "objc_msgSendId_stret_fixup"; |
| 5002 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5003 | else |
| 5004 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5005 | if (IsSuper) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5006 | Fn = ObjCTypes.getMessageSendSuper2StretFixupFn(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5007 | Name += "objc_msgSendSuper2_stret_fixup"; |
| 5008 | } |
| 5009 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5010 | { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5011 | Fn = ObjCTypes.getMessageSendStretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5012 | Name += "objc_msgSend_stret_fixup"; |
| 5013 | } |
| 5014 | } |
Fariborz Jahanian | 5b2bad0 | 2009-04-30 16:31:11 +0000 | [diff] [blame] | 5015 | else if (!IsSuper && ResultType->isFloatingType()) { |
Daniel Dunbar | c0183e8 | 2009-06-26 18:32:06 +0000 | [diff] [blame] | 5016 | if (ResultType->isSpecificBuiltinType(BuiltinType::LongDouble)) { |
| 5017 | Fn = ObjCTypes.getMessageSendFpretFixupFn(); |
| 5018 | Name += "objc_msgSend_fpret_fixup"; |
| 5019 | } |
| 5020 | else { |
| 5021 | Fn = ObjCTypes.getMessageSendFixupFn(); |
| 5022 | Name += "objc_msgSend_fixup"; |
Fariborz Jahanian | 5b2bad0 | 2009-04-30 16:31:11 +0000 | [diff] [blame] | 5023 | } |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5024 | } |
| 5025 | else { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5026 | #if 0 |
| 5027 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5028 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5029 | Fn = ObjCTypes.getMessageSendIdFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5030 | Name += "objc_msgSendId_fixup"; |
| 5031 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5032 | else |
| 5033 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5034 | if (IsSuper) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5035 | Fn = ObjCTypes.getMessageSendSuper2FixupFn(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5036 | Name += "objc_msgSendSuper2_fixup"; |
| 5037 | } |
| 5038 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5039 | { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5040 | Fn = ObjCTypes.getMessageSendFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5041 | Name += "objc_msgSend_fixup"; |
| 5042 | } |
| 5043 | } |
Fariborz Jahanian | 70b51c7 | 2009-04-30 23:08:58 +0000 | [diff] [blame] | 5044 | assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5045 | Name += '_'; |
| 5046 | std::string SelName(Sel.getAsString()); |
| 5047 | // Replace all ':' in selector name with '_' ouch! |
| 5048 | for(unsigned i = 0; i < SelName.size(); i++) |
| 5049 | if (SelName[i] == ':') |
| 5050 | SelName[i] = '_'; |
| 5051 | Name += SelName; |
| 5052 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5053 | if (!GV) { |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5054 | // Build message ref table entry. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5055 | std::vector<llvm::Constant*> Values(2); |
| 5056 | Values[0] = Fn; |
| 5057 | Values[1] = GetMethodVarName(Sel); |
| 5058 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5059 | GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 5060 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5061 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5062 | Name); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5063 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | f59c1a6 | 2009-04-15 19:04:46 +0000 | [diff] [blame] | 5064 | GV->setAlignment(16); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5065 | GV->setSection("__DATA, __objc_msgrefs, coalesced"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5066 | } |
| 5067 | llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5068 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5069 | CallArgList ActualArgs; |
| 5070 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 5071 | ActualArgs.push_back(std::make_pair(RValue::get(Arg1), |
| 5072 | ObjCTypes.MessageRefCPtrTy)); |
| 5073 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5074 | const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs); |
| 5075 | llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0); |
| 5076 | Callee = CGF.Builder.CreateLoad(Callee); |
Fariborz Jahanian | 3ab75bd | 2009-02-14 21:25:36 +0000 | [diff] [blame] | 5077 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5078 | Callee = CGF.Builder.CreateBitCast(Callee, |
| 5079 | llvm::PointerType::getUnqual(FTy)); |
| 5080 | return CGF.EmitCall(FnInfo1, Callee, ActualArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5081 | } |
| 5082 | |
| 5083 | /// Generate code for a message send expression in the nonfragile abi. |
| 5084 | CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend( |
| 5085 | CodeGen::CodeGenFunction &CGF, |
| 5086 | QualType ResultType, |
| 5087 | Selector Sel, |
| 5088 | llvm::Value *Receiver, |
| 5089 | bool IsClassMessage, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 5090 | const CallArgList &CallArgs, |
| 5091 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5092 | return LegacyDispatchedSelector(Sel) |
| 5093 | ? EmitLegacyMessageSend(CGF, ResultType, EmitSelector(CGF.Builder, Sel), |
| 5094 | Receiver, CGF.getContext().getObjCIdType(), |
| 5095 | false, CallArgs, ObjCTypes) |
| 5096 | : EmitMessageSend(CGF, ResultType, Sel, |
| 5097 | Receiver, CGF.getContext().getObjCIdType(), |
| 5098 | false, CallArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5099 | } |
| 5100 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5101 | llvm::GlobalVariable * |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5102 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5103 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5104 | |
Daniel Dunbar | dfff230 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 5105 | if (!GV) { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5106 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy, |
| 5107 | false, llvm::GlobalValue::ExternalLinkage, |
| 5108 | 0, Name); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5109 | } |
| 5110 | |
| 5111 | return GV; |
| 5112 | } |
| 5113 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5114 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5115 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5116 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 5117 | |
| 5118 | if (!Entry) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5119 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5120 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5121 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5122 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
| 5123 | false, llvm::GlobalValue::InternalLinkage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5124 | ClassGV, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5125 | "\01L_OBJC_CLASSLIST_REFERENCES_$_"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5126 | Entry->setAlignment( |
| 5127 | CGM.getTargetData().getPrefTypeAlignment( |
| 5128 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5129 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
| 5130 | UsedGlobals.push_back(Entry); |
| 5131 | } |
| 5132 | |
| 5133 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5134 | } |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5135 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5136 | llvm::Value * |
| 5137 | CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder, |
| 5138 | const ObjCInterfaceDecl *ID) { |
| 5139 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
| 5140 | |
| 5141 | if (!Entry) { |
| 5142 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5143 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
| 5144 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5145 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
| 5146 | false, llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5147 | ClassGV, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5148 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5149 | Entry->setAlignment( |
| 5150 | CGM.getTargetData().getPrefTypeAlignment( |
| 5151 | ObjCTypes.ClassnfABIPtrTy)); |
| 5152 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5153 | UsedGlobals.push_back(Entry); |
| 5154 | } |
| 5155 | |
| 5156 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5157 | } |
| 5158 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5159 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 5160 | /// meta-data |
| 5161 | /// |
| 5162 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 5163 | const ObjCInterfaceDecl *ID) { |
| 5164 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 5165 | if (Entry) |
| 5166 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5167 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5168 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5169 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5170 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5171 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5172 | llvm::GlobalValue::InternalLinkage, |
| 5173 | MetaClassGV, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5174 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5175 | Entry->setAlignment( |
| 5176 | CGM.getTargetData().getPrefTypeAlignment( |
| 5177 | ObjCTypes.ClassnfABIPtrTy)); |
| 5178 | |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5179 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5180 | UsedGlobals.push_back(Entry); |
| 5181 | |
| 5182 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5183 | } |
| 5184 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5185 | /// GetClass - Return a reference to the class for the given interface |
| 5186 | /// decl. |
| 5187 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 5188 | const ObjCInterfaceDecl *ID) { |
| 5189 | return EmitClassRef(Builder, ID); |
| 5190 | } |
| 5191 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5192 | /// Generates a message send where the super is the receiver. This is |
| 5193 | /// a message send to self with special delivery semantics indicating |
| 5194 | /// which class's method should be called. |
| 5195 | CodeGen::RValue |
| 5196 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 5197 | QualType ResultType, |
| 5198 | Selector Sel, |
| 5199 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5200 | bool isCategoryImpl, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5201 | llvm::Value *Receiver, |
| 5202 | bool IsClassMessage, |
| 5203 | const CodeGen::CallArgList &CallArgs) { |
| 5204 | // ... |
| 5205 | // Create and init a super structure; this is a (receiver, class) |
| 5206 | // pair we will pass to objc_msgSendSuper. |
| 5207 | llvm::Value *ObjCSuper = |
| 5208 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 5209 | |
| 5210 | llvm::Value *ReceiverAsObject = |
| 5211 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 5212 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 5213 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
| 5214 | |
| 5215 | // 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] | 5216 | llvm::Value *Target; |
| 5217 | if (IsClassMessage) { |
| 5218 | if (isCategoryImpl) { |
| 5219 | // Message sent to "super' in a class method defined in |
| 5220 | // a category implementation. |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5221 | Target = EmitClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5222 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 5223 | Target = CGF.Builder.CreateLoad(Target); |
| 5224 | } |
| 5225 | else |
| 5226 | Target = EmitMetaClassRef(CGF.Builder, Class); |
| 5227 | } |
| 5228 | else |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5229 | Target = EmitSuperClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5230 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5231 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 5232 | // ObjCTypes types. |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5233 | const llvm::Type *ClassTy = |
| 5234 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 5235 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 5236 | CGF.Builder.CreateStore(Target, |
| 5237 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 5238 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5239 | return (LegacyDispatchedSelector(Sel)) |
| 5240 | ? EmitLegacyMessageSend(CGF, ResultType,EmitSelector(CGF.Builder, Sel), |
| 5241 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 5242 | true, CallArgs, |
| 5243 | ObjCTypes) |
| 5244 | : EmitMessageSend(CGF, ResultType, Sel, |
| 5245 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 5246 | true, CallArgs); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5247 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5248 | |
| 5249 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
| 5250 | Selector Sel) { |
| 5251 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 5252 | |
| 5253 | if (!Entry) { |
| 5254 | llvm::Constant *Casted = |
| 5255 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 5256 | ObjCTypes.SelectorPtrTy); |
| 5257 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5258 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false, |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5259 | llvm::GlobalValue::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5260 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5261 | Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip"); |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5262 | UsedGlobals.push_back(Entry); |
| 5263 | } |
| 5264 | |
| 5265 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5266 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5267 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 5268 | /// objc_assign_ivar (id src, id *dst) |
| 5269 | /// |
| 5270 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 5271 | llvm::Value *src, llvm::Value *dst) |
| 5272 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5273 | const llvm::Type * SrcTy = src->getType(); |
| 5274 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5275 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5276 | assert(Size <= 8 && "does not support size > 8"); |
| 5277 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5278 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5279 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5280 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5281 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5282 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5283 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5284 | src, dst, "assignivar"); |
| 5285 | return; |
| 5286 | } |
| 5287 | |
| 5288 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 5289 | /// objc_assign_strongCast (id src, id *dst) |
| 5290 | /// |
| 5291 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
| 5292 | CodeGen::CodeGenFunction &CGF, |
| 5293 | llvm::Value *src, llvm::Value *dst) |
| 5294 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5295 | const llvm::Type * SrcTy = src->getType(); |
| 5296 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5297 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5298 | assert(Size <= 8 && "does not support size > 8"); |
| 5299 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5300 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5301 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5302 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5303 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5304 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5305 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5306 | src, dst, "weakassign"); |
| 5307 | return; |
| 5308 | } |
| 5309 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 5310 | void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable( |
| 5311 | CodeGen::CodeGenFunction &CGF, |
| 5312 | llvm::Value *DestPtr, |
| 5313 | llvm::Value *SrcPtr, |
| 5314 | unsigned long size) { |
| 5315 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 5316 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
| 5317 | llvm::Value *N = llvm::ConstantInt::get(ObjCTypes.LongTy, size); |
| 5318 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), |
| 5319 | DestPtr, SrcPtr, N); |
| 5320 | return; |
| 5321 | } |
| 5322 | |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5323 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 5324 | /// object: objc_read_weak (id *src) |
| 5325 | /// |
| 5326 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
| 5327 | CodeGen::CodeGenFunction &CGF, |
| 5328 | llvm::Value *AddrWeakObj) |
| 5329 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5330 | const llvm::Type* DestTy = |
| 5331 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5332 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 5333 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5334 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5335 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5336 | return read_weak; |
| 5337 | } |
| 5338 | |
| 5339 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 5340 | /// objc_assign_weak (id src, id *dst) |
| 5341 | /// |
| 5342 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 5343 | llvm::Value *src, llvm::Value *dst) |
| 5344 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5345 | const llvm::Type * SrcTy = src->getType(); |
| 5346 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5347 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5348 | assert(Size <= 8 && "does not support size > 8"); |
| 5349 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5350 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5351 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5352 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5353 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5354 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 5355 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5356 | src, dst, "weakassign"); |
| 5357 | return; |
| 5358 | } |
| 5359 | |
| 5360 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 5361 | /// objc_assign_global (id src, id *dst) |
| 5362 | /// |
| 5363 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 5364 | llvm::Value *src, llvm::Value *dst) |
| 5365 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5366 | const llvm::Type * SrcTy = src->getType(); |
| 5367 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5368 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5369 | assert(Size <= 8 && "does not support size > 8"); |
| 5370 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5371 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5372 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5373 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5374 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5375 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5376 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5377 | src, dst, "globalassign"); |
| 5378 | return; |
| 5379 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5380 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5381 | void |
| 5382 | CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 5383 | const Stmt &S) { |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5384 | bool isTry = isa<ObjCAtTryStmt>(S); |
| 5385 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 5386 | llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5387 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5388 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5389 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5390 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
| 5391 | |
| 5392 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 5393 | // evaluation of the expression must occur before we enter the |
| 5394 | // @synchronized. We can safely avoid a temp here because jumps into |
| 5395 | // @synchronized are illegal & this will dominate uses. |
| 5396 | llvm::Value *SyncArg = 0; |
| 5397 | if (!isTry) { |
| 5398 | SyncArg = |
| 5399 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 5400 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5401 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5402 | } |
| 5403 | |
| 5404 | // Push an EH context entry, used for handling rethrows and jumps |
| 5405 | // through finally. |
| 5406 | CGF.PushCleanupBlock(FinallyBlock); |
| 5407 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5408 | CGF.setInvokeDest(TryHandler); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5409 | |
| 5410 | CGF.EmitBlock(TryBlock); |
| 5411 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 5412 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
| 5413 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5414 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5415 | // Emit the exception handler. |
| 5416 | |
| 5417 | CGF.EmitBlock(TryHandler); |
| 5418 | |
| 5419 | llvm::Value *llvm_eh_exception = |
| 5420 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
| 5421 | llvm::Value *llvm_eh_selector_i64 = |
| 5422 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64); |
| 5423 | llvm::Value *llvm_eh_typeid_for_i64 = |
| 5424 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64); |
| 5425 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5426 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow"); |
| 5427 | |
| 5428 | llvm::SmallVector<llvm::Value*, 8> SelectorArgs; |
| 5429 | SelectorArgs.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5430 | SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5431 | |
| 5432 | // Construct the lists of (type, catch body) to handle. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5433 | llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5434 | bool HasCatchAll = false; |
| 5435 | if (isTry) { |
| 5436 | if (const ObjCAtCatchStmt* CatchStmt = |
| 5437 | cast<ObjCAtTryStmt>(S).getCatchStmts()) { |
| 5438 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5439 | const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5440 | Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody())); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5441 | |
| 5442 | // catch(...) always matches. |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5443 | if (!CatchDecl) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5444 | // 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^] | 5445 | llvm::Value *Null = VMContext.getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5446 | SelectorArgs.push_back(Null); |
| 5447 | HasCatchAll = true; |
| 5448 | break; |
| 5449 | } |
| 5450 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5451 | if (CatchDecl->getType()->isObjCIdType() || |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5452 | CatchDecl->getType()->isObjCQualifiedIdType()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5453 | llvm::Value *IDEHType = |
| 5454 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 5455 | if (!IDEHType) |
| 5456 | IDEHType = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5457 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, |
| 5458 | false, |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5459 | llvm::GlobalValue::ExternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5460 | 0, "OBJC_EHTYPE_id"); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5461 | SelectorArgs.push_back(IDEHType); |
| 5462 | HasCatchAll = true; |
| 5463 | break; |
| 5464 | } |
| 5465 | |
| 5466 | // All other types should be Objective-C interface pointer types. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5467 | const ObjCObjectPointerType *PT = |
| 5468 | CatchDecl->getType()->getAsObjCObjectPointerType(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5469 | assert(PT && "Invalid @catch type."); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5470 | const ObjCInterfaceType *IT = PT->getInterfaceType(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5471 | assert(IT && "Invalid @catch type."); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5472 | llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5473 | SelectorArgs.push_back(EHType); |
| 5474 | } |
| 5475 | } |
| 5476 | } |
| 5477 | |
| 5478 | // We use a cleanup unless there was already a catch all. |
| 5479 | if (!HasCatchAll) { |
| 5480 | SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5481 | Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5482 | } |
| 5483 | |
| 5484 | llvm::Value *Selector = |
| 5485 | CGF.Builder.CreateCall(llvm_eh_selector_i64, |
| 5486 | SelectorArgs.begin(), SelectorArgs.end(), |
| 5487 | "selector"); |
| 5488 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5489 | const ParmVarDecl *CatchParam = Handlers[i].first; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5490 | const Stmt *CatchBody = Handlers[i].second; |
| 5491 | |
| 5492 | llvm::BasicBlock *Next = 0; |
| 5493 | |
| 5494 | // The last handler always matches. |
| 5495 | if (i + 1 != e) { |
| 5496 | assert(CatchParam && "Only last handler can be a catch all."); |
| 5497 | |
| 5498 | llvm::BasicBlock *Match = CGF.createBasicBlock("match"); |
| 5499 | Next = CGF.createBasicBlock("catch.next"); |
| 5500 | llvm::Value *Id = |
| 5501 | CGF.Builder.CreateCall(llvm_eh_typeid_for_i64, |
| 5502 | CGF.Builder.CreateBitCast(SelectorArgs[i+2], |
| 5503 | ObjCTypes.Int8PtrTy)); |
| 5504 | CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id), |
| 5505 | Match, Next); |
| 5506 | |
| 5507 | CGF.EmitBlock(Match); |
| 5508 | } |
| 5509 | |
| 5510 | if (CatchBody) { |
| 5511 | llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end"); |
| 5512 | llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler"); |
| 5513 | |
| 5514 | // Cleanups must call objc_end_catch. |
| 5515 | // |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5516 | // FIXME: It seems incorrect for objc_begin_catch to be inside this |
| 5517 | // context, but this matches gcc. |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5518 | CGF.PushCleanupBlock(MatchEnd); |
| 5519 | CGF.setInvokeDest(MatchHandler); |
| 5520 | |
| 5521 | llvm::Value *ExcObject = |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5522 | CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5523 | |
| 5524 | // Bind the catch parameter if it exists. |
| 5525 | if (CatchParam) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5526 | ExcObject = |
| 5527 | CGF.Builder.CreateBitCast(ExcObject, |
| 5528 | CGF.ConvertType(CatchParam->getType())); |
| 5529 | // CatchParam is a ParmVarDecl because of the grammar |
| 5530 | // construction used to handle this, but for codegen purposes |
| 5531 | // we treat this as a local decl. |
| 5532 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
| 5533 | CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5534 | } |
| 5535 | |
| 5536 | CGF.ObjCEHValueStack.push_back(ExcObject); |
| 5537 | CGF.EmitStmt(CatchBody); |
| 5538 | CGF.ObjCEHValueStack.pop_back(); |
| 5539 | |
| 5540 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5541 | |
| 5542 | CGF.EmitBlock(MatchHandler); |
| 5543 | |
| 5544 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5545 | // We are required to emit this call to satisfy LLVM, even |
| 5546 | // though we don't use the result. |
| 5547 | llvm::SmallVector<llvm::Value*, 8> Args; |
| 5548 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5549 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5550 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5551 | 0)); |
| 5552 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5553 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5554 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5555 | |
| 5556 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5557 | |
| 5558 | CGF.EmitBlock(MatchEnd); |
| 5559 | |
| 5560 | // Unfortunately, we also have to generate another EH frame here |
| 5561 | // in case this throws. |
| 5562 | llvm::BasicBlock *MatchEndHandler = |
| 5563 | CGF.createBasicBlock("match.end.handler"); |
| 5564 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5565 | CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5566 | Cont, MatchEndHandler, |
| 5567 | Args.begin(), Args.begin()); |
| 5568 | |
| 5569 | CGF.EmitBlock(Cont); |
| 5570 | if (Info.SwitchBlock) |
| 5571 | CGF.EmitBlock(Info.SwitchBlock); |
| 5572 | if (Info.EndBlock) |
| 5573 | CGF.EmitBlock(Info.EndBlock); |
| 5574 | |
| 5575 | CGF.EmitBlock(MatchEndHandler); |
| 5576 | Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5577 | // We are required to emit this call to satisfy LLVM, even |
| 5578 | // though we don't use the result. |
| 5579 | Args.clear(); |
| 5580 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5581 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5582 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5583 | 0)); |
| 5584 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5585 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5586 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5587 | |
| 5588 | if (Next) |
| 5589 | CGF.EmitBlock(Next); |
| 5590 | } else { |
| 5591 | assert(!Next && "catchup should be last handler."); |
| 5592 | |
| 5593 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5594 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5595 | } |
| 5596 | } |
| 5597 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5598 | // Pop the cleanup entry, the @finally is outside this cleanup |
| 5599 | // scope. |
| 5600 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5601 | CGF.setInvokeDest(PrevLandingPad); |
| 5602 | |
| 5603 | CGF.EmitBlock(FinallyBlock); |
| 5604 | |
| 5605 | if (isTry) { |
| 5606 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 5607 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 5608 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 5609 | } else { |
| 5610 | // Emit 'objc_sync_exit(expr)' as finally's sole statement for |
| 5611 | // @synchronized. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5612 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5613 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5614 | |
| 5615 | if (Info.SwitchBlock) |
| 5616 | CGF.EmitBlock(Info.SwitchBlock); |
| 5617 | if (Info.EndBlock) |
| 5618 | CGF.EmitBlock(Info.EndBlock); |
| 5619 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5620 | // Branch around the rethrow code. |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5621 | CGF.EmitBranch(FinallyEnd); |
| 5622 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5623 | CGF.EmitBlock(FinallyRethrow); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5624 | CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5625 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5626 | CGF.Builder.CreateUnreachable(); |
| 5627 | |
| 5628 | CGF.EmitBlock(FinallyEnd); |
| 5629 | } |
| 5630 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5631 | /// EmitThrowStmt - Generate code for a throw statement. |
| 5632 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 5633 | const ObjCAtThrowStmt &S) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5634 | llvm::Value *Exception; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5635 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5636 | Exception = CGF.EmitScalarExpr(ThrowExpr); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5637 | } else { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5638 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
| 5639 | "Unexpected rethrow outside @catch block."); |
| 5640 | Exception = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5641 | } |
| 5642 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5643 | llvm::Value *ExceptionAsObject = |
| 5644 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 5645 | llvm::BasicBlock *InvokeDest = CGF.getInvokeDest(); |
| 5646 | if (InvokeDest) { |
| 5647 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5648 | CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5649 | Cont, InvokeDest, |
| 5650 | &ExceptionAsObject, &ExceptionAsObject + 1); |
| 5651 | CGF.EmitBlock(Cont); |
| 5652 | } else |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5653 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5654 | CGF.Builder.CreateUnreachable(); |
| 5655 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5656 | // Clear the insertion point to indicate we are in unreachable code. |
| 5657 | CGF.Builder.ClearInsertionPoint(); |
| 5658 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5659 | |
| 5660 | llvm::Value * |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5661 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 5662 | bool ForDefinition) { |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5663 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5664 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5665 | // If we don't need a definition, return the entry if found or check |
| 5666 | // if we use an external reference. |
| 5667 | if (!ForDefinition) { |
| 5668 | if (Entry) |
| 5669 | return Entry; |
Daniel Dunbar | 7e075cb | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 5670 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5671 | // If this type (or a super class) has the __objc_exception__ |
| 5672 | // attribute, emit an external reference. |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 5673 | if (hasObjCExceptionAttribute(CGM.getContext(), ID)) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5674 | return Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5675 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5676 | llvm::GlobalValue::ExternalLinkage, |
| 5677 | 0, |
| 5678 | (std::string("OBJC_EHTYPE_$_") + |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5679 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5680 | } |
| 5681 | |
| 5682 | // Otherwise we need to either make a new entry or fill in the |
| 5683 | // initializer. |
| 5684 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5685 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5686 | std::string VTableName = "objc_ehtype_vtable"; |
| 5687 | llvm::GlobalVariable *VTableGV = |
| 5688 | CGM.getModule().getGlobalVariable(VTableName); |
| 5689 | if (!VTableGV) |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5690 | VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy, |
| 5691 | false, |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5692 | llvm::GlobalValue::ExternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5693 | 0, VTableName); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5694 | |
| 5695 | llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2); |
| 5696 | |
| 5697 | std::vector<llvm::Constant*> Values(3); |
| 5698 | Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1); |
| 5699 | Values[1] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5700 | Values[2] = GetClassGlobal(ClassName); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5701 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
| 5702 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5703 | if (Entry) { |
| 5704 | Entry->setInitializer(Init); |
| 5705 | } else { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5706 | Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5707 | llvm::GlobalValue::WeakAnyLinkage, |
| 5708 | Init, |
| 5709 | (std::string("OBJC_EHTYPE_$_") + |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5710 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5711 | } |
| 5712 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 5713 | if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden) |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5714 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5715 | Entry->setAlignment(8); |
| 5716 | |
| 5717 | if (ForDefinition) { |
| 5718 | Entry->setSection("__DATA,__objc_const"); |
| 5719 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 5720 | } else { |
| 5721 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 5722 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5723 | |
| 5724 | return Entry; |
| 5725 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5726 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5727 | /* *** */ |
| 5728 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5729 | CodeGen::CGObjCRuntime * |
| 5730 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 5731 | return new CGObjCMac(CGM); |
| 5732 | } |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5733 | |
| 5734 | CodeGen::CGObjCRuntime * |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5735 | CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5736 | return new CGObjCNonFragileABIMac(CGM); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5737 | } |