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" |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 26 | #include "llvm/Module.h" |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/DenseSet.h" |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetData.h" |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 29 | #include <sstream> |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace clang; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 32 | using namespace CodeGen; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 33 | |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 34 | // Common CGObjCRuntime functions, these don't belong here, but they |
| 35 | // don't belong in CGObjCRuntime either so we will live with it for |
| 36 | // now. |
| 37 | |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 38 | /// FindIvarInterface - Find the interface containing the ivar. |
Daniel Dunbar | a243578 | 2009-04-22 12:00:04 +0000 | [diff] [blame] | 39 | /// |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 40 | /// FIXME: We shouldn't need to do this, the containing context should |
| 41 | /// be fixed. |
| 42 | static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context, |
| 43 | const ObjCInterfaceDecl *OID, |
| 44 | const ObjCIvarDecl *OIVD, |
| 45 | unsigned &Index) { |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 46 | // FIXME: The index here is closely tied to how |
| 47 | // ASTContext::getObjCLayout is implemented. This should be fixed to |
| 48 | // get the information from the layout directly. |
| 49 | Index = 0; |
| 50 | for (ObjCInterfaceDecl::ivar_iterator IVI = OID->ivar_begin(), |
| 51 | IVE = OID->ivar_end(); IVI != IVE; ++IVI, ++Index) |
| 52 | if (OIVD == *IVI) |
| 53 | return OID; |
| 54 | |
| 55 | // Also look in synthesized ivars. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 56 | llvm::SmallVector<ObjCIvarDecl*, 16> Ivars; |
| 57 | Context.CollectSynthesizedIvars(OID, Ivars); |
| 58 | for (unsigned k = 0, e = Ivars.size(); k != e; ++k) { |
| 59 | if (OIVD == Ivars[k]) |
| 60 | return OID; |
| 61 | ++Index; |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 62 | } |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 63 | |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 64 | // Otherwise check in the super class. |
Daniel Dunbar | a81419d | 2009-05-05 00:36:57 +0000 | [diff] [blame] | 65 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 66 | return FindIvarInterface(Context, Super, OIVD, Index); |
| 67 | |
| 68 | return 0; |
Daniel Dunbar | a243578 | 2009-04-22 12:00:04 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 71 | static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM, |
| 72 | const ObjCInterfaceDecl *OID, |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 73 | const ObjCImplementationDecl *ID, |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 74 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 75 | unsigned Index; |
| 76 | const ObjCInterfaceDecl *Container = |
| 77 | FindIvarInterface(CGM.getContext(), OID, Ivar, Index); |
| 78 | assert(Container && "Unable to find ivar container"); |
| 79 | |
| 80 | // If we know have an implementation (and the ivar is in it) then |
| 81 | // look up in the implementation layout. |
| 82 | const ASTRecordLayout *RL; |
| 83 | if (ID && ID->getClassInterface() == Container) |
| 84 | RL = &CGM.getContext().getASTObjCImplementationLayout(ID); |
| 85 | else |
| 86 | RL = &CGM.getContext().getASTObjCInterfaceLayout(Container); |
| 87 | return RL->getFieldOffset(Index); |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM, |
| 91 | const ObjCInterfaceDecl *OID, |
| 92 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 93 | return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8; |
| 94 | } |
| 95 | |
| 96 | uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM, |
| 97 | const ObjCImplementationDecl *OID, |
| 98 | const ObjCIvarDecl *Ivar) { |
| 99 | return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8; |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF, |
| 103 | const ObjCInterfaceDecl *OID, |
| 104 | llvm::Value *BaseValue, |
| 105 | const ObjCIvarDecl *Ivar, |
| 106 | unsigned CVRQualifiers, |
| 107 | llvm::Value *Offset) { |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 108 | // Compute (type*) ( (char *) BaseValue + Offset) |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 109 | llvm::Type *I8Ptr = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 110 | QualType IvarTy = Ivar->getType(); |
| 111 | const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 112 | llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 113 | V = CGF.Builder.CreateGEP(V, Offset, "add.ptr"); |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 114 | V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy)); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 115 | |
| 116 | if (Ivar->isBitField()) { |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 117 | // We need to compute the bit offset for the bit-field, the offset |
| 118 | // is to the byte. Note, there is a subtle invariant here: we can |
| 119 | // only call this routine on non-sythesized ivars but we may be |
| 120 | // called for synthesized ivars. However, a synthesized ivar can |
| 121 | // never be a bit-field so this is safe. |
| 122 | uint64_t BitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar) % 8; |
| 123 | |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 124 | uint64_t BitFieldSize = |
| 125 | Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue(); |
| 126 | return LValue::MakeBitfield(V, BitOffset, BitFieldSize, |
Daniel Dunbar | e38df86 | 2009-05-03 07:52:00 +0000 | [diff] [blame] | 127 | IvarTy->isSignedIntegerType(), |
| 128 | IvarTy.getCVRQualifiers()|CVRQualifiers); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 131 | LValue LV = LValue::MakeAddr(V, IvarTy.getCVRQualifiers()|CVRQualifiers, |
| 132 | CGF.CGM.getContext().getObjCGCAttrKind(IvarTy)); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 133 | LValue::SetObjCIvar(LV, true); |
| 134 | return LV; |
| 135 | } |
| 136 | |
| 137 | /// |
| 138 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 139 | namespace { |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 140 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 141 | typedef std::vector<llvm::Constant*> ConstantVector; |
| 142 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame^] | 143 | // FIXME: We should find a nicer way to make the labels for metadata, string |
| 144 | // concatenation is lame. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 145 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 146 | class ObjCCommonTypesHelper { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 147 | private: |
| 148 | llvm::Constant *getMessageSendFn() const { |
| 149 | // id objc_msgSend (id, SEL, ...) |
| 150 | std::vector<const llvm::Type*> Params; |
| 151 | Params.push_back(ObjectPtrTy); |
| 152 | Params.push_back(SelectorPtrTy); |
| 153 | return |
| 154 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 155 | Params, true), |
| 156 | "objc_msgSend"); |
| 157 | } |
| 158 | |
| 159 | llvm::Constant *getMessageSendStretFn() const { |
| 160 | // id objc_msgSend_stret (id, SEL, ...) |
| 161 | std::vector<const llvm::Type*> Params; |
| 162 | Params.push_back(ObjectPtrTy); |
| 163 | Params.push_back(SelectorPtrTy); |
| 164 | return |
| 165 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 166 | Params, true), |
| 167 | "objc_msgSend_stret"); |
| 168 | |
| 169 | } |
| 170 | |
| 171 | llvm::Constant *getMessageSendFpretFn() const { |
| 172 | // FIXME: This should be long double on x86_64? |
| 173 | // [double | long double] objc_msgSend_fpret(id self, SEL op, ...) |
| 174 | std::vector<const llvm::Type*> Params; |
| 175 | Params.push_back(ObjectPtrTy); |
| 176 | Params.push_back(SelectorPtrTy); |
| 177 | return |
| 178 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy, |
| 179 | Params, |
| 180 | true), |
| 181 | "objc_msgSend_fpret"); |
| 182 | |
| 183 | } |
| 184 | |
| 185 | llvm::Constant *getMessageSendSuperFn() const { |
| 186 | // id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
| 187 | const char *SuperName = "objc_msgSendSuper"; |
| 188 | std::vector<const llvm::Type*> Params; |
| 189 | Params.push_back(SuperPtrTy); |
| 190 | Params.push_back(SelectorPtrTy); |
| 191 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 192 | Params, true), |
| 193 | SuperName); |
| 194 | } |
| 195 | |
| 196 | llvm::Constant *getMessageSendSuperFn2() const { |
| 197 | // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...) |
| 198 | const char *SuperName = "objc_msgSendSuper2"; |
| 199 | std::vector<const llvm::Type*> Params; |
| 200 | Params.push_back(SuperPtrTy); |
| 201 | Params.push_back(SelectorPtrTy); |
| 202 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 203 | Params, true), |
| 204 | SuperName); |
| 205 | } |
| 206 | |
| 207 | llvm::Constant *getMessageSendSuperStretFn() const { |
| 208 | // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super, |
| 209 | // SEL op, ...) |
| 210 | std::vector<const llvm::Type*> Params; |
| 211 | Params.push_back(Int8PtrTy); |
| 212 | Params.push_back(SuperPtrTy); |
| 213 | Params.push_back(SelectorPtrTy); |
| 214 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 215 | Params, true), |
| 216 | "objc_msgSendSuper_stret"); |
| 217 | } |
| 218 | |
| 219 | llvm::Constant *getMessageSendSuperStretFn2() const { |
| 220 | // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super, |
| 221 | // SEL op, ...) |
| 222 | std::vector<const llvm::Type*> Params; |
| 223 | Params.push_back(Int8PtrTy); |
| 224 | Params.push_back(SuperPtrTy); |
| 225 | Params.push_back(SelectorPtrTy); |
| 226 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 227 | Params, true), |
| 228 | "objc_msgSendSuper2_stret"); |
| 229 | } |
| 230 | |
| 231 | llvm::Constant *getMessageSendSuperFpretFn() const { |
| 232 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 233 | return getMessageSendSuperFn(); |
| 234 | } |
| 235 | |
| 236 | llvm::Constant *getMessageSendSuperFpretFn2() const { |
| 237 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 238 | return getMessageSendSuperFn2(); |
| 239 | } |
| 240 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 241 | protected: |
| 242 | CodeGen::CodeGenModule &CGM; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 243 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 244 | public: |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 245 | const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 246 | const llvm::Type *Int8PtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 247 | |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 248 | /// ObjectPtrTy - LLVM type for object handles (typeof(id)) |
| 249 | const llvm::Type *ObjectPtrTy; |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 250 | |
| 251 | /// PtrObjectPtrTy - LLVM type for id * |
| 252 | const llvm::Type *PtrObjectPtrTy; |
| 253 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 254 | /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL)) |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 255 | const llvm::Type *SelectorPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 256 | /// ProtocolPtrTy - LLVM type for external protocol handles |
| 257 | /// (typeof(Protocol)) |
| 258 | const llvm::Type *ExternalProtocolPtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 259 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 260 | // SuperCTy - clang type for struct objc_super. |
| 261 | QualType SuperCTy; |
| 262 | // SuperPtrCTy - clang type for struct objc_super *. |
| 263 | QualType SuperPtrCTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 264 | |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 265 | /// SuperTy - LLVM type for struct objc_super. |
| 266 | const llvm::StructType *SuperTy; |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 267 | /// SuperPtrTy - LLVM type for struct objc_super *. |
| 268 | const llvm::Type *SuperPtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 269 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 270 | /// PropertyTy - LLVM type for struct objc_property (struct _prop_t |
| 271 | /// in GCC parlance). |
| 272 | const llvm::StructType *PropertyTy; |
| 273 | |
| 274 | /// PropertyListTy - LLVM type for struct objc_property_list |
| 275 | /// (_prop_list_t in GCC parlance). |
| 276 | const llvm::StructType *PropertyListTy; |
| 277 | /// PropertyListPtrTy - LLVM type for struct objc_property_list*. |
| 278 | const llvm::Type *PropertyListPtrTy; |
| 279 | |
| 280 | // MethodTy - LLVM type for struct objc_method. |
| 281 | const llvm::StructType *MethodTy; |
| 282 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 283 | /// CacheTy - LLVM type for struct objc_cache. |
| 284 | const llvm::Type *CacheTy; |
| 285 | /// CachePtrTy - LLVM type for struct objc_cache *. |
| 286 | const llvm::Type *CachePtrTy; |
| 287 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 288 | llvm::Constant *getGetPropertyFn() { |
| 289 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 290 | ASTContext &Ctx = CGM.getContext(); |
| 291 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
| 292 | llvm::SmallVector<QualType,16> Params; |
| 293 | QualType IdType = Ctx.getObjCIdType(); |
| 294 | QualType SelType = Ctx.getObjCSelType(); |
| 295 | Params.push_back(IdType); |
| 296 | Params.push_back(SelType); |
| 297 | Params.push_back(Ctx.LongTy); |
| 298 | Params.push_back(Ctx.BoolTy); |
| 299 | const llvm::FunctionType *FTy = |
| 300 | Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false); |
| 301 | return CGM.CreateRuntimeFunction(FTy, "objc_getProperty"); |
| 302 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 303 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 304 | llvm::Constant *getSetPropertyFn() { |
| 305 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 306 | ASTContext &Ctx = CGM.getContext(); |
| 307 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
| 308 | llvm::SmallVector<QualType,16> Params; |
| 309 | QualType IdType = Ctx.getObjCIdType(); |
| 310 | QualType SelType = Ctx.getObjCSelType(); |
| 311 | Params.push_back(IdType); |
| 312 | Params.push_back(SelType); |
| 313 | Params.push_back(Ctx.LongTy); |
| 314 | Params.push_back(IdType); |
| 315 | Params.push_back(Ctx.BoolTy); |
| 316 | Params.push_back(Ctx.BoolTy); |
| 317 | const llvm::FunctionType *FTy = |
| 318 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
| 319 | return CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); |
| 320 | } |
| 321 | |
| 322 | llvm::Constant *getEnumerationMutationFn() { |
| 323 | // void objc_enumerationMutation (id) |
| 324 | std::vector<const llvm::Type*> Args; |
| 325 | Args.push_back(ObjectPtrTy); |
| 326 | llvm::FunctionType *FTy = |
| 327 | llvm::FunctionType::get(llvm::Type::VoidTy, Args, false); |
| 328 | return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation"); |
| 329 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 330 | |
| 331 | /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 332 | llvm::Constant *getGcReadWeakFn() { |
| 333 | // id objc_read_weak (id *) |
| 334 | std::vector<const llvm::Type*> Args; |
| 335 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 336 | llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 337 | return CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
| 338 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 339 | |
| 340 | /// GcAssignWeakFn -- LLVM objc_assign_weak function. |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 341 | llvm::Constant *getGcAssignWeakFn() { |
| 342 | // id objc_assign_weak (id, id *) |
| 343 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 344 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 345 | llvm::FunctionType *FTy = |
| 346 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 347 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 348 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 349 | |
| 350 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 351 | llvm::Constant *getGcAssignGlobalFn() { |
| 352 | // id objc_assign_global(id, id *) |
| 353 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 354 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 355 | llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 356 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 357 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 358 | |
| 359 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 360 | llvm::Constant *getGcAssignIvarFn() { |
| 361 | // id objc_assign_ivar(id, id *) |
| 362 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 363 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 364 | llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 365 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 366 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 367 | |
| 368 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 369 | llvm::Constant *getGcAssignStrongCastFn() { |
| 370 | // id objc_assign_global(id, id *) |
| 371 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 372 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 373 | llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 374 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); |
| 375 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 376 | |
| 377 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 378 | llvm::Constant *getExceptionThrowFn() { |
| 379 | // void objc_exception_throw(id) |
| 380 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 381 | llvm::FunctionType *FTy = |
| 382 | llvm::FunctionType::get(llvm::Type::VoidTy, Args, false); |
| 383 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
| 384 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 385 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 386 | /// SyncEnterFn - LLVM object_sync_enter function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 387 | llvm::Constant *getSyncEnterFn() { |
| 388 | // void objc_sync_enter (id) |
| 389 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 390 | llvm::FunctionType *FTy = |
| 391 | llvm::FunctionType::get(llvm::Type::VoidTy, Args, false); |
| 392 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 393 | } |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 394 | |
| 395 | /// SyncExitFn - LLVM object_sync_exit function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 396 | llvm::Constant *getSyncExitFn() { |
| 397 | // void objc_sync_exit (id) |
| 398 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 399 | llvm::FunctionType *FTy = |
| 400 | llvm::FunctionType::get(llvm::Type::VoidTy, Args, false); |
| 401 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
| 402 | } |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 403 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 404 | llvm::Constant *getSendFn(bool IsSuper) const { |
| 405 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFn(); |
| 406 | } |
| 407 | |
| 408 | llvm::Constant *getSendFn2(bool IsSuper) const { |
| 409 | return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn(); |
| 410 | } |
| 411 | |
| 412 | llvm::Constant *getSendStretFn(bool IsSuper) const { |
| 413 | return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn(); |
| 414 | } |
| 415 | |
| 416 | llvm::Constant *getSendStretFn2(bool IsSuper) const { |
| 417 | return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn(); |
| 418 | } |
| 419 | |
| 420 | llvm::Constant *getSendFpretFn(bool IsSuper) const { |
| 421 | return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn(); |
| 422 | } |
| 423 | |
| 424 | llvm::Constant *getSendFpretFn2(bool IsSuper) const { |
| 425 | return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn(); |
| 426 | } |
| 427 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 428 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
| 429 | ~ObjCCommonTypesHelper(){} |
| 430 | }; |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 431 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 432 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 433 | /// construction of varies types used during ObjC generation. |
| 434 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 435 | public: |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 436 | /// SymtabTy - LLVM type for struct objc_symtab. |
| 437 | const llvm::StructType *SymtabTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 438 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
| 439 | const llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 440 | /// ModuleTy - LLVM type for struct objc_module. |
| 441 | const llvm::StructType *ModuleTy; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 442 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 443 | /// ProtocolTy - LLVM type for struct objc_protocol. |
| 444 | const llvm::StructType *ProtocolTy; |
| 445 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
| 446 | const llvm::Type *ProtocolPtrTy; |
| 447 | /// ProtocolExtensionTy - LLVM type for struct |
| 448 | /// objc_protocol_extension. |
| 449 | const llvm::StructType *ProtocolExtensionTy; |
| 450 | /// ProtocolExtensionTy - LLVM type for struct |
| 451 | /// objc_protocol_extension *. |
| 452 | const llvm::Type *ProtocolExtensionPtrTy; |
| 453 | /// MethodDescriptionTy - LLVM type for struct |
| 454 | /// objc_method_description. |
| 455 | const llvm::StructType *MethodDescriptionTy; |
| 456 | /// MethodDescriptionListTy - LLVM type for struct |
| 457 | /// objc_method_description_list. |
| 458 | const llvm::StructType *MethodDescriptionListTy; |
| 459 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 460 | /// objc_method_description_list *. |
| 461 | const llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 462 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
| 463 | const llvm::Type *ProtocolListTy; |
| 464 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
| 465 | const llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 466 | /// CategoryTy - LLVM type for struct objc_category. |
| 467 | const llvm::StructType *CategoryTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 468 | /// ClassTy - LLVM type for struct objc_class. |
| 469 | const llvm::StructType *ClassTy; |
| 470 | /// ClassPtrTy - LLVM type for struct objc_class *. |
| 471 | const llvm::Type *ClassPtrTy; |
| 472 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
| 473 | const llvm::StructType *ClassExtensionTy; |
| 474 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
| 475 | const llvm::Type *ClassExtensionPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 476 | // IvarTy - LLVM type for struct objc_ivar. |
| 477 | const llvm::StructType *IvarTy; |
| 478 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
| 479 | const llvm::Type *IvarListTy; |
| 480 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
| 481 | const llvm::Type *IvarListPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 482 | /// MethodListTy - LLVM type for struct objc_method_list. |
| 483 | const llvm::Type *MethodListTy; |
| 484 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
| 485 | const llvm::Type *MethodListPtrTy; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 486 | |
| 487 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
| 488 | const llvm::Type *ExceptionDataTy; |
| 489 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 490 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 491 | llvm::Constant *getExceptionTryEnterFn() { |
| 492 | std::vector<const llvm::Type*> Params; |
| 493 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 494 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 495 | Params, false), |
| 496 | "objc_exception_try_enter"); |
| 497 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 498 | |
| 499 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 500 | llvm::Constant *getExceptionTryExitFn() { |
| 501 | std::vector<const llvm::Type*> Params; |
| 502 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 503 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 504 | Params, false), |
| 505 | "objc_exception_try_exit"); |
| 506 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 507 | |
| 508 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 509 | llvm::Constant *getExceptionExtractFn() { |
| 510 | std::vector<const llvm::Type*> Params; |
| 511 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 512 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 513 | Params, false), |
| 514 | "objc_exception_extract"); |
| 515 | |
| 516 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 517 | |
| 518 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 519 | llvm::Constant *getExceptionMatchFn() { |
| 520 | std::vector<const llvm::Type*> Params; |
| 521 | Params.push_back(ClassPtrTy); |
| 522 | Params.push_back(ObjectPtrTy); |
| 523 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 524 | Params, false), |
| 525 | "objc_exception_match"); |
| 526 | |
| 527 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 528 | |
| 529 | /// SetJmpFn - LLVM _setjmp function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 530 | llvm::Constant *getSetJmpFn() { |
| 531 | std::vector<const llvm::Type*> Params; |
| 532 | Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty)); |
| 533 | return |
| 534 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 535 | Params, false), |
| 536 | "_setjmp"); |
| 537 | |
| 538 | } |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 539 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 540 | public: |
| 541 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 542 | ~ObjCTypesHelper() {} |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 543 | }; |
| 544 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 545 | /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 546 | /// modern abi |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 547 | class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 548 | public: |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 549 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 550 | // MethodListnfABITy - LLVM for struct _method_list_t |
| 551 | const llvm::StructType *MethodListnfABITy; |
| 552 | |
| 553 | // MethodListnfABIPtrTy - LLVM for struct _method_list_t* |
| 554 | const llvm::Type *MethodListnfABIPtrTy; |
| 555 | |
| 556 | // ProtocolnfABITy = LLVM for struct _protocol_t |
| 557 | const llvm::StructType *ProtocolnfABITy; |
| 558 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 559 | // ProtocolnfABIPtrTy = LLVM for struct _protocol_t* |
| 560 | const llvm::Type *ProtocolnfABIPtrTy; |
| 561 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 562 | // ProtocolListnfABITy - LLVM for struct _objc_protocol_list |
| 563 | const llvm::StructType *ProtocolListnfABITy; |
| 564 | |
| 565 | // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list* |
| 566 | const llvm::Type *ProtocolListnfABIPtrTy; |
| 567 | |
| 568 | // ClassnfABITy - LLVM for struct _class_t |
| 569 | const llvm::StructType *ClassnfABITy; |
| 570 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 571 | // ClassnfABIPtrTy - LLVM for struct _class_t* |
| 572 | const llvm::Type *ClassnfABIPtrTy; |
| 573 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 574 | // IvarnfABITy - LLVM for struct _ivar_t |
| 575 | const llvm::StructType *IvarnfABITy; |
| 576 | |
| 577 | // IvarListnfABITy - LLVM for struct _ivar_list_t |
| 578 | const llvm::StructType *IvarListnfABITy; |
| 579 | |
| 580 | // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t* |
| 581 | const llvm::Type *IvarListnfABIPtrTy; |
| 582 | |
| 583 | // ClassRonfABITy - LLVM for struct _class_ro_t |
| 584 | const llvm::StructType *ClassRonfABITy; |
| 585 | |
| 586 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 587 | const llvm::Type *ImpnfABITy; |
| 588 | |
| 589 | // CategorynfABITy - LLVM for struct _category_t |
| 590 | const llvm::StructType *CategorynfABITy; |
| 591 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 592 | // New types for nonfragile abi messaging. |
| 593 | |
| 594 | // MessageRefTy - LLVM for: |
| 595 | // struct _message_ref_t { |
| 596 | // IMP messenger; |
| 597 | // SEL name; |
| 598 | // }; |
| 599 | const llvm::StructType *MessageRefTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 600 | // MessageRefCTy - clang type for struct _message_ref_t |
| 601 | QualType MessageRefCTy; |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 602 | |
| 603 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 604 | const llvm::Type *MessageRefPtrTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 605 | // MessageRefCPtrTy - clang type for struct _message_ref_t* |
| 606 | QualType MessageRefCPtrTy; |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 607 | |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 608 | // MessengerTy - Type of the messenger (shown as IMP above) |
| 609 | const llvm::FunctionType *MessengerTy; |
| 610 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 611 | // SuperMessageRefTy - LLVM for: |
| 612 | // struct _super_message_ref_t { |
| 613 | // SUPER_IMP messenger; |
| 614 | // SEL name; |
| 615 | // }; |
| 616 | const llvm::StructType *SuperMessageRefTy; |
| 617 | |
| 618 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 619 | const llvm::Type *SuperMessageRefPtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 620 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 621 | llvm::Constant *getMessageSendFixupFn() { |
| 622 | // id objc_msgSend_fixup(id, struct message_ref_t*, ...) |
| 623 | std::vector<const llvm::Type*> Params; |
| 624 | Params.push_back(ObjectPtrTy); |
| 625 | Params.push_back(MessageRefPtrTy); |
| 626 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 627 | Params, true), |
| 628 | "objc_msgSend_fixup"); |
| 629 | } |
| 630 | |
| 631 | llvm::Constant *getMessageSendFpretFixupFn() { |
| 632 | // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...) |
| 633 | std::vector<const llvm::Type*> Params; |
| 634 | Params.push_back(ObjectPtrTy); |
| 635 | Params.push_back(MessageRefPtrTy); |
| 636 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 637 | Params, true), |
| 638 | "objc_msgSend_fpret_fixup"); |
| 639 | } |
| 640 | |
| 641 | llvm::Constant *getMessageSendStretFixupFn() { |
| 642 | // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...) |
| 643 | std::vector<const llvm::Type*> Params; |
| 644 | Params.push_back(ObjectPtrTy); |
| 645 | Params.push_back(MessageRefPtrTy); |
| 646 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 647 | Params, true), |
| 648 | "objc_msgSend_stret_fixup"); |
| 649 | } |
| 650 | |
| 651 | llvm::Constant *getMessageSendIdFixupFn() { |
| 652 | // id objc_msgSendId_fixup(id, struct message_ref_t*, ...) |
| 653 | std::vector<const llvm::Type*> Params; |
| 654 | Params.push_back(ObjectPtrTy); |
| 655 | Params.push_back(MessageRefPtrTy); |
| 656 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 657 | Params, true), |
| 658 | "objc_msgSendId_fixup"); |
| 659 | } |
| 660 | |
| 661 | llvm::Constant *getMessageSendIdStretFixupFn() { |
| 662 | // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...) |
| 663 | std::vector<const llvm::Type*> Params; |
| 664 | Params.push_back(ObjectPtrTy); |
| 665 | Params.push_back(MessageRefPtrTy); |
| 666 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 667 | Params, true), |
| 668 | "objc_msgSendId_stret_fixup"); |
| 669 | } |
| 670 | llvm::Constant *getMessageSendSuper2FixupFn() { |
| 671 | // id objc_msgSendSuper2_fixup (struct objc_super *, |
| 672 | // struct _super_message_ref_t*, ...) |
| 673 | std::vector<const llvm::Type*> Params; |
| 674 | Params.push_back(SuperPtrTy); |
| 675 | Params.push_back(SuperMessageRefPtrTy); |
| 676 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 677 | Params, true), |
| 678 | "objc_msgSendSuper2_fixup"); |
| 679 | } |
| 680 | |
| 681 | llvm::Constant *getMessageSendSuper2StretFixupFn() { |
| 682 | // id objc_msgSendSuper2_stret_fixup(struct objc_super *, |
| 683 | // struct _super_message_ref_t*, ...) |
| 684 | std::vector<const llvm::Type*> Params; |
| 685 | Params.push_back(SuperPtrTy); |
| 686 | Params.push_back(SuperMessageRefPtrTy); |
| 687 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 688 | Params, true), |
| 689 | "objc_msgSendSuper2_stret_fixup"); |
| 690 | } |
| 691 | |
| 692 | |
| 693 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 694 | /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C |
| 695 | /// exception personality function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 696 | llvm::Value *getEHPersonalityPtr() { |
| 697 | llvm::Constant *Personality = |
| 698 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 699 | std::vector<const llvm::Type*>(), |
| 700 | true), |
| 701 | "__objc_personality_v0"); |
| 702 | return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy); |
| 703 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 704 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 705 | llvm::Constant *getUnwindResumeOrRethrowFn() { |
| 706 | std::vector<const llvm::Type*> Params; |
| 707 | Params.push_back(Int8PtrTy); |
| 708 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 709 | Params, false), |
| 710 | "_Unwind_Resume_or_Rethrow"); |
| 711 | } |
| 712 | |
| 713 | llvm::Constant *getObjCEndCatchFn() { |
| 714 | std::vector<const llvm::Type*> Params; |
| 715 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 716 | Params, false), |
| 717 | "objc_end_catch"); |
| 718 | |
| 719 | } |
| 720 | |
| 721 | llvm::Constant *getObjCBeginCatchFn() { |
| 722 | std::vector<const llvm::Type*> Params; |
| 723 | Params.push_back(Int8PtrTy); |
| 724 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy, |
| 725 | Params, false), |
| 726 | "objc_begin_catch"); |
| 727 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 728 | |
| 729 | const llvm::StructType *EHTypeTy; |
| 730 | const llvm::Type *EHTypePtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 731 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 732 | ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); |
| 733 | ~ObjCNonFragileABITypesHelper(){} |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 734 | }; |
| 735 | |
| 736 | class CGObjCCommonMac : public CodeGen::CGObjCRuntime { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 737 | public: |
| 738 | // FIXME - accessibility |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 739 | class GC_IVAR { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 740 | public: |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 741 | unsigned ivar_bytepos; |
| 742 | unsigned ivar_size; |
| 743 | GC_IVAR(unsigned bytepos = 0, unsigned size = 0) |
| 744 | : ivar_bytepos(bytepos), ivar_size(size) {} |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 745 | |
| 746 | // Allow sorting based on byte pos. |
| 747 | bool operator<(const GC_IVAR &b) const { |
| 748 | return ivar_bytepos < b.ivar_bytepos; |
| 749 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 750 | }; |
| 751 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 752 | class SKIP_SCAN { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 753 | public: |
| 754 | unsigned skip; |
| 755 | unsigned scan; |
| 756 | SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0) |
| 757 | : skip(_skip), scan(_scan) {} |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 758 | }; |
| 759 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 760 | protected: |
| 761 | CodeGen::CodeGenModule &CGM; |
| 762 | // FIXME! May not be needing this after all. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 763 | unsigned ObjCABI; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 764 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 765 | // gc ivar layout bitmap calculation helper caches. |
| 766 | llvm::SmallVector<GC_IVAR, 16> SkipIvars; |
| 767 | llvm::SmallVector<GC_IVAR, 16> IvarsInfo; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 768 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 769 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 770 | /// DefinedSymbols and FinishModule(). |
| 771 | std::set<IdentifierInfo*> LazySymbols; |
| 772 | |
| 773 | /// DefinedSymbols - External symbols which are defined by this |
| 774 | /// module. The symbols in this list and LazySymbols are used to add |
| 775 | /// special linker symbols which ensure that Objective-C modules are |
| 776 | /// linked properly. |
| 777 | std::set<IdentifierInfo*> DefinedSymbols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 778 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 779 | /// ClassNames - uniqued class names. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 780 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 781 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 782 | /// MethodVarNames - uniqued method variable names. |
| 783 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 784 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 785 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 786 | /// a StringMap here because have no other unique reference. |
| 787 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 788 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 789 | /// MethodDefinitions - map of methods which have been defined in |
| 790 | /// this translation unit. |
| 791 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 792 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 793 | /// PropertyNames - uniqued method variable names. |
| 794 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 795 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 796 | /// ClassReferences - uniqued class references. |
| 797 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 798 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 799 | /// SelectorReferences - uniqued selector references. |
| 800 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 801 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 802 | /// Protocols - Protocols for which an objc_protocol structure has |
| 803 | /// been emitted. Forward declarations are handled by creating an |
| 804 | /// empty structure whose initializer is filled in when/if defined. |
| 805 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 806 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 807 | /// DefinedProtocols - Protocols which have actually been |
| 808 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 809 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 810 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 811 | /// DefinedClasses - List of defined classes. |
| 812 | std::vector<llvm::GlobalValue*> DefinedClasses; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 813 | |
| 814 | /// DefinedNonLazyClasses - List of defined "non-lazy" classes. |
| 815 | std::vector<llvm::GlobalValue*> DefinedNonLazyClasses; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 816 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 817 | /// DefinedCategories - List of defined categories. |
| 818 | std::vector<llvm::GlobalValue*> DefinedCategories; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 819 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 820 | /// DefinedNonLazyCategories - List of defined "non-lazy" categories. |
| 821 | std::vector<llvm::GlobalValue*> DefinedNonLazyCategories; |
| 822 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 823 | /// UsedGlobals - List of globals to pack into the llvm.used metadata |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 824 | /// to prevent them from being clobbered. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 825 | std::vector<llvm::GlobalVariable*> UsedGlobals; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 826 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 827 | /// GetNameForMethod - Return a name for the given method. |
| 828 | /// \param[out] NameOut - The return value. |
| 829 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 830 | const ObjCContainerDecl *CD, |
| 831 | std::string &NameOut); |
| 832 | |
| 833 | /// GetMethodVarName - Return a unique constant for the given |
| 834 | /// selector's name. The return value has type char *. |
| 835 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 836 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
| 837 | llvm::Constant *GetMethodVarName(const std::string &Name); |
| 838 | |
| 839 | /// GetMethodVarType - Return a unique constant for the given |
| 840 | /// selector's name. The return value has type char *. |
| 841 | |
| 842 | // FIXME: This is a horrible name. |
| 843 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 844 | llvm::Constant *GetMethodVarType(const FieldDecl *D); |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 845 | |
| 846 | /// GetPropertyName - Return a unique constant for the given |
| 847 | /// name. The return value has type char *. |
| 848 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
| 849 | |
| 850 | // FIXME: This can be dropped once string functions are unified. |
| 851 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 852 | const Decl *Container); |
| 853 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 854 | /// GetClassName - Return a unique constant for the given selector's |
| 855 | /// name. The return value has type char *. |
| 856 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
| 857 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 858 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 859 | /// implementation for the __strong or __weak case. |
| 860 | /// |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 861 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
| 862 | bool ForStrongLayout); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 863 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 864 | void BuildAggrIvarRecordLayout(const RecordType *RT, |
| 865 | unsigned int BytePos, bool ForStrongLayout, |
| 866 | bool &HasUnion); |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 867 | void BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 868 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 869 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 870 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 871 | unsigned int BytePos, bool ForStrongLayout, |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 872 | bool &HasUnion); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 873 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 874 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 875 | /// ivar layout bitmap. |
| 876 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 877 | const ObjCCommonTypesHelper &ObjCTypes); |
| 878 | |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 879 | /// EmitPropertyList - Emit the given property list. The return |
| 880 | /// value has type PropertyListPtrTy. |
| 881 | llvm::Constant *EmitPropertyList(const std::string &Name, |
| 882 | const Decl *Container, |
| 883 | const ObjCContainerDecl *OCD, |
| 884 | const ObjCCommonTypesHelper &ObjCTypes); |
| 885 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 886 | /// GetProtocolRef - Return a reference to the internal protocol |
| 887 | /// description, creating an empty one if it has not been |
| 888 | /// defined. The return value has type ProtocolPtrTy. |
| 889 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 890 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 891 | /// CreateMetadataVar - Create a global variable with internal |
| 892 | /// linkage for use by the Objective-C runtime. |
| 893 | /// |
| 894 | /// This is a convenience wrapper which not only creates the |
| 895 | /// variable, but also sets the section and alignment and adds the |
| 896 | /// global to the UsedGlobals list. |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 897 | /// |
| 898 | /// \param Name - The variable name. |
| 899 | /// \param Init - The variable initializer; this is also used to |
| 900 | /// define the type of the variable. |
| 901 | /// \param Section - The section the variable should go into, or 0. |
| 902 | /// \param Align - The alignment for the variable, or 0. |
| 903 | /// \param AddToUsed - Whether the variable should be added to |
Daniel Dunbar | c158306 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 904 | /// "llvm.used". |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 905 | llvm::GlobalVariable *CreateMetadataVar(const std::string &Name, |
| 906 | llvm::Constant *Init, |
| 907 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 908 | unsigned Align, |
| 909 | bool AddToUsed); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 910 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 911 | /// GetNamedIvarList - Return the list of ivars in the interface |
| 912 | /// itself (not including super classes and not including unnamed |
| 913 | /// bitfields). |
| 914 | /// |
| 915 | /// For the non-fragile ABI, this also includes synthesized property |
| 916 | /// ivars. |
| 917 | void GetNamedIvarList(const ObjCInterfaceDecl *OID, |
| 918 | llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const; |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 919 | |
| 920 | CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF, |
| 921 | QualType ResultType, |
| 922 | llvm::Value *Sel, |
| 923 | llvm::Value *Arg0, |
| 924 | QualType Arg0Ty, |
| 925 | bool IsSuper, |
| 926 | const CallArgList &CallArgs, |
| 927 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 928 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 929 | public: |
| 930 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm) |
| 931 | { } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 932 | |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 933 | virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 934 | |
| 935 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 936 | const ObjCContainerDecl *CD=0); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 937 | |
| 938 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
| 939 | |
| 940 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 941 | /// declaration, emitting it if necessary. The return value has type |
| 942 | /// ProtocolPtrTy. |
| 943 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
| 944 | |
| 945 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 946 | /// object for the given declaration, emitting it if needed. These |
| 947 | /// forward references will be filled in with empty bodies if no |
| 948 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 949 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 950 | }; |
| 951 | |
| 952 | class CGObjCMac : public CGObjCCommonMac { |
| 953 | private: |
| 954 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 955 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 956 | /// level information. |
| 957 | void EmitImageInfo(); |
| 958 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 959 | /// EmitModuleInfo - Another marker encoding module level |
| 960 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 961 | void EmitModuleInfo(); |
| 962 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 963 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 964 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 965 | llvm::Constant *EmitModuleSymbols(); |
| 966 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 967 | /// FinishModule - Write out global data structures at the end of |
| 968 | /// processing a translation unit. |
| 969 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 970 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 971 | /// EmitClassExtension - Generate the class extension structure used |
| 972 | /// to store the weak ivar layout and properties. The return value |
| 973 | /// has type ClassExtensionPtrTy. |
| 974 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 975 | |
| 976 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 977 | /// for the given class. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 978 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 979 | const ObjCInterfaceDecl *ID); |
| 980 | |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 981 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 982 | QualType ResultType, |
| 983 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 984 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 985 | QualType Arg0Ty, |
| 986 | bool IsSuper, |
| 987 | const CallArgList &CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 988 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 989 | /// EmitIvarList - Emit the ivar list for the given |
| 990 | /// implementation. If ForClass is true the list of class ivars |
| 991 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 992 | /// interface ivars will be emitted. The return value has type |
| 993 | /// IvarListPtrTy. |
| 994 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 995 | bool ForClass); |
| 996 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 997 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 998 | /// for the metaclass of the given interface. The return value has |
| 999 | /// type ClassPtrTy. |
| 1000 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 1001 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1002 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1003 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1004 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1005 | llvm::Constant *Protocols, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1006 | const ConstantVector &Methods); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1007 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1008 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1009 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1010 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1011 | |
| 1012 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1013 | /// implementation. The return value has type MethodListPtrTy. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1014 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 1015 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1016 | const ConstantVector &Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1017 | |
| 1018 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1019 | /// method declarations. |
| 1020 | /// - TypeName: The name for the type containing the methods. |
| 1021 | /// - IsProtocol: True iff these methods are for a protocol. |
| 1022 | /// - ClassMethds: True iff these are class methods. |
| 1023 | /// - Required: When true, only "required" methods are |
| 1024 | /// listed. Similarly, when false only "optional" methods are |
| 1025 | /// listed. For classes this should always be true. |
| 1026 | /// - begin, end: The method list to output. |
| 1027 | /// |
| 1028 | /// The return value has type MethodDescriptionListPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1029 | llvm::Constant *EmitMethodDescList(const std::string &Name, |
| 1030 | const char *Section, |
| 1031 | const ConstantVector &Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1032 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1033 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1034 | /// declaration, emitting it if necessary. The return value has type |
| 1035 | /// ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1036 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1037 | |
| 1038 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1039 | /// object for the given declaration, emitting it if needed. These |
| 1040 | /// forward references will be filled in with empty bodies if no |
| 1041 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1042 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1043 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1044 | /// EmitProtocolExtension - Generate the protocol extension |
| 1045 | /// structure used to store optional instance and class methods, and |
| 1046 | /// protocol properties. The return value has type |
| 1047 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1048 | llvm::Constant * |
| 1049 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1050 | const ConstantVector &OptInstanceMethods, |
| 1051 | const ConstantVector &OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1052 | |
| 1053 | /// EmitProtocolList - Generate the list of referenced |
| 1054 | /// protocols. The return value has type ProtocolListPtrTy. |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1055 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 1056 | ObjCProtocolDecl::protocol_iterator begin, |
| 1057 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1058 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1059 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1060 | /// for the given selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1061 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1062 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1063 | public: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1064 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1065 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1066 | virtual llvm::Function *ModuleInitFunction(); |
| 1067 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1068 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1069 | QualType ResultType, |
| 1070 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1071 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1072 | bool IsClassMessage, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1073 | const CallArgList &CallArgs, |
| 1074 | const ObjCMethodDecl *Method); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1075 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1076 | virtual CodeGen::RValue |
| 1077 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1078 | QualType ResultType, |
| 1079 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1080 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1081 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1082 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1083 | bool IsClassMessage, |
| 1084 | const CallArgList &CallArgs); |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1085 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1086 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1087 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1088 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1089 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel); |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1090 | |
| 1091 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1092 | /// untyped one. |
| 1093 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1094 | const ObjCMethodDecl *Method); |
| 1095 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1096 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1097 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1098 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1099 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1100 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1101 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1102 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1103 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 1104 | virtual llvm::Constant *GetPropertySetFunction(); |
| 1105 | virtual llvm::Constant *EnumerationMutationFunction(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1106 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 1107 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1108 | const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1109 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 1110 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1111 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 1112 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1113 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 1114 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1115 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 1116 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 1117 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 1118 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1119 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 1120 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 1121 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1122 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1123 | QualType ObjectTy, |
| 1124 | llvm::Value *BaseValue, |
| 1125 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1126 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1127 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1128 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1129 | const ObjCIvarDecl *Ivar); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1130 | }; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1131 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1132 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1133 | private: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1134 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1135 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 1136 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1137 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1138 | /// SuperClassReferences - uniqued super class references. |
| 1139 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; |
| 1140 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1141 | /// MetaClassReferences - uniqued meta class references. |
| 1142 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1143 | |
| 1144 | /// EHTypeReferences - uniqued class ehtype references. |
| 1145 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1146 | |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1147 | /// NonLegacyDispatchMethods - List of methods for which we do *not* generate |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1148 | /// legacy messaging dispatch. |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1149 | llvm::DenseSet<Selector> NonLegacyDispatchMethods; |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1150 | |
| 1151 | /// LegacyDispatchedSelector - Returns true if SEL is not in the list of |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1152 | /// NonLegacyDispatchMethods; false otherwise. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1153 | bool LegacyDispatchedSelector(Selector Sel); |
| 1154 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1155 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 1156 | /// processing a translation unit. |
| 1157 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1158 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 1159 | /// AddModuleClassList - Add the given list of class pointers to the |
| 1160 | /// module with the provided symbol and section names. |
| 1161 | void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container, |
| 1162 | const char *SymbolName, |
| 1163 | const char *SectionName); |
| 1164 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 1165 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 1166 | unsigned InstanceStart, |
| 1167 | unsigned InstanceSize, |
| 1168 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1169 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
| 1170 | llvm::Constant *IsAGV, |
| 1171 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 1172 | llvm::Constant *ClassRoGV, |
| 1173 | bool HiddenVisibility); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1174 | |
| 1175 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
| 1176 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 1177 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
| 1178 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1179 | /// EmitMethodList - Emit the method list for the given |
| 1180 | /// implementation. The return value has type MethodListnfABITy. |
| 1181 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 1182 | const char *Section, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 1183 | const ConstantVector &Methods); |
| 1184 | /// EmitIvarList - Emit the ivar list for the given |
| 1185 | /// implementation. If ForClass is true the list of class ivars |
| 1186 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1187 | /// interface ivars will be emitted. The return value has type |
| 1188 | /// IvarListnfABIPtrTy. |
| 1189 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 1190 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1191 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 1192 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 1193 | unsigned long int offset); |
| 1194 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1195 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1196 | /// declaration, emitting it if necessary. The return value has type |
| 1197 | /// ProtocolPtrTy. |
| 1198 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
| 1199 | |
| 1200 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1201 | /// object for the given declaration, emitting it if needed. These |
| 1202 | /// forward references will be filled in with empty bodies if no |
| 1203 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1204 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
| 1205 | |
| 1206 | /// EmitProtocolList - Generate the list of referenced |
| 1207 | /// protocols. The return value has type ProtocolListPtrTy. |
| 1208 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 1209 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1210 | ObjCProtocolDecl::protocol_iterator end); |
| 1211 | |
| 1212 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1213 | QualType ResultType, |
| 1214 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 1215 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1216 | QualType Arg0Ty, |
| 1217 | bool IsSuper, |
| 1218 | const CallArgList &CallArgs); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 1219 | |
| 1220 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1221 | /// class of the given name. |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 1222 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name); |
| 1223 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1224 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1225 | /// for the given class reference. |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1226 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1227 | const ObjCInterfaceDecl *ID); |
| 1228 | |
| 1229 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1230 | /// for the given super class reference. |
| 1231 | llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder, |
| 1232 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1233 | |
| 1234 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 1235 | /// meta-data |
| 1236 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
| 1237 | const ObjCInterfaceDecl *ID); |
| 1238 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1239 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 1240 | /// the given ivar. |
| 1241 | /// |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 1242 | llvm::GlobalVariable * ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1243 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1244 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1245 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1246 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1247 | /// for the given selector. |
| 1248 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1249 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1250 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1251 | /// interface. The return value has type EHTypePtrTy. |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1252 | llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 1253 | bool ForDefinition); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1254 | |
| 1255 | const char *getMetaclassSymbolPrefix() const { |
| 1256 | return "OBJC_METACLASS_$_"; |
| 1257 | } |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1258 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1259 | const char *getClassSymbolPrefix() const { |
| 1260 | return "OBJC_CLASS_$_"; |
| 1261 | } |
| 1262 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 1263 | void GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1264 | uint32_t &InstanceStart, |
| 1265 | uint32_t &InstanceSize); |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1266 | |
| 1267 | // Shamelessly stolen from Analysis/CFRefCount.cpp |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1268 | Selector GetNullarySelector(const char* name) const { |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1269 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1270 | return CGM.getContext().Selectors.getSelector(0, &II); |
| 1271 | } |
| 1272 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1273 | Selector GetUnarySelector(const char* name) const { |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1274 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1275 | return CGM.getContext().Selectors.getSelector(1, &II); |
| 1276 | } |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1277 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1278 | /// ImplementationIsNonLazy - Check whether the given category or |
| 1279 | /// class implementation is "non-lazy". |
| 1280 | bool ImplementationIsNonLazy(const DeclContext *DC) const; |
| 1281 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1282 | public: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1283 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1284 | // FIXME. All stubs for now! |
| 1285 | virtual llvm::Function *ModuleInitFunction(); |
| 1286 | |
| 1287 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1288 | QualType ResultType, |
| 1289 | Selector Sel, |
| 1290 | llvm::Value *Receiver, |
| 1291 | bool IsClassMessage, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1292 | const CallArgList &CallArgs, |
| 1293 | const ObjCMethodDecl *Method); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1294 | |
| 1295 | virtual CodeGen::RValue |
| 1296 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 1297 | QualType ResultType, |
| 1298 | Selector Sel, |
| 1299 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1300 | bool isCategoryImpl, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1301 | llvm::Value *Receiver, |
| 1302 | bool IsClassMessage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1303 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1304 | |
| 1305 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1306 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1307 | |
| 1308 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel) |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1309 | { return EmitSelector(Builder, Sel); } |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1310 | |
| 1311 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1312 | /// untyped one. |
| 1313 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1314 | const ObjCMethodDecl *Method) |
| 1315 | { return EmitSelector(Builder, Method->getSelector()); } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1316 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 1317 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1318 | |
| 1319 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1320 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 1321 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1322 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1323 | virtual llvm::Constant *GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1324 | return ObjCTypes.getGetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1325 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1326 | virtual llvm::Constant *GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1327 | return ObjCTypes.getSetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1328 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1329 | virtual llvm::Constant *EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1330 | return ObjCTypes.getEnumerationMutationFn(); |
Daniel Dunbar | 28ed084 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 1331 | } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1332 | |
| 1333 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 1334 | const Stmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1335 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 1336 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1337 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1338 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1339 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1340 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1341 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1342 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1343 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1344 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1345 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1346 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1347 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1348 | QualType ObjectTy, |
| 1349 | llvm::Value *BaseValue, |
| 1350 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1351 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1352 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1353 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1354 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1355 | }; |
| 1356 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1357 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1358 | |
| 1359 | /* *** Helper Functions *** */ |
| 1360 | |
| 1361 | /// getConstantGEP() - Help routine to construct simple GEPs. |
| 1362 | static llvm::Constant *getConstantGEP(llvm::Constant *C, |
| 1363 | unsigned idx0, |
| 1364 | unsigned idx1) { |
| 1365 | llvm::Value *Idxs[] = { |
| 1366 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0), |
| 1367 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1) |
| 1368 | }; |
| 1369 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2); |
| 1370 | } |
| 1371 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1372 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 1373 | /// class has the __objc_exception__ attribute. |
| 1374 | static bool hasObjCExceptionAttribute(const ObjCInterfaceDecl *OID) { |
Daniel Dunbar | b11fa0d | 2009-04-13 21:08:27 +0000 | [diff] [blame] | 1375 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1376 | return true; |
| 1377 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
| 1378 | return hasObjCExceptionAttribute(Super); |
| 1379 | return false; |
| 1380 | } |
| 1381 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1382 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1383 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1384 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
| 1385 | ObjCTypes(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1386 | { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1387 | ObjCABI = 1; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1388 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1391 | /// GetClass - Return a reference to the class for the given interface |
| 1392 | /// decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1393 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1394 | const ObjCInterfaceDecl *ID) { |
| 1395 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
| 1398 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1399 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 1400 | return EmitSelector(Builder, Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1401 | } |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1402 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl |
| 1403 | *Method) { |
| 1404 | return EmitSelector(Builder, Method->getSelector()); |
| 1405 | } |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1406 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1407 | /// Generate a constant CFString object. |
| 1408 | /* |
| 1409 | struct __builtin_CFString { |
| 1410 | const int *isa; // point to __CFConstantStringClassReference |
| 1411 | int flags; |
| 1412 | const char *str; |
| 1413 | long length; |
| 1414 | }; |
| 1415 | */ |
| 1416 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1417 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 1418 | const ObjCStringLiteral *SL) { |
Steve Naroff | 8d4141f | 2009-04-01 13:55:36 +0000 | [diff] [blame] | 1419 | return CGM.GetAddrOfConstantCFString(SL->getString()); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | /// Generates a message send where the super is the receiver. This is |
| 1423 | /// a message send to self with special delivery semantics indicating |
| 1424 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1425 | CodeGen::RValue |
| 1426 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1427 | QualType ResultType, |
| 1428 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1429 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1430 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1431 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1432 | bool IsClassMessage, |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1433 | const CodeGen::CallArgList &CallArgs) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1434 | // Create and init a super structure; this is a (receiver, class) |
| 1435 | // pair we will pass to objc_msgSendSuper. |
| 1436 | llvm::Value *ObjCSuper = |
| 1437 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 1438 | llvm::Value *ReceiverAsObject = |
| 1439 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 1440 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 1441 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1442 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1443 | // If this is a class message the metaclass is passed as the target. |
| 1444 | llvm::Value *Target; |
| 1445 | if (IsClassMessage) { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1446 | if (isCategoryImpl) { |
| 1447 | // Message sent to 'super' in a class method defined in a category |
| 1448 | // implementation requires an odd treatment. |
| 1449 | // If we are in a class method, we must retrieve the |
| 1450 | // _metaclass_ for the current class, pointed at by |
| 1451 | // the class's "isa" pointer. The following assumes that |
| 1452 | // isa" is the first ivar in a class (which it must be). |
| 1453 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1454 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 1455 | Target = CGF.Builder.CreateLoad(Target); |
| 1456 | } |
| 1457 | else { |
| 1458 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 1459 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 1460 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 1461 | Target = Super; |
| 1462 | } |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1463 | } else { |
| 1464 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1465 | } |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame^] | 1466 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 1467 | // ObjCTypes types. |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1468 | const llvm::Type *ClassTy = |
| 1469 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1470 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1471 | CGF.Builder.CreateStore(Target, |
| 1472 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1473 | return EmitLegacyMessageSend(CGF, ResultType, |
| 1474 | EmitSelector(CGF.Builder, Sel), |
| 1475 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 1476 | true, CallArgs, ObjCTypes); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1477 | } |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1478 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1479 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1480 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1481 | QualType ResultType, |
| 1482 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1483 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1484 | bool IsClassMessage, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1485 | const CallArgList &CallArgs, |
| 1486 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1487 | return EmitLegacyMessageSend(CGF, ResultType, |
| 1488 | EmitSelector(CGF.Builder, Sel), |
| 1489 | Receiver, CGF.getContext().getObjCIdType(), |
| 1490 | false, CallArgs, ObjCTypes); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1493 | CodeGen::RValue CGObjCCommonMac::EmitLegacyMessageSend( |
| 1494 | CodeGen::CodeGenFunction &CGF, |
| 1495 | QualType ResultType, |
| 1496 | llvm::Value *Sel, |
| 1497 | llvm::Value *Arg0, |
| 1498 | QualType Arg0Ty, |
| 1499 | bool IsSuper, |
| 1500 | const CallArgList &CallArgs, |
| 1501 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1502 | CallArgList ActualArgs; |
Fariborz Jahanian | d019d96 | 2009-04-24 21:07:43 +0000 | [diff] [blame] | 1503 | if (!IsSuper) |
| 1504 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1505 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1506 | ActualArgs.push_back(std::make_pair(RValue::get(Sel), |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1507 | CGF.getContext().getObjCSelType())); |
| 1508 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1509 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1510 | CodeGenTypes &Types = CGM.getTypes(); |
| 1511 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1512 | // In 64bit ABI, type must be assumed VARARG. In 32bit abi, |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1513 | // it seems not to matter. |
| 1514 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, (ObjCABI == 2)); |
| 1515 | |
| 1516 | llvm::Constant *Fn = NULL; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1517 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1518 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper) |
| 1519 | : ObjCTypes.getSendStretFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1520 | } else if (ResultType->isFloatingType()) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1521 | if (ObjCABI == 2) { |
| 1522 | if (const BuiltinType *BT = ResultType->getAsBuiltinType()) { |
| 1523 | BuiltinType::Kind k = BT->getKind(); |
| 1524 | Fn = (k == BuiltinType::LongDouble) ? ObjCTypes.getSendFpretFn2(IsSuper) |
| 1525 | : ObjCTypes.getSendFn2(IsSuper); |
| 1526 | } |
| 1527 | } |
| 1528 | else |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame^] | 1529 | // FIXME. This currently matches gcc's API for x86-32. May need to change |
| 1530 | // for others if we have their API. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1531 | Fn = ObjCTypes.getSendFpretFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1532 | } else { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1533 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper) |
| 1534 | : ObjCTypes.getSendFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1535 | } |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1536 | assert(Fn && "EmitLegacyMessageSend - unknown API"); |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 1537 | Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy)); |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1538 | return CGF.EmitCall(FnInfo, Fn, ActualArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1541 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1542 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1543 | // 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^] | 1544 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1545 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1546 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1547 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
| 1548 | ObjCTypes.ExternalProtocolPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1551 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame^] | 1552 | // FIXME: We shouldn't need this, the protocol decl should contain enough |
| 1553 | // information to tell us whether this was a declaration or a definition. |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1554 | DefinedProtocols.insert(PD->getIdentifier()); |
| 1555 | |
| 1556 | // If we have generated a forward reference to this protocol, emit |
| 1557 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 1558 | // emitted. |
| 1559 | if (Protocols.count(PD->getIdentifier())) |
| 1560 | GetOrEmitProtocol(PD); |
| 1561 | } |
| 1562 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1563 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1564 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 1565 | return GetOrEmitProtocol(PD); |
| 1566 | return GetOrEmitProtocolRef(PD); |
| 1567 | } |
| 1568 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1569 | /* |
| 1570 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 1571 | struct _objc_protocol { |
| 1572 | struct _objc_protocol_extension *isa; |
| 1573 | char *protocol_name; |
| 1574 | struct _objc_protocol_list *protocol_list; |
| 1575 | struct _objc__method_prototype_list *instance_methods; |
| 1576 | struct _objc__method_prototype_list *class_methods |
| 1577 | }; |
| 1578 | |
| 1579 | See EmitProtocolExtension(). |
| 1580 | */ |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1581 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
| 1582 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1583 | |
| 1584 | // Early exit if a defining object has already been generated. |
| 1585 | if (Entry && Entry->hasInitializer()) |
| 1586 | return Entry; |
| 1587 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1588 | // 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^] | 1589 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1590 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1591 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1592 | const char *ProtocolName = PD->getNameAsCString(); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1593 | |
| 1594 | // Construct method lists. |
| 1595 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1596 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1597 | for (ObjCProtocolDecl::instmeth_iterator |
| 1598 | i = PD->instmeth_begin(CGM.getContext()), |
| 1599 | e = PD->instmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1600 | ObjCMethodDecl *MD = *i; |
| 1601 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1602 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1603 | OptInstanceMethods.push_back(C); |
| 1604 | } else { |
| 1605 | InstanceMethods.push_back(C); |
| 1606 | } |
| 1607 | } |
| 1608 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1609 | for (ObjCProtocolDecl::classmeth_iterator |
| 1610 | i = PD->classmeth_begin(CGM.getContext()), |
| 1611 | e = PD->classmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1612 | ObjCMethodDecl *MD = *i; |
| 1613 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1614 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1615 | OptClassMethods.push_back(C); |
| 1616 | } else { |
| 1617 | ClassMethods.push_back(C); |
| 1618 | } |
| 1619 | } |
| 1620 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1621 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1622 | Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1623 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1624 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1625 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1626 | PD->protocol_begin(), |
| 1627 | PD->protocol_end()); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1628 | Values[3] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1629 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" |
| 1630 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1631 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1632 | InstanceMethods); |
| 1633 | Values[4] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1634 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" |
| 1635 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1636 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1637 | ClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1638 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 1639 | Values); |
| 1640 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1641 | if (Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1642 | // Already created, fix the linkage and update the initializer. |
| 1643 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1644 | Entry->setInitializer(Init); |
| 1645 | } else { |
| 1646 | Entry = |
| 1647 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
| 1648 | llvm::GlobalValue::InternalLinkage, |
| 1649 | Init, |
| 1650 | std::string("\01L_OBJC_PROTOCOL_")+ProtocolName, |
| 1651 | &CGM.getModule()); |
| 1652 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1653 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1654 | UsedGlobals.push_back(Entry); |
| 1655 | // FIXME: Is this necessary? Why only for protocol? |
| 1656 | Entry->setAlignment(4); |
| 1657 | } |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1658 | |
| 1659 | return Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1662 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1663 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1664 | |
| 1665 | if (!Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1666 | // We use the initializer as a marker of whether this is a forward |
| 1667 | // reference or not. At module finalization we add the empty |
| 1668 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1669 | Entry = |
| 1670 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1671 | llvm::GlobalValue::ExternalLinkage, |
| 1672 | 0, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1673 | "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1674 | &CGM.getModule()); |
| 1675 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1676 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1677 | UsedGlobals.push_back(Entry); |
| 1678 | // FIXME: Is this necessary? Why only for protocol? |
| 1679 | Entry->setAlignment(4); |
| 1680 | } |
| 1681 | |
| 1682 | return Entry; |
| 1683 | } |
| 1684 | |
| 1685 | /* |
| 1686 | struct _objc_protocol_extension { |
| 1687 | uint32_t size; |
| 1688 | struct objc_method_description_list *optional_instance_methods; |
| 1689 | struct objc_method_description_list *optional_class_methods; |
| 1690 | struct objc_property_list *instance_properties; |
| 1691 | }; |
| 1692 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1693 | llvm::Constant * |
| 1694 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1695 | const ConstantVector &OptInstanceMethods, |
| 1696 | const ConstantVector &OptClassMethods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1697 | uint64_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1698 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1699 | std::vector<llvm::Constant*> Values(4); |
| 1700 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1701 | Values[1] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1702 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
| 1703 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1704 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1705 | OptInstanceMethods); |
| 1706 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1707 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" |
| 1708 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1709 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1710 | OptClassMethods); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1711 | Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + |
| 1712 | PD->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1713 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1714 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1715 | // Return null if no extension bits are used. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1716 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
| 1717 | Values[3]->isNullValue()) |
| 1718 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 1719 | |
| 1720 | llvm::Constant *Init = |
| 1721 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1722 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1723 | // No special section, but goes in llvm.used |
| 1724 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(), |
| 1725 | Init, |
| 1726 | 0, 0, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | /* |
| 1730 | struct objc_protocol_list { |
| 1731 | struct objc_protocol_list *next; |
| 1732 | long count; |
| 1733 | Protocol *list[]; |
| 1734 | }; |
| 1735 | */ |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1736 | llvm::Constant * |
| 1737 | CGObjCMac::EmitProtocolList(const std::string &Name, |
| 1738 | ObjCProtocolDecl::protocol_iterator begin, |
| 1739 | ObjCProtocolDecl::protocol_iterator end) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1740 | std::vector<llvm::Constant*> ProtocolRefs; |
| 1741 | |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1742 | for (; begin != end; ++begin) |
| 1743 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1744 | |
| 1745 | // Just return null for empty protocol lists |
| 1746 | if (ProtocolRefs.empty()) |
| 1747 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1748 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1749 | // This list is null terminated. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1750 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
| 1751 | |
| 1752 | std::vector<llvm::Constant*> Values(3); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1753 | // This field is only used by the runtime. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1754 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1755 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 1756 | Values[2] = |
| 1757 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 1758 | ProtocolRefs.size()), |
| 1759 | ProtocolRefs); |
| 1760 | |
| 1761 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1762 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1763 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1764 | 4, false); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1765 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
| 1766 | } |
| 1767 | |
| 1768 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1769 | struct _objc_property { |
| 1770 | const char * const name; |
| 1771 | const char * const attributes; |
| 1772 | }; |
| 1773 | |
| 1774 | struct _objc_property_list { |
| 1775 | uint32_t entsize; // sizeof (struct _objc_property) |
| 1776 | uint32_t prop_count; |
| 1777 | struct _objc_property[prop_count]; |
| 1778 | }; |
| 1779 | */ |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1780 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name, |
| 1781 | const Decl *Container, |
| 1782 | const ObjCContainerDecl *OCD, |
| 1783 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1784 | std::vector<llvm::Constant*> Properties, Prop(2); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1785 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(CGM.getContext()), |
| 1786 | E = OCD->prop_end(CGM.getContext()); I != E; ++I) { |
Steve Naroff | 93983f8 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 1787 | const ObjCPropertyDecl *PD = *I; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1788 | Prop[0] = GetPropertyName(PD->getIdentifier()); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1789 | Prop[1] = GetPropertyTypeString(PD, Container); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1790 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
| 1791 | Prop)); |
| 1792 | } |
| 1793 | |
| 1794 | // Return null for empty list. |
| 1795 | if (Properties.empty()) |
| 1796 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1797 | |
| 1798 | unsigned PropertySize = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1799 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1800 | std::vector<llvm::Constant*> Values(3); |
| 1801 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 1802 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
| 1803 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
| 1804 | Properties.size()); |
| 1805 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
| 1806 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1807 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1808 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1809 | CreateMetadataVar(Name, Init, |
| 1810 | (ObjCABI == 2) ? "__DATA, __objc_const" : |
| 1811 | "__OBJC,__property,regular,no_dead_strip", |
| 1812 | (ObjCABI == 2) ? 8 : 4, |
| 1813 | true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1814 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1818 | struct objc_method_description_list { |
| 1819 | int count; |
| 1820 | struct objc_method_description list[]; |
| 1821 | }; |
| 1822 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1823 | llvm::Constant * |
| 1824 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 1825 | std::vector<llvm::Constant*> Desc(2); |
| 1826 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1827 | ObjCTypes.SelectorPtrTy); |
| 1828 | Desc[1] = GetMethodVarType(MD); |
| 1829 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
| 1830 | Desc); |
| 1831 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1832 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1833 | llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name, |
| 1834 | const char *Section, |
| 1835 | const ConstantVector &Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1836 | // Return null for empty list. |
| 1837 | if (Methods.empty()) |
| 1838 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 1839 | |
| 1840 | std::vector<llvm::Constant*> Values(2); |
| 1841 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 1842 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
| 1843 | Methods.size()); |
| 1844 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
| 1845 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1846 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1847 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1848 | return llvm::ConstantExpr::getBitCast(GV, |
| 1849 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1852 | /* |
| 1853 | struct _objc_category { |
| 1854 | char *category_name; |
| 1855 | char *class_name; |
| 1856 | struct _objc_method_list *instance_methods; |
| 1857 | struct _objc_method_list *class_methods; |
| 1858 | struct _objc_protocol_list *protocols; |
| 1859 | uint32_t size; // <rdar://4585769> |
| 1860 | struct _objc_property_list *instance_properties; |
| 1861 | }; |
| 1862 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1863 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1864 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1865 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame^] | 1866 | // FIXME: This is poor design, the OCD should have a pointer to the category |
| 1867 | // decl. Additionally, note that Category can be null for the @implementation |
| 1868 | // w/o an @interface case. Sema should just create one for us as it does for |
| 1869 | // @implementation so everyone else can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1870 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1871 | const ObjCCategoryDecl *Category = |
| 1872 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1873 | std::string ExtName(Interface->getNameAsString() + "_" + |
| 1874 | OCD->getNameAsString()); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1875 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1876 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1877 | for (ObjCCategoryImplDecl::instmeth_iterator |
| 1878 | i = OCD->instmeth_begin(CGM.getContext()), |
| 1879 | e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1880 | // Instance methods should always be defined. |
| 1881 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1882 | } |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1883 | for (ObjCCategoryImplDecl::classmeth_iterator |
| 1884 | i = OCD->classmeth_begin(CGM.getContext()), |
| 1885 | e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1886 | // Class methods should always be defined. |
| 1887 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1888 | } |
| 1889 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1890 | std::vector<llvm::Constant*> Values(7); |
| 1891 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 1892 | Values[1] = GetClassName(Interface->getIdentifier()); |
Fariborz Jahanian | 679cd7f | 2009-04-29 20:40:05 +0000 | [diff] [blame] | 1893 | LazySymbols.insert(Interface->getIdentifier()); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1894 | Values[2] = |
| 1895 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") + |
| 1896 | ExtName, |
| 1897 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1898 | InstanceMethods); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1899 | Values[3] = |
| 1900 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName, |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1901 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1902 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1903 | if (Category) { |
| 1904 | Values[4] = |
| 1905 | EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName, |
| 1906 | Category->protocol_begin(), |
| 1907 | Category->protocol_end()); |
| 1908 | } else { |
| 1909 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1910 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1911 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1912 | |
| 1913 | // If there is no category @interface then there can be no properties. |
| 1914 | if (Category) { |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1915 | Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1916 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1917 | } else { |
| 1918 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1919 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1920 | |
| 1921 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
| 1922 | Values); |
| 1923 | |
| 1924 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1925 | CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init, |
| 1926 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1927 | 4, true); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1928 | DefinedCategories.push_back(GV); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1931 | // FIXME: Get from somewhere? |
| 1932 | enum ClassFlags { |
| 1933 | eClassFlags_Factory = 0x00001, |
| 1934 | eClassFlags_Meta = 0x00002, |
| 1935 | // <rdr://5142207> |
| 1936 | eClassFlags_HasCXXStructors = 0x02000, |
| 1937 | eClassFlags_Hidden = 0x20000, |
| 1938 | eClassFlags_ABI2_Hidden = 0x00010, |
| 1939 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 1940 | }; |
| 1941 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1942 | /* |
| 1943 | struct _objc_class { |
| 1944 | Class isa; |
| 1945 | Class super_class; |
| 1946 | const char *name; |
| 1947 | long version; |
| 1948 | long info; |
| 1949 | long instance_size; |
| 1950 | struct _objc_ivar_list *ivars; |
| 1951 | struct _objc_method_list *methods; |
| 1952 | struct _objc_cache *cache; |
| 1953 | struct _objc_protocol_list *protocols; |
| 1954 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 1955 | const char *ivar_layout; |
| 1956 | struct _objc_class_ext *ext; |
| 1957 | }; |
| 1958 | |
| 1959 | See EmitClassExtension(); |
| 1960 | */ |
| 1961 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1962 | DefinedSymbols.insert(ID->getIdentifier()); |
| 1963 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1964 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1965 | // FIXME: Gross |
| 1966 | ObjCInterfaceDecl *Interface = |
| 1967 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1968 | llvm::Constant *Protocols = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1969 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1970 | Interface->protocol_begin(), |
| 1971 | Interface->protocol_end()); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1972 | unsigned Flags = eClassFlags_Factory; |
Daniel Dunbar | 2bebbf0 | 2009-05-03 10:46:44 +0000 | [diff] [blame] | 1973 | unsigned Size = |
| 1974 | CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1975 | |
| 1976 | // FIXME: Set CXX-structors flag. |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1977 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1978 | Flags |= eClassFlags_Hidden; |
| 1979 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1980 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1981 | for (ObjCImplementationDecl::instmeth_iterator |
| 1982 | i = ID->instmeth_begin(CGM.getContext()), |
| 1983 | e = ID->instmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1984 | // Instance methods should always be defined. |
| 1985 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1986 | } |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1987 | for (ObjCImplementationDecl::classmeth_iterator |
| 1988 | i = ID->classmeth_begin(CGM.getContext()), |
| 1989 | e = ID->classmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1990 | // Class methods should always be defined. |
| 1991 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1992 | } |
| 1993 | |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1994 | for (ObjCImplementationDecl::propimpl_iterator |
| 1995 | i = ID->propimpl_begin(CGM.getContext()), |
| 1996 | e = ID->propimpl_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1997 | ObjCPropertyImplDecl *PID = *i; |
| 1998 | |
| 1999 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 2000 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 2001 | |
| 2002 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 2003 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 2004 | InstanceMethods.push_back(C); |
| 2005 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 2006 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 2007 | InstanceMethods.push_back(C); |
| 2008 | } |
| 2009 | } |
| 2010 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2011 | std::vector<llvm::Constant*> Values(12); |
Daniel Dunbar | 5384b09 | 2009-05-03 08:56:52 +0000 | [diff] [blame] | 2012 | Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2013 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2014 | // Record a reference to the super class. |
| 2015 | LazySymbols.insert(Super->getIdentifier()); |
| 2016 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2017 | Values[ 1] = |
| 2018 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 2019 | ObjCTypes.ClassPtrTy); |
| 2020 | } else { |
| 2021 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 2022 | } |
| 2023 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 2024 | // Version is always 0. |
| 2025 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2026 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 2027 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2028 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2029 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2030 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2031 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2032 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2033 | // cache is always NULL. |
| 2034 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 2035 | Values[ 9] = Protocols; |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 2036 | Values[10] = BuildIvarLayout(ID, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2037 | Values[11] = EmitClassExtension(ID); |
| 2038 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 2039 | Values); |
| 2040 | |
| 2041 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2042 | CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init, |
| 2043 | "__OBJC,__class,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2044 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2045 | DefinedClasses.push_back(GV); |
| 2046 | } |
| 2047 | |
| 2048 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 2049 | llvm::Constant *Protocols, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2050 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2051 | unsigned Flags = eClassFlags_Meta; |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2052 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2053 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 2054 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2055 | Flags |= eClassFlags_Hidden; |
| 2056 | |
| 2057 | std::vector<llvm::Constant*> Values(12); |
| 2058 | // The isa for the metaclass is the root of the hierarchy. |
| 2059 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 2060 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 2061 | Root = Super; |
| 2062 | Values[ 0] = |
| 2063 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
| 2064 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2065 | // The super class for the metaclass is emitted as the name of the |
| 2066 | // super class. The runtime fixes this up to point to the |
| 2067 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2068 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
| 2069 | Values[ 1] = |
| 2070 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 2071 | ObjCTypes.ClassPtrTy); |
| 2072 | } else { |
| 2073 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 2074 | } |
| 2075 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 2076 | // Version is always 0. |
| 2077 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2078 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 2079 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2080 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2081 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2082 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2083 | "__OBJC,__cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2084 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2085 | // cache is always NULL. |
| 2086 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 2087 | Values[ 9] = Protocols; |
| 2088 | // ivar_layout for metaclass is always NULL. |
| 2089 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 2090 | // The class extension is always unused for metaclasses. |
| 2091 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 2092 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 2093 | Values); |
| 2094 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2095 | std::string Name("\01L_OBJC_METACLASS_"); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2096 | Name += ID->getNameAsCString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2097 | |
| 2098 | // Check for a forward reference. |
| 2099 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 2100 | if (GV) { |
| 2101 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2102 | "Forward metaclass reference has incorrect type."); |
| 2103 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 2104 | GV->setInitializer(Init); |
| 2105 | } else { |
| 2106 | GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 2107 | llvm::GlobalValue::InternalLinkage, |
| 2108 | Init, Name, |
| 2109 | &CGM.getModule()); |
| 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. |
| 2136 | return new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 2137 | llvm::GlobalValue::ExternalLinkage, |
| 2138 | 0, |
| 2139 | Name, |
| 2140 | &CGM.getModule()); |
| 2141 | } |
| 2142 | } |
| 2143 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2144 | /* |
| 2145 | struct objc_class_ext { |
| 2146 | uint32_t size; |
| 2147 | const char *weak_ivar_layout; |
| 2148 | struct _objc_property_list *properties; |
| 2149 | }; |
| 2150 | */ |
| 2151 | llvm::Constant * |
| 2152 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
| 2153 | uint64_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2154 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2155 | |
| 2156 | std::vector<llvm::Constant*> Values(3); |
| 2157 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 2158 | Values[1] = BuildIvarLayout(ID, false); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2159 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2160 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2161 | |
| 2162 | // Return null if no extension bits are used. |
| 2163 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
| 2164 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 2165 | |
| 2166 | llvm::Constant *Init = |
| 2167 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2168 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2169 | Init, "__OBJC,__class_ext,regular,no_dead_strip", |
| 2170 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2171 | } |
| 2172 | |
| 2173 | /* |
| 2174 | struct objc_ivar { |
| 2175 | char *ivar_name; |
| 2176 | char *ivar_type; |
| 2177 | int ivar_offset; |
| 2178 | }; |
| 2179 | |
| 2180 | struct objc_ivar_list { |
| 2181 | int ivar_count; |
| 2182 | struct objc_ivar list[count]; |
| 2183 | }; |
| 2184 | */ |
| 2185 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2186 | bool ForClass) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2187 | std::vector<llvm::Constant*> Ivars, Ivar(3); |
| 2188 | |
| 2189 | // When emitting the root class GCC emits ivar entries for the |
| 2190 | // actual class structure. It is not clear if we need to follow this |
| 2191 | // behavior; for now lets try and get away with not doing it. If so, |
| 2192 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 2193 | // for the class. |
| 2194 | if (ForClass) |
| 2195 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2196 | |
| 2197 | ObjCInterfaceDecl *OID = |
| 2198 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2199 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 2200 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
| 2201 | GetNamedIvarList(OID, OIvars); |
| 2202 | |
| 2203 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 2204 | ObjCIvarDecl *IVD = OIvars[i]; |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 2205 | Ivar[0] = GetMethodVarName(IVD->getIdentifier()); |
| 2206 | Ivar[1] = GetMethodVarType(IVD); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 2207 | Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2208 | ComputeIvarBaseOffset(CGM, OID, IVD)); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2209 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | // Return null for empty list. |
| 2213 | if (Ivars.empty()) |
| 2214 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
| 2215 | |
| 2216 | std::vector<llvm::Constant*> Values(2); |
| 2217 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 2218 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
| 2219 | Ivars.size()); |
| 2220 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
| 2221 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2222 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2223 | llvm::GlobalVariable *GV; |
| 2224 | if (ForClass) |
| 2225 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(), |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2226 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
| 2227 | 4, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2228 | else |
| 2229 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" |
| 2230 | + ID->getNameAsString(), |
| 2231 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2232 | 4, true); |
| 2233 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2234 | } |
| 2235 | |
| 2236 | /* |
| 2237 | struct objc_method { |
| 2238 | SEL method_name; |
| 2239 | char *method_types; |
| 2240 | void *method; |
| 2241 | }; |
| 2242 | |
| 2243 | struct objc_method_list { |
| 2244 | struct objc_method_list *obsolete; |
| 2245 | int count; |
| 2246 | struct objc_method methods_list[count]; |
| 2247 | }; |
| 2248 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2249 | |
| 2250 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 2251 | /// given method if it has been defined. The result is null if the |
| 2252 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2253 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2254 | // FIXME: Use DenseMap::lookup |
| 2255 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 2256 | if (!Fn) |
| 2257 | return 0; |
| 2258 | |
| 2259 | std::vector<llvm::Constant*> Method(3); |
| 2260 | Method[0] = |
| 2261 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 2262 | ObjCTypes.SelectorPtrTy); |
| 2263 | Method[1] = GetMethodVarType(MD); |
| 2264 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 2265 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 2266 | } |
| 2267 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2268 | llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name, |
| 2269 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2270 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2271 | // Return null for empty list. |
| 2272 | if (Methods.empty()) |
| 2273 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
| 2274 | |
| 2275 | std::vector<llvm::Constant*> Values(3); |
| 2276 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 2277 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 2278 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 2279 | Methods.size()); |
| 2280 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 2281 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2282 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2283 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2284 | return llvm::ConstantExpr::getBitCast(GV, |
| 2285 | ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2286 | } |
| 2287 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 2288 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 2289 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2290 | std::string Name; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 2291 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2292 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2293 | CodeGenTypes &Types = CGM.getTypes(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2294 | const llvm::FunctionType *MethodTy = |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2295 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2296 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2297 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2298 | llvm::GlobalValue::InternalLinkage, |
| 2299 | Name, |
| 2300 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2301 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2302 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2303 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2306 | llvm::GlobalVariable * |
| 2307 | CGObjCCommonMac::CreateMetadataVar(const std::string &Name, |
| 2308 | llvm::Constant *Init, |
| 2309 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2310 | unsigned Align, |
| 2311 | bool AddToUsed) { |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2312 | const llvm::Type *Ty = Init->getType(); |
| 2313 | llvm::GlobalVariable *GV = |
| 2314 | new llvm::GlobalVariable(Ty, false, |
| 2315 | llvm::GlobalValue::InternalLinkage, |
| 2316 | Init, |
| 2317 | Name, |
| 2318 | &CGM.getModule()); |
| 2319 | if (Section) |
| 2320 | GV->setSection(Section); |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2321 | if (Align) |
| 2322 | GV->setAlignment(Align); |
| 2323 | if (AddToUsed) |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2324 | UsedGlobals.push_back(GV); |
| 2325 | return GV; |
| 2326 | } |
| 2327 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2328 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2329 | // Abuse this interface function as a place to finalize. |
| 2330 | FinishModule(); |
| 2331 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2332 | return NULL; |
| 2333 | } |
| 2334 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2335 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2336 | return ObjCTypes.getGetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2337 | } |
| 2338 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2339 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2340 | return ObjCTypes.getSetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2341 | } |
| 2342 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2343 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2344 | return ObjCTypes.getEnumerationMutationFn(); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2345 | } |
| 2346 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2347 | /* |
| 2348 | |
| 2349 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 2350 | -- |
| 2351 | |
| 2352 | The basic framework for a @try-catch-finally is as follows: |
| 2353 | { |
| 2354 | objc_exception_data d; |
| 2355 | id _rethrow = null; |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2356 | bool _call_try_exit = true; |
| 2357 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2358 | objc_exception_try_enter(&d); |
| 2359 | if (!setjmp(d.jmp_buf)) { |
| 2360 | ... try body ... |
| 2361 | } else { |
| 2362 | // exception path |
| 2363 | id _caught = objc_exception_extract(&d); |
| 2364 | |
| 2365 | // enter new try scope for handlers |
| 2366 | if (!setjmp(d.jmp_buf)) { |
| 2367 | ... match exception and execute catch blocks ... |
| 2368 | |
| 2369 | // fell off end, rethrow. |
| 2370 | _rethrow = _caught; |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2371 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2372 | } else { |
| 2373 | // exception in catch block |
| 2374 | _rethrow = objc_exception_extract(&d); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2375 | _call_try_exit = false; |
| 2376 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2377 | } |
| 2378 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2379 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2380 | |
| 2381 | finally: |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2382 | if (_call_try_exit) |
| 2383 | objc_exception_try_exit(&d); |
| 2384 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2385 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2386 | ... dispatch to finally destination ... |
| 2387 | |
| 2388 | finally_rethrow: |
| 2389 | objc_exception_throw(_rethrow); |
| 2390 | |
| 2391 | finally_end: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | This framework differs slightly from the one gcc uses, in that gcc |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2395 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 2396 | and if the object should be rethrown. This breaks in the face of |
| 2397 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2398 | |
| 2399 | We specialize this framework for a few particular circumstances: |
| 2400 | |
| 2401 | - If there are no catch blocks, then we avoid emitting the second |
| 2402 | exception handling context. |
| 2403 | |
| 2404 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 2405 | e)) we avoid emitting the code to rethrow an uncaught exception. |
| 2406 | |
| 2407 | - FIXME: If there is no @finally block we can do a few more |
| 2408 | simplifications. |
| 2409 | |
| 2410 | Rethrows and Jumps-Through-Finally |
| 2411 | -- |
| 2412 | |
| 2413 | Support for implicit rethrows and jumping through the finally block is |
| 2414 | handled by storing the current exception-handling context in |
| 2415 | ObjCEHStack. |
| 2416 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2417 | In order to implement proper @finally semantics, we support one basic |
| 2418 | mechanism for jumping through the finally block to an arbitrary |
| 2419 | destination. Constructs which generate exits from a @try or @catch |
| 2420 | block use this mechanism to implement the proper semantics by chaining |
| 2421 | jumps, as necessary. |
| 2422 | |
| 2423 | This mechanism works like the one used for indirect goto: we |
| 2424 | arbitrarily assign an ID to each destination and store the ID for the |
| 2425 | destination in a variable prior to entering the finally block. At the |
| 2426 | end of the finally block we simply create a switch to the proper |
| 2427 | destination. |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2428 | |
| 2429 | Code gen for @synchronized(expr) stmt; |
| 2430 | Effectively generating code for: |
| 2431 | objc_sync_enter(expr); |
| 2432 | @try stmt @finally { objc_sync_exit(expr); } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2433 | */ |
| 2434 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2435 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 2436 | const Stmt &S) { |
| 2437 | bool isTry = isa<ObjCAtTryStmt>(S); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2438 | // Create various blocks we refer to for handling @finally. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2439 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2440 | llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit"); |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2441 | llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit"); |
| 2442 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
| 2443 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2444 | |
| 2445 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 2446 | // evaluation of the expression must occur before we enter the |
| 2447 | // @synchronized. We can safely avoid a temp here because jumps into |
| 2448 | // @synchronized are illegal & this will dominate uses. |
| 2449 | llvm::Value *SyncArg = 0; |
| 2450 | if (!isTry) { |
| 2451 | SyncArg = |
| 2452 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 2453 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 2454 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2455 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2456 | |
| 2457 | // Push an EH context entry, used for handling rethrows and jumps |
| 2458 | // through finally. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2459 | CGF.PushCleanupBlock(FinallyBlock); |
| 2460 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2461 | CGF.ObjCEHValueStack.push_back(0); |
| 2462 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2463 | // Allocate memory for the exception data and rethrow pointer. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2464 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 2465 | "exceptiondata.ptr"); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2466 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy, |
| 2467 | "_rethrow"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2468 | llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty, |
| 2469 | "_call_try_exit"); |
| 2470 | CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr); |
| 2471 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2472 | // Enter a new try block and call setjmp. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2473 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2474 | llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0, |
| 2475 | "jmpbufarray"); |
| 2476 | JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2477 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2478 | JmpBufPtr, "result"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2479 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2480 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 2481 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2482 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2483 | TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2484 | |
| 2485 | // Emit the @try block. |
| 2486 | CGF.EmitBlock(TryBlock); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2487 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 2488 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2489 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2490 | |
| 2491 | // Emit the "exception in @try" block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2492 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2493 | |
| 2494 | // Retrieve the exception object. We may emit multiple blocks but |
| 2495 | // nothing can cross this so the value is already in SSA form. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2496 | llvm::Value *Caught = |
| 2497 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2498 | ExceptionData, "caught"); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2499 | CGF.ObjCEHValueStack.back() = Caught; |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2500 | if (!isTry) |
| 2501 | { |
| 2502 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2503 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2504 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2505 | } |
| 2506 | else if (const ObjCAtCatchStmt* CatchStmt = |
| 2507 | cast<ObjCAtTryStmt>(S).getCatchStmts()) |
| 2508 | { |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2509 | // Enter a new exception try block (in case a @catch block throws |
| 2510 | // an exception). |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2511 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2512 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2513 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2514 | JmpBufPtr, "result"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2515 | llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2516 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2517 | llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch"); |
| 2518 | llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler"); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2519 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2520 | |
| 2521 | CGF.EmitBlock(CatchBlock); |
| 2522 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2523 | // Handle catch list. As a special case we check if everything is |
| 2524 | // matched and avoid generating code for falling off the end if |
| 2525 | // so. |
| 2526 | bool AllMatched = false; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2527 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2528 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2529 | |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2530 | const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2531 | const PointerType *PT = 0; |
| 2532 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2533 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2534 | if (!CatchParam) { |
| 2535 | AllMatched = true; |
| 2536 | } else { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2537 | PT = CatchParam->getType()->getAsPointerType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2538 | |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 2539 | // catch(id e) always matches. |
| 2540 | // FIXME: For the time being we also match id<X>; this should |
| 2541 | // be rejected by Sema instead. |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2542 | if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) || |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2543 | CatchParam->getType()->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 | |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2559 | assert(PT && "Unexpected non-pointer type in @catch"); |
| 2560 | QualType T = PT->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 | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2774 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 2775 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2776 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 2777 | QualType ObjectTy, |
| 2778 | llvm::Value *BaseValue, |
| 2779 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2780 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 2781 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2782 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 2783 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2784 | } |
| 2785 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2786 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 2787 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2788 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2789 | uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2790 | return llvm::ConstantInt::get( |
| 2791 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 2792 | Offset); |
| 2793 | } |
| 2794 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2795 | /* *** Private Interface *** */ |
| 2796 | |
| 2797 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 2798 | /// level information. |
| 2799 | /// |
| 2800 | /// See: <rdr://4810609&4810587&4810587> |
| 2801 | /// struct IMAGE_INFO { |
| 2802 | /// unsigned version; |
| 2803 | /// unsigned flags; |
| 2804 | /// }; |
| 2805 | enum ImageInfoFlags { |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2806 | eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what |
| 2807 | // this implies. |
| 2808 | eImageInfo_GarbageCollected = (1 << 1), |
| 2809 | eImageInfo_GCOnly = (1 << 2), |
| 2810 | eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set. |
| 2811 | |
| 2812 | // A flag indicating that the module has no instances of an |
| 2813 | // @synthesize of a superclass variable. <rdar://problem/6803242> |
| 2814 | eImageInfo_CorrectedSynthesize = (1 << 4) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2815 | }; |
| 2816 | |
| 2817 | void CGObjCMac::EmitImageInfo() { |
| 2818 | unsigned version = 0; // Version is unused? |
| 2819 | unsigned flags = 0; |
| 2820 | |
| 2821 | // FIXME: Fix and continue? |
| 2822 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 2823 | flags |= eImageInfo_GarbageCollected; |
| 2824 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 2825 | flags |= eImageInfo_GCOnly; |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2826 | |
| 2827 | // We never allow @synthesize of a superclass property. |
| 2828 | flags |= eImageInfo_CorrectedSynthesize; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2829 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2830 | // Emitted as int[2]; |
| 2831 | llvm::Constant *values[2] = { |
| 2832 | llvm::ConstantInt::get(llvm::Type::Int32Ty, version), |
| 2833 | llvm::ConstantInt::get(llvm::Type::Int32Ty, flags) |
| 2834 | }; |
| 2835 | llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2836 | |
| 2837 | const char *Section; |
| 2838 | if (ObjCABI == 1) |
| 2839 | Section = "__OBJC, __image_info,regular"; |
| 2840 | else |
| 2841 | Section = "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2842 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2843 | CreateMetadataVar("\01L_OBJC_IMAGE_INFO", |
| 2844 | llvm::ConstantArray::get(AT, values, 2), |
| 2845 | Section, |
| 2846 | 0, |
| 2847 | true); |
| 2848 | GV->setConstant(true); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2849 | } |
| 2850 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2851 | |
| 2852 | // struct objc_module { |
| 2853 | // unsigned long version; |
| 2854 | // unsigned long size; |
| 2855 | // const char *name; |
| 2856 | // Symtab symtab; |
| 2857 | // }; |
| 2858 | |
| 2859 | // FIXME: Get from somewhere |
| 2860 | static const int ModuleVersion = 7; |
| 2861 | |
| 2862 | void CGObjCMac::EmitModuleInfo() { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2863 | uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2864 | |
| 2865 | std::vector<llvm::Constant*> Values(4); |
| 2866 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion); |
| 2867 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2868 | // This used to be the filename, now it is unused. <rdr://4327263> |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2869 | Values[2] = GetClassName(&CGM.getContext().Idents.get("")); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2870 | Values[3] = EmitModuleSymbols(); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2871 | CreateMetadataVar("\01L_OBJC_MODULES", |
| 2872 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
| 2873 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2874 | 4, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2875 | } |
| 2876 | |
| 2877 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2878 | unsigned NumClasses = DefinedClasses.size(); |
| 2879 | unsigned NumCategories = DefinedCategories.size(); |
| 2880 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2881 | // Return null if no symbols were defined. |
| 2882 | if (!NumClasses && !NumCategories) |
| 2883 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
| 2884 | |
| 2885 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2886 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2887 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
| 2888 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 2889 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
| 2890 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2891 | // The runtime expects exactly the list of defined classes followed |
| 2892 | // by the list of defined categories, in a single array. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2893 | std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2894 | for (unsigned i=0; i<NumClasses; i++) |
| 2895 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 2896 | ObjCTypes.Int8PtrTy); |
| 2897 | for (unsigned i=0; i<NumCategories; i++) |
| 2898 | Symbols[NumClasses + i] = |
| 2899 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 2900 | ObjCTypes.Int8PtrTy); |
| 2901 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2902 | Values[4] = |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2903 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2904 | NumClasses + NumCategories), |
| 2905 | Symbols); |
| 2906 | |
| 2907 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2908 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2909 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2910 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 2911 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2912 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2913 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
| 2914 | } |
| 2915 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2916 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2917 | const ObjCInterfaceDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2918 | LazySymbols.insert(ID->getIdentifier()); |
| 2919 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2920 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 2921 | |
| 2922 | if (!Entry) { |
| 2923 | llvm::Constant *Casted = |
| 2924 | llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()), |
| 2925 | ObjCTypes.ClassPtrTy); |
| 2926 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2927 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 2928 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2929 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2930 | } |
| 2931 | |
| 2932 | return Builder.CreateLoad(Entry, false, "tmp"); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2933 | } |
| 2934 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2935 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2936 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 2937 | |
| 2938 | if (!Entry) { |
| 2939 | llvm::Constant *Casted = |
| 2940 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 2941 | ObjCTypes.SelectorPtrTy); |
| 2942 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2943 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 2944 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2945 | 4, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2946 | } |
| 2947 | |
| 2948 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 2949 | } |
| 2950 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 2951 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2952 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2953 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2954 | if (!Entry) |
| 2955 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 2956 | llvm::ConstantArray::get(Ident->getName()), |
| 2957 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 2958 | 1, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2959 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2960 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2961 | } |
| 2962 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 2963 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 2964 | /// ivar layout bitmap. |
| 2965 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
| 2966 | const ObjCCommonTypesHelper &ObjCTypes) { |
| 2967 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 2968 | } |
| 2969 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 2970 | static QualType::GCAttrTypes GetGCAttrTypeForType(ASTContext &Ctx, |
| 2971 | QualType FQT) { |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 2972 | if (FQT.isObjCGCStrong()) |
| 2973 | return QualType::Strong; |
| 2974 | |
| 2975 | if (FQT.isObjCGCWeak()) |
| 2976 | return QualType::Weak; |
| 2977 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 2978 | if (Ctx.isObjCObjectPointerType(FQT)) |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 2979 | return QualType::Strong; |
| 2980 | |
| 2981 | if (const PointerType *PT = FQT->getAsPointerType()) |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 2982 | return GetGCAttrTypeForType(Ctx, PT->getPointeeType()); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 2983 | |
| 2984 | return QualType::GCNone; |
| 2985 | } |
| 2986 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 2987 | void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT, |
| 2988 | unsigned int BytePos, |
| 2989 | bool ForStrongLayout, |
| 2990 | bool &HasUnion) { |
| 2991 | const RecordDecl *RD = RT->getDecl(); |
| 2992 | // FIXME - Use iterator. |
| 2993 | llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(CGM.getContext()), |
| 2994 | RD->field_end(CGM.getContext())); |
| 2995 | const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0)); |
| 2996 | const llvm::StructLayout *RecLayout = |
| 2997 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); |
| 2998 | |
| 2999 | BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos, |
| 3000 | ForStrongLayout, HasUnion); |
| 3001 | } |
| 3002 | |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 3003 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3004 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3005 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3006 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3007 | unsigned int BytePos, bool ForStrongLayout, |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3008 | bool &HasUnion) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3009 | bool IsUnion = (RD && RD->isUnion()); |
| 3010 | uint64_t MaxUnionIvarSize = 0; |
| 3011 | uint64_t MaxSkippedUnionIvarSize = 0; |
| 3012 | FieldDecl *MaxField = 0; |
| 3013 | FieldDecl *MaxSkippedField = 0; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3014 | FieldDecl *LastFieldBitfield = 0; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3015 | uint64_t MaxFieldOffset = 0; |
| 3016 | uint64_t MaxSkippedFieldOffset = 0; |
| 3017 | uint64_t LastBitfieldOffset = 0; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3018 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3019 | if (RecFields.empty()) |
| 3020 | return; |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3021 | unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0); |
| 3022 | unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth(); |
| 3023 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3024 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3025 | FieldDecl *Field = RecFields[i]; |
Daniel Dunbar | e05cc98 | 2009-05-03 23:35:23 +0000 | [diff] [blame] | 3026 | uint64_t FieldOffset; |
| 3027 | if (RD) |
| 3028 | FieldOffset = |
| 3029 | Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); |
| 3030 | else |
| 3031 | FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)); |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3032 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3033 | // Skip over unnamed or bitfields |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3034 | if (!Field->getIdentifier() || Field->isBitField()) { |
| 3035 | LastFieldBitfield = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3036 | LastBitfieldOffset = FieldOffset; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3037 | continue; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3038 | } |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3039 | |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3040 | LastFieldBitfield = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3041 | QualType FQT = Field->getType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3042 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3043 | if (FQT->isUnionType()) |
| 3044 | HasUnion = true; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3045 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3046 | BuildAggrIvarRecordLayout(FQT->getAsRecordType(), |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3047 | BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3048 | ForStrongLayout, HasUnion); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3049 | continue; |
| 3050 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3051 | |
| 3052 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3053 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3054 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3055 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3056 | assert(CArray && "only array with known element size is supported"); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3057 | FQT = CArray->getElementType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3058 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 3059 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3060 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3061 | ElCount *= CArray->getSize().getZExtValue(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3062 | FQT = CArray->getElementType(); |
| 3063 | } |
| 3064 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3065 | assert(!FQT->isUnionType() && |
| 3066 | "layout for array of unions not supported"); |
| 3067 | if (FQT->isRecordType()) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3068 | int OldIndex = IvarsInfo.size() - 1; |
| 3069 | int OldSkIndex = SkipIvars.size() -1; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3070 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3071 | const RecordType *RT = FQT->getAsRecordType(); |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3072 | BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3073 | ForStrongLayout, HasUnion); |
| 3074 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3075 | // Replicate layout information for each array element. Note that |
| 3076 | // one element is already done. |
| 3077 | uint64_t ElIx = 1; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3078 | for (int FirstIndex = IvarsInfo.size() - 1, |
| 3079 | FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3080 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3081 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 3082 | IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx, |
| 3083 | IvarsInfo[i].ivar_size)); |
| 3084 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) |
| 3085 | SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx, |
| 3086 | SkipIvars[i].ivar_size)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3087 | } |
| 3088 | continue; |
| 3089 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3090 | } |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3091 | // At this point, we are done with Record/Union and array there of. |
| 3092 | // For other arrays we are down to its element type. |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3093 | QualType::GCAttrTypes GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3094 | |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3095 | unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType()); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3096 | if ((ForStrongLayout && GCAttr == QualType::Strong) |
| 3097 | || (!ForStrongLayout && GCAttr == QualType::Weak)) { |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3098 | if (IsUnion) { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3099 | uint64_t UnionIvarSize = FieldSize / WordSizeInBits; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3100 | if (UnionIvarSize > MaxUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3101 | MaxUnionIvarSize = UnionIvarSize; |
| 3102 | MaxField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3103 | MaxFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3104 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3105 | } else { |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3106 | IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3107 | FieldSize / WordSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3108 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3109 | } else if ((ForStrongLayout && |
| 3110 | (GCAttr == QualType::GCNone || GCAttr == QualType::Weak)) |
| 3111 | || (!ForStrongLayout && GCAttr != QualType::Weak)) { |
| 3112 | if (IsUnion) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame^] | 3113 | // FIXME: Why the asymmetry? We divide by word size in bits on other |
| 3114 | // side. |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3115 | uint64_t UnionIvarSize = FieldSize; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3116 | if (UnionIvarSize > MaxSkippedUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3117 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 3118 | MaxSkippedField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3119 | MaxSkippedFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3120 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3121 | } else { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3122 | // FIXME: Why the asymmetry, we divide by byte size in bits here? |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3123 | SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3124 | FieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3125 | } |
| 3126 | } |
| 3127 | } |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3128 | |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3129 | if (LastFieldBitfield) { |
| 3130 | // Last field was a bitfield. Must update skip info. |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3131 | Expr *BitWidth = LastFieldBitfield->getBitWidth(); |
| 3132 | uint64_t BitFieldSize = |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 3133 | BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue(); |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3134 | GC_IVAR skivar; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3135 | skivar.ivar_bytepos = BytePos + LastBitfieldOffset; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3136 | skivar.ivar_size = (BitFieldSize / ByteSizeInBits) |
| 3137 | + ((BitFieldSize % ByteSizeInBits) != 0); |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3138 | SkipIvars.push_back(skivar); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3139 | } |
| 3140 | |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3141 | if (MaxField) |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3142 | IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3143 | MaxUnionIvarSize)); |
| 3144 | if (MaxSkippedField) |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3145 | SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3146 | MaxSkippedUnionIvarSize)); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3147 | } |
| 3148 | |
| 3149 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 3150 | /// implementation for the __strong or __weak case. |
| 3151 | /// The layout map displays which words in ivar list must be skipped |
| 3152 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 3153 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
| 3154 | /// of words to skip and right nibble is count of words to scan. So, each |
| 3155 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
| 3156 | /// represented by a 0x00 byte which also ends the string. |
| 3157 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 3158 | /// - id, Class |
| 3159 | /// - object * |
| 3160 | /// - __strong anything |
| 3161 | /// |
| 3162 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 3163 | /// - __weak anything |
| 3164 | /// |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3165 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3166 | const ObjCImplementationDecl *OMD, |
| 3167 | bool ForStrongLayout) { |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3168 | bool hasUnion = false; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3169 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3170 | unsigned int WordsToScan, WordsToSkip; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3171 | const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 3172 | if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) |
| 3173 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3174 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3175 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3176 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3177 | CGM.getContext().CollectObjCIvars(OI, RecFields); |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 3178 | |
Daniel Dunbar | 3715328 | 2009-05-04 04:10:48 +0000 | [diff] [blame] | 3179 | // Add this implementations synthesized ivars. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 3180 | llvm::SmallVector<ObjCIvarDecl*, 16> Ivars; |
| 3181 | CGM.getContext().CollectSynthesizedIvars(OI, Ivars); |
| 3182 | for (unsigned k = 0, e = Ivars.size(); k != e; ++k) |
| 3183 | RecFields.push_back(cast<FieldDecl>(Ivars[k])); |
| 3184 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3185 | if (RecFields.empty()) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3186 | return llvm::Constant::getNullValue(PtrTy); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3187 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3188 | SkipIvars.clear(); |
| 3189 | IvarsInfo.clear(); |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 3190 | |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 3191 | BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion); |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3192 | if (IvarsInfo.empty()) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3193 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3194 | |
| 3195 | // Sort on byte position in case we encounterred a union nested in |
| 3196 | // the ivar list. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3197 | if (hasUnion && !IvarsInfo.empty()) |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 3198 | std::sort(IvarsInfo.begin(), IvarsInfo.end()); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3199 | if (hasUnion && !SkipIvars.empty()) |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 3200 | std::sort(SkipIvars.begin(), SkipIvars.end()); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3201 | |
| 3202 | // Build the string of skip/scan nibbles |
Fariborz Jahanian | 8c2f2d1 | 2009-04-24 17:15:27 +0000 | [diff] [blame] | 3203 | llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3204 | unsigned int WordSize = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3205 | CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3206 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 3207 | WordsToSkip = 0; |
| 3208 | WordsToScan = IvarsInfo[0].ivar_size; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3209 | } else { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3210 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 3211 | WordsToScan = IvarsInfo[0].ivar_size; |
| 3212 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3213 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3214 | unsigned int TailPrevGCObjC = |
| 3215 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3216 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3217 | // consecutive 'scanned' object pointers. |
| 3218 | WordsToScan += IvarsInfo[i].ivar_size; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3219 | } else { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3220 | // Skip over 'gc'able object pointer which lay over each other. |
| 3221 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 3222 | continue; |
| 3223 | // Must skip over 1 or more words. We save current skip/scan values |
| 3224 | // and start a new pair. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3225 | SKIP_SCAN SkScan; |
| 3226 | SkScan.skip = WordsToSkip; |
| 3227 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3228 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3229 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3230 | // Skip the hole. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3231 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 3232 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3233 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3234 | WordsToSkip = 0; |
| 3235 | WordsToScan = IvarsInfo[i].ivar_size; |
| 3236 | } |
| 3237 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3238 | if (WordsToScan > 0) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3239 | SKIP_SCAN SkScan; |
| 3240 | SkScan.skip = WordsToSkip; |
| 3241 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3242 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3243 | } |
| 3244 | |
| 3245 | bool BytesSkipped = false; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3246 | if (!SkipIvars.empty()) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3247 | unsigned int LastIndex = SkipIvars.size()-1; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3248 | int LastByteSkipped = |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3249 | SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size; |
| 3250 | LastIndex = IvarsInfo.size()-1; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3251 | int LastByteScanned = |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3252 | IvarsInfo[LastIndex].ivar_bytepos + |
| 3253 | IvarsInfo[LastIndex].ivar_size * WordSize; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3254 | BytesSkipped = (LastByteSkipped > LastByteScanned); |
| 3255 | // 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] | 3256 | if (BytesSkipped) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3257 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3258 | SKIP_SCAN SkScan; |
| 3259 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
| 3260 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3261 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3262 | } |
| 3263 | } |
| 3264 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 3265 | // as 0xMN. |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3266 | int SkipScan = SkipScanIvars.size()-1; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3267 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3268 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 3269 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 3270 | // 0xM0 followed by 0x0N detected. |
| 3271 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 3272 | for (int j = i+1; j < SkipScan; j++) |
| 3273 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 3274 | --SkipScan; |
| 3275 | } |
| 3276 | } |
| 3277 | |
| 3278 | // Generate the string. |
| 3279 | std::string BitMap; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3280 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3281 | unsigned char byte; |
| 3282 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 3283 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 3284 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 3285 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
| 3286 | |
| 3287 | if (skip_small > 0 || skip_big > 0) |
| 3288 | BytesSkipped = true; |
| 3289 | // first skip big. |
| 3290 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 3291 | BitMap += (unsigned char)(0xf0); |
| 3292 | |
| 3293 | // next (skip small, scan) |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3294 | if (skip_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3295 | byte = skip_small << 4; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3296 | if (scan_big > 0) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3297 | byte |= 0xf; |
| 3298 | --scan_big; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3299 | } else if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3300 | byte |= scan_small; |
| 3301 | scan_small = 0; |
| 3302 | } |
| 3303 | BitMap += byte; |
| 3304 | } |
| 3305 | // next scan big |
| 3306 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 3307 | BitMap += (unsigned char)(0x0f); |
| 3308 | // last scan small |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3309 | if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3310 | byte = scan_small; |
| 3311 | BitMap += byte; |
| 3312 | } |
| 3313 | } |
| 3314 | // null terminate string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3315 | unsigned char zero = 0; |
| 3316 | BitMap += zero; |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3317 | |
| 3318 | if (CGM.getLangOptions().ObjCGCBitmapPrint) { |
| 3319 | printf("\n%s ivar layout for class '%s': ", |
| 3320 | ForStrongLayout ? "strong" : "weak", |
| 3321 | OMD->getClassInterface()->getNameAsCString()); |
| 3322 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
| 3323 | for (unsigned i = 0; i < BitMap.size(); i++) |
| 3324 | if (!(s[i] & 0xf0)) |
| 3325 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3326 | else |
| 3327 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3328 | printf("\n"); |
| 3329 | } |
| 3330 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3331 | // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as |
| 3332 | // final layout. |
| 3333 | if (ForStrongLayout && !BytesSkipped) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3334 | return llvm::Constant::getNullValue(PtrTy); |
| 3335 | llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 3336 | llvm::ConstantArray::get(BitMap.c_str()), |
| 3337 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3338 | 1, true); |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3339 | return getConstantGEP(Entry, 0, 0); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3340 | } |
| 3341 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3342 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3343 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 3344 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3345 | // FIXME: Avoid std::string copying. |
| 3346 | if (!Entry) |
| 3347 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
| 3348 | llvm::ConstantArray::get(Sel.getAsString()), |
| 3349 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3350 | 1, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3351 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3352 | return getConstantGEP(Entry, 0, 0); |
| 3353 | } |
| 3354 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3355 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3356 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3357 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 3358 | } |
| 3359 | |
| 3360 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3361 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3362 | return GetMethodVarName(&CGM.getContext().Idents.get(Name)); |
| 3363 | } |
| 3364 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 3365 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3366 | std::string TypeStr; |
| 3367 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 3368 | |
| 3369 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3370 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3371 | if (!Entry) |
| 3372 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3373 | llvm::ConstantArray::get(TypeStr), |
| 3374 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3375 | 1, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3376 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3377 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3378 | } |
| 3379 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3380 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3381 | std::string TypeStr; |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3382 | CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D), |
| 3383 | TypeStr); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3384 | |
| 3385 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 3386 | |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3387 | if (!Entry) |
| 3388 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3389 | llvm::ConstantArray::get(TypeStr), |
| 3390 | "__TEXT,__cstring,cstring_literals", |
| 3391 | 1, true); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3392 | |
| 3393 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3394 | } |
| 3395 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3396 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3397 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3398 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
| 3399 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3400 | if (!Entry) |
| 3401 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
| 3402 | llvm::ConstantArray::get(Ident->getName()), |
| 3403 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3404 | 1, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3405 | |
| 3406 | return getConstantGEP(Entry, 0, 0); |
| 3407 | } |
| 3408 | |
| 3409 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3410 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3411 | llvm::Constant * |
| 3412 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 3413 | const Decl *Container) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3414 | std::string TypeStr; |
| 3415 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3416 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 3417 | } |
| 3418 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3419 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
| 3420 | const ObjCContainerDecl *CD, |
| 3421 | std::string &NameOut) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 3422 | NameOut = '\01'; |
| 3423 | NameOut += (D->isInstanceMethod() ? '-' : '+'); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3424 | NameOut += '['; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 3425 | assert (CD && "Missing container decl in GetNameForMethod"); |
| 3426 | NameOut += CD->getNameAsString(); |
Fariborz Jahanian | 1e9aef3 | 2009-04-16 18:34:20 +0000 | [diff] [blame] | 3427 | if (const ObjCCategoryImplDecl *CID = |
| 3428 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) { |
| 3429 | NameOut += '('; |
| 3430 | NameOut += CID->getNameAsString(); |
| 3431 | NameOut+= ')'; |
| 3432 | } |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3433 | NameOut += ' '; |
| 3434 | NameOut += D->getSelector().getAsString(); |
| 3435 | NameOut += ']'; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3436 | } |
| 3437 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3438 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3439 | EmitModuleInfo(); |
| 3440 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3441 | // Emit the dummy bodies for any protocols which were referenced but |
| 3442 | // never defined. |
| 3443 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
| 3444 | i = Protocols.begin(), e = Protocols.end(); i != e; ++i) { |
| 3445 | if (i->second->hasInitializer()) |
| 3446 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3447 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3448 | std::vector<llvm::Constant*> Values(5); |
| 3449 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 3450 | Values[1] = GetClassName(i->first); |
| 3451 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 3452 | Values[3] = Values[4] = |
| 3453 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 3454 | i->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 3455 | i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 3456 | Values)); |
| 3457 | } |
| 3458 | |
| 3459 | std::vector<llvm::Constant*> Used; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3460 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3461 | e = UsedGlobals.end(); i != e; ++i) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3462 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3463 | } |
| 3464 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3465 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3466 | llvm::GlobalValue *GV = |
| 3467 | new llvm::GlobalVariable(AT, false, |
| 3468 | llvm::GlobalValue::AppendingLinkage, |
| 3469 | llvm::ConstantArray::get(AT, Used), |
| 3470 | "llvm.used", |
| 3471 | &CGM.getModule()); |
| 3472 | |
| 3473 | GV->setSection("llvm.metadata"); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3474 | |
| 3475 | // Add assembler directives to add lazy undefined symbol references |
| 3476 | // for classes which are referenced but not defined. This is |
| 3477 | // important for correct linker interaction. |
| 3478 | |
| 3479 | // FIXME: Uh, this isn't particularly portable. |
| 3480 | std::stringstream s; |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3481 | |
| 3482 | if (!CGM.getModule().getModuleInlineAsm().empty()) |
| 3483 | s << "\n"; |
| 3484 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3485 | for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(), |
| 3486 | e = LazySymbols.end(); i != e; ++i) { |
| 3487 | s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n"; |
| 3488 | } |
| 3489 | for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(), |
| 3490 | e = DefinedSymbols.end(); i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3491 | s << "\t.objc_class_name_" << (*i)->getName() << "=0\n" |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3492 | << "\t.globl .objc_class_name_" << (*i)->getName() << "\n"; |
| 3493 | } |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3494 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3495 | CGM.getModule().appendModuleInlineAsm(s.str()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3496 | } |
| 3497 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3498 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3499 | : CGObjCCommonMac(cgm), |
| 3500 | ObjCTypes(cgm) |
| 3501 | { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3502 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3503 | ObjCABI = 2; |
| 3504 | } |
| 3505 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3506 | /* *** */ |
| 3507 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3508 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3509 | : CGM(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3510 | { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3511 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3512 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3513 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3514 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3515 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3516 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3517 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3518 | Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 3519 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3520 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 3521 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3522 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3523 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame^] | 3524 | // FIXME: It would be nice to unify this with the opaque type, so that the IR |
| 3525 | // comes out a bit cleaner. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3526 | const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 3527 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3528 | |
| 3529 | // I'm not sure I like this. The implicit coordination is a bit |
| 3530 | // gross. We should solve this in a reasonable fashion because this |
| 3531 | // is a pretty common task (match some runtime data structure with |
| 3532 | // an LLVM data structure). |
| 3533 | |
| 3534 | // FIXME: This is leaked. |
| 3535 | // FIXME: Merge with rewriter code? |
| 3536 | |
| 3537 | // struct _objc_super { |
| 3538 | // id self; |
| 3539 | // Class cls; |
| 3540 | // } |
| 3541 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3542 | SourceLocation(), |
| 3543 | &Ctx.Idents.get("_objc_super")); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3544 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3545 | Ctx.getObjCIdType(), 0, false)); |
| 3546 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3547 | Ctx.getObjCClassType(), 0, false)); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3548 | RD->completeDefinition(Ctx); |
| 3549 | |
| 3550 | SuperCTy = Ctx.getTagDeclType(RD); |
| 3551 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
| 3552 | |
| 3553 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3554 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 3555 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3556 | // struct _prop_t { |
| 3557 | // char *name; |
| 3558 | // char *attributes; |
| 3559 | // } |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 3560 | PropertyTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, NULL); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3561 | CGM.getModule().addTypeName("struct._prop_t", |
| 3562 | PropertyTy); |
| 3563 | |
| 3564 | // struct _prop_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3565 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3566 | // uint32_t count_of_properties; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3567 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3568 | // } |
| 3569 | PropertyListTy = llvm::StructType::get(IntTy, |
| 3570 | IntTy, |
| 3571 | llvm::ArrayType::get(PropertyTy, 0), |
| 3572 | NULL); |
| 3573 | CGM.getModule().addTypeName("struct._prop_list_t", |
| 3574 | PropertyListTy); |
| 3575 | // struct _prop_list_t * |
| 3576 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
| 3577 | |
| 3578 | // struct _objc_method { |
| 3579 | // SEL _cmd; |
| 3580 | // char *method_type; |
| 3581 | // char *_imp; |
| 3582 | // } |
| 3583 | MethodTy = llvm::StructType::get(SelectorPtrTy, |
| 3584 | Int8PtrTy, |
| 3585 | Int8PtrTy, |
| 3586 | NULL); |
| 3587 | CGM.getModule().addTypeName("struct._objc_method", MethodTy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3588 | |
| 3589 | // struct _objc_cache * |
| 3590 | CacheTy = llvm::OpaqueType::get(); |
| 3591 | CGM.getModule().addTypeName("struct._objc_cache", CacheTy); |
| 3592 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3593 | } |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3594 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3595 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3596 | : ObjCCommonTypesHelper(cgm) |
| 3597 | { |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3598 | // struct _objc_method_description { |
| 3599 | // SEL name; |
| 3600 | // char *types; |
| 3601 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3602 | MethodDescriptionTy = |
| 3603 | llvm::StructType::get(SelectorPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3604 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3605 | NULL); |
| 3606 | CGM.getModule().addTypeName("struct._objc_method_description", |
| 3607 | MethodDescriptionTy); |
| 3608 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3609 | // struct _objc_method_description_list { |
| 3610 | // int count; |
| 3611 | // struct _objc_method_description[1]; |
| 3612 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3613 | MethodDescriptionListTy = |
| 3614 | llvm::StructType::get(IntTy, |
| 3615 | llvm::ArrayType::get(MethodDescriptionTy, 0), |
| 3616 | NULL); |
| 3617 | CGM.getModule().addTypeName("struct._objc_method_description_list", |
| 3618 | MethodDescriptionListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3619 | |
| 3620 | // struct _objc_method_description_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3621 | MethodDescriptionListPtrTy = |
| 3622 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
| 3623 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3624 | // Protocol description structures |
| 3625 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3626 | // struct _objc_protocol_extension { |
| 3627 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 3628 | // struct _objc_method_description_list *optional_instance_methods; |
| 3629 | // struct _objc_method_description_list *optional_class_methods; |
| 3630 | // struct _objc_property_list *instance_properties; |
| 3631 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3632 | ProtocolExtensionTy = |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3633 | llvm::StructType::get(IntTy, |
| 3634 | MethodDescriptionListPtrTy, |
| 3635 | MethodDescriptionListPtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3636 | PropertyListPtrTy, |
| 3637 | NULL); |
| 3638 | CGM.getModule().addTypeName("struct._objc_protocol_extension", |
| 3639 | ProtocolExtensionTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3640 | |
| 3641 | // struct _objc_protocol_extension * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3642 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
| 3643 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3644 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3645 | |
| 3646 | llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(); |
| 3647 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3648 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3649 | const llvm::Type *T = |
| 3650 | llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3651 | LongTy, |
| 3652 | llvm::ArrayType::get(ProtocolTyHolder, 0), |
| 3653 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3654 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T); |
| 3655 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3656 | // struct _objc_protocol { |
| 3657 | // struct _objc_protocol_extension *isa; |
| 3658 | // char *protocol_name; |
| 3659 | // struct _objc_protocol **_objc_protocol_list; |
| 3660 | // struct _objc_method_description_list *instance_methods; |
| 3661 | // struct _objc_method_description_list *class_methods; |
| 3662 | // } |
| 3663 | T = llvm::StructType::get(ProtocolExtensionPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3664 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3665 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3666 | MethodDescriptionListPtrTy, |
| 3667 | MethodDescriptionListPtrTy, |
| 3668 | NULL); |
| 3669 | cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T); |
| 3670 | |
| 3671 | ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get()); |
| 3672 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3673 | ProtocolListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3674 | // struct _objc_protocol_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3675 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
| 3676 | |
| 3677 | ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get()); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3678 | CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3679 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3680 | |
| 3681 | // Class description structures |
| 3682 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3683 | // struct _objc_ivar { |
| 3684 | // char *ivar_name; |
| 3685 | // char *ivar_type; |
| 3686 | // int ivar_offset; |
| 3687 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3688 | IvarTy = llvm::StructType::get(Int8PtrTy, |
| 3689 | Int8PtrTy, |
| 3690 | IntTy, |
| 3691 | NULL); |
| 3692 | CGM.getModule().addTypeName("struct._objc_ivar", IvarTy); |
| 3693 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3694 | // struct _objc_ivar_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3695 | IvarListTy = llvm::OpaqueType::get(); |
| 3696 | CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy); |
| 3697 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
| 3698 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3699 | // struct _objc_method_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3700 | MethodListTy = llvm::OpaqueType::get(); |
| 3701 | CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy); |
| 3702 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
| 3703 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3704 | // struct _objc_class_extension * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3705 | ClassExtensionTy = |
| 3706 | llvm::StructType::get(IntTy, |
| 3707 | Int8PtrTy, |
| 3708 | PropertyListPtrTy, |
| 3709 | NULL); |
| 3710 | CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy); |
| 3711 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
| 3712 | |
| 3713 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3714 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3715 | // struct _objc_class { |
| 3716 | // Class isa; |
| 3717 | // Class super_class; |
| 3718 | // char *name; |
| 3719 | // long version; |
| 3720 | // long info; |
| 3721 | // long instance_size; |
| 3722 | // struct _objc_ivar_list *ivars; |
| 3723 | // struct _objc_method_list *methods; |
| 3724 | // struct _objc_cache *cache; |
| 3725 | // struct _objc_protocol_list *protocols; |
| 3726 | // char *ivar_layout; |
| 3727 | // struct _objc_class_ext *ext; |
| 3728 | // }; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3729 | T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3730 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3731 | Int8PtrTy, |
| 3732 | LongTy, |
| 3733 | LongTy, |
| 3734 | LongTy, |
| 3735 | IvarListPtrTy, |
| 3736 | MethodListPtrTy, |
| 3737 | CachePtrTy, |
| 3738 | ProtocolListPtrTy, |
| 3739 | Int8PtrTy, |
| 3740 | ClassExtensionPtrTy, |
| 3741 | NULL); |
| 3742 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T); |
| 3743 | |
| 3744 | ClassTy = cast<llvm::StructType>(ClassTyHolder.get()); |
| 3745 | CGM.getModule().addTypeName("struct._objc_class", ClassTy); |
| 3746 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
| 3747 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3748 | // struct _objc_category { |
| 3749 | // char *category_name; |
| 3750 | // char *class_name; |
| 3751 | // struct _objc_method_list *instance_method; |
| 3752 | // struct _objc_method_list *class_method; |
| 3753 | // uint32_t size; // sizeof(struct _objc_category) |
| 3754 | // struct _objc_property_list *instance_properties;// category's @property |
| 3755 | // } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3756 | CategoryTy = llvm::StructType::get(Int8PtrTy, |
| 3757 | Int8PtrTy, |
| 3758 | MethodListPtrTy, |
| 3759 | MethodListPtrTy, |
| 3760 | ProtocolListPtrTy, |
| 3761 | IntTy, |
| 3762 | PropertyListPtrTy, |
| 3763 | NULL); |
| 3764 | CGM.getModule().addTypeName("struct._objc_category", CategoryTy); |
| 3765 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3766 | // Global metadata structures |
| 3767 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3768 | // struct _objc_symtab { |
| 3769 | // long sel_ref_cnt; |
| 3770 | // SEL *refs; |
| 3771 | // short cls_def_cnt; |
| 3772 | // short cat_def_cnt; |
| 3773 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 3774 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3775 | SymtabTy = llvm::StructType::get(LongTy, |
| 3776 | SelectorPtrTy, |
| 3777 | ShortTy, |
| 3778 | ShortTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3779 | llvm::ArrayType::get(Int8PtrTy, 0), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3780 | NULL); |
| 3781 | CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy); |
| 3782 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
| 3783 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3784 | // struct _objc_module { |
| 3785 | // long version; |
| 3786 | // long size; // sizeof(struct _objc_module) |
| 3787 | // char *name; |
| 3788 | // struct _objc_symtab* symtab; |
| 3789 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3790 | ModuleTy = |
| 3791 | llvm::StructType::get(LongTy, |
| 3792 | LongTy, |
| 3793 | Int8PtrTy, |
| 3794 | SymtabPtrTy, |
| 3795 | NULL); |
| 3796 | CGM.getModule().addTypeName("struct._objc_module", ModuleTy); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3797 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 3798 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame^] | 3799 | // FIXME: This is the size of the setjmp buffer and should be target |
| 3800 | // specific. 18 is what's used on 32-bit X86. |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3801 | uint64_t SetJmpBufferSize = 18; |
| 3802 | |
| 3803 | // Exceptions |
| 3804 | const llvm::Type *StackPtrTy = |
Daniel Dunbar | 1000491 | 2008-09-27 06:32:25 +0000 | [diff] [blame] | 3805 | llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3806 | |
| 3807 | ExceptionDataTy = |
| 3808 | llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty, |
| 3809 | SetJmpBufferSize), |
| 3810 | StackPtrTy, NULL); |
| 3811 | CGM.getModule().addTypeName("struct._objc_exception_data", |
| 3812 | ExceptionDataTy); |
| 3813 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3814 | } |
| 3815 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3816 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3817 | : ObjCCommonTypesHelper(cgm) |
| 3818 | { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3819 | // struct _method_list_t { |
| 3820 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 3821 | // uint32_t method_count; |
| 3822 | // struct _objc_method method_list[method_count]; |
| 3823 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3824 | MethodListnfABITy = llvm::StructType::get(IntTy, |
| 3825 | IntTy, |
| 3826 | llvm::ArrayType::get(MethodTy, 0), |
| 3827 | NULL); |
| 3828 | CGM.getModule().addTypeName("struct.__method_list_t", |
| 3829 | MethodListnfABITy); |
| 3830 | // struct method_list_t * |
| 3831 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3832 | |
| 3833 | // struct _protocol_t { |
| 3834 | // id isa; // NULL |
| 3835 | // const char * const protocol_name; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3836 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3837 | // const struct method_list_t * const instance_methods; |
| 3838 | // const struct method_list_t * const class_methods; |
| 3839 | // const struct method_list_t *optionalInstanceMethods; |
| 3840 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3841 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3842 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 3843 | // const uint32_t flags; // = 0 |
| 3844 | // } |
| 3845 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3846 | // Holder for struct _protocol_list_t * |
| 3847 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3848 | |
| 3849 | ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy, |
| 3850 | Int8PtrTy, |
| 3851 | llvm::PointerType::getUnqual( |
| 3852 | ProtocolListTyHolder), |
| 3853 | MethodListnfABIPtrTy, |
| 3854 | MethodListnfABIPtrTy, |
| 3855 | MethodListnfABIPtrTy, |
| 3856 | MethodListnfABIPtrTy, |
| 3857 | PropertyListPtrTy, |
| 3858 | IntTy, |
| 3859 | IntTy, |
| 3860 | NULL); |
| 3861 | CGM.getModule().addTypeName("struct._protocol_t", |
| 3862 | ProtocolnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3863 | |
| 3864 | // struct _protocol_t* |
| 3865 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3866 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3867 | // struct _protocol_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3868 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3869 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3870 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3871 | ProtocolListnfABITy = llvm::StructType::get(LongTy, |
| 3872 | llvm::ArrayType::get( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3873 | ProtocolnfABIPtrTy, 0), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3874 | NULL); |
| 3875 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3876 | ProtocolListnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3877 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo( |
| 3878 | ProtocolListnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3879 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3880 | // struct _objc_protocol_list* |
| 3881 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3882 | |
| 3883 | // struct _ivar_t { |
| 3884 | // unsigned long int *offset; // pointer to ivar offset location |
| 3885 | // char *name; |
| 3886 | // char *type; |
| 3887 | // uint32_t alignment; |
| 3888 | // uint32_t size; |
| 3889 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3890 | IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy), |
| 3891 | Int8PtrTy, |
| 3892 | Int8PtrTy, |
| 3893 | IntTy, |
| 3894 | IntTy, |
| 3895 | NULL); |
| 3896 | CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy); |
| 3897 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3898 | // struct _ivar_list_t { |
| 3899 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 3900 | // uint32 count; |
| 3901 | // struct _iver_t list[count]; |
| 3902 | // } |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3903 | IvarListnfABITy = llvm::StructType::get(IntTy, |
| 3904 | IntTy, |
| 3905 | llvm::ArrayType::get( |
| 3906 | IvarnfABITy, 0), |
| 3907 | NULL); |
| 3908 | CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy); |
| 3909 | |
| 3910 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3911 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3912 | // struct _class_ro_t { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3913 | // uint32_t const flags; |
| 3914 | // uint32_t const instanceStart; |
| 3915 | // uint32_t const instanceSize; |
| 3916 | // uint32_t const reserved; // only when building for 64bit targets |
| 3917 | // const uint8_t * const ivarLayout; |
| 3918 | // const char *const name; |
| 3919 | // const struct _method_list_t * const baseMethods; |
| 3920 | // const struct _objc_protocol_list *const baseProtocols; |
| 3921 | // const struct _ivar_list_t *const ivars; |
| 3922 | // const uint8_t * const weakIvarLayout; |
| 3923 | // const struct _prop_list_t * const properties; |
| 3924 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3925 | |
| 3926 | // FIXME. Add 'reserved' field in 64bit abi mode! |
| 3927 | ClassRonfABITy = llvm::StructType::get(IntTy, |
| 3928 | IntTy, |
| 3929 | IntTy, |
| 3930 | Int8PtrTy, |
| 3931 | Int8PtrTy, |
| 3932 | MethodListnfABIPtrTy, |
| 3933 | ProtocolListnfABIPtrTy, |
| 3934 | IvarListnfABIPtrTy, |
| 3935 | Int8PtrTy, |
| 3936 | PropertyListPtrTy, |
| 3937 | NULL); |
| 3938 | CGM.getModule().addTypeName("struct._class_ro_t", |
| 3939 | ClassRonfABITy); |
| 3940 | |
| 3941 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 3942 | std::vector<const llvm::Type*> Params; |
| 3943 | Params.push_back(ObjectPtrTy); |
| 3944 | Params.push_back(SelectorPtrTy); |
| 3945 | ImpnfABITy = llvm::PointerType::getUnqual( |
| 3946 | llvm::FunctionType::get(ObjectPtrTy, Params, false)); |
| 3947 | |
| 3948 | // struct _class_t { |
| 3949 | // struct _class_t *isa; |
| 3950 | // struct _class_t * const superclass; |
| 3951 | // void *cache; |
| 3952 | // IMP *vtable; |
| 3953 | // struct class_ro_t *ro; |
| 3954 | // } |
| 3955 | |
| 3956 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3957 | ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3958 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3959 | CachePtrTy, |
| 3960 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 3961 | llvm::PointerType::getUnqual( |
| 3962 | ClassRonfABITy), |
| 3963 | NULL); |
| 3964 | CGM.getModule().addTypeName("struct._class_t", ClassnfABITy); |
| 3965 | |
| 3966 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo( |
| 3967 | ClassnfABITy); |
| 3968 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3969 | // LLVM for struct _class_t * |
| 3970 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
| 3971 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3972 | // struct _category_t { |
| 3973 | // const char * const name; |
| 3974 | // struct _class_t *const cls; |
| 3975 | // const struct _method_list_t * const instance_methods; |
| 3976 | // const struct _method_list_t * const class_methods; |
| 3977 | // const struct _protocol_list_t * const protocols; |
| 3978 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3979 | // } |
| 3980 | CategorynfABITy = llvm::StructType::get(Int8PtrTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3981 | ClassnfABIPtrTy, |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3982 | MethodListnfABIPtrTy, |
| 3983 | MethodListnfABIPtrTy, |
| 3984 | ProtocolListnfABIPtrTy, |
| 3985 | PropertyListPtrTy, |
| 3986 | NULL); |
| 3987 | CGM.getModule().addTypeName("struct._category_t", CategorynfABITy); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3988 | |
| 3989 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3990 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3991 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3992 | |
| 3993 | // MessageRefTy - LLVM for: |
| 3994 | // struct _message_ref_t { |
| 3995 | // IMP messenger; |
| 3996 | // SEL name; |
| 3997 | // }; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3998 | |
| 3999 | // First the clang type for struct _message_ref_t |
| 4000 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 4001 | SourceLocation(), |
| 4002 | &Ctx.Idents.get("_message_ref_t")); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4003 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 4004 | Ctx.VoidPtrTy, 0, false)); |
| 4005 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 4006 | Ctx.getObjCSelType(), 0, false)); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4007 | RD->completeDefinition(Ctx); |
| 4008 | |
| 4009 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 4010 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 4011 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4012 | |
| 4013 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 4014 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
| 4015 | |
| 4016 | // SuperMessageRefTy - LLVM for: |
| 4017 | // struct _super_message_ref_t { |
| 4018 | // SUPER_IMP messenger; |
| 4019 | // SEL name; |
| 4020 | // }; |
| 4021 | SuperMessageRefTy = llvm::StructType::get(ImpnfABITy, |
| 4022 | SelectorPtrTy, |
| 4023 | NULL); |
| 4024 | CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy); |
| 4025 | |
| 4026 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 4027 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
| 4028 | |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 4029 | |
| 4030 | // struct objc_typeinfo { |
| 4031 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 4032 | // const char* name; // c++ typeinfo string |
| 4033 | // Class cls; |
| 4034 | // }; |
| 4035 | EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy), |
| 4036 | Int8PtrTy, |
| 4037 | ClassnfABIPtrTy, |
| 4038 | NULL); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 4039 | CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 4040 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4041 | } |
| 4042 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4043 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
| 4044 | FinishNonFragileABIModule(); |
| 4045 | |
| 4046 | return NULL; |
| 4047 | } |
| 4048 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4049 | void CGObjCNonFragileABIMac::AddModuleClassList(const |
| 4050 | std::vector<llvm::GlobalValue*> |
| 4051 | &Container, |
| 4052 | const char *SymbolName, |
| 4053 | const char *SectionName) { |
| 4054 | unsigned NumClasses = Container.size(); |
| 4055 | |
| 4056 | if (!NumClasses) |
| 4057 | return; |
| 4058 | |
| 4059 | std::vector<llvm::Constant*> Symbols(NumClasses); |
| 4060 | for (unsigned i=0; i<NumClasses; i++) |
| 4061 | Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i], |
| 4062 | ObjCTypes.Int8PtrTy); |
| 4063 | llvm::Constant* Init = |
| 4064 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 4065 | NumClasses), |
| 4066 | Symbols); |
| 4067 | |
| 4068 | llvm::GlobalVariable *GV = |
| 4069 | new llvm::GlobalVariable(Init->getType(), false, |
| 4070 | llvm::GlobalValue::InternalLinkage, |
| 4071 | Init, |
| 4072 | SymbolName, |
| 4073 | &CGM.getModule()); |
| 4074 | GV->setAlignment(8); |
| 4075 | GV->setSection(SectionName); |
| 4076 | UsedGlobals.push_back(GV); |
| 4077 | } |
| 4078 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4079 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 4080 | // nonfragile abi has no module definition. |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4081 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4082 | // Build list of all implemented class addresses in array |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4083 | // L_OBJC_LABEL_CLASS_$. |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4084 | AddModuleClassList(DefinedClasses, |
| 4085 | "\01L_OBJC_LABEL_CLASS_$", |
| 4086 | "__DATA, __objc_classlist, regular, no_dead_strip"); |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4087 | AddModuleClassList(DefinedNonLazyClasses, |
| 4088 | "\01L_OBJC_LABEL_NONLAZY_CLASS_$", |
| 4089 | "__DATA, __objc_nlclslist, regular, no_dead_strip"); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4090 | |
| 4091 | // Build list of all implemented category addresses in array |
| 4092 | // L_OBJC_LABEL_CATEGORY_$. |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4093 | AddModuleClassList(DefinedCategories, |
| 4094 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 4095 | "__DATA, __objc_catlist, regular, no_dead_strip"); |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4096 | AddModuleClassList(DefinedNonLazyCategories, |
| 4097 | "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$", |
| 4098 | "__DATA, __objc_nlcatlist, regular, no_dead_strip"); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4099 | |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4100 | // static int L_OBJC_IMAGE_INFO[2] = { 0, flags }; |
| 4101 | // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0 |
| 4102 | std::vector<llvm::Constant*> Values(2); |
| 4103 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0); |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4104 | unsigned int flags = 0; |
Fariborz Jahanian | 66a5c2c | 2009-02-24 23:34:44 +0000 | [diff] [blame] | 4105 | // FIXME: Fix and continue? |
| 4106 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 4107 | flags |= eImageInfo_GarbageCollected; |
| 4108 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 4109 | flags |= eImageInfo_GCOnly; |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4110 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4111 | llvm::Constant* Init = llvm::ConstantArray::get( |
| 4112 | llvm::ArrayType::get(ObjCTypes.IntTy, 2), |
| 4113 | Values); |
| 4114 | llvm::GlobalVariable *IMGV = |
| 4115 | new llvm::GlobalVariable(Init->getType(), false, |
| 4116 | llvm::GlobalValue::InternalLinkage, |
| 4117 | Init, |
| 4118 | "\01L_OBJC_IMAGE_INFO", |
| 4119 | &CGM.getModule()); |
| 4120 | IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); |
Daniel Dunbar | 325f758 | 2009-04-23 08:03:21 +0000 | [diff] [blame] | 4121 | IMGV->setConstant(true); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4122 | UsedGlobals.push_back(IMGV); |
| 4123 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4124 | std::vector<llvm::Constant*> Used; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4125 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4126 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
| 4127 | e = UsedGlobals.end(); i != e; ++i) { |
| 4128 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
| 4129 | } |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4130 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4131 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
| 4132 | llvm::GlobalValue *GV = |
| 4133 | new llvm::GlobalVariable(AT, false, |
| 4134 | llvm::GlobalValue::AppendingLinkage, |
| 4135 | llvm::ConstantArray::get(AT, Used), |
| 4136 | "llvm.used", |
| 4137 | &CGM.getModule()); |
| 4138 | |
| 4139 | GV->setSection("llvm.metadata"); |
| 4140 | |
| 4141 | } |
| 4142 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4143 | /// LegacyDispatchedSelector - Returns true if SEL is not in the list of |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4144 | /// NonLegacyDispatchMethods; false otherwise. What this means is that |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4145 | /// except for the 19 selectors in the list, we generate 32bit-style |
| 4146 | /// message dispatch call for all the rest. |
| 4147 | /// |
| 4148 | bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) { |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4149 | if (NonLegacyDispatchMethods.empty()) { |
| 4150 | NonLegacyDispatchMethods.insert(GetNullarySelector("alloc")); |
| 4151 | NonLegacyDispatchMethods.insert(GetNullarySelector("class")); |
| 4152 | NonLegacyDispatchMethods.insert(GetNullarySelector("self")); |
| 4153 | NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped")); |
| 4154 | NonLegacyDispatchMethods.insert(GetNullarySelector("length")); |
| 4155 | NonLegacyDispatchMethods.insert(GetNullarySelector("count")); |
| 4156 | NonLegacyDispatchMethods.insert(GetNullarySelector("retain")); |
| 4157 | NonLegacyDispatchMethods.insert(GetNullarySelector("release")); |
| 4158 | NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease")); |
| 4159 | NonLegacyDispatchMethods.insert(GetNullarySelector("hash")); |
| 4160 | |
| 4161 | NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone")); |
| 4162 | NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass")); |
| 4163 | NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector")); |
| 4164 | NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey")); |
| 4165 | NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex")); |
| 4166 | NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString")); |
| 4167 | NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual")); |
| 4168 | NonLegacyDispatchMethods.insert(GetUnarySelector("addObject")); |
Fariborz Jahanian | be53be4 | 2009-05-13 16:19:02 +0000 | [diff] [blame] | 4169 | // "countByEnumeratingWithState:objects:count" |
| 4170 | IdentifierInfo *KeyIdents[] = { |
| 4171 | &CGM.getContext().Idents.get("countByEnumeratingWithState"), |
| 4172 | &CGM.getContext().Idents.get("objects"), |
| 4173 | &CGM.getContext().Idents.get("count") |
| 4174 | }; |
| 4175 | NonLegacyDispatchMethods.insert( |
| 4176 | CGM.getContext().Selectors.getSelector(3, KeyIdents)); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4177 | } |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4178 | return (NonLegacyDispatchMethods.count(Sel) == 0); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4179 | } |
| 4180 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4181 | // Metadata flags |
| 4182 | enum MetaDataDlags { |
| 4183 | CLS = 0x0, |
| 4184 | CLS_META = 0x1, |
| 4185 | CLS_ROOT = 0x2, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4186 | OBJC2_CLS_HIDDEN = 0x10, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4187 | CLS_EXCEPTION = 0x20 |
| 4188 | }; |
| 4189 | /// BuildClassRoTInitializer - generate meta-data for: |
| 4190 | /// struct _class_ro_t { |
| 4191 | /// uint32_t const flags; |
| 4192 | /// uint32_t const instanceStart; |
| 4193 | /// uint32_t const instanceSize; |
| 4194 | /// uint32_t const reserved; // only when building for 64bit targets |
| 4195 | /// const uint8_t * const ivarLayout; |
| 4196 | /// const char *const name; |
| 4197 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4198 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4199 | /// const struct _ivar_list_t *const ivars; |
| 4200 | /// const uint8_t * const weakIvarLayout; |
| 4201 | /// const struct _prop_list_t * const properties; |
| 4202 | /// } |
| 4203 | /// |
| 4204 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
| 4205 | unsigned flags, |
| 4206 | unsigned InstanceStart, |
| 4207 | unsigned InstanceSize, |
| 4208 | const ObjCImplementationDecl *ID) { |
| 4209 | std::string ClassName = ID->getNameAsString(); |
| 4210 | std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets! |
| 4211 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 4212 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 4213 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
| 4214 | // FIXME. For 64bit targets add 0 here. |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 4215 | Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4216 | : BuildIvarLayout(ID, true); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4217 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4218 | // const struct _method_list_t * const baseMethods; |
| 4219 | std::vector<llvm::Constant*> Methods; |
| 4220 | std::string MethodListName("\01l_OBJC_$_"); |
| 4221 | if (flags & CLS_META) { |
| 4222 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4223 | for (ObjCImplementationDecl::classmeth_iterator |
| 4224 | i = ID->classmeth_begin(CGM.getContext()), |
| 4225 | e = ID->classmeth_end(CGM.getContext()); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4226 | // Class methods should always be defined. |
| 4227 | Methods.push_back(GetMethodConstant(*i)); |
| 4228 | } |
| 4229 | } else { |
| 4230 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4231 | for (ObjCImplementationDecl::instmeth_iterator |
| 4232 | i = ID->instmeth_begin(CGM.getContext()), |
| 4233 | e = ID->instmeth_end(CGM.getContext()); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4234 | // Instance methods should always be defined. |
| 4235 | Methods.push_back(GetMethodConstant(*i)); |
| 4236 | } |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4237 | for (ObjCImplementationDecl::propimpl_iterator |
| 4238 | i = ID->propimpl_begin(CGM.getContext()), |
| 4239 | e = ID->propimpl_end(CGM.getContext()); i != e; ++i) { |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4240 | ObjCPropertyImplDecl *PID = *i; |
| 4241 | |
| 4242 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 4243 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 4244 | |
| 4245 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 4246 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4247 | Methods.push_back(C); |
| 4248 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 4249 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4250 | Methods.push_back(C); |
| 4251 | } |
| 4252 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4253 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4254 | Values[ 5] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4255 | "__DATA, __objc_const", Methods); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4256 | |
| 4257 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4258 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
| 4259 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
| 4260 | + OID->getNameAsString(), |
| 4261 | OID->protocol_begin(), |
| 4262 | OID->protocol_end()); |
| 4263 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4264 | if (flags & CLS_META) |
| 4265 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4266 | else |
| 4267 | Values[ 7] = EmitIvarList(ID); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 4268 | Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4269 | : BuildIvarLayout(ID, false); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4270 | if (flags & CLS_META) |
| 4271 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4272 | else |
| 4273 | Values[ 9] = |
| 4274 | EmitPropertyList( |
| 4275 | "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
| 4276 | ID, ID->getClassInterface(), ObjCTypes); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4277 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
| 4278 | Values); |
| 4279 | llvm::GlobalVariable *CLASS_RO_GV = |
| 4280 | new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false, |
| 4281 | llvm::GlobalValue::InternalLinkage, |
| 4282 | Init, |
| 4283 | (flags & CLS_META) ? |
| 4284 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 4285 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName, |
| 4286 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4287 | CLASS_RO_GV->setAlignment( |
| 4288 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4289 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4290 | return CLASS_RO_GV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4291 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4292 | } |
| 4293 | |
| 4294 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 4295 | /// for the given ClassName for: |
| 4296 | /// struct _class_t { |
| 4297 | /// struct _class_t *isa; |
| 4298 | /// struct _class_t * const superclass; |
| 4299 | /// void *cache; |
| 4300 | /// IMP *vtable; |
| 4301 | /// struct class_ro_t *ro; |
| 4302 | /// } |
| 4303 | /// |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4304 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
| 4305 | std::string &ClassName, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4306 | llvm::Constant *IsAGV, |
| 4307 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4308 | llvm::Constant *ClassRoGV, |
| 4309 | bool HiddenVisibility) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4310 | std::vector<llvm::Constant*> Values(5); |
| 4311 | Values[0] = IsAGV; |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4312 | Values[1] = SuperClassGV |
| 4313 | ? SuperClassGV |
| 4314 | : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4315 | Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar |
| 4316 | Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar |
| 4317 | Values[4] = ClassRoGV; // &CLASS_RO_GV |
| 4318 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
| 4319 | Values); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4320 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 4321 | GV->setInitializer(Init); |
Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 4322 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4323 | GV->setAlignment( |
| 4324 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4325 | if (HiddenVisibility) |
| 4326 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4327 | return GV; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4328 | } |
| 4329 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4330 | bool |
| 4331 | CGObjCNonFragileABIMac::ImplementationIsNonLazy(const DeclContext *DC) const { |
| 4332 | DeclContext::lookup_const_result res = |
| 4333 | DC->lookup(CGM.getContext(), GetNullarySelector("load")); |
| 4334 | |
| 4335 | for (; res.first != res.second; ++res.first) |
| 4336 | if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(*res.first)) |
| 4337 | if (OMD->isClassMethod()) |
| 4338 | return true; |
| 4339 | |
| 4340 | return false; |
| 4341 | } |
| 4342 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4343 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4344 | uint32_t &InstanceStart, |
| 4345 | uint32_t &InstanceSize) { |
Daniel Dunbar | b4c79e0 | 2009-05-04 21:26:30 +0000 | [diff] [blame] | 4346 | const ASTRecordLayout &RL = |
| 4347 | CGM.getContext().getASTObjCImplementationLayout(OID); |
| 4348 | |
Daniel Dunbar | 6e8575b | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 4349 | // InstanceSize is really instance end. |
Daniel Dunbar | b4c79e0 | 2009-05-04 21:26:30 +0000 | [diff] [blame] | 4350 | InstanceSize = llvm::RoundUpToAlignment(RL.getNextOffset(), 8) / 8; |
Daniel Dunbar | 6e8575b | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 4351 | |
| 4352 | // If there are no fields, the start is the same as the end. |
| 4353 | if (!RL.getFieldCount()) |
| 4354 | InstanceStart = InstanceSize; |
| 4355 | else |
| 4356 | InstanceStart = RL.getFieldOffset(0) / 8; |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4357 | } |
| 4358 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4359 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 4360 | std::string ClassName = ID->getNameAsString(); |
| 4361 | if (!ObjCEmptyCacheVar) { |
| 4362 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4363 | ObjCTypes.CacheTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4364 | false, |
| 4365 | llvm::GlobalValue::ExternalLinkage, |
| 4366 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4367 | "_objc_empty_cache", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4368 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4369 | |
| 4370 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4371 | ObjCTypes.ImpnfABITy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4372 | false, |
| 4373 | llvm::GlobalValue::ExternalLinkage, |
| 4374 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4375 | "_objc_empty_vtable", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4376 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4377 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4378 | assert(ID->getClassInterface() && |
| 4379 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 4380 | // FIXME: Is this correct (that meta class size is never computed)? |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4381 | uint32_t InstanceStart = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4382 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4383 | uint32_t InstanceSize = InstanceStart; |
| 4384 | uint32_t flags = CLS_META; |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4385 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 4386 | std::string ObjCClassName(getClassSymbolPrefix()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4387 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4388 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4389 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4390 | bool classIsHidden = |
| 4391 | CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4392 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4393 | flags |= OBJC2_CLS_HIDDEN; |
| 4394 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4395 | // class is root |
| 4396 | flags |= CLS_ROOT; |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4397 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4398 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4399 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4400 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4401 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 4402 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 4403 | Root = Super; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4404 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4405 | // work on super class metadata symbol. |
| 4406 | std::string SuperClassName = |
| 4407 | ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4408 | SuperClassGV = GetClassGlobal(SuperClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4409 | } |
| 4410 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 4411 | InstanceStart, |
| 4412 | InstanceSize,ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4413 | std::string TClassName = ObjCMetaClassName + ClassName; |
| 4414 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4415 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 4416 | classIsHidden); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4417 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4418 | // Metadata for the class |
| 4419 | flags = CLS; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4420 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4421 | flags |= OBJC2_CLS_HIDDEN; |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4422 | |
| 4423 | if (hasObjCExceptionAttribute(ID->getClassInterface())) |
| 4424 | flags |= CLS_EXCEPTION; |
| 4425 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4426 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4427 | flags |= CLS_ROOT; |
| 4428 | SuperClassGV = 0; |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 4429 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4430 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4431 | std::string RootClassName = |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4432 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4433 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4434 | } |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4435 | GetClassSizeInfo(ID, InstanceStart, InstanceSize); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4436 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4437 | InstanceStart, |
| 4438 | InstanceSize, |
| 4439 | ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4440 | |
| 4441 | TClassName = ObjCClassName + ClassName; |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4442 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4443 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 4444 | classIsHidden); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4445 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4446 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4447 | // Determine if this class is also "non-lazy". |
| 4448 | if (ImplementationIsNonLazy(ID)) |
| 4449 | DefinedNonLazyClasses.push_back(ClassMD); |
| 4450 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4451 | // Force the definition of the EHType if necessary. |
| 4452 | if (flags & CLS_EXCEPTION) |
| 4453 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4454 | } |
| 4455 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4456 | /// GenerateProtocolRef - This routine is called to generate code for |
| 4457 | /// a protocol reference expression; as in: |
| 4458 | /// @code |
| 4459 | /// @protocol(Proto1); |
| 4460 | /// @endcode |
| 4461 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 4462 | /// which will hold address of the protocol meta-data. |
| 4463 | /// |
| 4464 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
| 4465 | const ObjCProtocolDecl *PD) { |
| 4466 | |
Fariborz Jahanian | 960cd06 | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 4467 | // This routine is called for @protocol only. So, we must build definition |
| 4468 | // of protocol's meta-data (not a reference to it!) |
| 4469 | // |
| 4470 | llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4471 | ObjCTypes.ExternalProtocolPtrTy); |
| 4472 | |
| 4473 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
| 4474 | ProtocolName += PD->getNameAsCString(); |
| 4475 | |
| 4476 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 4477 | if (PTGV) |
| 4478 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4479 | PTGV = new llvm::GlobalVariable( |
| 4480 | Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4481 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4482 | Init, |
| 4483 | ProtocolName, |
| 4484 | &CGM.getModule()); |
| 4485 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 4486 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4487 | UsedGlobals.push_back(PTGV); |
| 4488 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4489 | } |
| 4490 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4491 | /// GenerateCategory - Build metadata for a category implementation. |
| 4492 | /// struct _category_t { |
| 4493 | /// const char * const name; |
| 4494 | /// struct _class_t *const cls; |
| 4495 | /// const struct _method_list_t * const instance_methods; |
| 4496 | /// const struct _method_list_t * const class_methods; |
| 4497 | /// const struct _protocol_list_t * const protocols; |
| 4498 | /// const struct _prop_list_t * const properties; |
| 4499 | /// } |
| 4500 | /// |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4501 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4502 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4503 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
| 4504 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4505 | "_$_" + OCD->getNameAsString()); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4506 | std::string ExtClassName(getClassSymbolPrefix() + |
| 4507 | Interface->getNameAsString()); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4508 | |
| 4509 | std::vector<llvm::Constant*> Values(6); |
| 4510 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 4511 | // meta-class entry symbol |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4512 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4513 | Values[1] = ClassGV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4514 | std::vector<llvm::Constant*> Methods; |
| 4515 | std::string MethodListName(Prefix); |
| 4516 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
| 4517 | "_$_" + OCD->getNameAsString(); |
| 4518 | |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4519 | for (ObjCCategoryImplDecl::instmeth_iterator |
| 4520 | i = OCD->instmeth_begin(CGM.getContext()), |
| 4521 | e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4522 | // Instance methods should always be defined. |
| 4523 | Methods.push_back(GetMethodConstant(*i)); |
| 4524 | } |
| 4525 | |
| 4526 | Values[2] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4527 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4528 | Methods); |
| 4529 | |
| 4530 | MethodListName = Prefix; |
| 4531 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 4532 | OCD->getNameAsString(); |
| 4533 | Methods.clear(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4534 | for (ObjCCategoryImplDecl::classmeth_iterator |
| 4535 | i = OCD->classmeth_begin(CGM.getContext()), |
| 4536 | e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4537 | // Class methods should always be defined. |
| 4538 | Methods.push_back(GetMethodConstant(*i)); |
| 4539 | } |
| 4540 | |
| 4541 | Values[3] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4542 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4543 | Methods); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4544 | const ObjCCategoryDecl *Category = |
| 4545 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4546 | if (Category) { |
| 4547 | std::string ExtName(Interface->getNameAsString() + "_$_" + |
| 4548 | OCD->getNameAsString()); |
| 4549 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
| 4550 | + Interface->getNameAsString() + "_$_" |
| 4551 | + Category->getNameAsString(), |
| 4552 | Category->protocol_begin(), |
| 4553 | Category->protocol_end()); |
| 4554 | Values[5] = |
| 4555 | EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
| 4556 | OCD, Category, ObjCTypes); |
| 4557 | } |
| 4558 | else { |
| 4559 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4560 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4561 | } |
| 4562 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4563 | llvm::Constant *Init = |
| 4564 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
| 4565 | Values); |
| 4566 | llvm::GlobalVariable *GCATV |
| 4567 | = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy, |
| 4568 | false, |
| 4569 | llvm::GlobalValue::InternalLinkage, |
| 4570 | Init, |
| 4571 | ExtCatName, |
| 4572 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4573 | GCATV->setAlignment( |
| 4574 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4575 | GCATV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4576 | UsedGlobals.push_back(GCATV); |
| 4577 | DefinedCategories.push_back(GCATV); |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4578 | |
| 4579 | // Determine if this category is also "non-lazy". |
| 4580 | if (ImplementationIsNonLazy(OCD)) |
| 4581 | DefinedNonLazyCategories.push_back(GCATV); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4582 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4583 | |
| 4584 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 4585 | /// given method if it has been defined. The result is null if the |
| 4586 | /// method has not been defined. The return value has type MethodPtrTy. |
| 4587 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
| 4588 | const ObjCMethodDecl *MD) { |
| 4589 | // FIXME: Use DenseMap::lookup |
| 4590 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 4591 | if (!Fn) |
| 4592 | return 0; |
| 4593 | |
| 4594 | std::vector<llvm::Constant*> Method(3); |
| 4595 | Method[0] = |
| 4596 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4597 | ObjCTypes.SelectorPtrTy); |
| 4598 | Method[1] = GetMethodVarType(MD); |
| 4599 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 4600 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 4601 | } |
| 4602 | |
| 4603 | /// EmitMethodList - Build meta-data for method declarations |
| 4604 | /// struct _method_list_t { |
| 4605 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 4606 | /// uint32_t method_count; |
| 4607 | /// struct _objc_method method_list[method_count]; |
| 4608 | /// } |
| 4609 | /// |
| 4610 | llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList( |
| 4611 | const std::string &Name, |
| 4612 | const char *Section, |
| 4613 | const ConstantVector &Methods) { |
| 4614 | // Return null for empty list. |
| 4615 | if (Methods.empty()) |
| 4616 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
| 4617 | |
| 4618 | std::vector<llvm::Constant*> Values(3); |
| 4619 | // sizeof(struct _objc_method) |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4620 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4621 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4622 | // method_count |
| 4623 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 4624 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 4625 | Methods.size()); |
| 4626 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 4627 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4628 | |
| 4629 | llvm::GlobalVariable *GV = |
| 4630 | new llvm::GlobalVariable(Init->getType(), false, |
| 4631 | llvm::GlobalValue::InternalLinkage, |
| 4632 | Init, |
| 4633 | Name, |
| 4634 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4635 | GV->setAlignment( |
| 4636 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4637 | GV->setSection(Section); |
| 4638 | UsedGlobals.push_back(GV); |
| 4639 | return llvm::ConstantExpr::getBitCast(GV, |
| 4640 | ObjCTypes.MethodListnfABIPtrTy); |
| 4641 | } |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4642 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4643 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 4644 | /// the given ivar. |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4645 | llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 4646 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4647 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | a81419d | 2009-05-05 00:36:57 +0000 | [diff] [blame] | 4648 | // FIXME: We shouldn't need to do this lookup. |
| 4649 | unsigned Index; |
| 4650 | const ObjCInterfaceDecl *Container = |
| 4651 | FindIvarInterface(CGM.getContext(), ID, Ivar, Index); |
| 4652 | assert(Container && "Unable to find ivar container!"); |
| 4653 | std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() + |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4654 | '.' + Ivar->getNameAsString(); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4655 | llvm::GlobalVariable *IvarOffsetGV = |
| 4656 | CGM.getModule().getGlobalVariable(Name); |
| 4657 | if (!IvarOffsetGV) |
| 4658 | IvarOffsetGV = |
| 4659 | new llvm::GlobalVariable(ObjCTypes.LongTy, |
| 4660 | false, |
| 4661 | llvm::GlobalValue::ExternalLinkage, |
| 4662 | 0, |
| 4663 | Name, |
| 4664 | &CGM.getModule()); |
| 4665 | return IvarOffsetGV; |
| 4666 | } |
| 4667 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4668 | llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar( |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4669 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4670 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4671 | unsigned long int Offset) { |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4672 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
| 4673 | IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy, |
| 4674 | Offset)); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4675 | IvarOffsetGV->setAlignment( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4676 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy)); |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4677 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame^] | 4678 | // FIXME: This matches gcc, but shouldn't the visibility be set on the use as |
| 4679 | // well (i.e., in ObjCIvarOffsetVariable). |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4680 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 4681 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
| 4682 | CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden) |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4683 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4684 | else |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4685 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4686 | IvarOffsetGV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4687 | return IvarOffsetGV; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4688 | } |
| 4689 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4690 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 4691 | /// implementation. The return value has type |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4692 | /// IvarListnfABIPtrTy. |
| 4693 | /// struct _ivar_t { |
| 4694 | /// unsigned long int *offset; // pointer to ivar offset location |
| 4695 | /// char *name; |
| 4696 | /// char *type; |
| 4697 | /// uint32_t alignment; |
| 4698 | /// uint32_t size; |
| 4699 | /// } |
| 4700 | /// struct _ivar_list_t { |
| 4701 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 4702 | /// uint32 count; |
| 4703 | /// struct _iver_t list[count]; |
| 4704 | /// } |
| 4705 | /// |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4706 | |
| 4707 | void CGObjCCommonMac::GetNamedIvarList(const ObjCInterfaceDecl *OID, |
| 4708 | llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const { |
| 4709 | for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), |
| 4710 | E = OID->ivar_end(); I != E; ++I) { |
| 4711 | // Ignore unnamed bit-fields. |
| 4712 | if (!(*I)->getDeclName()) |
| 4713 | continue; |
| 4714 | |
| 4715 | Res.push_back(*I); |
| 4716 | } |
| 4717 | |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 4718 | // Also save synthesize ivars. |
| 4719 | // FIXME. Why can't we just use passed in Res small vector? |
| 4720 | llvm::SmallVector<ObjCIvarDecl*, 16> Ivars; |
| 4721 | CGM.getContext().CollectSynthesizedIvars(OID, Ivars); |
| 4722 | for (unsigned k = 0, e = Ivars.size(); k != e; ++k) |
| 4723 | Res.push_back(Ivars[k]); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4724 | } |
| 4725 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4726 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
| 4727 | const ObjCImplementationDecl *ID) { |
| 4728 | |
| 4729 | std::vector<llvm::Constant*> Ivars, Ivar(5); |
| 4730 | |
| 4731 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4732 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
| 4733 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4734 | // FIXME. Consolidate this with similar code in GenerateClass. |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 4735 | |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4736 | // Collect declared and synthesized ivars in a small vector. |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4737 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4738 | GetNamedIvarList(OID, OIvars); |
Fariborz Jahanian | 99eee36 | 2009-04-01 19:37:34 +0000 | [diff] [blame] | 4739 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4740 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 4741 | ObjCIvarDecl *IVD = OIvars[i]; |
Daniel Dunbar | 3eec8aa | 2009-04-20 05:53:40 +0000 | [diff] [blame] | 4742 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD, |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4743 | ComputeIvarBaseOffset(CGM, ID, IVD)); |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4744 | Ivar[1] = GetMethodVarName(IVD->getIdentifier()); |
| 4745 | Ivar[2] = GetMethodVarType(IVD); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4746 | const llvm::Type *FieldTy = |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4747 | CGM.getTypes().ConvertTypeForMem(IVD->getType()); |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4748 | unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4749 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4750 | IVD->getType().getTypePtr()) >> 3; |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4751 | Align = llvm::Log2_32(Align); |
| 4752 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4753 | // NOTE. Size of a bitfield does not match gcc's, because of the |
| 4754 | // way bitfields are treated special in each. But I am told that |
| 4755 | // 'size' for bitfield ivars is ignored by the runtime so it does |
| 4756 | // not matter. If it matters, there is enough info to get the |
| 4757 | // bitfield right! |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4758 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4759 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
| 4760 | } |
| 4761 | // Return null for empty list. |
| 4762 | if (Ivars.empty()) |
| 4763 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4764 | std::vector<llvm::Constant*> Values(3); |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4765 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4766 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4767 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 4768 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
| 4769 | Ivars.size()); |
| 4770 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
| 4771 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4772 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 4773 | llvm::GlobalVariable *GV = |
| 4774 | new llvm::GlobalVariable(Init->getType(), false, |
| 4775 | llvm::GlobalValue::InternalLinkage, |
| 4776 | Init, |
| 4777 | Prefix + OID->getNameAsString(), |
| 4778 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4779 | GV->setAlignment( |
| 4780 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4781 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4782 | |
| 4783 | UsedGlobals.push_back(GV); |
| 4784 | return llvm::ConstantExpr::getBitCast(GV, |
| 4785 | ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4786 | } |
| 4787 | |
| 4788 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
| 4789 | const ObjCProtocolDecl *PD) { |
| 4790 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4791 | |
| 4792 | if (!Entry) { |
| 4793 | // We use the initializer as a marker of whether this is a forward |
| 4794 | // reference or not. At module finalization we add the empty |
| 4795 | // contents for protocols which were referenced but never defined. |
| 4796 | Entry = |
| 4797 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
| 4798 | llvm::GlobalValue::ExternalLinkage, |
| 4799 | 0, |
| 4800 | "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(), |
| 4801 | &CGM.getModule()); |
| 4802 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 4803 | UsedGlobals.push_back(Entry); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4804 | } |
| 4805 | |
| 4806 | return Entry; |
| 4807 | } |
| 4808 | |
| 4809 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 4810 | /// @code |
| 4811 | /// struct _protocol_t { |
| 4812 | /// id isa; // NULL |
| 4813 | /// const char * const protocol_name; |
| 4814 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 4815 | /// const struct method_list_t * const instance_methods; |
| 4816 | /// const struct method_list_t * const class_methods; |
| 4817 | /// const struct method_list_t *optionalInstanceMethods; |
| 4818 | /// const struct method_list_t *optionalClassMethods; |
| 4819 | /// const struct _prop_list_t * properties; |
| 4820 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 4821 | /// const uint32_t flags; // = 0 |
| 4822 | /// } |
| 4823 | /// @endcode |
| 4824 | /// |
| 4825 | |
| 4826 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
| 4827 | const ObjCProtocolDecl *PD) { |
| 4828 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4829 | |
| 4830 | // Early exit if a defining object has already been generated. |
| 4831 | if (Entry && Entry->hasInitializer()) |
| 4832 | return Entry; |
| 4833 | |
| 4834 | const char *ProtocolName = PD->getNameAsCString(); |
| 4835 | |
| 4836 | // Construct method lists. |
| 4837 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 4838 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4839 | for (ObjCProtocolDecl::instmeth_iterator |
| 4840 | i = PD->instmeth_begin(CGM.getContext()), |
| 4841 | e = PD->instmeth_end(CGM.getContext()); |
| 4842 | i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4843 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4844 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4845 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4846 | OptInstanceMethods.push_back(C); |
| 4847 | } else { |
| 4848 | InstanceMethods.push_back(C); |
| 4849 | } |
| 4850 | } |
| 4851 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4852 | for (ObjCProtocolDecl::classmeth_iterator |
| 4853 | i = PD->classmeth_begin(CGM.getContext()), |
| 4854 | e = PD->classmeth_end(CGM.getContext()); |
| 4855 | i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4856 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4857 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4858 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4859 | OptClassMethods.push_back(C); |
| 4860 | } else { |
| 4861 | ClassMethods.push_back(C); |
| 4862 | } |
| 4863 | } |
| 4864 | |
| 4865 | std::vector<llvm::Constant*> Values(10); |
| 4866 | // isa is NULL |
| 4867 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
| 4868 | Values[1] = GetClassName(PD->getIdentifier()); |
| 4869 | Values[2] = EmitProtocolList( |
| 4870 | "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(), |
| 4871 | PD->protocol_begin(), |
| 4872 | PD->protocol_end()); |
| 4873 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4874 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4875 | + PD->getNameAsString(), |
| 4876 | "__DATA, __objc_const", |
| 4877 | InstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4878 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4879 | + PD->getNameAsString(), |
| 4880 | "__DATA, __objc_const", |
| 4881 | ClassMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4882 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4883 | + PD->getNameAsString(), |
| 4884 | "__DATA, __objc_const", |
| 4885 | OptInstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4886 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4887 | + PD->getNameAsString(), |
| 4888 | "__DATA, __objc_const", |
| 4889 | OptClassMethods); |
| 4890 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(), |
| 4891 | 0, PD, ObjCTypes); |
| 4892 | uint32_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4893 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4894 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4895 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
| 4896 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
| 4897 | Values); |
| 4898 | |
| 4899 | if (Entry) { |
| 4900 | // Already created, fix the linkage and update the initializer. |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4901 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4902 | Entry->setInitializer(Init); |
| 4903 | } else { |
| 4904 | Entry = |
| 4905 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4906 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4907 | Init, |
| 4908 | std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName, |
| 4909 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4910 | Entry->setAlignment( |
| 4911 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4912 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4913 | } |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4914 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4915 | |
| 4916 | // Use this protocol meta-data to build protocol list table in section |
| 4917 | // __DATA, __objc_protolist |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4918 | llvm::GlobalVariable *PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4919 | ObjCTypes.ProtocolnfABIPtrTy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4920 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4921 | Entry, |
| 4922 | std::string("\01l_OBJC_LABEL_PROTOCOL_$_") |
| 4923 | +ProtocolName, |
| 4924 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4925 | PTGV->setAlignment( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4926 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 4927 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4928 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4929 | UsedGlobals.push_back(PTGV); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4930 | return Entry; |
| 4931 | } |
| 4932 | |
| 4933 | /// EmitProtocolList - Generate protocol list meta-data: |
| 4934 | /// @code |
| 4935 | /// struct _protocol_list_t { |
| 4936 | /// long protocol_count; // Note, this is 32/64 bit |
| 4937 | /// struct _protocol_t[protocol_count]; |
| 4938 | /// } |
| 4939 | /// @endcode |
| 4940 | /// |
| 4941 | llvm::Constant * |
| 4942 | CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name, |
| 4943 | ObjCProtocolDecl::protocol_iterator begin, |
| 4944 | ObjCProtocolDecl::protocol_iterator end) { |
| 4945 | std::vector<llvm::Constant*> ProtocolRefs; |
| 4946 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4947 | // Just return null for empty protocol lists |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4948 | if (begin == end) |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4949 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4950 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4951 | // FIXME: We shouldn't need to do this lookup here, should we? |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4952 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
| 4953 | if (GV) |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4954 | return llvm::ConstantExpr::getBitCast(GV, |
| 4955 | ObjCTypes.ProtocolListnfABIPtrTy); |
| 4956 | |
| 4957 | for (; begin != end; ++begin) |
| 4958 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 4959 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4960 | // This list is null terminated. |
| 4961 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4962 | ObjCTypes.ProtocolnfABIPtrTy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4963 | |
| 4964 | std::vector<llvm::Constant*> Values(2); |
| 4965 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 4966 | Values[1] = |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4967 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4968 | ProtocolRefs.size()), |
| 4969 | ProtocolRefs); |
| 4970 | |
| 4971 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4972 | GV = new llvm::GlobalVariable(Init->getType(), false, |
| 4973 | llvm::GlobalValue::InternalLinkage, |
| 4974 | Init, |
| 4975 | Name, |
| 4976 | &CGM.getModule()); |
| 4977 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4978 | GV->setAlignment( |
| 4979 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4980 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4981 | return llvm::ConstantExpr::getBitCast(GV, |
| 4982 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4983 | } |
| 4984 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4985 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 4986 | /// struct _objc_method { |
| 4987 | /// SEL _cmd; |
| 4988 | /// char *method_type; |
| 4989 | /// char *_imp; |
| 4990 | /// } |
| 4991 | |
| 4992 | llvm::Constant * |
| 4993 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 4994 | std::vector<llvm::Constant*> Desc(3); |
| 4995 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4996 | ObjCTypes.SelectorPtrTy); |
| 4997 | Desc[1] = GetMethodVarType(MD); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4998 | // Protocol methods have no implementation. So, this entry is always NULL. |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4999 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 5000 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
| 5001 | } |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5002 | |
| 5003 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 5004 | /// This code gen. amounts to generating code for: |
| 5005 | /// @code |
| 5006 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 5007 | /// @encode |
| 5008 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 5009 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5010 | CodeGen::CodeGenFunction &CGF, |
| 5011 | QualType ObjectTy, |
| 5012 | llvm::Value *BaseValue, |
| 5013 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5014 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 5015 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 5016 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 5017 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5018 | } |
| 5019 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 5020 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
| 5021 | CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 5022 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 5023 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 5024 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar), |
| 5025 | false, "ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 5026 | } |
| 5027 | |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5028 | CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend( |
| 5029 | CodeGen::CodeGenFunction &CGF, |
| 5030 | QualType ResultType, |
| 5031 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5032 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5033 | QualType Arg0Ty, |
| 5034 | bool IsSuper, |
| 5035 | const CallArgList &CallArgs) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame^] | 5036 | // FIXME. Even though IsSuper is passes. This function doese not handle calls |
| 5037 | // to 'super' receivers. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5038 | CodeGenTypes &Types = CGM.getTypes(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5039 | llvm::Value *Arg0 = Receiver; |
| 5040 | if (!IsSuper) |
| 5041 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5042 | |
| 5043 | // Find the message function name. |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame^] | 5044 | // FIXME. This is too much work to get the ABI-specific result type needed to |
| 5045 | // find the message name. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5046 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, |
| 5047 | llvm::SmallVector<QualType, 16>()); |
Fariborz Jahanian | 70b51c7 | 2009-04-30 23:08:58 +0000 | [diff] [blame] | 5048 | llvm::Constant *Fn = 0; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5049 | std::string Name("\01l_"); |
| 5050 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5051 | #if 0 |
| 5052 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5053 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5054 | Fn = ObjCTypes.getMessageSendIdStretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5055 | // FIXME. Is there a better way of getting these names. |
| 5056 | // They are available in RuntimeFunctions vector pair. |
| 5057 | Name += "objc_msgSendId_stret_fixup"; |
| 5058 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5059 | else |
| 5060 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5061 | if (IsSuper) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5062 | Fn = ObjCTypes.getMessageSendSuper2StretFixupFn(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5063 | Name += "objc_msgSendSuper2_stret_fixup"; |
| 5064 | } |
| 5065 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5066 | { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5067 | Fn = ObjCTypes.getMessageSendStretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5068 | Name += "objc_msgSend_stret_fixup"; |
| 5069 | } |
| 5070 | } |
Fariborz Jahanian | 5b2bad0 | 2009-04-30 16:31:11 +0000 | [diff] [blame] | 5071 | else if (!IsSuper && ResultType->isFloatingType()) { |
| 5072 | if (const BuiltinType *BT = ResultType->getAsBuiltinType()) { |
| 5073 | BuiltinType::Kind k = BT->getKind(); |
| 5074 | if (k == BuiltinType::LongDouble) { |
| 5075 | Fn = ObjCTypes.getMessageSendFpretFixupFn(); |
| 5076 | Name += "objc_msgSend_fpret_fixup"; |
| 5077 | } |
| 5078 | else { |
| 5079 | Fn = ObjCTypes.getMessageSendFixupFn(); |
| 5080 | Name += "objc_msgSend_fixup"; |
| 5081 | } |
| 5082 | } |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5083 | } |
| 5084 | else { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5085 | #if 0 |
| 5086 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5087 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5088 | Fn = ObjCTypes.getMessageSendIdFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5089 | Name += "objc_msgSendId_fixup"; |
| 5090 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5091 | else |
| 5092 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5093 | if (IsSuper) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5094 | Fn = ObjCTypes.getMessageSendSuper2FixupFn(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5095 | Name += "objc_msgSendSuper2_fixup"; |
| 5096 | } |
| 5097 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5098 | { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5099 | Fn = ObjCTypes.getMessageSendFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5100 | Name += "objc_msgSend_fixup"; |
| 5101 | } |
| 5102 | } |
Fariborz Jahanian | 70b51c7 | 2009-04-30 23:08:58 +0000 | [diff] [blame] | 5103 | assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5104 | Name += '_'; |
| 5105 | std::string SelName(Sel.getAsString()); |
| 5106 | // Replace all ':' in selector name with '_' ouch! |
| 5107 | for(unsigned i = 0; i < SelName.size(); i++) |
| 5108 | if (SelName[i] == ':') |
| 5109 | SelName[i] = '_'; |
| 5110 | Name += SelName; |
| 5111 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5112 | if (!GV) { |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5113 | // Build message ref table entry. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5114 | std::vector<llvm::Constant*> Values(2); |
| 5115 | Values[0] = Fn; |
| 5116 | Values[1] = GetMethodVarName(Sel); |
| 5117 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 5118 | GV = new llvm::GlobalVariable(Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 5119 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5120 | Init, |
| 5121 | Name, |
| 5122 | &CGM.getModule()); |
| 5123 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | f59c1a6 | 2009-04-15 19:04:46 +0000 | [diff] [blame] | 5124 | GV->setAlignment(16); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5125 | GV->setSection("__DATA, __objc_msgrefs, coalesced"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5126 | } |
| 5127 | llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5128 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5129 | CallArgList ActualArgs; |
| 5130 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 5131 | ActualArgs.push_back(std::make_pair(RValue::get(Arg1), |
| 5132 | ObjCTypes.MessageRefCPtrTy)); |
| 5133 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5134 | const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs); |
| 5135 | llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0); |
| 5136 | Callee = CGF.Builder.CreateLoad(Callee); |
Fariborz Jahanian | 3ab75bd | 2009-02-14 21:25:36 +0000 | [diff] [blame] | 5137 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5138 | Callee = CGF.Builder.CreateBitCast(Callee, |
| 5139 | llvm::PointerType::getUnqual(FTy)); |
| 5140 | return CGF.EmitCall(FnInfo1, Callee, ActualArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5141 | } |
| 5142 | |
| 5143 | /// Generate code for a message send expression in the nonfragile abi. |
| 5144 | CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend( |
| 5145 | CodeGen::CodeGenFunction &CGF, |
| 5146 | QualType ResultType, |
| 5147 | Selector Sel, |
| 5148 | llvm::Value *Receiver, |
| 5149 | bool IsClassMessage, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 5150 | const CallArgList &CallArgs, |
| 5151 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5152 | return LegacyDispatchedSelector(Sel) |
| 5153 | ? EmitLegacyMessageSend(CGF, ResultType, EmitSelector(CGF.Builder, Sel), |
| 5154 | Receiver, CGF.getContext().getObjCIdType(), |
| 5155 | false, CallArgs, ObjCTypes) |
| 5156 | : EmitMessageSend(CGF, ResultType, Sel, |
| 5157 | Receiver, CGF.getContext().getObjCIdType(), |
| 5158 | false, CallArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5159 | } |
| 5160 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5161 | llvm::GlobalVariable * |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5162 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5163 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5164 | |
Daniel Dunbar | dfff230 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 5165 | if (!GV) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5166 | GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 5167 | llvm::GlobalValue::ExternalLinkage, |
| 5168 | 0, Name, &CGM.getModule()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5169 | } |
| 5170 | |
| 5171 | return GV; |
| 5172 | } |
| 5173 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5174 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5175 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5176 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 5177 | |
| 5178 | if (!Entry) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5179 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5180 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5181 | Entry = |
| 5182 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5183 | llvm::GlobalValue::InternalLinkage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5184 | ClassGV, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5185 | "\01L_OBJC_CLASSLIST_REFERENCES_$_", |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5186 | &CGM.getModule()); |
| 5187 | Entry->setAlignment( |
| 5188 | CGM.getTargetData().getPrefTypeAlignment( |
| 5189 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5190 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
| 5191 | UsedGlobals.push_back(Entry); |
| 5192 | } |
| 5193 | |
| 5194 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5195 | } |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5196 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5197 | llvm::Value * |
| 5198 | CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder, |
| 5199 | const ObjCInterfaceDecl *ID) { |
| 5200 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
| 5201 | |
| 5202 | if (!Entry) { |
| 5203 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5204 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
| 5205 | Entry = |
| 5206 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5207 | llvm::GlobalValue::InternalLinkage, |
| 5208 | ClassGV, |
| 5209 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 5210 | &CGM.getModule()); |
| 5211 | Entry->setAlignment( |
| 5212 | CGM.getTargetData().getPrefTypeAlignment( |
| 5213 | ObjCTypes.ClassnfABIPtrTy)); |
| 5214 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5215 | UsedGlobals.push_back(Entry); |
| 5216 | } |
| 5217 | |
| 5218 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5219 | } |
| 5220 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5221 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 5222 | /// meta-data |
| 5223 | /// |
| 5224 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 5225 | const ObjCInterfaceDecl *ID) { |
| 5226 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 5227 | if (Entry) |
| 5228 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5229 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5230 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5231 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5232 | Entry = |
| 5233 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5234 | llvm::GlobalValue::InternalLinkage, |
| 5235 | MetaClassGV, |
| 5236 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 5237 | &CGM.getModule()); |
| 5238 | Entry->setAlignment( |
| 5239 | CGM.getTargetData().getPrefTypeAlignment( |
| 5240 | ObjCTypes.ClassnfABIPtrTy)); |
| 5241 | |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5242 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5243 | UsedGlobals.push_back(Entry); |
| 5244 | |
| 5245 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5246 | } |
| 5247 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5248 | /// GetClass - Return a reference to the class for the given interface |
| 5249 | /// decl. |
| 5250 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 5251 | const ObjCInterfaceDecl *ID) { |
| 5252 | return EmitClassRef(Builder, ID); |
| 5253 | } |
| 5254 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5255 | /// Generates a message send where the super is the receiver. This is |
| 5256 | /// a message send to self with special delivery semantics indicating |
| 5257 | /// which class's method should be called. |
| 5258 | CodeGen::RValue |
| 5259 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 5260 | QualType ResultType, |
| 5261 | Selector Sel, |
| 5262 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5263 | bool isCategoryImpl, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5264 | llvm::Value *Receiver, |
| 5265 | bool IsClassMessage, |
| 5266 | const CodeGen::CallArgList &CallArgs) { |
| 5267 | // ... |
| 5268 | // Create and init a super structure; this is a (receiver, class) |
| 5269 | // pair we will pass to objc_msgSendSuper. |
| 5270 | llvm::Value *ObjCSuper = |
| 5271 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 5272 | |
| 5273 | llvm::Value *ReceiverAsObject = |
| 5274 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 5275 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 5276 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
| 5277 | |
| 5278 | // 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] | 5279 | llvm::Value *Target; |
| 5280 | if (IsClassMessage) { |
| 5281 | if (isCategoryImpl) { |
| 5282 | // Message sent to "super' in a class method defined in |
| 5283 | // a category implementation. |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5284 | Target = EmitClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5285 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 5286 | Target = CGF.Builder.CreateLoad(Target); |
| 5287 | } |
| 5288 | else |
| 5289 | Target = EmitMetaClassRef(CGF.Builder, Class); |
| 5290 | } |
| 5291 | else |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5292 | Target = EmitSuperClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5293 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame^] | 5294 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 5295 | // ObjCTypes types. |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5296 | const llvm::Type *ClassTy = |
| 5297 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 5298 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 5299 | CGF.Builder.CreateStore(Target, |
| 5300 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 5301 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5302 | return (LegacyDispatchedSelector(Sel)) |
| 5303 | ? EmitLegacyMessageSend(CGF, ResultType,EmitSelector(CGF.Builder, Sel), |
| 5304 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 5305 | true, CallArgs, |
| 5306 | ObjCTypes) |
| 5307 | : EmitMessageSend(CGF, ResultType, Sel, |
| 5308 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 5309 | true, CallArgs); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5310 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5311 | |
| 5312 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
| 5313 | Selector Sel) { |
| 5314 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 5315 | |
| 5316 | if (!Entry) { |
| 5317 | llvm::Constant *Casted = |
| 5318 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 5319 | ObjCTypes.SelectorPtrTy); |
| 5320 | Entry = |
| 5321 | new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false, |
| 5322 | llvm::GlobalValue::InternalLinkage, |
| 5323 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_", |
| 5324 | &CGM.getModule()); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5325 | Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip"); |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5326 | UsedGlobals.push_back(Entry); |
| 5327 | } |
| 5328 | |
| 5329 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5330 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5331 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 5332 | /// objc_assign_ivar (id src, id *dst) |
| 5333 | /// |
| 5334 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 5335 | llvm::Value *src, llvm::Value *dst) |
| 5336 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5337 | const llvm::Type * SrcTy = src->getType(); |
| 5338 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5339 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5340 | assert(Size <= 8 && "does not support size > 8"); |
| 5341 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5342 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5343 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5344 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5345 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5346 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5347 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5348 | src, dst, "assignivar"); |
| 5349 | return; |
| 5350 | } |
| 5351 | |
| 5352 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 5353 | /// objc_assign_strongCast (id src, id *dst) |
| 5354 | /// |
| 5355 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
| 5356 | CodeGen::CodeGenFunction &CGF, |
| 5357 | llvm::Value *src, llvm::Value *dst) |
| 5358 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5359 | const llvm::Type * SrcTy = src->getType(); |
| 5360 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5361 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5362 | assert(Size <= 8 && "does not support size > 8"); |
| 5363 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5364 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5365 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5366 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5367 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5368 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5369 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5370 | src, dst, "weakassign"); |
| 5371 | return; |
| 5372 | } |
| 5373 | |
| 5374 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 5375 | /// object: objc_read_weak (id *src) |
| 5376 | /// |
| 5377 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
| 5378 | CodeGen::CodeGenFunction &CGF, |
| 5379 | llvm::Value *AddrWeakObj) |
| 5380 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5381 | const llvm::Type* DestTy = |
| 5382 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5383 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 5384 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5385 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5386 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5387 | return read_weak; |
| 5388 | } |
| 5389 | |
| 5390 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 5391 | /// objc_assign_weak (id src, id *dst) |
| 5392 | /// |
| 5393 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 5394 | llvm::Value *src, llvm::Value *dst) |
| 5395 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5396 | const llvm::Type * SrcTy = src->getType(); |
| 5397 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5398 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5399 | assert(Size <= 8 && "does not support size > 8"); |
| 5400 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5401 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5402 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5403 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5404 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5405 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 5406 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5407 | src, dst, "weakassign"); |
| 5408 | return; |
| 5409 | } |
| 5410 | |
| 5411 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 5412 | /// objc_assign_global (id src, id *dst) |
| 5413 | /// |
| 5414 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 5415 | llvm::Value *src, llvm::Value *dst) |
| 5416 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5417 | const llvm::Type * SrcTy = src->getType(); |
| 5418 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5419 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5420 | assert(Size <= 8 && "does not support size > 8"); |
| 5421 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5422 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5423 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5424 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5425 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5426 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5427 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5428 | src, dst, "globalassign"); |
| 5429 | return; |
| 5430 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5431 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5432 | void |
| 5433 | CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 5434 | const Stmt &S) { |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5435 | bool isTry = isa<ObjCAtTryStmt>(S); |
| 5436 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 5437 | llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5438 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5439 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5440 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5441 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
| 5442 | |
| 5443 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 5444 | // evaluation of the expression must occur before we enter the |
| 5445 | // @synchronized. We can safely avoid a temp here because jumps into |
| 5446 | // @synchronized are illegal & this will dominate uses. |
| 5447 | llvm::Value *SyncArg = 0; |
| 5448 | if (!isTry) { |
| 5449 | SyncArg = |
| 5450 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 5451 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5452 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5453 | } |
| 5454 | |
| 5455 | // Push an EH context entry, used for handling rethrows and jumps |
| 5456 | // through finally. |
| 5457 | CGF.PushCleanupBlock(FinallyBlock); |
| 5458 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5459 | CGF.setInvokeDest(TryHandler); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5460 | |
| 5461 | CGF.EmitBlock(TryBlock); |
| 5462 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 5463 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
| 5464 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5465 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5466 | // Emit the exception handler. |
| 5467 | |
| 5468 | CGF.EmitBlock(TryHandler); |
| 5469 | |
| 5470 | llvm::Value *llvm_eh_exception = |
| 5471 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
| 5472 | llvm::Value *llvm_eh_selector_i64 = |
| 5473 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64); |
| 5474 | llvm::Value *llvm_eh_typeid_for_i64 = |
| 5475 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64); |
| 5476 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5477 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow"); |
| 5478 | |
| 5479 | llvm::SmallVector<llvm::Value*, 8> SelectorArgs; |
| 5480 | SelectorArgs.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5481 | SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5482 | |
| 5483 | // Construct the lists of (type, catch body) to handle. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5484 | llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5485 | bool HasCatchAll = false; |
| 5486 | if (isTry) { |
| 5487 | if (const ObjCAtCatchStmt* CatchStmt = |
| 5488 | cast<ObjCAtTryStmt>(S).getCatchStmts()) { |
| 5489 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5490 | const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5491 | Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody())); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5492 | |
| 5493 | // catch(...) always matches. |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5494 | if (!CatchDecl) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5495 | // Use i8* null here to signal this is a catch all, not a cleanup. |
| 5496 | llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 5497 | SelectorArgs.push_back(Null); |
| 5498 | HasCatchAll = true; |
| 5499 | break; |
| 5500 | } |
| 5501 | |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5502 | if (CGF.getContext().isObjCIdType(CatchDecl->getType()) || |
| 5503 | CatchDecl->getType()->isObjCQualifiedIdType()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5504 | llvm::Value *IDEHType = |
| 5505 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 5506 | if (!IDEHType) |
| 5507 | IDEHType = |
| 5508 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5509 | llvm::GlobalValue::ExternalLinkage, |
| 5510 | 0, "OBJC_EHTYPE_id", &CGM.getModule()); |
| 5511 | SelectorArgs.push_back(IDEHType); |
| 5512 | HasCatchAll = true; |
| 5513 | break; |
| 5514 | } |
| 5515 | |
| 5516 | // All other types should be Objective-C interface pointer types. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5517 | const PointerType *PT = CatchDecl->getType()->getAsPointerType(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5518 | assert(PT && "Invalid @catch type."); |
| 5519 | const ObjCInterfaceType *IT = |
| 5520 | PT->getPointeeType()->getAsObjCInterfaceType(); |
| 5521 | assert(IT && "Invalid @catch type."); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5522 | llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5523 | SelectorArgs.push_back(EHType); |
| 5524 | } |
| 5525 | } |
| 5526 | } |
| 5527 | |
| 5528 | // We use a cleanup unless there was already a catch all. |
| 5529 | if (!HasCatchAll) { |
| 5530 | SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5531 | Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5532 | } |
| 5533 | |
| 5534 | llvm::Value *Selector = |
| 5535 | CGF.Builder.CreateCall(llvm_eh_selector_i64, |
| 5536 | SelectorArgs.begin(), SelectorArgs.end(), |
| 5537 | "selector"); |
| 5538 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5539 | const ParmVarDecl *CatchParam = Handlers[i].first; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5540 | const Stmt *CatchBody = Handlers[i].second; |
| 5541 | |
| 5542 | llvm::BasicBlock *Next = 0; |
| 5543 | |
| 5544 | // The last handler always matches. |
| 5545 | if (i + 1 != e) { |
| 5546 | assert(CatchParam && "Only last handler can be a catch all."); |
| 5547 | |
| 5548 | llvm::BasicBlock *Match = CGF.createBasicBlock("match"); |
| 5549 | Next = CGF.createBasicBlock("catch.next"); |
| 5550 | llvm::Value *Id = |
| 5551 | CGF.Builder.CreateCall(llvm_eh_typeid_for_i64, |
| 5552 | CGF.Builder.CreateBitCast(SelectorArgs[i+2], |
| 5553 | ObjCTypes.Int8PtrTy)); |
| 5554 | CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id), |
| 5555 | Match, Next); |
| 5556 | |
| 5557 | CGF.EmitBlock(Match); |
| 5558 | } |
| 5559 | |
| 5560 | if (CatchBody) { |
| 5561 | llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end"); |
| 5562 | llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler"); |
| 5563 | |
| 5564 | // Cleanups must call objc_end_catch. |
| 5565 | // |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame^] | 5566 | // FIXME: It seems incorrect for objc_begin_catch to be inside this |
| 5567 | // context, but this matches gcc. |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5568 | CGF.PushCleanupBlock(MatchEnd); |
| 5569 | CGF.setInvokeDest(MatchHandler); |
| 5570 | |
| 5571 | llvm::Value *ExcObject = |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5572 | CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5573 | |
| 5574 | // Bind the catch parameter if it exists. |
| 5575 | if (CatchParam) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5576 | ExcObject = |
| 5577 | CGF.Builder.CreateBitCast(ExcObject, |
| 5578 | CGF.ConvertType(CatchParam->getType())); |
| 5579 | // CatchParam is a ParmVarDecl because of the grammar |
| 5580 | // construction used to handle this, but for codegen purposes |
| 5581 | // we treat this as a local decl. |
| 5582 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
| 5583 | CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5584 | } |
| 5585 | |
| 5586 | CGF.ObjCEHValueStack.push_back(ExcObject); |
| 5587 | CGF.EmitStmt(CatchBody); |
| 5588 | CGF.ObjCEHValueStack.pop_back(); |
| 5589 | |
| 5590 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5591 | |
| 5592 | CGF.EmitBlock(MatchHandler); |
| 5593 | |
| 5594 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5595 | // We are required to emit this call to satisfy LLVM, even |
| 5596 | // though we don't use the result. |
| 5597 | llvm::SmallVector<llvm::Value*, 8> Args; |
| 5598 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5599 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5600 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5601 | 0)); |
| 5602 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5603 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5604 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5605 | |
| 5606 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5607 | |
| 5608 | CGF.EmitBlock(MatchEnd); |
| 5609 | |
| 5610 | // Unfortunately, we also have to generate another EH frame here |
| 5611 | // in case this throws. |
| 5612 | llvm::BasicBlock *MatchEndHandler = |
| 5613 | CGF.createBasicBlock("match.end.handler"); |
| 5614 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5615 | CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5616 | Cont, MatchEndHandler, |
| 5617 | Args.begin(), Args.begin()); |
| 5618 | |
| 5619 | CGF.EmitBlock(Cont); |
| 5620 | if (Info.SwitchBlock) |
| 5621 | CGF.EmitBlock(Info.SwitchBlock); |
| 5622 | if (Info.EndBlock) |
| 5623 | CGF.EmitBlock(Info.EndBlock); |
| 5624 | |
| 5625 | CGF.EmitBlock(MatchEndHandler); |
| 5626 | Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5627 | // We are required to emit this call to satisfy LLVM, even |
| 5628 | // though we don't use the result. |
| 5629 | Args.clear(); |
| 5630 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5631 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5632 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5633 | 0)); |
| 5634 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5635 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5636 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5637 | |
| 5638 | if (Next) |
| 5639 | CGF.EmitBlock(Next); |
| 5640 | } else { |
| 5641 | assert(!Next && "catchup should be last handler."); |
| 5642 | |
| 5643 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5644 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5645 | } |
| 5646 | } |
| 5647 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5648 | // Pop the cleanup entry, the @finally is outside this cleanup |
| 5649 | // scope. |
| 5650 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5651 | CGF.setInvokeDest(PrevLandingPad); |
| 5652 | |
| 5653 | CGF.EmitBlock(FinallyBlock); |
| 5654 | |
| 5655 | if (isTry) { |
| 5656 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 5657 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 5658 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 5659 | } else { |
| 5660 | // Emit 'objc_sync_exit(expr)' as finally's sole statement for |
| 5661 | // @synchronized. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5662 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5663 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5664 | |
| 5665 | if (Info.SwitchBlock) |
| 5666 | CGF.EmitBlock(Info.SwitchBlock); |
| 5667 | if (Info.EndBlock) |
| 5668 | CGF.EmitBlock(Info.EndBlock); |
| 5669 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5670 | // Branch around the rethrow code. |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5671 | CGF.EmitBranch(FinallyEnd); |
| 5672 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5673 | CGF.EmitBlock(FinallyRethrow); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5674 | CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5675 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5676 | CGF.Builder.CreateUnreachable(); |
| 5677 | |
| 5678 | CGF.EmitBlock(FinallyEnd); |
| 5679 | } |
| 5680 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5681 | /// EmitThrowStmt - Generate code for a throw statement. |
| 5682 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 5683 | const ObjCAtThrowStmt &S) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5684 | llvm::Value *Exception; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5685 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5686 | Exception = CGF.EmitScalarExpr(ThrowExpr); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5687 | } else { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5688 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
| 5689 | "Unexpected rethrow outside @catch block."); |
| 5690 | Exception = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5691 | } |
| 5692 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5693 | llvm::Value *ExceptionAsObject = |
| 5694 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 5695 | llvm::BasicBlock *InvokeDest = CGF.getInvokeDest(); |
| 5696 | if (InvokeDest) { |
| 5697 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5698 | CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5699 | Cont, InvokeDest, |
| 5700 | &ExceptionAsObject, &ExceptionAsObject + 1); |
| 5701 | CGF.EmitBlock(Cont); |
| 5702 | } else |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5703 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5704 | CGF.Builder.CreateUnreachable(); |
| 5705 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5706 | // Clear the insertion point to indicate we are in unreachable code. |
| 5707 | CGF.Builder.ClearInsertionPoint(); |
| 5708 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5709 | |
| 5710 | llvm::Value * |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5711 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 5712 | bool ForDefinition) { |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5713 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5714 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5715 | // If we don't need a definition, return the entry if found or check |
| 5716 | // if we use an external reference. |
| 5717 | if (!ForDefinition) { |
| 5718 | if (Entry) |
| 5719 | return Entry; |
Daniel Dunbar | 7e075cb | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 5720 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5721 | // If this type (or a super class) has the __objc_exception__ |
| 5722 | // attribute, emit an external reference. |
| 5723 | if (hasObjCExceptionAttribute(ID)) |
| 5724 | return Entry = |
| 5725 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5726 | llvm::GlobalValue::ExternalLinkage, |
| 5727 | 0, |
| 5728 | (std::string("OBJC_EHTYPE_$_") + |
| 5729 | ID->getIdentifier()->getName()), |
| 5730 | &CGM.getModule()); |
| 5731 | } |
| 5732 | |
| 5733 | // Otherwise we need to either make a new entry or fill in the |
| 5734 | // initializer. |
| 5735 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5736 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5737 | std::string VTableName = "objc_ehtype_vtable"; |
| 5738 | llvm::GlobalVariable *VTableGV = |
| 5739 | CGM.getModule().getGlobalVariable(VTableName); |
| 5740 | if (!VTableGV) |
| 5741 | VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false, |
| 5742 | llvm::GlobalValue::ExternalLinkage, |
| 5743 | 0, VTableName, &CGM.getModule()); |
| 5744 | |
| 5745 | llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2); |
| 5746 | |
| 5747 | std::vector<llvm::Constant*> Values(3); |
| 5748 | Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1); |
| 5749 | Values[1] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5750 | Values[2] = GetClassGlobal(ClassName); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5751 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
| 5752 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5753 | if (Entry) { |
| 5754 | Entry->setInitializer(Init); |
| 5755 | } else { |
| 5756 | Entry = new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5757 | llvm::GlobalValue::WeakAnyLinkage, |
| 5758 | Init, |
| 5759 | (std::string("OBJC_EHTYPE_$_") + |
| 5760 | ID->getIdentifier()->getName()), |
| 5761 | &CGM.getModule()); |
| 5762 | } |
| 5763 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 5764 | if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden) |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5765 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5766 | Entry->setAlignment(8); |
| 5767 | |
| 5768 | if (ForDefinition) { |
| 5769 | Entry->setSection("__DATA,__objc_const"); |
| 5770 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 5771 | } else { |
| 5772 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 5773 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5774 | |
| 5775 | return Entry; |
| 5776 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5777 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5778 | /* *** */ |
| 5779 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5780 | CodeGen::CGObjCRuntime * |
| 5781 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 5782 | return new CGObjCMac(CGM); |
| 5783 | } |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5784 | |
| 5785 | CodeGen::CGObjCRuntime * |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5786 | CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5787 | return new CGObjCNonFragileABIMac(CGM); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5788 | } |