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 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 143 | // FIXME: We should find a nicer way to make the labels for |
| 144 | // metadata, string concatenation is lame. |
| 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; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 813 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 814 | /// DefinedCategories - List of defined categories. |
| 815 | std::vector<llvm::GlobalValue*> DefinedCategories; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 816 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 817 | /// UsedGlobals - List of globals to pack into the llvm.used metadata |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 818 | /// to prevent them from being clobbered. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 819 | std::vector<llvm::GlobalVariable*> UsedGlobals; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 820 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 821 | /// GetNameForMethod - Return a name for the given method. |
| 822 | /// \param[out] NameOut - The return value. |
| 823 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 824 | const ObjCContainerDecl *CD, |
| 825 | std::string &NameOut); |
| 826 | |
| 827 | /// GetMethodVarName - Return a unique constant for the given |
| 828 | /// selector's name. The return value has type char *. |
| 829 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 830 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
| 831 | llvm::Constant *GetMethodVarName(const std::string &Name); |
| 832 | |
| 833 | /// GetMethodVarType - Return a unique constant for the given |
| 834 | /// selector's name. The return value has type char *. |
| 835 | |
| 836 | // FIXME: This is a horrible name. |
| 837 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 838 | llvm::Constant *GetMethodVarType(const FieldDecl *D); |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 839 | |
| 840 | /// GetPropertyName - Return a unique constant for the given |
| 841 | /// name. The return value has type char *. |
| 842 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
| 843 | |
| 844 | // FIXME: This can be dropped once string functions are unified. |
| 845 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 846 | const Decl *Container); |
| 847 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 848 | /// GetClassName - Return a unique constant for the given selector's |
| 849 | /// name. The return value has type char *. |
| 850 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
| 851 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 852 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 853 | /// implementation for the __strong or __weak case. |
| 854 | /// |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 855 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
| 856 | bool ForStrongLayout); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 857 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 858 | void BuildAggrIvarRecordLayout(const RecordType *RT, |
| 859 | unsigned int BytePos, bool ForStrongLayout, |
| 860 | bool &HasUnion); |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 861 | void BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 862 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 863 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 864 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 865 | unsigned int BytePos, bool ForStrongLayout, |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 866 | bool &HasUnion); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 867 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 868 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 869 | /// ivar layout bitmap. |
| 870 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 871 | const ObjCCommonTypesHelper &ObjCTypes); |
| 872 | |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 873 | /// EmitPropertyList - Emit the given property list. The return |
| 874 | /// value has type PropertyListPtrTy. |
| 875 | llvm::Constant *EmitPropertyList(const std::string &Name, |
| 876 | const Decl *Container, |
| 877 | const ObjCContainerDecl *OCD, |
| 878 | const ObjCCommonTypesHelper &ObjCTypes); |
| 879 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 880 | /// GetProtocolRef - Return a reference to the internal protocol |
| 881 | /// description, creating an empty one if it has not been |
| 882 | /// defined. The return value has type ProtocolPtrTy. |
| 883 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 884 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 885 | /// CreateMetadataVar - Create a global variable with internal |
| 886 | /// linkage for use by the Objective-C runtime. |
| 887 | /// |
| 888 | /// This is a convenience wrapper which not only creates the |
| 889 | /// variable, but also sets the section and alignment and adds the |
| 890 | /// global to the UsedGlobals list. |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 891 | /// |
| 892 | /// \param Name - The variable name. |
| 893 | /// \param Init - The variable initializer; this is also used to |
| 894 | /// define the type of the variable. |
| 895 | /// \param Section - The section the variable should go into, or 0. |
| 896 | /// \param Align - The alignment for the variable, or 0. |
| 897 | /// \param AddToUsed - Whether the variable should be added to |
Daniel Dunbar | c158306 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 898 | /// "llvm.used". |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 899 | llvm::GlobalVariable *CreateMetadataVar(const std::string &Name, |
| 900 | llvm::Constant *Init, |
| 901 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 902 | unsigned Align, |
| 903 | bool AddToUsed); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 904 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 905 | /// GetNamedIvarList - Return the list of ivars in the interface |
| 906 | /// itself (not including super classes and not including unnamed |
| 907 | /// bitfields). |
| 908 | /// |
| 909 | /// For the non-fragile ABI, this also includes synthesized property |
| 910 | /// ivars. |
| 911 | void GetNamedIvarList(const ObjCInterfaceDecl *OID, |
| 912 | llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const; |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 913 | |
| 914 | CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF, |
| 915 | QualType ResultType, |
| 916 | llvm::Value *Sel, |
| 917 | llvm::Value *Arg0, |
| 918 | QualType Arg0Ty, |
| 919 | bool IsSuper, |
| 920 | const CallArgList &CallArgs, |
| 921 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 922 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 923 | public: |
| 924 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm) |
| 925 | { } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 926 | |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 927 | virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 928 | |
| 929 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 930 | const ObjCContainerDecl *CD=0); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 931 | |
| 932 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
| 933 | |
| 934 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 935 | /// declaration, emitting it if necessary. The return value has type |
| 936 | /// ProtocolPtrTy. |
| 937 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
| 938 | |
| 939 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 940 | /// object for the given declaration, emitting it if needed. These |
| 941 | /// forward references will be filled in with empty bodies if no |
| 942 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 943 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 944 | }; |
| 945 | |
| 946 | class CGObjCMac : public CGObjCCommonMac { |
| 947 | private: |
| 948 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 949 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 950 | /// level information. |
| 951 | void EmitImageInfo(); |
| 952 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 953 | /// EmitModuleInfo - Another marker encoding module level |
| 954 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 955 | void EmitModuleInfo(); |
| 956 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 957 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 958 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 959 | llvm::Constant *EmitModuleSymbols(); |
| 960 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 961 | /// FinishModule - Write out global data structures at the end of |
| 962 | /// processing a translation unit. |
| 963 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 964 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 965 | /// EmitClassExtension - Generate the class extension structure used |
| 966 | /// to store the weak ivar layout and properties. The return value |
| 967 | /// has type ClassExtensionPtrTy. |
| 968 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 969 | |
| 970 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 971 | /// for the given class. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 972 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 973 | const ObjCInterfaceDecl *ID); |
| 974 | |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 975 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 976 | QualType ResultType, |
| 977 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 978 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 979 | QualType Arg0Ty, |
| 980 | bool IsSuper, |
| 981 | const CallArgList &CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 982 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 983 | /// EmitIvarList - Emit the ivar list for the given |
| 984 | /// implementation. If ForClass is true the list of class ivars |
| 985 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 986 | /// interface ivars will be emitted. The return value has type |
| 987 | /// IvarListPtrTy. |
| 988 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 989 | bool ForClass); |
| 990 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 991 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 992 | /// for the metaclass of the given interface. The return value has |
| 993 | /// type ClassPtrTy. |
| 994 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 995 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 996 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 997 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 998 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 999 | llvm::Constant *Protocols, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1000 | const ConstantVector &Methods); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1001 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1002 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1003 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1004 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1005 | |
| 1006 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1007 | /// implementation. The return value has type MethodListPtrTy. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1008 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 1009 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1010 | const ConstantVector &Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1011 | |
| 1012 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1013 | /// method declarations. |
| 1014 | /// - TypeName: The name for the type containing the methods. |
| 1015 | /// - IsProtocol: True iff these methods are for a protocol. |
| 1016 | /// - ClassMethds: True iff these are class methods. |
| 1017 | /// - Required: When true, only "required" methods are |
| 1018 | /// listed. Similarly, when false only "optional" methods are |
| 1019 | /// listed. For classes this should always be true. |
| 1020 | /// - begin, end: The method list to output. |
| 1021 | /// |
| 1022 | /// The return value has type MethodDescriptionListPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1023 | llvm::Constant *EmitMethodDescList(const std::string &Name, |
| 1024 | const char *Section, |
| 1025 | const ConstantVector &Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1026 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1027 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1028 | /// declaration, emitting it if necessary. The return value has type |
| 1029 | /// ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1030 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1031 | |
| 1032 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1033 | /// object for the given declaration, emitting it if needed. These |
| 1034 | /// forward references will be filled in with empty bodies if no |
| 1035 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1036 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1037 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1038 | /// EmitProtocolExtension - Generate the protocol extension |
| 1039 | /// structure used to store optional instance and class methods, and |
| 1040 | /// protocol properties. The return value has type |
| 1041 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1042 | llvm::Constant * |
| 1043 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1044 | const ConstantVector &OptInstanceMethods, |
| 1045 | const ConstantVector &OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1046 | |
| 1047 | /// EmitProtocolList - Generate the list of referenced |
| 1048 | /// protocols. The return value has type ProtocolListPtrTy. |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1049 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 1050 | ObjCProtocolDecl::protocol_iterator begin, |
| 1051 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1052 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1053 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1054 | /// for the given selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1055 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1056 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1057 | public: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1058 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1059 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1060 | virtual llvm::Function *ModuleInitFunction(); |
| 1061 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1062 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1063 | QualType ResultType, |
| 1064 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1065 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1066 | bool IsClassMessage, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1067 | const CallArgList &CallArgs, |
| 1068 | const ObjCMethodDecl *Method); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1069 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1070 | virtual CodeGen::RValue |
| 1071 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1072 | QualType ResultType, |
| 1073 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1074 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1075 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1076 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1077 | bool IsClassMessage, |
| 1078 | const CallArgList &CallArgs); |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1079 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1080 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1081 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1082 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1083 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel); |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1084 | |
| 1085 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1086 | /// untyped one. |
| 1087 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1088 | const ObjCMethodDecl *Method); |
| 1089 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1090 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1091 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1092 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1093 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1094 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1095 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1096 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1097 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 1098 | virtual llvm::Constant *GetPropertySetFunction(); |
| 1099 | virtual llvm::Constant *EnumerationMutationFunction(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1100 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 1101 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1102 | const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1103 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 1104 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1105 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 1106 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1107 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 1108 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1109 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 1110 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 1111 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 1112 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1113 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 1114 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 1115 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1116 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1117 | QualType ObjectTy, |
| 1118 | llvm::Value *BaseValue, |
| 1119 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1120 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1121 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1122 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1123 | const ObjCIvarDecl *Ivar); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1124 | }; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1125 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1126 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1127 | private: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1128 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1129 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 1130 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1131 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1132 | /// SuperClassReferences - uniqued super class references. |
| 1133 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; |
| 1134 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1135 | /// MetaClassReferences - uniqued meta class references. |
| 1136 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1137 | |
| 1138 | /// EHTypeReferences - uniqued class ehtype references. |
| 1139 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1140 | |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1141 | /// NonLegacyDispatchMethods - List of methods for which we do *not* generate |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1142 | /// legacy messaging dispatch. |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1143 | llvm::DenseSet<Selector> NonLegacyDispatchMethods; |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1144 | |
| 1145 | /// LegacyDispatchedSelector - Returns true if SEL is not in the list of |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1146 | /// NonLegacyDispatchMethods; false otherwise. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1147 | bool LegacyDispatchedSelector(Selector Sel); |
| 1148 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1149 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 1150 | /// processing a translation unit. |
| 1151 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1152 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 1153 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 1154 | unsigned InstanceStart, |
| 1155 | unsigned InstanceSize, |
| 1156 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1157 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
| 1158 | llvm::Constant *IsAGV, |
| 1159 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 1160 | llvm::Constant *ClassRoGV, |
| 1161 | bool HiddenVisibility); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1162 | |
| 1163 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
| 1164 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 1165 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
| 1166 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1167 | /// EmitMethodList - Emit the method list for the given |
| 1168 | /// implementation. The return value has type MethodListnfABITy. |
| 1169 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 1170 | const char *Section, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 1171 | const ConstantVector &Methods); |
| 1172 | /// EmitIvarList - Emit the ivar list for the given |
| 1173 | /// implementation. If ForClass is true the list of class ivars |
| 1174 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1175 | /// interface ivars will be emitted. The return value has type |
| 1176 | /// IvarListnfABIPtrTy. |
| 1177 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 1178 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1179 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 1180 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 1181 | unsigned long int offset); |
| 1182 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1183 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1184 | /// declaration, emitting it if necessary. The return value has type |
| 1185 | /// ProtocolPtrTy. |
| 1186 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
| 1187 | |
| 1188 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1189 | /// object for the given declaration, emitting it if needed. These |
| 1190 | /// forward references will be filled in with empty bodies if no |
| 1191 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1192 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
| 1193 | |
| 1194 | /// EmitProtocolList - Generate the list of referenced |
| 1195 | /// protocols. The return value has type ProtocolListPtrTy. |
| 1196 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 1197 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1198 | ObjCProtocolDecl::protocol_iterator end); |
| 1199 | |
| 1200 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1201 | QualType ResultType, |
| 1202 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 1203 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1204 | QualType Arg0Ty, |
| 1205 | bool IsSuper, |
| 1206 | const CallArgList &CallArgs); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 1207 | |
| 1208 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1209 | /// class of the given name. |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 1210 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name); |
| 1211 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1212 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1213 | /// for the given class reference. |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1214 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1215 | const ObjCInterfaceDecl *ID); |
| 1216 | |
| 1217 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1218 | /// for the given super class reference. |
| 1219 | llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder, |
| 1220 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1221 | |
| 1222 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 1223 | /// meta-data |
| 1224 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
| 1225 | const ObjCInterfaceDecl *ID); |
| 1226 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1227 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 1228 | /// the given ivar. |
| 1229 | /// |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 1230 | llvm::GlobalVariable * ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1231 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1232 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1233 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1234 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1235 | /// for the given selector. |
| 1236 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1237 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1238 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1239 | /// interface. The return value has type EHTypePtrTy. |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1240 | llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 1241 | bool ForDefinition); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1242 | |
| 1243 | const char *getMetaclassSymbolPrefix() const { |
| 1244 | return "OBJC_METACLASS_$_"; |
| 1245 | } |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1246 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1247 | const char *getClassSymbolPrefix() const { |
| 1248 | return "OBJC_CLASS_$_"; |
| 1249 | } |
| 1250 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 1251 | void GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1252 | uint32_t &InstanceStart, |
| 1253 | uint32_t &InstanceSize); |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1254 | |
| 1255 | // Shamelessly stolen from Analysis/CFRefCount.cpp |
| 1256 | Selector GetNullarySelector(const char* name) { |
| 1257 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1258 | return CGM.getContext().Selectors.getSelector(0, &II); |
| 1259 | } |
| 1260 | |
| 1261 | Selector GetUnarySelector(const char* name) { |
| 1262 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1263 | return CGM.getContext().Selectors.getSelector(1, &II); |
| 1264 | } |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1265 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1266 | public: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1267 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1268 | // FIXME. All stubs for now! |
| 1269 | virtual llvm::Function *ModuleInitFunction(); |
| 1270 | |
| 1271 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1272 | QualType ResultType, |
| 1273 | Selector Sel, |
| 1274 | llvm::Value *Receiver, |
| 1275 | bool IsClassMessage, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1276 | const CallArgList &CallArgs, |
| 1277 | const ObjCMethodDecl *Method); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1278 | |
| 1279 | virtual CodeGen::RValue |
| 1280 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 1281 | QualType ResultType, |
| 1282 | Selector Sel, |
| 1283 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1284 | bool isCategoryImpl, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1285 | llvm::Value *Receiver, |
| 1286 | bool IsClassMessage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1287 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1288 | |
| 1289 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1290 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1291 | |
| 1292 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel) |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1293 | { return EmitSelector(Builder, Sel); } |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1294 | |
| 1295 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1296 | /// untyped one. |
| 1297 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1298 | const ObjCMethodDecl *Method) |
| 1299 | { return EmitSelector(Builder, Method->getSelector()); } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1300 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 1301 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1302 | |
| 1303 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1304 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 1305 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1306 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1307 | virtual llvm::Constant *GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1308 | return ObjCTypes.getGetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1309 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1310 | virtual llvm::Constant *GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1311 | return ObjCTypes.getSetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1312 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1313 | virtual llvm::Constant *EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1314 | return ObjCTypes.getEnumerationMutationFn(); |
Daniel Dunbar | 28ed084 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 1315 | } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1316 | |
| 1317 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 1318 | const Stmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1319 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 1320 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1321 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1322 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1323 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1324 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1325 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1326 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1327 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1328 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1329 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1330 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1331 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1332 | QualType ObjectTy, |
| 1333 | llvm::Value *BaseValue, |
| 1334 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1335 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1336 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1337 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1338 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1339 | }; |
| 1340 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1341 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1342 | |
| 1343 | /* *** Helper Functions *** */ |
| 1344 | |
| 1345 | /// getConstantGEP() - Help routine to construct simple GEPs. |
| 1346 | static llvm::Constant *getConstantGEP(llvm::Constant *C, |
| 1347 | unsigned idx0, |
| 1348 | unsigned idx1) { |
| 1349 | llvm::Value *Idxs[] = { |
| 1350 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0), |
| 1351 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1) |
| 1352 | }; |
| 1353 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2); |
| 1354 | } |
| 1355 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1356 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 1357 | /// class has the __objc_exception__ attribute. |
| 1358 | static bool hasObjCExceptionAttribute(const ObjCInterfaceDecl *OID) { |
Daniel Dunbar | b11fa0d | 2009-04-13 21:08:27 +0000 | [diff] [blame] | 1359 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1360 | return true; |
| 1361 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
| 1362 | return hasObjCExceptionAttribute(Super); |
| 1363 | return false; |
| 1364 | } |
| 1365 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1366 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1367 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1368 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
| 1369 | ObjCTypes(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1370 | { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1371 | ObjCABI = 1; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1372 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1375 | /// GetClass - Return a reference to the class for the given interface |
| 1376 | /// decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1377 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1378 | const ObjCInterfaceDecl *ID) { |
| 1379 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1383 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 1384 | return EmitSelector(Builder, Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1385 | } |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1386 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl |
| 1387 | *Method) { |
| 1388 | return EmitSelector(Builder, Method->getSelector()); |
| 1389 | } |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1390 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1391 | /// Generate a constant CFString object. |
| 1392 | /* |
| 1393 | struct __builtin_CFString { |
| 1394 | const int *isa; // point to __CFConstantStringClassReference |
| 1395 | int flags; |
| 1396 | const char *str; |
| 1397 | long length; |
| 1398 | }; |
| 1399 | */ |
| 1400 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1401 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 1402 | const ObjCStringLiteral *SL) { |
Steve Naroff | 8d4141f | 2009-04-01 13:55:36 +0000 | [diff] [blame] | 1403 | return CGM.GetAddrOfConstantCFString(SL->getString()); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | /// Generates a message send where the super is the receiver. This is |
| 1407 | /// a message send to self with special delivery semantics indicating |
| 1408 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1409 | CodeGen::RValue |
| 1410 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1411 | QualType ResultType, |
| 1412 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1413 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1414 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1415 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1416 | bool IsClassMessage, |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1417 | const CodeGen::CallArgList &CallArgs) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1418 | // Create and init a super structure; this is a (receiver, class) |
| 1419 | // pair we will pass to objc_msgSendSuper. |
| 1420 | llvm::Value *ObjCSuper = |
| 1421 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 1422 | llvm::Value *ReceiverAsObject = |
| 1423 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 1424 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 1425 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1426 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1427 | // If this is a class message the metaclass is passed as the target. |
| 1428 | llvm::Value *Target; |
| 1429 | if (IsClassMessage) { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1430 | if (isCategoryImpl) { |
| 1431 | // Message sent to 'super' in a class method defined in a category |
| 1432 | // implementation requires an odd treatment. |
| 1433 | // If we are in a class method, we must retrieve the |
| 1434 | // _metaclass_ for the current class, pointed at by |
| 1435 | // the class's "isa" pointer. The following assumes that |
| 1436 | // isa" is the first ivar in a class (which it must be). |
| 1437 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1438 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 1439 | Target = CGF.Builder.CreateLoad(Target); |
| 1440 | } |
| 1441 | else { |
| 1442 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 1443 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 1444 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 1445 | Target = Super; |
| 1446 | } |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1447 | } else { |
| 1448 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1449 | } |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1450 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 1451 | // and ObjCTypes types. |
| 1452 | const llvm::Type *ClassTy = |
| 1453 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1454 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1455 | CGF.Builder.CreateStore(Target, |
| 1456 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1457 | return EmitLegacyMessageSend(CGF, ResultType, |
| 1458 | EmitSelector(CGF.Builder, Sel), |
| 1459 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 1460 | true, CallArgs, ObjCTypes); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1461 | } |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1462 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1463 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1464 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1465 | QualType ResultType, |
| 1466 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1467 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1468 | bool IsClassMessage, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1469 | const CallArgList &CallArgs, |
| 1470 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1471 | return EmitLegacyMessageSend(CGF, ResultType, |
| 1472 | EmitSelector(CGF.Builder, Sel), |
| 1473 | Receiver, CGF.getContext().getObjCIdType(), |
| 1474 | false, CallArgs, ObjCTypes); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1477 | CodeGen::RValue CGObjCCommonMac::EmitLegacyMessageSend( |
| 1478 | CodeGen::CodeGenFunction &CGF, |
| 1479 | QualType ResultType, |
| 1480 | llvm::Value *Sel, |
| 1481 | llvm::Value *Arg0, |
| 1482 | QualType Arg0Ty, |
| 1483 | bool IsSuper, |
| 1484 | const CallArgList &CallArgs, |
| 1485 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1486 | CallArgList ActualArgs; |
Fariborz Jahanian | d019d96 | 2009-04-24 21:07:43 +0000 | [diff] [blame] | 1487 | if (!IsSuper) |
| 1488 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1489 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1490 | ActualArgs.push_back(std::make_pair(RValue::get(Sel), |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1491 | CGF.getContext().getObjCSelType())); |
| 1492 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1493 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1494 | CodeGenTypes &Types = CGM.getTypes(); |
| 1495 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1496 | // In 64bit ABI, type must be assumed VARARG. In 32bit abi, |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1497 | // it seems not to matter. |
| 1498 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, (ObjCABI == 2)); |
| 1499 | |
| 1500 | llvm::Constant *Fn = NULL; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1501 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1502 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper) |
| 1503 | : ObjCTypes.getSendStretFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1504 | } else if (ResultType->isFloatingType()) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1505 | if (ObjCABI == 2) { |
| 1506 | if (const BuiltinType *BT = ResultType->getAsBuiltinType()) { |
| 1507 | BuiltinType::Kind k = BT->getKind(); |
| 1508 | Fn = (k == BuiltinType::LongDouble) ? ObjCTypes.getSendFpretFn2(IsSuper) |
| 1509 | : ObjCTypes.getSendFn2(IsSuper); |
| 1510 | } |
| 1511 | } |
| 1512 | else |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1513 | // FIXME. This currently matches gcc's API for x86-32. May need |
| 1514 | // to change for others if we have their API. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1515 | Fn = ObjCTypes.getSendFpretFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1516 | } else { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1517 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper) |
| 1518 | : ObjCTypes.getSendFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1519 | } |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1520 | assert(Fn && "EmitLegacyMessageSend - unknown API"); |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 1521 | Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy)); |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1522 | return CGF.EmitCall(FnInfo, Fn, ActualArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1525 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1526 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1527 | // FIXME: I don't understand why gcc generates this, or where it is |
| 1528 | // resolved. Investigate. Its also wasteful to look this up over and |
| 1529 | // over. |
| 1530 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1531 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1532 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
| 1533 | ObjCTypes.ExternalProtocolPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1536 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1537 | // FIXME: We shouldn't need this, the protocol decl should contain |
| 1538 | // enough information to tell us whether this was a declaration or a |
| 1539 | // definition. |
| 1540 | DefinedProtocols.insert(PD->getIdentifier()); |
| 1541 | |
| 1542 | // If we have generated a forward reference to this protocol, emit |
| 1543 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 1544 | // emitted. |
| 1545 | if (Protocols.count(PD->getIdentifier())) |
| 1546 | GetOrEmitProtocol(PD); |
| 1547 | } |
| 1548 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1549 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1550 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 1551 | return GetOrEmitProtocol(PD); |
| 1552 | return GetOrEmitProtocolRef(PD); |
| 1553 | } |
| 1554 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1555 | /* |
| 1556 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 1557 | struct _objc_protocol { |
| 1558 | struct _objc_protocol_extension *isa; |
| 1559 | char *protocol_name; |
| 1560 | struct _objc_protocol_list *protocol_list; |
| 1561 | struct _objc__method_prototype_list *instance_methods; |
| 1562 | struct _objc__method_prototype_list *class_methods |
| 1563 | }; |
| 1564 | |
| 1565 | See EmitProtocolExtension(). |
| 1566 | */ |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1567 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
| 1568 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1569 | |
| 1570 | // Early exit if a defining object has already been generated. |
| 1571 | if (Entry && Entry->hasInitializer()) |
| 1572 | return Entry; |
| 1573 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1574 | // FIXME: I don't understand why gcc generates this, or where it is |
| 1575 | // resolved. Investigate. Its also wasteful to look this up over and |
| 1576 | // over. |
| 1577 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1578 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1579 | const char *ProtocolName = PD->getNameAsCString(); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1580 | |
| 1581 | // Construct method lists. |
| 1582 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1583 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1584 | for (ObjCProtocolDecl::instmeth_iterator |
| 1585 | i = PD->instmeth_begin(CGM.getContext()), |
| 1586 | e = PD->instmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1587 | ObjCMethodDecl *MD = *i; |
| 1588 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1589 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1590 | OptInstanceMethods.push_back(C); |
| 1591 | } else { |
| 1592 | InstanceMethods.push_back(C); |
| 1593 | } |
| 1594 | } |
| 1595 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1596 | for (ObjCProtocolDecl::classmeth_iterator |
| 1597 | i = PD->classmeth_begin(CGM.getContext()), |
| 1598 | e = PD->classmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1599 | ObjCMethodDecl *MD = *i; |
| 1600 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1601 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1602 | OptClassMethods.push_back(C); |
| 1603 | } else { |
| 1604 | ClassMethods.push_back(C); |
| 1605 | } |
| 1606 | } |
| 1607 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1608 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1609 | Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1610 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1611 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1612 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1613 | PD->protocol_begin(), |
| 1614 | PD->protocol_end()); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1615 | Values[3] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1616 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" |
| 1617 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1618 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1619 | InstanceMethods); |
| 1620 | Values[4] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1621 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" |
| 1622 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1623 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1624 | ClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1625 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 1626 | Values); |
| 1627 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1628 | if (Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1629 | // Already created, fix the linkage and update the initializer. |
| 1630 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1631 | Entry->setInitializer(Init); |
| 1632 | } else { |
| 1633 | Entry = |
| 1634 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
| 1635 | llvm::GlobalValue::InternalLinkage, |
| 1636 | Init, |
| 1637 | std::string("\01L_OBJC_PROTOCOL_")+ProtocolName, |
| 1638 | &CGM.getModule()); |
| 1639 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1640 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1641 | UsedGlobals.push_back(Entry); |
| 1642 | // FIXME: Is this necessary? Why only for protocol? |
| 1643 | Entry->setAlignment(4); |
| 1644 | } |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1645 | |
| 1646 | return Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1649 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1650 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1651 | |
| 1652 | if (!Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1653 | // We use the initializer as a marker of whether this is a forward |
| 1654 | // reference or not. At module finalization we add the empty |
| 1655 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1656 | Entry = |
| 1657 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1658 | llvm::GlobalValue::ExternalLinkage, |
| 1659 | 0, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1660 | "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1661 | &CGM.getModule()); |
| 1662 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1663 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1664 | UsedGlobals.push_back(Entry); |
| 1665 | // FIXME: Is this necessary? Why only for protocol? |
| 1666 | Entry->setAlignment(4); |
| 1667 | } |
| 1668 | |
| 1669 | return Entry; |
| 1670 | } |
| 1671 | |
| 1672 | /* |
| 1673 | struct _objc_protocol_extension { |
| 1674 | uint32_t size; |
| 1675 | struct objc_method_description_list *optional_instance_methods; |
| 1676 | struct objc_method_description_list *optional_class_methods; |
| 1677 | struct objc_property_list *instance_properties; |
| 1678 | }; |
| 1679 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1680 | llvm::Constant * |
| 1681 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1682 | const ConstantVector &OptInstanceMethods, |
| 1683 | const ConstantVector &OptClassMethods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1684 | uint64_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1685 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1686 | std::vector<llvm::Constant*> Values(4); |
| 1687 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1688 | Values[1] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1689 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
| 1690 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1691 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1692 | OptInstanceMethods); |
| 1693 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1694 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" |
| 1695 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1696 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1697 | OptClassMethods); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1698 | Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + |
| 1699 | PD->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1700 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1701 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1702 | // Return null if no extension bits are used. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1703 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
| 1704 | Values[3]->isNullValue()) |
| 1705 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 1706 | |
| 1707 | llvm::Constant *Init = |
| 1708 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1709 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1710 | // No special section, but goes in llvm.used |
| 1711 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(), |
| 1712 | Init, |
| 1713 | 0, 0, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | /* |
| 1717 | struct objc_protocol_list { |
| 1718 | struct objc_protocol_list *next; |
| 1719 | long count; |
| 1720 | Protocol *list[]; |
| 1721 | }; |
| 1722 | */ |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1723 | llvm::Constant * |
| 1724 | CGObjCMac::EmitProtocolList(const std::string &Name, |
| 1725 | ObjCProtocolDecl::protocol_iterator begin, |
| 1726 | ObjCProtocolDecl::protocol_iterator end) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1727 | std::vector<llvm::Constant*> ProtocolRefs; |
| 1728 | |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1729 | for (; begin != end; ++begin) |
| 1730 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1731 | |
| 1732 | // Just return null for empty protocol lists |
| 1733 | if (ProtocolRefs.empty()) |
| 1734 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1735 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1736 | // This list is null terminated. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1737 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
| 1738 | |
| 1739 | std::vector<llvm::Constant*> Values(3); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1740 | // This field is only used by the runtime. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1741 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1742 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 1743 | Values[2] = |
| 1744 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 1745 | ProtocolRefs.size()), |
| 1746 | ProtocolRefs); |
| 1747 | |
| 1748 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1749 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1750 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1751 | 4, false); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1752 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
| 1753 | } |
| 1754 | |
| 1755 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1756 | struct _objc_property { |
| 1757 | const char * const name; |
| 1758 | const char * const attributes; |
| 1759 | }; |
| 1760 | |
| 1761 | struct _objc_property_list { |
| 1762 | uint32_t entsize; // sizeof (struct _objc_property) |
| 1763 | uint32_t prop_count; |
| 1764 | struct _objc_property[prop_count]; |
| 1765 | }; |
| 1766 | */ |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1767 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name, |
| 1768 | const Decl *Container, |
| 1769 | const ObjCContainerDecl *OCD, |
| 1770 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1771 | std::vector<llvm::Constant*> Properties, Prop(2); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1772 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(CGM.getContext()), |
| 1773 | E = OCD->prop_end(CGM.getContext()); I != E; ++I) { |
Steve Naroff | 93983f8 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 1774 | const ObjCPropertyDecl *PD = *I; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1775 | Prop[0] = GetPropertyName(PD->getIdentifier()); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1776 | Prop[1] = GetPropertyTypeString(PD, Container); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1777 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
| 1778 | Prop)); |
| 1779 | } |
| 1780 | |
| 1781 | // Return null for empty list. |
| 1782 | if (Properties.empty()) |
| 1783 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1784 | |
| 1785 | unsigned PropertySize = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1786 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1787 | std::vector<llvm::Constant*> Values(3); |
| 1788 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 1789 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
| 1790 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
| 1791 | Properties.size()); |
| 1792 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
| 1793 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1794 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1795 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1796 | CreateMetadataVar(Name, Init, |
| 1797 | (ObjCABI == 2) ? "__DATA, __objc_const" : |
| 1798 | "__OBJC,__property,regular,no_dead_strip", |
| 1799 | (ObjCABI == 2) ? 8 : 4, |
| 1800 | true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1801 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1805 | struct objc_method_description_list { |
| 1806 | int count; |
| 1807 | struct objc_method_description list[]; |
| 1808 | }; |
| 1809 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1810 | llvm::Constant * |
| 1811 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 1812 | std::vector<llvm::Constant*> Desc(2); |
| 1813 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1814 | ObjCTypes.SelectorPtrTy); |
| 1815 | Desc[1] = GetMethodVarType(MD); |
| 1816 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
| 1817 | Desc); |
| 1818 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1819 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1820 | llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name, |
| 1821 | const char *Section, |
| 1822 | const ConstantVector &Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1823 | // Return null for empty list. |
| 1824 | if (Methods.empty()) |
| 1825 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 1826 | |
| 1827 | std::vector<llvm::Constant*> Values(2); |
| 1828 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 1829 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
| 1830 | Methods.size()); |
| 1831 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
| 1832 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1833 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1834 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1835 | return llvm::ConstantExpr::getBitCast(GV, |
| 1836 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1837 | } |
| 1838 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1839 | /* |
| 1840 | struct _objc_category { |
| 1841 | char *category_name; |
| 1842 | char *class_name; |
| 1843 | struct _objc_method_list *instance_methods; |
| 1844 | struct _objc_method_list *class_methods; |
| 1845 | struct _objc_protocol_list *protocols; |
| 1846 | uint32_t size; // <rdar://4585769> |
| 1847 | struct _objc_property_list *instance_properties; |
| 1848 | }; |
| 1849 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1850 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1851 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1852 | |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1853 | // FIXME: This is poor design, the OCD should have a pointer to the |
| 1854 | // category decl. Additionally, note that Category can be null for |
| 1855 | // the @implementation w/o an @interface case. Sema should just |
| 1856 | // create one for us as it does for @implementation so everyone else |
| 1857 | // can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1858 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1859 | const ObjCCategoryDecl *Category = |
| 1860 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1861 | std::string ExtName(Interface->getNameAsString() + "_" + |
| 1862 | OCD->getNameAsString()); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1863 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1864 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1865 | for (ObjCCategoryImplDecl::instmeth_iterator |
| 1866 | i = OCD->instmeth_begin(CGM.getContext()), |
| 1867 | e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1868 | // Instance methods should always be defined. |
| 1869 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1870 | } |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1871 | for (ObjCCategoryImplDecl::classmeth_iterator |
| 1872 | i = OCD->classmeth_begin(CGM.getContext()), |
| 1873 | e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1874 | // Class methods should always be defined. |
| 1875 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1876 | } |
| 1877 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1878 | std::vector<llvm::Constant*> Values(7); |
| 1879 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 1880 | Values[1] = GetClassName(Interface->getIdentifier()); |
Fariborz Jahanian | 679cd7f | 2009-04-29 20:40:05 +0000 | [diff] [blame] | 1881 | LazySymbols.insert(Interface->getIdentifier()); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1882 | Values[2] = |
| 1883 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") + |
| 1884 | ExtName, |
| 1885 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1886 | InstanceMethods); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1887 | Values[3] = |
| 1888 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName, |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1889 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1890 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1891 | if (Category) { |
| 1892 | Values[4] = |
| 1893 | EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName, |
| 1894 | Category->protocol_begin(), |
| 1895 | Category->protocol_end()); |
| 1896 | } else { |
| 1897 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1898 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1899 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1900 | |
| 1901 | // If there is no category @interface then there can be no properties. |
| 1902 | if (Category) { |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1903 | Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1904 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1905 | } else { |
| 1906 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1907 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1908 | |
| 1909 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
| 1910 | Values); |
| 1911 | |
| 1912 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1913 | CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init, |
| 1914 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1915 | 4, true); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1916 | DefinedCategories.push_back(GV); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1919 | // FIXME: Get from somewhere? |
| 1920 | enum ClassFlags { |
| 1921 | eClassFlags_Factory = 0x00001, |
| 1922 | eClassFlags_Meta = 0x00002, |
| 1923 | // <rdr://5142207> |
| 1924 | eClassFlags_HasCXXStructors = 0x02000, |
| 1925 | eClassFlags_Hidden = 0x20000, |
| 1926 | eClassFlags_ABI2_Hidden = 0x00010, |
| 1927 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 1928 | }; |
| 1929 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1930 | /* |
| 1931 | struct _objc_class { |
| 1932 | Class isa; |
| 1933 | Class super_class; |
| 1934 | const char *name; |
| 1935 | long version; |
| 1936 | long info; |
| 1937 | long instance_size; |
| 1938 | struct _objc_ivar_list *ivars; |
| 1939 | struct _objc_method_list *methods; |
| 1940 | struct _objc_cache *cache; |
| 1941 | struct _objc_protocol_list *protocols; |
| 1942 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 1943 | const char *ivar_layout; |
| 1944 | struct _objc_class_ext *ext; |
| 1945 | }; |
| 1946 | |
| 1947 | See EmitClassExtension(); |
| 1948 | */ |
| 1949 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1950 | DefinedSymbols.insert(ID->getIdentifier()); |
| 1951 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1952 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1953 | // FIXME: Gross |
| 1954 | ObjCInterfaceDecl *Interface = |
| 1955 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1956 | llvm::Constant *Protocols = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1957 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1958 | Interface->protocol_begin(), |
| 1959 | Interface->protocol_end()); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1960 | unsigned Flags = eClassFlags_Factory; |
Daniel Dunbar | 2bebbf0 | 2009-05-03 10:46:44 +0000 | [diff] [blame] | 1961 | unsigned Size = |
| 1962 | CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1963 | |
| 1964 | // FIXME: Set CXX-structors flag. |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1965 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1966 | Flags |= eClassFlags_Hidden; |
| 1967 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1968 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1969 | for (ObjCImplementationDecl::instmeth_iterator |
| 1970 | i = ID->instmeth_begin(CGM.getContext()), |
| 1971 | e = ID->instmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1972 | // Instance methods should always be defined. |
| 1973 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1974 | } |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1975 | for (ObjCImplementationDecl::classmeth_iterator |
| 1976 | i = ID->classmeth_begin(CGM.getContext()), |
| 1977 | e = ID->classmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1978 | // Class methods should always be defined. |
| 1979 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1980 | } |
| 1981 | |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1982 | for (ObjCImplementationDecl::propimpl_iterator |
| 1983 | i = ID->propimpl_begin(CGM.getContext()), |
| 1984 | e = ID->propimpl_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1985 | ObjCPropertyImplDecl *PID = *i; |
| 1986 | |
| 1987 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 1988 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 1989 | |
| 1990 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 1991 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1992 | InstanceMethods.push_back(C); |
| 1993 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 1994 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1995 | InstanceMethods.push_back(C); |
| 1996 | } |
| 1997 | } |
| 1998 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1999 | std::vector<llvm::Constant*> Values(12); |
Daniel Dunbar | 5384b09 | 2009-05-03 08:56:52 +0000 | [diff] [blame] | 2000 | Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2001 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2002 | // Record a reference to the super class. |
| 2003 | LazySymbols.insert(Super->getIdentifier()); |
| 2004 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2005 | Values[ 1] = |
| 2006 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 2007 | ObjCTypes.ClassPtrTy); |
| 2008 | } else { |
| 2009 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 2010 | } |
| 2011 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 2012 | // Version is always 0. |
| 2013 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2014 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 2015 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2016 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2017 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2018 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2019 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2020 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2021 | // cache is always NULL. |
| 2022 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 2023 | Values[ 9] = Protocols; |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 2024 | Values[10] = BuildIvarLayout(ID, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2025 | Values[11] = EmitClassExtension(ID); |
| 2026 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 2027 | Values); |
| 2028 | |
| 2029 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2030 | CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init, |
| 2031 | "__OBJC,__class,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2032 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2033 | DefinedClasses.push_back(GV); |
| 2034 | } |
| 2035 | |
| 2036 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 2037 | llvm::Constant *Protocols, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2038 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2039 | unsigned Flags = eClassFlags_Meta; |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2040 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2041 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 2042 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2043 | Flags |= eClassFlags_Hidden; |
| 2044 | |
| 2045 | std::vector<llvm::Constant*> Values(12); |
| 2046 | // The isa for the metaclass is the root of the hierarchy. |
| 2047 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 2048 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 2049 | Root = Super; |
| 2050 | Values[ 0] = |
| 2051 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
| 2052 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2053 | // The super class for the metaclass is emitted as the name of the |
| 2054 | // super class. The runtime fixes this up to point to the |
| 2055 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2056 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
| 2057 | Values[ 1] = |
| 2058 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 2059 | ObjCTypes.ClassPtrTy); |
| 2060 | } else { |
| 2061 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 2062 | } |
| 2063 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 2064 | // Version is always 0. |
| 2065 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2066 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 2067 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2068 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2069 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2070 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2071 | "__OBJC,__cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2072 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2073 | // cache is always NULL. |
| 2074 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 2075 | Values[ 9] = Protocols; |
| 2076 | // ivar_layout for metaclass is always NULL. |
| 2077 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 2078 | // The class extension is always unused for metaclasses. |
| 2079 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 2080 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 2081 | Values); |
| 2082 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2083 | std::string Name("\01L_OBJC_METACLASS_"); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2084 | Name += ID->getNameAsCString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2085 | |
| 2086 | // Check for a forward reference. |
| 2087 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 2088 | if (GV) { |
| 2089 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2090 | "Forward metaclass reference has incorrect type."); |
| 2091 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 2092 | GV->setInitializer(Init); |
| 2093 | } else { |
| 2094 | GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 2095 | llvm::GlobalValue::InternalLinkage, |
| 2096 | Init, Name, |
| 2097 | &CGM.getModule()); |
| 2098 | } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2099 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2100 | GV->setAlignment(4); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2101 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2102 | |
| 2103 | return GV; |
| 2104 | } |
| 2105 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2106 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2107 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2108 | |
| 2109 | // FIXME: Should we look these up somewhere other than the |
| 2110 | // module. Its a bit silly since we only generate these while |
| 2111 | // processing an implementation, so exactly one pointer would work |
| 2112 | // if know when we entered/exitted an implementation block. |
| 2113 | |
| 2114 | // Check for an existing forward reference. |
Fariborz Jahanian | b0d2794 | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 2115 | // Previously, metaclass with internal linkage may have been defined. |
| 2116 | // pass 'true' as 2nd argument so it is returned. |
| 2117 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) { |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2118 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2119 | "Forward metaclass reference has incorrect type."); |
| 2120 | return GV; |
| 2121 | } else { |
| 2122 | // Generate as an external reference to keep a consistent |
| 2123 | // module. This will be patched up when we emit the metaclass. |
| 2124 | return new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 2125 | llvm::GlobalValue::ExternalLinkage, |
| 2126 | 0, |
| 2127 | Name, |
| 2128 | &CGM.getModule()); |
| 2129 | } |
| 2130 | } |
| 2131 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2132 | /* |
| 2133 | struct objc_class_ext { |
| 2134 | uint32_t size; |
| 2135 | const char *weak_ivar_layout; |
| 2136 | struct _objc_property_list *properties; |
| 2137 | }; |
| 2138 | */ |
| 2139 | llvm::Constant * |
| 2140 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
| 2141 | uint64_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2142 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2143 | |
| 2144 | std::vector<llvm::Constant*> Values(3); |
| 2145 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 2146 | Values[1] = BuildIvarLayout(ID, false); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2147 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2148 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2149 | |
| 2150 | // Return null if no extension bits are used. |
| 2151 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
| 2152 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 2153 | |
| 2154 | llvm::Constant *Init = |
| 2155 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2156 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2157 | Init, "__OBJC,__class_ext,regular,no_dead_strip", |
| 2158 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2159 | } |
| 2160 | |
| 2161 | /* |
| 2162 | struct objc_ivar { |
| 2163 | char *ivar_name; |
| 2164 | char *ivar_type; |
| 2165 | int ivar_offset; |
| 2166 | }; |
| 2167 | |
| 2168 | struct objc_ivar_list { |
| 2169 | int ivar_count; |
| 2170 | struct objc_ivar list[count]; |
| 2171 | }; |
| 2172 | */ |
| 2173 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2174 | bool ForClass) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2175 | std::vector<llvm::Constant*> Ivars, Ivar(3); |
| 2176 | |
| 2177 | // When emitting the root class GCC emits ivar entries for the |
| 2178 | // actual class structure. It is not clear if we need to follow this |
| 2179 | // behavior; for now lets try and get away with not doing it. If so, |
| 2180 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 2181 | // for the class. |
| 2182 | if (ForClass) |
| 2183 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2184 | |
| 2185 | ObjCInterfaceDecl *OID = |
| 2186 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2187 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 2188 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
| 2189 | GetNamedIvarList(OID, OIvars); |
| 2190 | |
| 2191 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 2192 | ObjCIvarDecl *IVD = OIvars[i]; |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 2193 | Ivar[0] = GetMethodVarName(IVD->getIdentifier()); |
| 2194 | Ivar[1] = GetMethodVarType(IVD); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 2195 | Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2196 | ComputeIvarBaseOffset(CGM, OID, IVD)); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2197 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2198 | } |
| 2199 | |
| 2200 | // Return null for empty list. |
| 2201 | if (Ivars.empty()) |
| 2202 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
| 2203 | |
| 2204 | std::vector<llvm::Constant*> Values(2); |
| 2205 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 2206 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
| 2207 | Ivars.size()); |
| 2208 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
| 2209 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2210 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2211 | llvm::GlobalVariable *GV; |
| 2212 | if (ForClass) |
| 2213 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(), |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2214 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
| 2215 | 4, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2216 | else |
| 2217 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" |
| 2218 | + ID->getNameAsString(), |
| 2219 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2220 | 4, true); |
| 2221 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2222 | } |
| 2223 | |
| 2224 | /* |
| 2225 | struct objc_method { |
| 2226 | SEL method_name; |
| 2227 | char *method_types; |
| 2228 | void *method; |
| 2229 | }; |
| 2230 | |
| 2231 | struct objc_method_list { |
| 2232 | struct objc_method_list *obsolete; |
| 2233 | int count; |
| 2234 | struct objc_method methods_list[count]; |
| 2235 | }; |
| 2236 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2237 | |
| 2238 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 2239 | /// given method if it has been defined. The result is null if the |
| 2240 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2241 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2242 | // FIXME: Use DenseMap::lookup |
| 2243 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 2244 | if (!Fn) |
| 2245 | return 0; |
| 2246 | |
| 2247 | std::vector<llvm::Constant*> Method(3); |
| 2248 | Method[0] = |
| 2249 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 2250 | ObjCTypes.SelectorPtrTy); |
| 2251 | Method[1] = GetMethodVarType(MD); |
| 2252 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 2253 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 2254 | } |
| 2255 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2256 | llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name, |
| 2257 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2258 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2259 | // Return null for empty list. |
| 2260 | if (Methods.empty()) |
| 2261 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
| 2262 | |
| 2263 | std::vector<llvm::Constant*> Values(3); |
| 2264 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 2265 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 2266 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 2267 | Methods.size()); |
| 2268 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 2269 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2270 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2271 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2272 | return llvm::ConstantExpr::getBitCast(GV, |
| 2273 | ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2274 | } |
| 2275 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 2276 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 2277 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2278 | std::string Name; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 2279 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2280 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2281 | CodeGenTypes &Types = CGM.getTypes(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2282 | const llvm::FunctionType *MethodTy = |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2283 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2284 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2285 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2286 | llvm::GlobalValue::InternalLinkage, |
| 2287 | Name, |
| 2288 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2289 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2290 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2291 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2292 | } |
| 2293 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2294 | llvm::GlobalVariable * |
| 2295 | CGObjCCommonMac::CreateMetadataVar(const std::string &Name, |
| 2296 | llvm::Constant *Init, |
| 2297 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2298 | unsigned Align, |
| 2299 | bool AddToUsed) { |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2300 | const llvm::Type *Ty = Init->getType(); |
| 2301 | llvm::GlobalVariable *GV = |
| 2302 | new llvm::GlobalVariable(Ty, false, |
| 2303 | llvm::GlobalValue::InternalLinkage, |
| 2304 | Init, |
| 2305 | Name, |
| 2306 | &CGM.getModule()); |
| 2307 | if (Section) |
| 2308 | GV->setSection(Section); |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2309 | if (Align) |
| 2310 | GV->setAlignment(Align); |
| 2311 | if (AddToUsed) |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2312 | UsedGlobals.push_back(GV); |
| 2313 | return GV; |
| 2314 | } |
| 2315 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2316 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2317 | // Abuse this interface function as a place to finalize. |
| 2318 | FinishModule(); |
| 2319 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2320 | return NULL; |
| 2321 | } |
| 2322 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2323 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2324 | return ObjCTypes.getGetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2325 | } |
| 2326 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2327 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2328 | return ObjCTypes.getSetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2329 | } |
| 2330 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2331 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2332 | return ObjCTypes.getEnumerationMutationFn(); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2333 | } |
| 2334 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2335 | /* |
| 2336 | |
| 2337 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 2338 | -- |
| 2339 | |
| 2340 | The basic framework for a @try-catch-finally is as follows: |
| 2341 | { |
| 2342 | objc_exception_data d; |
| 2343 | id _rethrow = null; |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2344 | bool _call_try_exit = true; |
| 2345 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2346 | objc_exception_try_enter(&d); |
| 2347 | if (!setjmp(d.jmp_buf)) { |
| 2348 | ... try body ... |
| 2349 | } else { |
| 2350 | // exception path |
| 2351 | id _caught = objc_exception_extract(&d); |
| 2352 | |
| 2353 | // enter new try scope for handlers |
| 2354 | if (!setjmp(d.jmp_buf)) { |
| 2355 | ... match exception and execute catch blocks ... |
| 2356 | |
| 2357 | // fell off end, rethrow. |
| 2358 | _rethrow = _caught; |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2359 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2360 | } else { |
| 2361 | // exception in catch block |
| 2362 | _rethrow = objc_exception_extract(&d); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2363 | _call_try_exit = false; |
| 2364 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2365 | } |
| 2366 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2367 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2368 | |
| 2369 | finally: |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2370 | if (_call_try_exit) |
| 2371 | objc_exception_try_exit(&d); |
| 2372 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2373 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2374 | ... dispatch to finally destination ... |
| 2375 | |
| 2376 | finally_rethrow: |
| 2377 | objc_exception_throw(_rethrow); |
| 2378 | |
| 2379 | finally_end: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
| 2382 | This framework differs slightly from the one gcc uses, in that gcc |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2383 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 2384 | and if the object should be rethrown. This breaks in the face of |
| 2385 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2386 | |
| 2387 | We specialize this framework for a few particular circumstances: |
| 2388 | |
| 2389 | - If there are no catch blocks, then we avoid emitting the second |
| 2390 | exception handling context. |
| 2391 | |
| 2392 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 2393 | e)) we avoid emitting the code to rethrow an uncaught exception. |
| 2394 | |
| 2395 | - FIXME: If there is no @finally block we can do a few more |
| 2396 | simplifications. |
| 2397 | |
| 2398 | Rethrows and Jumps-Through-Finally |
| 2399 | -- |
| 2400 | |
| 2401 | Support for implicit rethrows and jumping through the finally block is |
| 2402 | handled by storing the current exception-handling context in |
| 2403 | ObjCEHStack. |
| 2404 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2405 | In order to implement proper @finally semantics, we support one basic |
| 2406 | mechanism for jumping through the finally block to an arbitrary |
| 2407 | destination. Constructs which generate exits from a @try or @catch |
| 2408 | block use this mechanism to implement the proper semantics by chaining |
| 2409 | jumps, as necessary. |
| 2410 | |
| 2411 | This mechanism works like the one used for indirect goto: we |
| 2412 | arbitrarily assign an ID to each destination and store the ID for the |
| 2413 | destination in a variable prior to entering the finally block. At the |
| 2414 | end of the finally block we simply create a switch to the proper |
| 2415 | destination. |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2416 | |
| 2417 | Code gen for @synchronized(expr) stmt; |
| 2418 | Effectively generating code for: |
| 2419 | objc_sync_enter(expr); |
| 2420 | @try stmt @finally { objc_sync_exit(expr); } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2421 | */ |
| 2422 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2423 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 2424 | const Stmt &S) { |
| 2425 | bool isTry = isa<ObjCAtTryStmt>(S); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2426 | // Create various blocks we refer to for handling @finally. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2427 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2428 | llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit"); |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2429 | llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit"); |
| 2430 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
| 2431 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2432 | |
| 2433 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 2434 | // evaluation of the expression must occur before we enter the |
| 2435 | // @synchronized. We can safely avoid a temp here because jumps into |
| 2436 | // @synchronized are illegal & this will dominate uses. |
| 2437 | llvm::Value *SyncArg = 0; |
| 2438 | if (!isTry) { |
| 2439 | SyncArg = |
| 2440 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 2441 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 2442 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2443 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2444 | |
| 2445 | // Push an EH context entry, used for handling rethrows and jumps |
| 2446 | // through finally. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2447 | CGF.PushCleanupBlock(FinallyBlock); |
| 2448 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2449 | CGF.ObjCEHValueStack.push_back(0); |
| 2450 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2451 | // Allocate memory for the exception data and rethrow pointer. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2452 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 2453 | "exceptiondata.ptr"); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2454 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy, |
| 2455 | "_rethrow"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2456 | llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty, |
| 2457 | "_call_try_exit"); |
| 2458 | CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr); |
| 2459 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2460 | // Enter a new try block and call setjmp. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2461 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2462 | llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0, |
| 2463 | "jmpbufarray"); |
| 2464 | JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2465 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2466 | JmpBufPtr, "result"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2467 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2468 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 2469 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2470 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2471 | TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2472 | |
| 2473 | // Emit the @try block. |
| 2474 | CGF.EmitBlock(TryBlock); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2475 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 2476 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2477 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2478 | |
| 2479 | // Emit the "exception in @try" block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2480 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2481 | |
| 2482 | // Retrieve the exception object. We may emit multiple blocks but |
| 2483 | // nothing can cross this so the value is already in SSA form. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2484 | llvm::Value *Caught = |
| 2485 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2486 | ExceptionData, "caught"); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2487 | CGF.ObjCEHValueStack.back() = Caught; |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2488 | if (!isTry) |
| 2489 | { |
| 2490 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2491 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2492 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2493 | } |
| 2494 | else if (const ObjCAtCatchStmt* CatchStmt = |
| 2495 | cast<ObjCAtTryStmt>(S).getCatchStmts()) |
| 2496 | { |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2497 | // Enter a new exception try block (in case a @catch block throws |
| 2498 | // an exception). |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2499 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2500 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2501 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2502 | JmpBufPtr, "result"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2503 | llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2504 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2505 | llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch"); |
| 2506 | llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler"); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2507 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2508 | |
| 2509 | CGF.EmitBlock(CatchBlock); |
| 2510 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2511 | // Handle catch list. As a special case we check if everything is |
| 2512 | // matched and avoid generating code for falling off the end if |
| 2513 | // so. |
| 2514 | bool AllMatched = false; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2515 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2516 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2517 | |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2518 | const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2519 | const PointerType *PT = 0; |
| 2520 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2521 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2522 | if (!CatchParam) { |
| 2523 | AllMatched = true; |
| 2524 | } else { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2525 | PT = CatchParam->getType()->getAsPointerType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2526 | |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 2527 | // catch(id e) always matches. |
| 2528 | // FIXME: For the time being we also match id<X>; this should |
| 2529 | // be rejected by Sema instead. |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2530 | if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) || |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2531 | CatchParam->getType()->isObjCQualifiedIdType()) |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2532 | AllMatched = true; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2533 | } |
| 2534 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2535 | if (AllMatched) { |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2536 | if (CatchParam) { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2537 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2538 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2539 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2540 | } |
Anders Carlsson | 1452f55 | 2008-09-11 08:21:54 +0000 | [diff] [blame] | 2541 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2542 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2543 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2544 | break; |
| 2545 | } |
| 2546 | |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2547 | assert(PT && "Unexpected non-pointer type in @catch"); |
| 2548 | QualType T = PT->getPointeeType(); |
Anders Carlsson | 4b7ff6e | 2008-09-11 06:35:14 +0000 | [diff] [blame] | 2549 | const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2550 | assert(ObjCType && "Catch parameter must have Objective-C type!"); |
| 2551 | |
| 2552 | // Check if the @catch block matches the exception object. |
| 2553 | llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl()); |
| 2554 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2555 | llvm::Value *Match = |
| 2556 | CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(), |
| 2557 | Class, Caught, "match"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2558 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2559 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2560 | |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2561 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2562 | MatchedBlock, NextCatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2563 | |
| 2564 | // Emit the @catch block. |
| 2565 | CGF.EmitBlock(MatchedBlock); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2566 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2567 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2568 | |
| 2569 | llvm::Value *Tmp = |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2570 | CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()), |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2571 | "tmp"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2572 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2573 | |
| 2574 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2575 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2576 | |
| 2577 | CGF.EmitBlock(NextCatchBlock); |
| 2578 | } |
| 2579 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2580 | if (!AllMatched) { |
| 2581 | // None of the handlers caught the exception, so store it to be |
| 2582 | // rethrown at the end of the @finally block. |
| 2583 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2584 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | // Emit the exception handler for the @catch blocks. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2588 | CGF.EmitBlock(CatchHandler); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2589 | CGF.Builder.CreateStore( |
| 2590 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2591 | ExceptionData), |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2592 | RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2593 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2594 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2595 | } else { |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2596 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2597 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2598 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2599 | } |
| 2600 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2601 | // Pop the exception-handling stack entry. It is important to do |
| 2602 | // this now, because the code in the @finally block is not in this |
| 2603 | // context. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2604 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 2605 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2606 | CGF.ObjCEHValueStack.pop_back(); |
| 2607 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2608 | // Emit the @finally block. |
| 2609 | CGF.EmitBlock(FinallyBlock); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2610 | llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp"); |
| 2611 | |
| 2612 | CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit); |
| 2613 | |
| 2614 | CGF.EmitBlock(FinallyExit); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2615 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2616 | |
| 2617 | CGF.EmitBlock(FinallyNoExit); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2618 | if (isTry) { |
| 2619 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 2620 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 2621 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2622 | } else { |
| 2623 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 2624 | // @synchronized. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2625 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg); |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 2626 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2627 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2628 | // Emit the switch block |
| 2629 | if (Info.SwitchBlock) |
| 2630 | CGF.EmitBlock(Info.SwitchBlock); |
| 2631 | if (Info.EndBlock) |
| 2632 | CGF.EmitBlock(Info.EndBlock); |
| 2633 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2634 | CGF.EmitBlock(FinallyRethrow); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2635 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2636 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2637 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2638 | |
| 2639 | CGF.EmitBlock(FinallyEnd); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2640 | } |
| 2641 | |
| 2642 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2643 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2644 | llvm::Value *ExceptionAsObject; |
| 2645 | |
| 2646 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 2647 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
| 2648 | ExceptionAsObject = |
| 2649 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 2650 | } else { |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2651 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2652 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2653 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2656 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2657 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2658 | |
| 2659 | // Clear the insertion point to indicate we are in unreachable code. |
| 2660 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2661 | } |
| 2662 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2663 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2664 | /// object: objc_read_weak (id *src) |
| 2665 | /// |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2666 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2667 | llvm::Value *AddrWeakObj) |
| 2668 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2669 | const llvm::Type* DestTy = |
| 2670 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2671 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2672 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2673 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2674 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2675 | return read_weak; |
| 2676 | } |
| 2677 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2678 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 2679 | /// objc_assign_weak (id src, id *dst) |
| 2680 | /// |
| 2681 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 2682 | llvm::Value *src, llvm::Value *dst) |
| 2683 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2684 | const llvm::Type * SrcTy = src->getType(); |
| 2685 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2686 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2687 | assert(Size <= 8 && "does not support size > 8"); |
| 2688 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2689 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2690 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2691 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2692 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2693 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 2694 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2695 | src, dst, "weakassign"); |
| 2696 | return; |
| 2697 | } |
| 2698 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2699 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 2700 | /// objc_assign_global (id src, id *dst) |
| 2701 | /// |
| 2702 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 2703 | llvm::Value *src, llvm::Value *dst) |
| 2704 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2705 | const llvm::Type * SrcTy = src->getType(); |
| 2706 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2707 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2708 | assert(Size <= 8 && "does not support size > 8"); |
| 2709 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2710 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2711 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2712 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2713 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2714 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2715 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2716 | src, dst, "globalassign"); |
| 2717 | return; |
| 2718 | } |
| 2719 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2720 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 2721 | /// objc_assign_ivar (id src, id *dst) |
| 2722 | /// |
| 2723 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 2724 | llvm::Value *src, llvm::Value *dst) |
| 2725 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2726 | const llvm::Type * SrcTy = src->getType(); |
| 2727 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2728 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2729 | assert(Size <= 8 && "does not support size > 8"); |
| 2730 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2731 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2732 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2733 | } |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2734 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2735 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2736 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(), |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2737 | src, dst, "assignivar"); |
| 2738 | return; |
| 2739 | } |
| 2740 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2741 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 2742 | /// objc_assign_strongCast (id src, id *dst) |
| 2743 | /// |
| 2744 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 2745 | llvm::Value *src, llvm::Value *dst) |
| 2746 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2747 | const llvm::Type * SrcTy = src->getType(); |
| 2748 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2749 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2750 | assert(Size <= 8 && "does not support size > 8"); |
| 2751 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2752 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2753 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2754 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2755 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2756 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2757 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2758 | src, dst, "weakassign"); |
| 2759 | return; |
| 2760 | } |
| 2761 | |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2762 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 2763 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2764 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 2765 | QualType ObjectTy, |
| 2766 | llvm::Value *BaseValue, |
| 2767 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2768 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 2769 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2770 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 2771 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2774 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 2775 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2776 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2777 | uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2778 | return llvm::ConstantInt::get( |
| 2779 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 2780 | Offset); |
| 2781 | } |
| 2782 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2783 | /* *** Private Interface *** */ |
| 2784 | |
| 2785 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 2786 | /// level information. |
| 2787 | /// |
| 2788 | /// See: <rdr://4810609&4810587&4810587> |
| 2789 | /// struct IMAGE_INFO { |
| 2790 | /// unsigned version; |
| 2791 | /// unsigned flags; |
| 2792 | /// }; |
| 2793 | enum ImageInfoFlags { |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2794 | eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what |
| 2795 | // this implies. |
| 2796 | eImageInfo_GarbageCollected = (1 << 1), |
| 2797 | eImageInfo_GCOnly = (1 << 2), |
| 2798 | eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set. |
| 2799 | |
| 2800 | // A flag indicating that the module has no instances of an |
| 2801 | // @synthesize of a superclass variable. <rdar://problem/6803242> |
| 2802 | eImageInfo_CorrectedSynthesize = (1 << 4) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2803 | }; |
| 2804 | |
| 2805 | void CGObjCMac::EmitImageInfo() { |
| 2806 | unsigned version = 0; // Version is unused? |
| 2807 | unsigned flags = 0; |
| 2808 | |
| 2809 | // FIXME: Fix and continue? |
| 2810 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 2811 | flags |= eImageInfo_GarbageCollected; |
| 2812 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 2813 | flags |= eImageInfo_GCOnly; |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2814 | |
| 2815 | // We never allow @synthesize of a superclass property. |
| 2816 | flags |= eImageInfo_CorrectedSynthesize; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2817 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2818 | // Emitted as int[2]; |
| 2819 | llvm::Constant *values[2] = { |
| 2820 | llvm::ConstantInt::get(llvm::Type::Int32Ty, version), |
| 2821 | llvm::ConstantInt::get(llvm::Type::Int32Ty, flags) |
| 2822 | }; |
| 2823 | llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2824 | |
| 2825 | const char *Section; |
| 2826 | if (ObjCABI == 1) |
| 2827 | Section = "__OBJC, __image_info,regular"; |
| 2828 | else |
| 2829 | Section = "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2830 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2831 | CreateMetadataVar("\01L_OBJC_IMAGE_INFO", |
| 2832 | llvm::ConstantArray::get(AT, values, 2), |
| 2833 | Section, |
| 2834 | 0, |
| 2835 | true); |
| 2836 | GV->setConstant(true); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2839 | |
| 2840 | // struct objc_module { |
| 2841 | // unsigned long version; |
| 2842 | // unsigned long size; |
| 2843 | // const char *name; |
| 2844 | // Symtab symtab; |
| 2845 | // }; |
| 2846 | |
| 2847 | // FIXME: Get from somewhere |
| 2848 | static const int ModuleVersion = 7; |
| 2849 | |
| 2850 | void CGObjCMac::EmitModuleInfo() { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2851 | uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2852 | |
| 2853 | std::vector<llvm::Constant*> Values(4); |
| 2854 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion); |
| 2855 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2856 | // This used to be the filename, now it is unused. <rdr://4327263> |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2857 | Values[2] = GetClassName(&CGM.getContext().Idents.get("")); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2858 | Values[3] = EmitModuleSymbols(); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2859 | CreateMetadataVar("\01L_OBJC_MODULES", |
| 2860 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
| 2861 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2862 | 4, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2863 | } |
| 2864 | |
| 2865 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2866 | unsigned NumClasses = DefinedClasses.size(); |
| 2867 | unsigned NumCategories = DefinedCategories.size(); |
| 2868 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2869 | // Return null if no symbols were defined. |
| 2870 | if (!NumClasses && !NumCategories) |
| 2871 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
| 2872 | |
| 2873 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2874 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2875 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
| 2876 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 2877 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
| 2878 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2879 | // The runtime expects exactly the list of defined classes followed |
| 2880 | // by the list of defined categories, in a single array. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2881 | std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2882 | for (unsigned i=0; i<NumClasses; i++) |
| 2883 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 2884 | ObjCTypes.Int8PtrTy); |
| 2885 | for (unsigned i=0; i<NumCategories; i++) |
| 2886 | Symbols[NumClasses + i] = |
| 2887 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 2888 | ObjCTypes.Int8PtrTy); |
| 2889 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2890 | Values[4] = |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2891 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2892 | NumClasses + NumCategories), |
| 2893 | Symbols); |
| 2894 | |
| 2895 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2896 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2897 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2898 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 2899 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2900 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2901 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
| 2902 | } |
| 2903 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2904 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2905 | const ObjCInterfaceDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2906 | LazySymbols.insert(ID->getIdentifier()); |
| 2907 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2908 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 2909 | |
| 2910 | if (!Entry) { |
| 2911 | llvm::Constant *Casted = |
| 2912 | llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()), |
| 2913 | ObjCTypes.ClassPtrTy); |
| 2914 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2915 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 2916 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2917 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2918 | } |
| 2919 | |
| 2920 | return Builder.CreateLoad(Entry, false, "tmp"); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2921 | } |
| 2922 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2923 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2924 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 2925 | |
| 2926 | if (!Entry) { |
| 2927 | llvm::Constant *Casted = |
| 2928 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 2929 | ObjCTypes.SelectorPtrTy); |
| 2930 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2931 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 2932 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2933 | 4, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2934 | } |
| 2935 | |
| 2936 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 2937 | } |
| 2938 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 2939 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2940 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2941 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2942 | if (!Entry) |
| 2943 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 2944 | llvm::ConstantArray::get(Ident->getName()), |
| 2945 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 2946 | 1, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2947 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2948 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2949 | } |
| 2950 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 2951 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 2952 | /// ivar layout bitmap. |
| 2953 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
| 2954 | const ObjCCommonTypesHelper &ObjCTypes) { |
| 2955 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 2956 | } |
| 2957 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 2958 | static QualType::GCAttrTypes GetGCAttrTypeForType(ASTContext &Ctx, |
| 2959 | QualType FQT) { |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 2960 | if (FQT.isObjCGCStrong()) |
| 2961 | return QualType::Strong; |
| 2962 | |
| 2963 | if (FQT.isObjCGCWeak()) |
| 2964 | return QualType::Weak; |
| 2965 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 2966 | if (Ctx.isObjCObjectPointerType(FQT)) |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 2967 | return QualType::Strong; |
| 2968 | |
| 2969 | if (const PointerType *PT = FQT->getAsPointerType()) |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 2970 | return GetGCAttrTypeForType(Ctx, PT->getPointeeType()); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 2971 | |
| 2972 | return QualType::GCNone; |
| 2973 | } |
| 2974 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 2975 | void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT, |
| 2976 | unsigned int BytePos, |
| 2977 | bool ForStrongLayout, |
| 2978 | bool &HasUnion) { |
| 2979 | const RecordDecl *RD = RT->getDecl(); |
| 2980 | // FIXME - Use iterator. |
| 2981 | llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(CGM.getContext()), |
| 2982 | RD->field_end(CGM.getContext())); |
| 2983 | const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0)); |
| 2984 | const llvm::StructLayout *RecLayout = |
| 2985 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); |
| 2986 | |
| 2987 | BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos, |
| 2988 | ForStrongLayout, HasUnion); |
| 2989 | } |
| 2990 | |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 2991 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2992 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2993 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2994 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2995 | unsigned int BytePos, bool ForStrongLayout, |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 2996 | bool &HasUnion) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2997 | bool IsUnion = (RD && RD->isUnion()); |
| 2998 | uint64_t MaxUnionIvarSize = 0; |
| 2999 | uint64_t MaxSkippedUnionIvarSize = 0; |
| 3000 | FieldDecl *MaxField = 0; |
| 3001 | FieldDecl *MaxSkippedField = 0; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3002 | FieldDecl *LastFieldBitfield = 0; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3003 | uint64_t MaxFieldOffset = 0; |
| 3004 | uint64_t MaxSkippedFieldOffset = 0; |
| 3005 | uint64_t LastBitfieldOffset = 0; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3006 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3007 | if (RecFields.empty()) |
| 3008 | return; |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3009 | unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0); |
| 3010 | unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth(); |
| 3011 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3012 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3013 | FieldDecl *Field = RecFields[i]; |
Daniel Dunbar | e05cc98 | 2009-05-03 23:35:23 +0000 | [diff] [blame] | 3014 | uint64_t FieldOffset; |
| 3015 | if (RD) |
| 3016 | FieldOffset = |
| 3017 | Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); |
| 3018 | else |
| 3019 | FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)); |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3020 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3021 | // Skip over unnamed or bitfields |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3022 | if (!Field->getIdentifier() || Field->isBitField()) { |
| 3023 | LastFieldBitfield = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3024 | LastBitfieldOffset = FieldOffset; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3025 | continue; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3026 | } |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3027 | |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3028 | LastFieldBitfield = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3029 | QualType FQT = Field->getType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3030 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3031 | if (FQT->isUnionType()) |
| 3032 | HasUnion = true; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3033 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3034 | BuildAggrIvarRecordLayout(FQT->getAsRecordType(), |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3035 | BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3036 | ForStrongLayout, HasUnion); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3037 | continue; |
| 3038 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3039 | |
| 3040 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3041 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3042 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3043 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3044 | assert(CArray && "only array with known element size is supported"); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3045 | FQT = CArray->getElementType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3046 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 3047 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3048 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3049 | ElCount *= CArray->getSize().getZExtValue(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3050 | FQT = CArray->getElementType(); |
| 3051 | } |
| 3052 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3053 | assert(!FQT->isUnionType() && |
| 3054 | "layout for array of unions not supported"); |
| 3055 | if (FQT->isRecordType()) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3056 | int OldIndex = IvarsInfo.size() - 1; |
| 3057 | int OldSkIndex = SkipIvars.size() -1; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3058 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3059 | const RecordType *RT = FQT->getAsRecordType(); |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3060 | BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3061 | ForStrongLayout, HasUnion); |
| 3062 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3063 | // Replicate layout information for each array element. Note that |
| 3064 | // one element is already done. |
| 3065 | uint64_t ElIx = 1; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3066 | for (int FirstIndex = IvarsInfo.size() - 1, |
| 3067 | FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3068 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3069 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 3070 | IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx, |
| 3071 | IvarsInfo[i].ivar_size)); |
| 3072 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) |
| 3073 | SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx, |
| 3074 | SkipIvars[i].ivar_size)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3075 | } |
| 3076 | continue; |
| 3077 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3078 | } |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3079 | // At this point, we are done with Record/Union and array there of. |
| 3080 | // For other arrays we are down to its element type. |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3081 | QualType::GCAttrTypes GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3082 | |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3083 | unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType()); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3084 | if ((ForStrongLayout && GCAttr == QualType::Strong) |
| 3085 | || (!ForStrongLayout && GCAttr == QualType::Weak)) { |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3086 | if (IsUnion) { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3087 | uint64_t UnionIvarSize = FieldSize / WordSizeInBits; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3088 | if (UnionIvarSize > MaxUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3089 | MaxUnionIvarSize = UnionIvarSize; |
| 3090 | MaxField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3091 | MaxFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3092 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3093 | } else { |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3094 | IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3095 | FieldSize / WordSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3096 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3097 | } else if ((ForStrongLayout && |
| 3098 | (GCAttr == QualType::GCNone || GCAttr == QualType::Weak)) |
| 3099 | || (!ForStrongLayout && GCAttr != QualType::Weak)) { |
| 3100 | if (IsUnion) { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3101 | // FIXME: Why the asymmetry? We divide by word size in bits on |
| 3102 | // other side. |
| 3103 | uint64_t UnionIvarSize = FieldSize; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3104 | if (UnionIvarSize > MaxSkippedUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3105 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 3106 | MaxSkippedField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3107 | MaxSkippedFieldOffset = FieldOffset; |
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 { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3110 | // FIXME: Why the asymmetry, we divide by byte size in bits here? |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3111 | SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3112 | FieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3113 | } |
| 3114 | } |
| 3115 | } |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3116 | |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3117 | if (LastFieldBitfield) { |
| 3118 | // Last field was a bitfield. Must update skip info. |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3119 | Expr *BitWidth = LastFieldBitfield->getBitWidth(); |
| 3120 | uint64_t BitFieldSize = |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 3121 | BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue(); |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3122 | GC_IVAR skivar; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3123 | skivar.ivar_bytepos = BytePos + LastBitfieldOffset; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3124 | skivar.ivar_size = (BitFieldSize / ByteSizeInBits) |
| 3125 | + ((BitFieldSize % ByteSizeInBits) != 0); |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3126 | SkipIvars.push_back(skivar); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3127 | } |
| 3128 | |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3129 | if (MaxField) |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3130 | IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3131 | MaxUnionIvarSize)); |
| 3132 | if (MaxSkippedField) |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3133 | SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3134 | MaxSkippedUnionIvarSize)); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3135 | } |
| 3136 | |
| 3137 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 3138 | /// implementation for the __strong or __weak case. |
| 3139 | /// The layout map displays which words in ivar list must be skipped |
| 3140 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 3141 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
| 3142 | /// of words to skip and right nibble is count of words to scan. So, each |
| 3143 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
| 3144 | /// represented by a 0x00 byte which also ends the string. |
| 3145 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 3146 | /// - id, Class |
| 3147 | /// - object * |
| 3148 | /// - __strong anything |
| 3149 | /// |
| 3150 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 3151 | /// - __weak anything |
| 3152 | /// |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3153 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3154 | const ObjCImplementationDecl *OMD, |
| 3155 | bool ForStrongLayout) { |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3156 | bool hasUnion = false; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3157 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3158 | unsigned int WordsToScan, WordsToSkip; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3159 | const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 3160 | if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) |
| 3161 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3162 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3163 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3164 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3165 | CGM.getContext().CollectObjCIvars(OI, RecFields); |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 3166 | |
Daniel Dunbar | 3715328 | 2009-05-04 04:10:48 +0000 | [diff] [blame] | 3167 | // Add this implementations synthesized ivars. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 3168 | llvm::SmallVector<ObjCIvarDecl*, 16> Ivars; |
| 3169 | CGM.getContext().CollectSynthesizedIvars(OI, Ivars); |
| 3170 | for (unsigned k = 0, e = Ivars.size(); k != e; ++k) |
| 3171 | RecFields.push_back(cast<FieldDecl>(Ivars[k])); |
| 3172 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3173 | if (RecFields.empty()) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3174 | return llvm::Constant::getNullValue(PtrTy); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3175 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3176 | SkipIvars.clear(); |
| 3177 | IvarsInfo.clear(); |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 3178 | |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 3179 | BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion); |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3180 | if (IvarsInfo.empty()) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3181 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3182 | |
| 3183 | // Sort on byte position in case we encounterred a union nested in |
| 3184 | // the ivar list. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3185 | if (hasUnion && !IvarsInfo.empty()) |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 3186 | std::sort(IvarsInfo.begin(), IvarsInfo.end()); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3187 | if (hasUnion && !SkipIvars.empty()) |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 3188 | std::sort(SkipIvars.begin(), SkipIvars.end()); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3189 | |
| 3190 | // Build the string of skip/scan nibbles |
Fariborz Jahanian | 8c2f2d1 | 2009-04-24 17:15:27 +0000 | [diff] [blame] | 3191 | llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3192 | unsigned int WordSize = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3193 | CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3194 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 3195 | WordsToSkip = 0; |
| 3196 | WordsToScan = IvarsInfo[0].ivar_size; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3197 | } else { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3198 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 3199 | WordsToScan = IvarsInfo[0].ivar_size; |
| 3200 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3201 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3202 | unsigned int TailPrevGCObjC = |
| 3203 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3204 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3205 | // consecutive 'scanned' object pointers. |
| 3206 | WordsToScan += IvarsInfo[i].ivar_size; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3207 | } else { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3208 | // Skip over 'gc'able object pointer which lay over each other. |
| 3209 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 3210 | continue; |
| 3211 | // Must skip over 1 or more words. We save current skip/scan values |
| 3212 | // and start a new pair. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3213 | SKIP_SCAN SkScan; |
| 3214 | SkScan.skip = WordsToSkip; |
| 3215 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3216 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3217 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3218 | // Skip the hole. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3219 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 3220 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3221 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3222 | WordsToSkip = 0; |
| 3223 | WordsToScan = IvarsInfo[i].ivar_size; |
| 3224 | } |
| 3225 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3226 | if (WordsToScan > 0) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3227 | SKIP_SCAN SkScan; |
| 3228 | SkScan.skip = WordsToSkip; |
| 3229 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3230 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3231 | } |
| 3232 | |
| 3233 | bool BytesSkipped = false; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3234 | if (!SkipIvars.empty()) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3235 | unsigned int LastIndex = SkipIvars.size()-1; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3236 | int LastByteSkipped = |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3237 | SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size; |
| 3238 | LastIndex = IvarsInfo.size()-1; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3239 | int LastByteScanned = |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3240 | IvarsInfo[LastIndex].ivar_bytepos + |
| 3241 | IvarsInfo[LastIndex].ivar_size * WordSize; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3242 | BytesSkipped = (LastByteSkipped > LastByteScanned); |
| 3243 | // 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] | 3244 | if (BytesSkipped) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3245 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3246 | SKIP_SCAN SkScan; |
| 3247 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
| 3248 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3249 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3250 | } |
| 3251 | } |
| 3252 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 3253 | // as 0xMN. |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3254 | int SkipScan = SkipScanIvars.size()-1; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3255 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3256 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 3257 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 3258 | // 0xM0 followed by 0x0N detected. |
| 3259 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 3260 | for (int j = i+1; j < SkipScan; j++) |
| 3261 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 3262 | --SkipScan; |
| 3263 | } |
| 3264 | } |
| 3265 | |
| 3266 | // Generate the string. |
| 3267 | std::string BitMap; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3268 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3269 | unsigned char byte; |
| 3270 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 3271 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 3272 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 3273 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
| 3274 | |
| 3275 | if (skip_small > 0 || skip_big > 0) |
| 3276 | BytesSkipped = true; |
| 3277 | // first skip big. |
| 3278 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 3279 | BitMap += (unsigned char)(0xf0); |
| 3280 | |
| 3281 | // next (skip small, scan) |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3282 | if (skip_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3283 | byte = skip_small << 4; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3284 | if (scan_big > 0) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3285 | byte |= 0xf; |
| 3286 | --scan_big; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3287 | } else if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3288 | byte |= scan_small; |
| 3289 | scan_small = 0; |
| 3290 | } |
| 3291 | BitMap += byte; |
| 3292 | } |
| 3293 | // next scan big |
| 3294 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 3295 | BitMap += (unsigned char)(0x0f); |
| 3296 | // last scan small |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3297 | if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3298 | byte = scan_small; |
| 3299 | BitMap += byte; |
| 3300 | } |
| 3301 | } |
| 3302 | // null terminate string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3303 | unsigned char zero = 0; |
| 3304 | BitMap += zero; |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3305 | |
| 3306 | if (CGM.getLangOptions().ObjCGCBitmapPrint) { |
| 3307 | printf("\n%s ivar layout for class '%s': ", |
| 3308 | ForStrongLayout ? "strong" : "weak", |
| 3309 | OMD->getClassInterface()->getNameAsCString()); |
| 3310 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
| 3311 | for (unsigned i = 0; i < BitMap.size(); i++) |
| 3312 | if (!(s[i] & 0xf0)) |
| 3313 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3314 | else |
| 3315 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3316 | printf("\n"); |
| 3317 | } |
| 3318 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3319 | // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as |
| 3320 | // final layout. |
| 3321 | if (ForStrongLayout && !BytesSkipped) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3322 | return llvm::Constant::getNullValue(PtrTy); |
| 3323 | llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 3324 | llvm::ConstantArray::get(BitMap.c_str()), |
| 3325 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3326 | 1, true); |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3327 | return getConstantGEP(Entry, 0, 0); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3328 | } |
| 3329 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3330 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3331 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 3332 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3333 | // FIXME: Avoid std::string copying. |
| 3334 | if (!Entry) |
| 3335 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
| 3336 | llvm::ConstantArray::get(Sel.getAsString()), |
| 3337 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3338 | 1, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3339 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3340 | return getConstantGEP(Entry, 0, 0); |
| 3341 | } |
| 3342 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3343 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3344 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3345 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 3346 | } |
| 3347 | |
| 3348 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3349 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3350 | return GetMethodVarName(&CGM.getContext().Idents.get(Name)); |
| 3351 | } |
| 3352 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 3353 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3354 | std::string TypeStr; |
| 3355 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 3356 | |
| 3357 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3358 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3359 | if (!Entry) |
| 3360 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3361 | llvm::ConstantArray::get(TypeStr), |
| 3362 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3363 | 1, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3364 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3365 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3366 | } |
| 3367 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3368 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3369 | std::string TypeStr; |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3370 | CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D), |
| 3371 | TypeStr); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3372 | |
| 3373 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 3374 | |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3375 | if (!Entry) |
| 3376 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3377 | llvm::ConstantArray::get(TypeStr), |
| 3378 | "__TEXT,__cstring,cstring_literals", |
| 3379 | 1, true); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3380 | |
| 3381 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3382 | } |
| 3383 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3384 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3385 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3386 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
| 3387 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3388 | if (!Entry) |
| 3389 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
| 3390 | llvm::ConstantArray::get(Ident->getName()), |
| 3391 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3392 | 1, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3393 | |
| 3394 | return getConstantGEP(Entry, 0, 0); |
| 3395 | } |
| 3396 | |
| 3397 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3398 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3399 | llvm::Constant * |
| 3400 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 3401 | const Decl *Container) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3402 | std::string TypeStr; |
| 3403 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3404 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 3405 | } |
| 3406 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3407 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
| 3408 | const ObjCContainerDecl *CD, |
| 3409 | std::string &NameOut) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 3410 | NameOut = '\01'; |
| 3411 | NameOut += (D->isInstanceMethod() ? '-' : '+'); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3412 | NameOut += '['; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 3413 | assert (CD && "Missing container decl in GetNameForMethod"); |
| 3414 | NameOut += CD->getNameAsString(); |
Fariborz Jahanian | 1e9aef3 | 2009-04-16 18:34:20 +0000 | [diff] [blame] | 3415 | if (const ObjCCategoryImplDecl *CID = |
| 3416 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) { |
| 3417 | NameOut += '('; |
| 3418 | NameOut += CID->getNameAsString(); |
| 3419 | NameOut+= ')'; |
| 3420 | } |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3421 | NameOut += ' '; |
| 3422 | NameOut += D->getSelector().getAsString(); |
| 3423 | NameOut += ']'; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3424 | } |
| 3425 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3426 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3427 | EmitModuleInfo(); |
| 3428 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3429 | // Emit the dummy bodies for any protocols which were referenced but |
| 3430 | // never defined. |
| 3431 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
| 3432 | i = Protocols.begin(), e = Protocols.end(); i != e; ++i) { |
| 3433 | if (i->second->hasInitializer()) |
| 3434 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3435 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3436 | std::vector<llvm::Constant*> Values(5); |
| 3437 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 3438 | Values[1] = GetClassName(i->first); |
| 3439 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 3440 | Values[3] = Values[4] = |
| 3441 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 3442 | i->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 3443 | i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 3444 | Values)); |
| 3445 | } |
| 3446 | |
| 3447 | std::vector<llvm::Constant*> Used; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3448 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3449 | e = UsedGlobals.end(); i != e; ++i) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3450 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3451 | } |
| 3452 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3453 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3454 | llvm::GlobalValue *GV = |
| 3455 | new llvm::GlobalVariable(AT, false, |
| 3456 | llvm::GlobalValue::AppendingLinkage, |
| 3457 | llvm::ConstantArray::get(AT, Used), |
| 3458 | "llvm.used", |
| 3459 | &CGM.getModule()); |
| 3460 | |
| 3461 | GV->setSection("llvm.metadata"); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3462 | |
| 3463 | // Add assembler directives to add lazy undefined symbol references |
| 3464 | // for classes which are referenced but not defined. This is |
| 3465 | // important for correct linker interaction. |
| 3466 | |
| 3467 | // FIXME: Uh, this isn't particularly portable. |
| 3468 | std::stringstream s; |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3469 | |
| 3470 | if (!CGM.getModule().getModuleInlineAsm().empty()) |
| 3471 | s << "\n"; |
| 3472 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3473 | for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(), |
| 3474 | e = LazySymbols.end(); i != e; ++i) { |
| 3475 | s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n"; |
| 3476 | } |
| 3477 | for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(), |
| 3478 | e = DefinedSymbols.end(); i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3479 | s << "\t.objc_class_name_" << (*i)->getName() << "=0\n" |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3480 | << "\t.globl .objc_class_name_" << (*i)->getName() << "\n"; |
| 3481 | } |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3482 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3483 | CGM.getModule().appendModuleInlineAsm(s.str()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3484 | } |
| 3485 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3486 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3487 | : CGObjCCommonMac(cgm), |
| 3488 | ObjCTypes(cgm) |
| 3489 | { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3490 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3491 | ObjCABI = 2; |
| 3492 | } |
| 3493 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3494 | /* *** */ |
| 3495 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3496 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3497 | : CGM(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3498 | { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3499 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3500 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3501 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3502 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3503 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3504 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3505 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3506 | Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 3507 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3508 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 3509 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3510 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3511 | |
| 3512 | // FIXME: It would be nice to unify this with the opaque type, so |
| 3513 | // that the IR comes out a bit cleaner. |
| 3514 | const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 3515 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3516 | |
| 3517 | // I'm not sure I like this. The implicit coordination is a bit |
| 3518 | // gross. We should solve this in a reasonable fashion because this |
| 3519 | // is a pretty common task (match some runtime data structure with |
| 3520 | // an LLVM data structure). |
| 3521 | |
| 3522 | // FIXME: This is leaked. |
| 3523 | // FIXME: Merge with rewriter code? |
| 3524 | |
| 3525 | // struct _objc_super { |
| 3526 | // id self; |
| 3527 | // Class cls; |
| 3528 | // } |
| 3529 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3530 | SourceLocation(), |
| 3531 | &Ctx.Idents.get("_objc_super")); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3532 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3533 | Ctx.getObjCIdType(), 0, false)); |
| 3534 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3535 | Ctx.getObjCClassType(), 0, false)); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3536 | RD->completeDefinition(Ctx); |
| 3537 | |
| 3538 | SuperCTy = Ctx.getTagDeclType(RD); |
| 3539 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
| 3540 | |
| 3541 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3542 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 3543 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3544 | // struct _prop_t { |
| 3545 | // char *name; |
| 3546 | // char *attributes; |
| 3547 | // } |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 3548 | PropertyTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, NULL); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3549 | CGM.getModule().addTypeName("struct._prop_t", |
| 3550 | PropertyTy); |
| 3551 | |
| 3552 | // struct _prop_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3553 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3554 | // uint32_t count_of_properties; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3555 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3556 | // } |
| 3557 | PropertyListTy = llvm::StructType::get(IntTy, |
| 3558 | IntTy, |
| 3559 | llvm::ArrayType::get(PropertyTy, 0), |
| 3560 | NULL); |
| 3561 | CGM.getModule().addTypeName("struct._prop_list_t", |
| 3562 | PropertyListTy); |
| 3563 | // struct _prop_list_t * |
| 3564 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
| 3565 | |
| 3566 | // struct _objc_method { |
| 3567 | // SEL _cmd; |
| 3568 | // char *method_type; |
| 3569 | // char *_imp; |
| 3570 | // } |
| 3571 | MethodTy = llvm::StructType::get(SelectorPtrTy, |
| 3572 | Int8PtrTy, |
| 3573 | Int8PtrTy, |
| 3574 | NULL); |
| 3575 | CGM.getModule().addTypeName("struct._objc_method", MethodTy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3576 | |
| 3577 | // struct _objc_cache * |
| 3578 | CacheTy = llvm::OpaqueType::get(); |
| 3579 | CGM.getModule().addTypeName("struct._objc_cache", CacheTy); |
| 3580 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3581 | } |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3582 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3583 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3584 | : ObjCCommonTypesHelper(cgm) |
| 3585 | { |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3586 | // struct _objc_method_description { |
| 3587 | // SEL name; |
| 3588 | // char *types; |
| 3589 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3590 | MethodDescriptionTy = |
| 3591 | llvm::StructType::get(SelectorPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3592 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3593 | NULL); |
| 3594 | CGM.getModule().addTypeName("struct._objc_method_description", |
| 3595 | MethodDescriptionTy); |
| 3596 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3597 | // struct _objc_method_description_list { |
| 3598 | // int count; |
| 3599 | // struct _objc_method_description[1]; |
| 3600 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3601 | MethodDescriptionListTy = |
| 3602 | llvm::StructType::get(IntTy, |
| 3603 | llvm::ArrayType::get(MethodDescriptionTy, 0), |
| 3604 | NULL); |
| 3605 | CGM.getModule().addTypeName("struct._objc_method_description_list", |
| 3606 | MethodDescriptionListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3607 | |
| 3608 | // struct _objc_method_description_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3609 | MethodDescriptionListPtrTy = |
| 3610 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
| 3611 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3612 | // Protocol description structures |
| 3613 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3614 | // struct _objc_protocol_extension { |
| 3615 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 3616 | // struct _objc_method_description_list *optional_instance_methods; |
| 3617 | // struct _objc_method_description_list *optional_class_methods; |
| 3618 | // struct _objc_property_list *instance_properties; |
| 3619 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3620 | ProtocolExtensionTy = |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3621 | llvm::StructType::get(IntTy, |
| 3622 | MethodDescriptionListPtrTy, |
| 3623 | MethodDescriptionListPtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3624 | PropertyListPtrTy, |
| 3625 | NULL); |
| 3626 | CGM.getModule().addTypeName("struct._objc_protocol_extension", |
| 3627 | ProtocolExtensionTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3628 | |
| 3629 | // struct _objc_protocol_extension * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3630 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
| 3631 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3632 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3633 | |
| 3634 | llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(); |
| 3635 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3636 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3637 | const llvm::Type *T = |
| 3638 | llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3639 | LongTy, |
| 3640 | llvm::ArrayType::get(ProtocolTyHolder, 0), |
| 3641 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3642 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T); |
| 3643 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3644 | // struct _objc_protocol { |
| 3645 | // struct _objc_protocol_extension *isa; |
| 3646 | // char *protocol_name; |
| 3647 | // struct _objc_protocol **_objc_protocol_list; |
| 3648 | // struct _objc_method_description_list *instance_methods; |
| 3649 | // struct _objc_method_description_list *class_methods; |
| 3650 | // } |
| 3651 | T = llvm::StructType::get(ProtocolExtensionPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3652 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3653 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3654 | MethodDescriptionListPtrTy, |
| 3655 | MethodDescriptionListPtrTy, |
| 3656 | NULL); |
| 3657 | cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T); |
| 3658 | |
| 3659 | ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get()); |
| 3660 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3661 | ProtocolListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3662 | // struct _objc_protocol_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3663 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
| 3664 | |
| 3665 | ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get()); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3666 | CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3667 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3668 | |
| 3669 | // Class description structures |
| 3670 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3671 | // struct _objc_ivar { |
| 3672 | // char *ivar_name; |
| 3673 | // char *ivar_type; |
| 3674 | // int ivar_offset; |
| 3675 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3676 | IvarTy = llvm::StructType::get(Int8PtrTy, |
| 3677 | Int8PtrTy, |
| 3678 | IntTy, |
| 3679 | NULL); |
| 3680 | CGM.getModule().addTypeName("struct._objc_ivar", IvarTy); |
| 3681 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3682 | // struct _objc_ivar_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3683 | IvarListTy = llvm::OpaqueType::get(); |
| 3684 | CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy); |
| 3685 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
| 3686 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3687 | // struct _objc_method_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3688 | MethodListTy = llvm::OpaqueType::get(); |
| 3689 | CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy); |
| 3690 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
| 3691 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3692 | // struct _objc_class_extension * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3693 | ClassExtensionTy = |
| 3694 | llvm::StructType::get(IntTy, |
| 3695 | Int8PtrTy, |
| 3696 | PropertyListPtrTy, |
| 3697 | NULL); |
| 3698 | CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy); |
| 3699 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
| 3700 | |
| 3701 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3702 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3703 | // struct _objc_class { |
| 3704 | // Class isa; |
| 3705 | // Class super_class; |
| 3706 | // char *name; |
| 3707 | // long version; |
| 3708 | // long info; |
| 3709 | // long instance_size; |
| 3710 | // struct _objc_ivar_list *ivars; |
| 3711 | // struct _objc_method_list *methods; |
| 3712 | // struct _objc_cache *cache; |
| 3713 | // struct _objc_protocol_list *protocols; |
| 3714 | // char *ivar_layout; |
| 3715 | // struct _objc_class_ext *ext; |
| 3716 | // }; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3717 | T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3718 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3719 | Int8PtrTy, |
| 3720 | LongTy, |
| 3721 | LongTy, |
| 3722 | LongTy, |
| 3723 | IvarListPtrTy, |
| 3724 | MethodListPtrTy, |
| 3725 | CachePtrTy, |
| 3726 | ProtocolListPtrTy, |
| 3727 | Int8PtrTy, |
| 3728 | ClassExtensionPtrTy, |
| 3729 | NULL); |
| 3730 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T); |
| 3731 | |
| 3732 | ClassTy = cast<llvm::StructType>(ClassTyHolder.get()); |
| 3733 | CGM.getModule().addTypeName("struct._objc_class", ClassTy); |
| 3734 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
| 3735 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3736 | // struct _objc_category { |
| 3737 | // char *category_name; |
| 3738 | // char *class_name; |
| 3739 | // struct _objc_method_list *instance_method; |
| 3740 | // struct _objc_method_list *class_method; |
| 3741 | // uint32_t size; // sizeof(struct _objc_category) |
| 3742 | // struct _objc_property_list *instance_properties;// category's @property |
| 3743 | // } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3744 | CategoryTy = llvm::StructType::get(Int8PtrTy, |
| 3745 | Int8PtrTy, |
| 3746 | MethodListPtrTy, |
| 3747 | MethodListPtrTy, |
| 3748 | ProtocolListPtrTy, |
| 3749 | IntTy, |
| 3750 | PropertyListPtrTy, |
| 3751 | NULL); |
| 3752 | CGM.getModule().addTypeName("struct._objc_category", CategoryTy); |
| 3753 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3754 | // Global metadata structures |
| 3755 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3756 | // struct _objc_symtab { |
| 3757 | // long sel_ref_cnt; |
| 3758 | // SEL *refs; |
| 3759 | // short cls_def_cnt; |
| 3760 | // short cat_def_cnt; |
| 3761 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 3762 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3763 | SymtabTy = llvm::StructType::get(LongTy, |
| 3764 | SelectorPtrTy, |
| 3765 | ShortTy, |
| 3766 | ShortTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3767 | llvm::ArrayType::get(Int8PtrTy, 0), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3768 | NULL); |
| 3769 | CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy); |
| 3770 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
| 3771 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3772 | // struct _objc_module { |
| 3773 | // long version; |
| 3774 | // long size; // sizeof(struct _objc_module) |
| 3775 | // char *name; |
| 3776 | // struct _objc_symtab* symtab; |
| 3777 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3778 | ModuleTy = |
| 3779 | llvm::StructType::get(LongTy, |
| 3780 | LongTy, |
| 3781 | Int8PtrTy, |
| 3782 | SymtabPtrTy, |
| 3783 | NULL); |
| 3784 | CGM.getModule().addTypeName("struct._objc_module", ModuleTy); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3785 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 3786 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3787 | // FIXME: This is the size of the setjmp buffer and should be |
| 3788 | // target specific. 18 is what's used on 32-bit X86. |
| 3789 | uint64_t SetJmpBufferSize = 18; |
| 3790 | |
| 3791 | // Exceptions |
| 3792 | const llvm::Type *StackPtrTy = |
Daniel Dunbar | 1000491 | 2008-09-27 06:32:25 +0000 | [diff] [blame] | 3793 | llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3794 | |
| 3795 | ExceptionDataTy = |
| 3796 | llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty, |
| 3797 | SetJmpBufferSize), |
| 3798 | StackPtrTy, NULL); |
| 3799 | CGM.getModule().addTypeName("struct._objc_exception_data", |
| 3800 | ExceptionDataTy); |
| 3801 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3802 | } |
| 3803 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3804 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3805 | : ObjCCommonTypesHelper(cgm) |
| 3806 | { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3807 | // struct _method_list_t { |
| 3808 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 3809 | // uint32_t method_count; |
| 3810 | // struct _objc_method method_list[method_count]; |
| 3811 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3812 | MethodListnfABITy = llvm::StructType::get(IntTy, |
| 3813 | IntTy, |
| 3814 | llvm::ArrayType::get(MethodTy, 0), |
| 3815 | NULL); |
| 3816 | CGM.getModule().addTypeName("struct.__method_list_t", |
| 3817 | MethodListnfABITy); |
| 3818 | // struct method_list_t * |
| 3819 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3820 | |
| 3821 | // struct _protocol_t { |
| 3822 | // id isa; // NULL |
| 3823 | // const char * const protocol_name; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3824 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3825 | // const struct method_list_t * const instance_methods; |
| 3826 | // const struct method_list_t * const class_methods; |
| 3827 | // const struct method_list_t *optionalInstanceMethods; |
| 3828 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3829 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3830 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 3831 | // const uint32_t flags; // = 0 |
| 3832 | // } |
| 3833 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3834 | // Holder for struct _protocol_list_t * |
| 3835 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3836 | |
| 3837 | ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy, |
| 3838 | Int8PtrTy, |
| 3839 | llvm::PointerType::getUnqual( |
| 3840 | ProtocolListTyHolder), |
| 3841 | MethodListnfABIPtrTy, |
| 3842 | MethodListnfABIPtrTy, |
| 3843 | MethodListnfABIPtrTy, |
| 3844 | MethodListnfABIPtrTy, |
| 3845 | PropertyListPtrTy, |
| 3846 | IntTy, |
| 3847 | IntTy, |
| 3848 | NULL); |
| 3849 | CGM.getModule().addTypeName("struct._protocol_t", |
| 3850 | ProtocolnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3851 | |
| 3852 | // struct _protocol_t* |
| 3853 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3854 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3855 | // struct _protocol_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3856 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3857 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3858 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3859 | ProtocolListnfABITy = llvm::StructType::get(LongTy, |
| 3860 | llvm::ArrayType::get( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3861 | ProtocolnfABIPtrTy, 0), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3862 | NULL); |
| 3863 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3864 | ProtocolListnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3865 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo( |
| 3866 | ProtocolListnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3867 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3868 | // struct _objc_protocol_list* |
| 3869 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3870 | |
| 3871 | // struct _ivar_t { |
| 3872 | // unsigned long int *offset; // pointer to ivar offset location |
| 3873 | // char *name; |
| 3874 | // char *type; |
| 3875 | // uint32_t alignment; |
| 3876 | // uint32_t size; |
| 3877 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3878 | IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy), |
| 3879 | Int8PtrTy, |
| 3880 | Int8PtrTy, |
| 3881 | IntTy, |
| 3882 | IntTy, |
| 3883 | NULL); |
| 3884 | CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy); |
| 3885 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3886 | // struct _ivar_list_t { |
| 3887 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 3888 | // uint32 count; |
| 3889 | // struct _iver_t list[count]; |
| 3890 | // } |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3891 | IvarListnfABITy = llvm::StructType::get(IntTy, |
| 3892 | IntTy, |
| 3893 | llvm::ArrayType::get( |
| 3894 | IvarnfABITy, 0), |
| 3895 | NULL); |
| 3896 | CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy); |
| 3897 | |
| 3898 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3899 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3900 | // struct _class_ro_t { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3901 | // uint32_t const flags; |
| 3902 | // uint32_t const instanceStart; |
| 3903 | // uint32_t const instanceSize; |
| 3904 | // uint32_t const reserved; // only when building for 64bit targets |
| 3905 | // const uint8_t * const ivarLayout; |
| 3906 | // const char *const name; |
| 3907 | // const struct _method_list_t * const baseMethods; |
| 3908 | // const struct _objc_protocol_list *const baseProtocols; |
| 3909 | // const struct _ivar_list_t *const ivars; |
| 3910 | // const uint8_t * const weakIvarLayout; |
| 3911 | // const struct _prop_list_t * const properties; |
| 3912 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3913 | |
| 3914 | // FIXME. Add 'reserved' field in 64bit abi mode! |
| 3915 | ClassRonfABITy = llvm::StructType::get(IntTy, |
| 3916 | IntTy, |
| 3917 | IntTy, |
| 3918 | Int8PtrTy, |
| 3919 | Int8PtrTy, |
| 3920 | MethodListnfABIPtrTy, |
| 3921 | ProtocolListnfABIPtrTy, |
| 3922 | IvarListnfABIPtrTy, |
| 3923 | Int8PtrTy, |
| 3924 | PropertyListPtrTy, |
| 3925 | NULL); |
| 3926 | CGM.getModule().addTypeName("struct._class_ro_t", |
| 3927 | ClassRonfABITy); |
| 3928 | |
| 3929 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 3930 | std::vector<const llvm::Type*> Params; |
| 3931 | Params.push_back(ObjectPtrTy); |
| 3932 | Params.push_back(SelectorPtrTy); |
| 3933 | ImpnfABITy = llvm::PointerType::getUnqual( |
| 3934 | llvm::FunctionType::get(ObjectPtrTy, Params, false)); |
| 3935 | |
| 3936 | // struct _class_t { |
| 3937 | // struct _class_t *isa; |
| 3938 | // struct _class_t * const superclass; |
| 3939 | // void *cache; |
| 3940 | // IMP *vtable; |
| 3941 | // struct class_ro_t *ro; |
| 3942 | // } |
| 3943 | |
| 3944 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3945 | ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3946 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3947 | CachePtrTy, |
| 3948 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 3949 | llvm::PointerType::getUnqual( |
| 3950 | ClassRonfABITy), |
| 3951 | NULL); |
| 3952 | CGM.getModule().addTypeName("struct._class_t", ClassnfABITy); |
| 3953 | |
| 3954 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo( |
| 3955 | ClassnfABITy); |
| 3956 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3957 | // LLVM for struct _class_t * |
| 3958 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
| 3959 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3960 | // struct _category_t { |
| 3961 | // const char * const name; |
| 3962 | // struct _class_t *const cls; |
| 3963 | // const struct _method_list_t * const instance_methods; |
| 3964 | // const struct _method_list_t * const class_methods; |
| 3965 | // const struct _protocol_list_t * const protocols; |
| 3966 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3967 | // } |
| 3968 | CategorynfABITy = llvm::StructType::get(Int8PtrTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3969 | ClassnfABIPtrTy, |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3970 | MethodListnfABIPtrTy, |
| 3971 | MethodListnfABIPtrTy, |
| 3972 | ProtocolListnfABIPtrTy, |
| 3973 | PropertyListPtrTy, |
| 3974 | NULL); |
| 3975 | CGM.getModule().addTypeName("struct._category_t", CategorynfABITy); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3976 | |
| 3977 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3978 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3979 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3980 | |
| 3981 | // MessageRefTy - LLVM for: |
| 3982 | // struct _message_ref_t { |
| 3983 | // IMP messenger; |
| 3984 | // SEL name; |
| 3985 | // }; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3986 | |
| 3987 | // First the clang type for struct _message_ref_t |
| 3988 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3989 | SourceLocation(), |
| 3990 | &Ctx.Idents.get("_message_ref_t")); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3991 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3992 | Ctx.VoidPtrTy, 0, false)); |
| 3993 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3994 | Ctx.getObjCSelType(), 0, false)); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3995 | RD->completeDefinition(Ctx); |
| 3996 | |
| 3997 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 3998 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 3999 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4000 | |
| 4001 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 4002 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
| 4003 | |
| 4004 | // SuperMessageRefTy - LLVM for: |
| 4005 | // struct _super_message_ref_t { |
| 4006 | // SUPER_IMP messenger; |
| 4007 | // SEL name; |
| 4008 | // }; |
| 4009 | SuperMessageRefTy = llvm::StructType::get(ImpnfABITy, |
| 4010 | SelectorPtrTy, |
| 4011 | NULL); |
| 4012 | CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy); |
| 4013 | |
| 4014 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 4015 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
| 4016 | |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 4017 | |
| 4018 | // struct objc_typeinfo { |
| 4019 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 4020 | // const char* name; // c++ typeinfo string |
| 4021 | // Class cls; |
| 4022 | // }; |
| 4023 | EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy), |
| 4024 | Int8PtrTy, |
| 4025 | ClassnfABIPtrTy, |
| 4026 | NULL); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 4027 | CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 4028 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4029 | } |
| 4030 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4031 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
| 4032 | FinishNonFragileABIModule(); |
| 4033 | |
| 4034 | return NULL; |
| 4035 | } |
| 4036 | |
| 4037 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 4038 | // nonfragile abi has no module definition. |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4039 | |
| 4040 | // Build list of all implemented classe addresses in array |
| 4041 | // L_OBJC_LABEL_CLASS_$. |
| 4042 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$ |
| 4043 | // list of 'nonlazy' implementations (defined as those with a +load{} |
| 4044 | // method!!). |
| 4045 | unsigned NumClasses = DefinedClasses.size(); |
| 4046 | if (NumClasses) { |
| 4047 | std::vector<llvm::Constant*> Symbols(NumClasses); |
| 4048 | for (unsigned i=0; i<NumClasses; i++) |
| 4049 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 4050 | ObjCTypes.Int8PtrTy); |
| 4051 | llvm::Constant* Init = |
| 4052 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 4053 | NumClasses), |
| 4054 | Symbols); |
| 4055 | |
| 4056 | llvm::GlobalVariable *GV = |
| 4057 | new llvm::GlobalVariable(Init->getType(), false, |
| 4058 | llvm::GlobalValue::InternalLinkage, |
| 4059 | Init, |
| 4060 | "\01L_OBJC_LABEL_CLASS_$", |
| 4061 | &CGM.getModule()); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 4062 | GV->setAlignment(8); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4063 | GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip"); |
| 4064 | UsedGlobals.push_back(GV); |
| 4065 | } |
| 4066 | |
| 4067 | // Build list of all implemented category addresses in array |
| 4068 | // L_OBJC_LABEL_CATEGORY_$. |
| 4069 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$ |
| 4070 | // list of 'nonlazy' category implementations (defined as those with a +load{} |
| 4071 | // method!!). |
| 4072 | unsigned NumCategory = DefinedCategories.size(); |
| 4073 | if (NumCategory) { |
| 4074 | std::vector<llvm::Constant*> Symbols(NumCategory); |
| 4075 | for (unsigned i=0; i<NumCategory; i++) |
| 4076 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 4077 | ObjCTypes.Int8PtrTy); |
| 4078 | llvm::Constant* Init = |
| 4079 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 4080 | NumCategory), |
| 4081 | Symbols); |
| 4082 | |
| 4083 | llvm::GlobalVariable *GV = |
| 4084 | new llvm::GlobalVariable(Init->getType(), false, |
| 4085 | llvm::GlobalValue::InternalLinkage, |
| 4086 | Init, |
| 4087 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 4088 | &CGM.getModule()); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 4089 | GV->setAlignment(8); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4090 | GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip"); |
| 4091 | UsedGlobals.push_back(GV); |
| 4092 | } |
| 4093 | |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4094 | // static int L_OBJC_IMAGE_INFO[2] = { 0, flags }; |
| 4095 | // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0 |
| 4096 | std::vector<llvm::Constant*> Values(2); |
| 4097 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0); |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4098 | unsigned int flags = 0; |
Fariborz Jahanian | 66a5c2c | 2009-02-24 23:34:44 +0000 | [diff] [blame] | 4099 | // FIXME: Fix and continue? |
| 4100 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 4101 | flags |= eImageInfo_GarbageCollected; |
| 4102 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 4103 | flags |= eImageInfo_GCOnly; |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4104 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4105 | llvm::Constant* Init = llvm::ConstantArray::get( |
| 4106 | llvm::ArrayType::get(ObjCTypes.IntTy, 2), |
| 4107 | Values); |
| 4108 | llvm::GlobalVariable *IMGV = |
| 4109 | new llvm::GlobalVariable(Init->getType(), false, |
| 4110 | llvm::GlobalValue::InternalLinkage, |
| 4111 | Init, |
| 4112 | "\01L_OBJC_IMAGE_INFO", |
| 4113 | &CGM.getModule()); |
| 4114 | IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); |
Daniel Dunbar | 325f758 | 2009-04-23 08:03:21 +0000 | [diff] [blame] | 4115 | IMGV->setConstant(true); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4116 | UsedGlobals.push_back(IMGV); |
| 4117 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4118 | std::vector<llvm::Constant*> Used; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4119 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4120 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
| 4121 | e = UsedGlobals.end(); i != e; ++i) { |
| 4122 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
| 4123 | } |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4124 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4125 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
| 4126 | llvm::GlobalValue *GV = |
| 4127 | new llvm::GlobalVariable(AT, false, |
| 4128 | llvm::GlobalValue::AppendingLinkage, |
| 4129 | llvm::ConstantArray::get(AT, Used), |
| 4130 | "llvm.used", |
| 4131 | &CGM.getModule()); |
| 4132 | |
| 4133 | GV->setSection("llvm.metadata"); |
| 4134 | |
| 4135 | } |
| 4136 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4137 | /// LegacyDispatchedSelector - Returns true if SEL is not in the list of |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4138 | /// NonLegacyDispatchMethods; false otherwise. What this means is that |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4139 | /// except for the 19 selectors in the list, we generate 32bit-style |
| 4140 | /// message dispatch call for all the rest. |
| 4141 | /// |
| 4142 | bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) { |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4143 | if (NonLegacyDispatchMethods.empty()) { |
| 4144 | NonLegacyDispatchMethods.insert(GetNullarySelector("alloc")); |
| 4145 | NonLegacyDispatchMethods.insert(GetNullarySelector("class")); |
| 4146 | NonLegacyDispatchMethods.insert(GetNullarySelector("self")); |
| 4147 | NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped")); |
| 4148 | NonLegacyDispatchMethods.insert(GetNullarySelector("length")); |
| 4149 | NonLegacyDispatchMethods.insert(GetNullarySelector("count")); |
| 4150 | NonLegacyDispatchMethods.insert(GetNullarySelector("retain")); |
| 4151 | NonLegacyDispatchMethods.insert(GetNullarySelector("release")); |
| 4152 | NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease")); |
| 4153 | NonLegacyDispatchMethods.insert(GetNullarySelector("hash")); |
| 4154 | |
| 4155 | NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone")); |
| 4156 | NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass")); |
| 4157 | NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector")); |
| 4158 | NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey")); |
| 4159 | NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex")); |
| 4160 | NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString")); |
| 4161 | NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual")); |
| 4162 | NonLegacyDispatchMethods.insert(GetUnarySelector("addObject")); |
Fariborz Jahanian | be53be4 | 2009-05-13 16:19:02 +0000 | [diff] [blame^] | 4163 | // "countByEnumeratingWithState:objects:count" |
| 4164 | IdentifierInfo *KeyIdents[] = { |
| 4165 | &CGM.getContext().Idents.get("countByEnumeratingWithState"), |
| 4166 | &CGM.getContext().Idents.get("objects"), |
| 4167 | &CGM.getContext().Idents.get("count") |
| 4168 | }; |
| 4169 | NonLegacyDispatchMethods.insert( |
| 4170 | CGM.getContext().Selectors.getSelector(3, KeyIdents)); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4171 | } |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4172 | return (NonLegacyDispatchMethods.count(Sel) == 0); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4173 | } |
| 4174 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4175 | // Metadata flags |
| 4176 | enum MetaDataDlags { |
| 4177 | CLS = 0x0, |
| 4178 | CLS_META = 0x1, |
| 4179 | CLS_ROOT = 0x2, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4180 | OBJC2_CLS_HIDDEN = 0x10, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4181 | CLS_EXCEPTION = 0x20 |
| 4182 | }; |
| 4183 | /// BuildClassRoTInitializer - generate meta-data for: |
| 4184 | /// struct _class_ro_t { |
| 4185 | /// uint32_t const flags; |
| 4186 | /// uint32_t const instanceStart; |
| 4187 | /// uint32_t const instanceSize; |
| 4188 | /// uint32_t const reserved; // only when building for 64bit targets |
| 4189 | /// const uint8_t * const ivarLayout; |
| 4190 | /// const char *const name; |
| 4191 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4192 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4193 | /// const struct _ivar_list_t *const ivars; |
| 4194 | /// const uint8_t * const weakIvarLayout; |
| 4195 | /// const struct _prop_list_t * const properties; |
| 4196 | /// } |
| 4197 | /// |
| 4198 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
| 4199 | unsigned flags, |
| 4200 | unsigned InstanceStart, |
| 4201 | unsigned InstanceSize, |
| 4202 | const ObjCImplementationDecl *ID) { |
| 4203 | std::string ClassName = ID->getNameAsString(); |
| 4204 | std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets! |
| 4205 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 4206 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 4207 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
| 4208 | // FIXME. For 64bit targets add 0 here. |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 4209 | Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4210 | : BuildIvarLayout(ID, true); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4211 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4212 | // const struct _method_list_t * const baseMethods; |
| 4213 | std::vector<llvm::Constant*> Methods; |
| 4214 | std::string MethodListName("\01l_OBJC_$_"); |
| 4215 | if (flags & CLS_META) { |
| 4216 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4217 | for (ObjCImplementationDecl::classmeth_iterator |
| 4218 | i = ID->classmeth_begin(CGM.getContext()), |
| 4219 | e = ID->classmeth_end(CGM.getContext()); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4220 | // Class methods should always be defined. |
| 4221 | Methods.push_back(GetMethodConstant(*i)); |
| 4222 | } |
| 4223 | } else { |
| 4224 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4225 | for (ObjCImplementationDecl::instmeth_iterator |
| 4226 | i = ID->instmeth_begin(CGM.getContext()), |
| 4227 | e = ID->instmeth_end(CGM.getContext()); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4228 | // Instance methods should always be defined. |
| 4229 | Methods.push_back(GetMethodConstant(*i)); |
| 4230 | } |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4231 | for (ObjCImplementationDecl::propimpl_iterator |
| 4232 | i = ID->propimpl_begin(CGM.getContext()), |
| 4233 | e = ID->propimpl_end(CGM.getContext()); i != e; ++i) { |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4234 | ObjCPropertyImplDecl *PID = *i; |
| 4235 | |
| 4236 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 4237 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 4238 | |
| 4239 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 4240 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4241 | Methods.push_back(C); |
| 4242 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 4243 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4244 | Methods.push_back(C); |
| 4245 | } |
| 4246 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4247 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4248 | Values[ 5] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4249 | "__DATA, __objc_const", Methods); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4250 | |
| 4251 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4252 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
| 4253 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
| 4254 | + OID->getNameAsString(), |
| 4255 | OID->protocol_begin(), |
| 4256 | OID->protocol_end()); |
| 4257 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4258 | if (flags & CLS_META) |
| 4259 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4260 | else |
| 4261 | Values[ 7] = EmitIvarList(ID); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 4262 | Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4263 | : BuildIvarLayout(ID, false); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4264 | if (flags & CLS_META) |
| 4265 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4266 | else |
| 4267 | Values[ 9] = |
| 4268 | EmitPropertyList( |
| 4269 | "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
| 4270 | ID, ID->getClassInterface(), ObjCTypes); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4271 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
| 4272 | Values); |
| 4273 | llvm::GlobalVariable *CLASS_RO_GV = |
| 4274 | new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false, |
| 4275 | llvm::GlobalValue::InternalLinkage, |
| 4276 | Init, |
| 4277 | (flags & CLS_META) ? |
| 4278 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 4279 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName, |
| 4280 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4281 | CLASS_RO_GV->setAlignment( |
| 4282 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4283 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4284 | return CLASS_RO_GV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4285 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4286 | } |
| 4287 | |
| 4288 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 4289 | /// for the given ClassName for: |
| 4290 | /// struct _class_t { |
| 4291 | /// struct _class_t *isa; |
| 4292 | /// struct _class_t * const superclass; |
| 4293 | /// void *cache; |
| 4294 | /// IMP *vtable; |
| 4295 | /// struct class_ro_t *ro; |
| 4296 | /// } |
| 4297 | /// |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4298 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
| 4299 | std::string &ClassName, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4300 | llvm::Constant *IsAGV, |
| 4301 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4302 | llvm::Constant *ClassRoGV, |
| 4303 | bool HiddenVisibility) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4304 | std::vector<llvm::Constant*> Values(5); |
| 4305 | Values[0] = IsAGV; |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4306 | Values[1] = SuperClassGV |
| 4307 | ? SuperClassGV |
| 4308 | : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4309 | Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar |
| 4310 | Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar |
| 4311 | Values[4] = ClassRoGV; // &CLASS_RO_GV |
| 4312 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
| 4313 | Values); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4314 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 4315 | GV->setInitializer(Init); |
Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 4316 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4317 | GV->setAlignment( |
| 4318 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4319 | if (HiddenVisibility) |
| 4320 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4321 | return GV; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4322 | } |
| 4323 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4324 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4325 | uint32_t &InstanceStart, |
| 4326 | uint32_t &InstanceSize) { |
Daniel Dunbar | b4c79e0 | 2009-05-04 21:26:30 +0000 | [diff] [blame] | 4327 | const ASTRecordLayout &RL = |
| 4328 | CGM.getContext().getASTObjCImplementationLayout(OID); |
| 4329 | |
Daniel Dunbar | 6e8575b | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 4330 | // InstanceSize is really instance end. |
Daniel Dunbar | b4c79e0 | 2009-05-04 21:26:30 +0000 | [diff] [blame] | 4331 | InstanceSize = llvm::RoundUpToAlignment(RL.getNextOffset(), 8) / 8; |
Daniel Dunbar | 6e8575b | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 4332 | |
| 4333 | // If there are no fields, the start is the same as the end. |
| 4334 | if (!RL.getFieldCount()) |
| 4335 | InstanceStart = InstanceSize; |
| 4336 | else |
| 4337 | InstanceStart = RL.getFieldOffset(0) / 8; |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4338 | } |
| 4339 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4340 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 4341 | std::string ClassName = ID->getNameAsString(); |
| 4342 | if (!ObjCEmptyCacheVar) { |
| 4343 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4344 | ObjCTypes.CacheTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4345 | false, |
| 4346 | llvm::GlobalValue::ExternalLinkage, |
| 4347 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4348 | "_objc_empty_cache", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4349 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4350 | |
| 4351 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4352 | ObjCTypes.ImpnfABITy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4353 | false, |
| 4354 | llvm::GlobalValue::ExternalLinkage, |
| 4355 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4356 | "_objc_empty_vtable", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4357 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4358 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4359 | assert(ID->getClassInterface() && |
| 4360 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 4361 | // FIXME: Is this correct (that meta class size is never computed)? |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4362 | uint32_t InstanceStart = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4363 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4364 | uint32_t InstanceSize = InstanceStart; |
| 4365 | uint32_t flags = CLS_META; |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4366 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 4367 | std::string ObjCClassName(getClassSymbolPrefix()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4368 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4369 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4370 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4371 | bool classIsHidden = |
| 4372 | CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4373 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4374 | flags |= OBJC2_CLS_HIDDEN; |
| 4375 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4376 | // class is root |
| 4377 | flags |= CLS_ROOT; |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4378 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4379 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4380 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4381 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4382 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 4383 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 4384 | Root = Super; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4385 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4386 | // work on super class metadata symbol. |
| 4387 | std::string SuperClassName = |
| 4388 | ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4389 | SuperClassGV = GetClassGlobal(SuperClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4390 | } |
| 4391 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 4392 | InstanceStart, |
| 4393 | InstanceSize,ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4394 | std::string TClassName = ObjCMetaClassName + ClassName; |
| 4395 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4396 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 4397 | classIsHidden); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4398 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4399 | // Metadata for the class |
| 4400 | flags = CLS; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4401 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4402 | flags |= OBJC2_CLS_HIDDEN; |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4403 | |
| 4404 | if (hasObjCExceptionAttribute(ID->getClassInterface())) |
| 4405 | flags |= CLS_EXCEPTION; |
| 4406 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4407 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4408 | flags |= CLS_ROOT; |
| 4409 | SuperClassGV = 0; |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 4410 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4411 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4412 | std::string RootClassName = |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4413 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4414 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4415 | } |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4416 | GetClassSizeInfo(ID, InstanceStart, InstanceSize); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4417 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4418 | InstanceStart, |
| 4419 | InstanceSize, |
| 4420 | ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4421 | |
| 4422 | TClassName = ObjCClassName + ClassName; |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4423 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4424 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 4425 | classIsHidden); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4426 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4427 | |
| 4428 | // Force the definition of the EHType if necessary. |
| 4429 | if (flags & CLS_EXCEPTION) |
| 4430 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4431 | } |
| 4432 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4433 | /// GenerateProtocolRef - This routine is called to generate code for |
| 4434 | /// a protocol reference expression; as in: |
| 4435 | /// @code |
| 4436 | /// @protocol(Proto1); |
| 4437 | /// @endcode |
| 4438 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 4439 | /// which will hold address of the protocol meta-data. |
| 4440 | /// |
| 4441 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
| 4442 | const ObjCProtocolDecl *PD) { |
| 4443 | |
Fariborz Jahanian | 960cd06 | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 4444 | // This routine is called for @protocol only. So, we must build definition |
| 4445 | // of protocol's meta-data (not a reference to it!) |
| 4446 | // |
| 4447 | llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4448 | ObjCTypes.ExternalProtocolPtrTy); |
| 4449 | |
| 4450 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
| 4451 | ProtocolName += PD->getNameAsCString(); |
| 4452 | |
| 4453 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 4454 | if (PTGV) |
| 4455 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4456 | PTGV = new llvm::GlobalVariable( |
| 4457 | Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4458 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4459 | Init, |
| 4460 | ProtocolName, |
| 4461 | &CGM.getModule()); |
| 4462 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 4463 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4464 | UsedGlobals.push_back(PTGV); |
| 4465 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4466 | } |
| 4467 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4468 | /// GenerateCategory - Build metadata for a category implementation. |
| 4469 | /// struct _category_t { |
| 4470 | /// const char * const name; |
| 4471 | /// struct _class_t *const cls; |
| 4472 | /// const struct _method_list_t * const instance_methods; |
| 4473 | /// const struct _method_list_t * const class_methods; |
| 4474 | /// const struct _protocol_list_t * const protocols; |
| 4475 | /// const struct _prop_list_t * const properties; |
| 4476 | /// } |
| 4477 | /// |
| 4478 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) |
| 4479 | { |
| 4480 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4481 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
| 4482 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4483 | "_$_" + OCD->getNameAsString()); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4484 | std::string ExtClassName(getClassSymbolPrefix() + |
| 4485 | Interface->getNameAsString()); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4486 | |
| 4487 | std::vector<llvm::Constant*> Values(6); |
| 4488 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 4489 | // meta-class entry symbol |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4490 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4491 | Values[1] = ClassGV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4492 | std::vector<llvm::Constant*> Methods; |
| 4493 | std::string MethodListName(Prefix); |
| 4494 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
| 4495 | "_$_" + OCD->getNameAsString(); |
| 4496 | |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4497 | for (ObjCCategoryImplDecl::instmeth_iterator |
| 4498 | i = OCD->instmeth_begin(CGM.getContext()), |
| 4499 | e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4500 | // Instance methods should always be defined. |
| 4501 | Methods.push_back(GetMethodConstant(*i)); |
| 4502 | } |
| 4503 | |
| 4504 | Values[2] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4505 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4506 | Methods); |
| 4507 | |
| 4508 | MethodListName = Prefix; |
| 4509 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 4510 | OCD->getNameAsString(); |
| 4511 | Methods.clear(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4512 | for (ObjCCategoryImplDecl::classmeth_iterator |
| 4513 | i = OCD->classmeth_begin(CGM.getContext()), |
| 4514 | e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4515 | // Class methods should always be defined. |
| 4516 | Methods.push_back(GetMethodConstant(*i)); |
| 4517 | } |
| 4518 | |
| 4519 | Values[3] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4520 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4521 | Methods); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4522 | const ObjCCategoryDecl *Category = |
| 4523 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4524 | if (Category) { |
| 4525 | std::string ExtName(Interface->getNameAsString() + "_$_" + |
| 4526 | OCD->getNameAsString()); |
| 4527 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
| 4528 | + Interface->getNameAsString() + "_$_" |
| 4529 | + Category->getNameAsString(), |
| 4530 | Category->protocol_begin(), |
| 4531 | Category->protocol_end()); |
| 4532 | Values[5] = |
| 4533 | EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
| 4534 | OCD, Category, ObjCTypes); |
| 4535 | } |
| 4536 | else { |
| 4537 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4538 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4539 | } |
| 4540 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4541 | llvm::Constant *Init = |
| 4542 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
| 4543 | Values); |
| 4544 | llvm::GlobalVariable *GCATV |
| 4545 | = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy, |
| 4546 | false, |
| 4547 | llvm::GlobalValue::InternalLinkage, |
| 4548 | Init, |
| 4549 | ExtCatName, |
| 4550 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4551 | GCATV->setAlignment( |
| 4552 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4553 | GCATV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4554 | UsedGlobals.push_back(GCATV); |
| 4555 | DefinedCategories.push_back(GCATV); |
| 4556 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4557 | |
| 4558 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 4559 | /// given method if it has been defined. The result is null if the |
| 4560 | /// method has not been defined. The return value has type MethodPtrTy. |
| 4561 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
| 4562 | const ObjCMethodDecl *MD) { |
| 4563 | // FIXME: Use DenseMap::lookup |
| 4564 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 4565 | if (!Fn) |
| 4566 | return 0; |
| 4567 | |
| 4568 | std::vector<llvm::Constant*> Method(3); |
| 4569 | Method[0] = |
| 4570 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4571 | ObjCTypes.SelectorPtrTy); |
| 4572 | Method[1] = GetMethodVarType(MD); |
| 4573 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 4574 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 4575 | } |
| 4576 | |
| 4577 | /// EmitMethodList - Build meta-data for method declarations |
| 4578 | /// struct _method_list_t { |
| 4579 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 4580 | /// uint32_t method_count; |
| 4581 | /// struct _objc_method method_list[method_count]; |
| 4582 | /// } |
| 4583 | /// |
| 4584 | llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList( |
| 4585 | const std::string &Name, |
| 4586 | const char *Section, |
| 4587 | const ConstantVector &Methods) { |
| 4588 | // Return null for empty list. |
| 4589 | if (Methods.empty()) |
| 4590 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
| 4591 | |
| 4592 | std::vector<llvm::Constant*> Values(3); |
| 4593 | // sizeof(struct _objc_method) |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4594 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4595 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4596 | // method_count |
| 4597 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 4598 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 4599 | Methods.size()); |
| 4600 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 4601 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4602 | |
| 4603 | llvm::GlobalVariable *GV = |
| 4604 | new llvm::GlobalVariable(Init->getType(), false, |
| 4605 | llvm::GlobalValue::InternalLinkage, |
| 4606 | Init, |
| 4607 | Name, |
| 4608 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4609 | GV->setAlignment( |
| 4610 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4611 | GV->setSection(Section); |
| 4612 | UsedGlobals.push_back(GV); |
| 4613 | return llvm::ConstantExpr::getBitCast(GV, |
| 4614 | ObjCTypes.MethodListnfABIPtrTy); |
| 4615 | } |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4616 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4617 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 4618 | /// the given ivar. |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4619 | llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 4620 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4621 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | a81419d | 2009-05-05 00:36:57 +0000 | [diff] [blame] | 4622 | // FIXME: We shouldn't need to do this lookup. |
| 4623 | unsigned Index; |
| 4624 | const ObjCInterfaceDecl *Container = |
| 4625 | FindIvarInterface(CGM.getContext(), ID, Ivar, Index); |
| 4626 | assert(Container && "Unable to find ivar container!"); |
| 4627 | std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() + |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4628 | '.' + Ivar->getNameAsString(); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4629 | llvm::GlobalVariable *IvarOffsetGV = |
| 4630 | CGM.getModule().getGlobalVariable(Name); |
| 4631 | if (!IvarOffsetGV) |
| 4632 | IvarOffsetGV = |
| 4633 | new llvm::GlobalVariable(ObjCTypes.LongTy, |
| 4634 | false, |
| 4635 | llvm::GlobalValue::ExternalLinkage, |
| 4636 | 0, |
| 4637 | Name, |
| 4638 | &CGM.getModule()); |
| 4639 | return IvarOffsetGV; |
| 4640 | } |
| 4641 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4642 | llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar( |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4643 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4644 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4645 | unsigned long int Offset) { |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4646 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
| 4647 | IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy, |
| 4648 | Offset)); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4649 | IvarOffsetGV->setAlignment( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4650 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy)); |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4651 | |
| 4652 | // FIXME: This matches gcc, but shouldn't the visibility be set on |
| 4653 | // the use as well (i.e., in ObjCIvarOffsetVariable). |
| 4654 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 4655 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
| 4656 | CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden) |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4657 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4658 | else |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4659 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4660 | IvarOffsetGV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4661 | return IvarOffsetGV; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4662 | } |
| 4663 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4664 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 4665 | /// implementation. The return value has type |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4666 | /// IvarListnfABIPtrTy. |
| 4667 | /// struct _ivar_t { |
| 4668 | /// unsigned long int *offset; // pointer to ivar offset location |
| 4669 | /// char *name; |
| 4670 | /// char *type; |
| 4671 | /// uint32_t alignment; |
| 4672 | /// uint32_t size; |
| 4673 | /// } |
| 4674 | /// struct _ivar_list_t { |
| 4675 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 4676 | /// uint32 count; |
| 4677 | /// struct _iver_t list[count]; |
| 4678 | /// } |
| 4679 | /// |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4680 | |
| 4681 | void CGObjCCommonMac::GetNamedIvarList(const ObjCInterfaceDecl *OID, |
| 4682 | llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const { |
| 4683 | for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), |
| 4684 | E = OID->ivar_end(); I != E; ++I) { |
| 4685 | // Ignore unnamed bit-fields. |
| 4686 | if (!(*I)->getDeclName()) |
| 4687 | continue; |
| 4688 | |
| 4689 | Res.push_back(*I); |
| 4690 | } |
| 4691 | |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 4692 | // Also save synthesize ivars. |
| 4693 | // FIXME. Why can't we just use passed in Res small vector? |
| 4694 | llvm::SmallVector<ObjCIvarDecl*, 16> Ivars; |
| 4695 | CGM.getContext().CollectSynthesizedIvars(OID, Ivars); |
| 4696 | for (unsigned k = 0, e = Ivars.size(); k != e; ++k) |
| 4697 | Res.push_back(Ivars[k]); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4698 | } |
| 4699 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4700 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
| 4701 | const ObjCImplementationDecl *ID) { |
| 4702 | |
| 4703 | std::vector<llvm::Constant*> Ivars, Ivar(5); |
| 4704 | |
| 4705 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4706 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
| 4707 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4708 | // FIXME. Consolidate this with similar code in GenerateClass. |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 4709 | |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4710 | // Collect declared and synthesized ivars in a small vector. |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4711 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4712 | GetNamedIvarList(OID, OIvars); |
Fariborz Jahanian | 99eee36 | 2009-04-01 19:37:34 +0000 | [diff] [blame] | 4713 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4714 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 4715 | ObjCIvarDecl *IVD = OIvars[i]; |
Daniel Dunbar | 3eec8aa | 2009-04-20 05:53:40 +0000 | [diff] [blame] | 4716 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD, |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4717 | ComputeIvarBaseOffset(CGM, ID, IVD)); |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4718 | Ivar[1] = GetMethodVarName(IVD->getIdentifier()); |
| 4719 | Ivar[2] = GetMethodVarType(IVD); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4720 | const llvm::Type *FieldTy = |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4721 | CGM.getTypes().ConvertTypeForMem(IVD->getType()); |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4722 | unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4723 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4724 | IVD->getType().getTypePtr()) >> 3; |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4725 | Align = llvm::Log2_32(Align); |
| 4726 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4727 | // NOTE. Size of a bitfield does not match gcc's, because of the |
| 4728 | // way bitfields are treated special in each. But I am told that |
| 4729 | // 'size' for bitfield ivars is ignored by the runtime so it does |
| 4730 | // not matter. If it matters, there is enough info to get the |
| 4731 | // bitfield right! |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4732 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4733 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
| 4734 | } |
| 4735 | // Return null for empty list. |
| 4736 | if (Ivars.empty()) |
| 4737 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4738 | std::vector<llvm::Constant*> Values(3); |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4739 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4740 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4741 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 4742 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
| 4743 | Ivars.size()); |
| 4744 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
| 4745 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4746 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 4747 | llvm::GlobalVariable *GV = |
| 4748 | new llvm::GlobalVariable(Init->getType(), false, |
| 4749 | llvm::GlobalValue::InternalLinkage, |
| 4750 | Init, |
| 4751 | Prefix + OID->getNameAsString(), |
| 4752 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4753 | GV->setAlignment( |
| 4754 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4755 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4756 | |
| 4757 | UsedGlobals.push_back(GV); |
| 4758 | return llvm::ConstantExpr::getBitCast(GV, |
| 4759 | ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4760 | } |
| 4761 | |
| 4762 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
| 4763 | const ObjCProtocolDecl *PD) { |
| 4764 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4765 | |
| 4766 | if (!Entry) { |
| 4767 | // We use the initializer as a marker of whether this is a forward |
| 4768 | // reference or not. At module finalization we add the empty |
| 4769 | // contents for protocols which were referenced but never defined. |
| 4770 | Entry = |
| 4771 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
| 4772 | llvm::GlobalValue::ExternalLinkage, |
| 4773 | 0, |
| 4774 | "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(), |
| 4775 | &CGM.getModule()); |
| 4776 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 4777 | UsedGlobals.push_back(Entry); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4778 | } |
| 4779 | |
| 4780 | return Entry; |
| 4781 | } |
| 4782 | |
| 4783 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 4784 | /// @code |
| 4785 | /// struct _protocol_t { |
| 4786 | /// id isa; // NULL |
| 4787 | /// const char * const protocol_name; |
| 4788 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 4789 | /// const struct method_list_t * const instance_methods; |
| 4790 | /// const struct method_list_t * const class_methods; |
| 4791 | /// const struct method_list_t *optionalInstanceMethods; |
| 4792 | /// const struct method_list_t *optionalClassMethods; |
| 4793 | /// const struct _prop_list_t * properties; |
| 4794 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 4795 | /// const uint32_t flags; // = 0 |
| 4796 | /// } |
| 4797 | /// @endcode |
| 4798 | /// |
| 4799 | |
| 4800 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
| 4801 | const ObjCProtocolDecl *PD) { |
| 4802 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4803 | |
| 4804 | // Early exit if a defining object has already been generated. |
| 4805 | if (Entry && Entry->hasInitializer()) |
| 4806 | return Entry; |
| 4807 | |
| 4808 | const char *ProtocolName = PD->getNameAsCString(); |
| 4809 | |
| 4810 | // Construct method lists. |
| 4811 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 4812 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4813 | for (ObjCProtocolDecl::instmeth_iterator |
| 4814 | i = PD->instmeth_begin(CGM.getContext()), |
| 4815 | e = PD->instmeth_end(CGM.getContext()); |
| 4816 | i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4817 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4818 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4819 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4820 | OptInstanceMethods.push_back(C); |
| 4821 | } else { |
| 4822 | InstanceMethods.push_back(C); |
| 4823 | } |
| 4824 | } |
| 4825 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4826 | for (ObjCProtocolDecl::classmeth_iterator |
| 4827 | i = PD->classmeth_begin(CGM.getContext()), |
| 4828 | e = PD->classmeth_end(CGM.getContext()); |
| 4829 | i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4830 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4831 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4832 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4833 | OptClassMethods.push_back(C); |
| 4834 | } else { |
| 4835 | ClassMethods.push_back(C); |
| 4836 | } |
| 4837 | } |
| 4838 | |
| 4839 | std::vector<llvm::Constant*> Values(10); |
| 4840 | // isa is NULL |
| 4841 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
| 4842 | Values[1] = GetClassName(PD->getIdentifier()); |
| 4843 | Values[2] = EmitProtocolList( |
| 4844 | "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(), |
| 4845 | PD->protocol_begin(), |
| 4846 | PD->protocol_end()); |
| 4847 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4848 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4849 | + PD->getNameAsString(), |
| 4850 | "__DATA, __objc_const", |
| 4851 | InstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4852 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4853 | + PD->getNameAsString(), |
| 4854 | "__DATA, __objc_const", |
| 4855 | ClassMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4856 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4857 | + PD->getNameAsString(), |
| 4858 | "__DATA, __objc_const", |
| 4859 | OptInstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4860 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4861 | + PD->getNameAsString(), |
| 4862 | "__DATA, __objc_const", |
| 4863 | OptClassMethods); |
| 4864 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(), |
| 4865 | 0, PD, ObjCTypes); |
| 4866 | uint32_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4867 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4868 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4869 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
| 4870 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
| 4871 | Values); |
| 4872 | |
| 4873 | if (Entry) { |
| 4874 | // Already created, fix the linkage and update the initializer. |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4875 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4876 | Entry->setInitializer(Init); |
| 4877 | } else { |
| 4878 | Entry = |
| 4879 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4880 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4881 | Init, |
| 4882 | std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName, |
| 4883 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4884 | Entry->setAlignment( |
| 4885 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4886 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4887 | } |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4888 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4889 | |
| 4890 | // Use this protocol meta-data to build protocol list table in section |
| 4891 | // __DATA, __objc_protolist |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4892 | llvm::GlobalVariable *PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4893 | ObjCTypes.ProtocolnfABIPtrTy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4894 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4895 | Entry, |
| 4896 | std::string("\01l_OBJC_LABEL_PROTOCOL_$_") |
| 4897 | +ProtocolName, |
| 4898 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4899 | PTGV->setAlignment( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4900 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 4901 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4902 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4903 | UsedGlobals.push_back(PTGV); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4904 | return Entry; |
| 4905 | } |
| 4906 | |
| 4907 | /// EmitProtocolList - Generate protocol list meta-data: |
| 4908 | /// @code |
| 4909 | /// struct _protocol_list_t { |
| 4910 | /// long protocol_count; // Note, this is 32/64 bit |
| 4911 | /// struct _protocol_t[protocol_count]; |
| 4912 | /// } |
| 4913 | /// @endcode |
| 4914 | /// |
| 4915 | llvm::Constant * |
| 4916 | CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name, |
| 4917 | ObjCProtocolDecl::protocol_iterator begin, |
| 4918 | ObjCProtocolDecl::protocol_iterator end) { |
| 4919 | std::vector<llvm::Constant*> ProtocolRefs; |
| 4920 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4921 | // Just return null for empty protocol lists |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4922 | if (begin == end) |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4923 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4924 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4925 | // FIXME: We shouldn't need to do this lookup here, should we? |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4926 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
| 4927 | if (GV) |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4928 | return llvm::ConstantExpr::getBitCast(GV, |
| 4929 | ObjCTypes.ProtocolListnfABIPtrTy); |
| 4930 | |
| 4931 | for (; begin != end; ++begin) |
| 4932 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 4933 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4934 | // This list is null terminated. |
| 4935 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4936 | ObjCTypes.ProtocolnfABIPtrTy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4937 | |
| 4938 | std::vector<llvm::Constant*> Values(2); |
| 4939 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 4940 | Values[1] = |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4941 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4942 | ProtocolRefs.size()), |
| 4943 | ProtocolRefs); |
| 4944 | |
| 4945 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4946 | GV = new llvm::GlobalVariable(Init->getType(), false, |
| 4947 | llvm::GlobalValue::InternalLinkage, |
| 4948 | Init, |
| 4949 | Name, |
| 4950 | &CGM.getModule()); |
| 4951 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4952 | GV->setAlignment( |
| 4953 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4954 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4955 | return llvm::ConstantExpr::getBitCast(GV, |
| 4956 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4957 | } |
| 4958 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4959 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 4960 | /// struct _objc_method { |
| 4961 | /// SEL _cmd; |
| 4962 | /// char *method_type; |
| 4963 | /// char *_imp; |
| 4964 | /// } |
| 4965 | |
| 4966 | llvm::Constant * |
| 4967 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 4968 | std::vector<llvm::Constant*> Desc(3); |
| 4969 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4970 | ObjCTypes.SelectorPtrTy); |
| 4971 | Desc[1] = GetMethodVarType(MD); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4972 | // Protocol methods have no implementation. So, this entry is always NULL. |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4973 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 4974 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
| 4975 | } |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4976 | |
| 4977 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 4978 | /// This code gen. amounts to generating code for: |
| 4979 | /// @code |
| 4980 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 4981 | /// @encode |
| 4982 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4983 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4984 | CodeGen::CodeGenFunction &CGF, |
| 4985 | QualType ObjectTy, |
| 4986 | llvm::Value *BaseValue, |
| 4987 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4988 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 4989 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4990 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 4991 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4992 | } |
| 4993 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4994 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
| 4995 | CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 4996 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4997 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 4998 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar), |
| 4999 | false, "ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 5000 | } |
| 5001 | |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5002 | CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend( |
| 5003 | CodeGen::CodeGenFunction &CGF, |
| 5004 | QualType ResultType, |
| 5005 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5006 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5007 | QualType Arg0Ty, |
| 5008 | bool IsSuper, |
| 5009 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5010 | // FIXME. Even though IsSuper is passes. This function doese not |
| 5011 | // handle calls to 'super' receivers. |
| 5012 | CodeGenTypes &Types = CGM.getTypes(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5013 | llvm::Value *Arg0 = Receiver; |
| 5014 | if (!IsSuper) |
| 5015 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5016 | |
| 5017 | // Find the message function name. |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5018 | // FIXME. This is too much work to get the ABI-specific result type |
| 5019 | // needed to find the message name. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5020 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, |
| 5021 | llvm::SmallVector<QualType, 16>()); |
Fariborz Jahanian | 70b51c7 | 2009-04-30 23:08:58 +0000 | [diff] [blame] | 5022 | llvm::Constant *Fn = 0; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5023 | std::string Name("\01l_"); |
| 5024 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5025 | #if 0 |
| 5026 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5027 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5028 | Fn = ObjCTypes.getMessageSendIdStretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5029 | // FIXME. Is there a better way of getting these names. |
| 5030 | // They are available in RuntimeFunctions vector pair. |
| 5031 | Name += "objc_msgSendId_stret_fixup"; |
| 5032 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5033 | else |
| 5034 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5035 | if (IsSuper) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5036 | Fn = ObjCTypes.getMessageSendSuper2StretFixupFn(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5037 | Name += "objc_msgSendSuper2_stret_fixup"; |
| 5038 | } |
| 5039 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5040 | { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5041 | Fn = ObjCTypes.getMessageSendStretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5042 | Name += "objc_msgSend_stret_fixup"; |
| 5043 | } |
| 5044 | } |
Fariborz Jahanian | 5b2bad0 | 2009-04-30 16:31:11 +0000 | [diff] [blame] | 5045 | else if (!IsSuper && ResultType->isFloatingType()) { |
| 5046 | if (const BuiltinType *BT = ResultType->getAsBuiltinType()) { |
| 5047 | BuiltinType::Kind k = BT->getKind(); |
| 5048 | if (k == BuiltinType::LongDouble) { |
| 5049 | Fn = ObjCTypes.getMessageSendFpretFixupFn(); |
| 5050 | Name += "objc_msgSend_fpret_fixup"; |
| 5051 | } |
| 5052 | else { |
| 5053 | Fn = ObjCTypes.getMessageSendFixupFn(); |
| 5054 | Name += "objc_msgSend_fixup"; |
| 5055 | } |
| 5056 | } |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5057 | } |
| 5058 | else { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5059 | #if 0 |
| 5060 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5061 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5062 | Fn = ObjCTypes.getMessageSendIdFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5063 | Name += "objc_msgSendId_fixup"; |
| 5064 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5065 | else |
| 5066 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5067 | if (IsSuper) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5068 | Fn = ObjCTypes.getMessageSendSuper2FixupFn(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5069 | Name += "objc_msgSendSuper2_fixup"; |
| 5070 | } |
| 5071 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5072 | { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5073 | Fn = ObjCTypes.getMessageSendFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5074 | Name += "objc_msgSend_fixup"; |
| 5075 | } |
| 5076 | } |
Fariborz Jahanian | 70b51c7 | 2009-04-30 23:08:58 +0000 | [diff] [blame] | 5077 | assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5078 | Name += '_'; |
| 5079 | std::string SelName(Sel.getAsString()); |
| 5080 | // Replace all ':' in selector name with '_' ouch! |
| 5081 | for(unsigned i = 0; i < SelName.size(); i++) |
| 5082 | if (SelName[i] == ':') |
| 5083 | SelName[i] = '_'; |
| 5084 | Name += SelName; |
| 5085 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5086 | if (!GV) { |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5087 | // Build message ref table entry. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5088 | std::vector<llvm::Constant*> Values(2); |
| 5089 | Values[0] = Fn; |
| 5090 | Values[1] = GetMethodVarName(Sel); |
| 5091 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 5092 | GV = new llvm::GlobalVariable(Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 5093 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5094 | Init, |
| 5095 | Name, |
| 5096 | &CGM.getModule()); |
| 5097 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | f59c1a6 | 2009-04-15 19:04:46 +0000 | [diff] [blame] | 5098 | GV->setAlignment(16); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5099 | GV->setSection("__DATA, __objc_msgrefs, coalesced"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5100 | } |
| 5101 | llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5102 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5103 | CallArgList ActualArgs; |
| 5104 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 5105 | ActualArgs.push_back(std::make_pair(RValue::get(Arg1), |
| 5106 | ObjCTypes.MessageRefCPtrTy)); |
| 5107 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5108 | const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs); |
| 5109 | llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0); |
| 5110 | Callee = CGF.Builder.CreateLoad(Callee); |
Fariborz Jahanian | 3ab75bd | 2009-02-14 21:25:36 +0000 | [diff] [blame] | 5111 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5112 | Callee = CGF.Builder.CreateBitCast(Callee, |
| 5113 | llvm::PointerType::getUnqual(FTy)); |
| 5114 | return CGF.EmitCall(FnInfo1, Callee, ActualArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5115 | } |
| 5116 | |
| 5117 | /// Generate code for a message send expression in the nonfragile abi. |
| 5118 | CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend( |
| 5119 | CodeGen::CodeGenFunction &CGF, |
| 5120 | QualType ResultType, |
| 5121 | Selector Sel, |
| 5122 | llvm::Value *Receiver, |
| 5123 | bool IsClassMessage, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 5124 | const CallArgList &CallArgs, |
| 5125 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5126 | return LegacyDispatchedSelector(Sel) |
| 5127 | ? EmitLegacyMessageSend(CGF, ResultType, EmitSelector(CGF.Builder, Sel), |
| 5128 | Receiver, CGF.getContext().getObjCIdType(), |
| 5129 | false, CallArgs, ObjCTypes) |
| 5130 | : EmitMessageSend(CGF, ResultType, Sel, |
| 5131 | Receiver, CGF.getContext().getObjCIdType(), |
| 5132 | false, CallArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5133 | } |
| 5134 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5135 | llvm::GlobalVariable * |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5136 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5137 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5138 | |
Daniel Dunbar | dfff230 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 5139 | if (!GV) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5140 | GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 5141 | llvm::GlobalValue::ExternalLinkage, |
| 5142 | 0, Name, &CGM.getModule()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5143 | } |
| 5144 | |
| 5145 | return GV; |
| 5146 | } |
| 5147 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5148 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5149 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5150 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 5151 | |
| 5152 | if (!Entry) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5153 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5154 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5155 | Entry = |
| 5156 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5157 | llvm::GlobalValue::InternalLinkage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5158 | ClassGV, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5159 | "\01L_OBJC_CLASSLIST_REFERENCES_$_", |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5160 | &CGM.getModule()); |
| 5161 | Entry->setAlignment( |
| 5162 | CGM.getTargetData().getPrefTypeAlignment( |
| 5163 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5164 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
| 5165 | UsedGlobals.push_back(Entry); |
| 5166 | } |
| 5167 | |
| 5168 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5169 | } |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5170 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5171 | llvm::Value * |
| 5172 | CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder, |
| 5173 | const ObjCInterfaceDecl *ID) { |
| 5174 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
| 5175 | |
| 5176 | if (!Entry) { |
| 5177 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5178 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
| 5179 | Entry = |
| 5180 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5181 | llvm::GlobalValue::InternalLinkage, |
| 5182 | ClassGV, |
| 5183 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 5184 | &CGM.getModule()); |
| 5185 | Entry->setAlignment( |
| 5186 | CGM.getTargetData().getPrefTypeAlignment( |
| 5187 | ObjCTypes.ClassnfABIPtrTy)); |
| 5188 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5189 | UsedGlobals.push_back(Entry); |
| 5190 | } |
| 5191 | |
| 5192 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5193 | } |
| 5194 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5195 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 5196 | /// meta-data |
| 5197 | /// |
| 5198 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 5199 | const ObjCInterfaceDecl *ID) { |
| 5200 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 5201 | if (Entry) |
| 5202 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5203 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5204 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5205 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5206 | Entry = |
| 5207 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5208 | llvm::GlobalValue::InternalLinkage, |
| 5209 | MetaClassGV, |
| 5210 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 5211 | &CGM.getModule()); |
| 5212 | Entry->setAlignment( |
| 5213 | CGM.getTargetData().getPrefTypeAlignment( |
| 5214 | ObjCTypes.ClassnfABIPtrTy)); |
| 5215 | |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5216 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5217 | UsedGlobals.push_back(Entry); |
| 5218 | |
| 5219 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5220 | } |
| 5221 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5222 | /// GetClass - Return a reference to the class for the given interface |
| 5223 | /// decl. |
| 5224 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 5225 | const ObjCInterfaceDecl *ID) { |
| 5226 | return EmitClassRef(Builder, ID); |
| 5227 | } |
| 5228 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5229 | /// Generates a message send where the super is the receiver. This is |
| 5230 | /// a message send to self with special delivery semantics indicating |
| 5231 | /// which class's method should be called. |
| 5232 | CodeGen::RValue |
| 5233 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 5234 | QualType ResultType, |
| 5235 | Selector Sel, |
| 5236 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5237 | bool isCategoryImpl, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5238 | llvm::Value *Receiver, |
| 5239 | bool IsClassMessage, |
| 5240 | const CodeGen::CallArgList &CallArgs) { |
| 5241 | // ... |
| 5242 | // Create and init a super structure; this is a (receiver, class) |
| 5243 | // pair we will pass to objc_msgSendSuper. |
| 5244 | llvm::Value *ObjCSuper = |
| 5245 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 5246 | |
| 5247 | llvm::Value *ReceiverAsObject = |
| 5248 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 5249 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 5250 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
| 5251 | |
| 5252 | // 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] | 5253 | llvm::Value *Target; |
| 5254 | if (IsClassMessage) { |
| 5255 | if (isCategoryImpl) { |
| 5256 | // Message sent to "super' in a class method defined in |
| 5257 | // a category implementation. |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5258 | Target = EmitClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5259 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 5260 | Target = CGF.Builder.CreateLoad(Target); |
| 5261 | } |
| 5262 | else |
| 5263 | Target = EmitMetaClassRef(CGF.Builder, Class); |
| 5264 | } |
| 5265 | else |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5266 | Target = EmitSuperClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5267 | |
| 5268 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 5269 | // and ObjCTypes types. |
| 5270 | const llvm::Type *ClassTy = |
| 5271 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 5272 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 5273 | CGF.Builder.CreateStore(Target, |
| 5274 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 5275 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5276 | return (LegacyDispatchedSelector(Sel)) |
| 5277 | ? EmitLegacyMessageSend(CGF, ResultType,EmitSelector(CGF.Builder, Sel), |
| 5278 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 5279 | true, CallArgs, |
| 5280 | ObjCTypes) |
| 5281 | : EmitMessageSend(CGF, ResultType, Sel, |
| 5282 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 5283 | true, CallArgs); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5284 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5285 | |
| 5286 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
| 5287 | Selector Sel) { |
| 5288 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 5289 | |
| 5290 | if (!Entry) { |
| 5291 | llvm::Constant *Casted = |
| 5292 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 5293 | ObjCTypes.SelectorPtrTy); |
| 5294 | Entry = |
| 5295 | new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false, |
| 5296 | llvm::GlobalValue::InternalLinkage, |
| 5297 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_", |
| 5298 | &CGM.getModule()); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5299 | Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip"); |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5300 | UsedGlobals.push_back(Entry); |
| 5301 | } |
| 5302 | |
| 5303 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5304 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5305 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 5306 | /// objc_assign_ivar (id src, id *dst) |
| 5307 | /// |
| 5308 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 5309 | llvm::Value *src, llvm::Value *dst) |
| 5310 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5311 | const llvm::Type * SrcTy = src->getType(); |
| 5312 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5313 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5314 | assert(Size <= 8 && "does not support size > 8"); |
| 5315 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5316 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5317 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5318 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5319 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5320 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5321 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5322 | src, dst, "assignivar"); |
| 5323 | return; |
| 5324 | } |
| 5325 | |
| 5326 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 5327 | /// objc_assign_strongCast (id src, id *dst) |
| 5328 | /// |
| 5329 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
| 5330 | CodeGen::CodeGenFunction &CGF, |
| 5331 | llvm::Value *src, llvm::Value *dst) |
| 5332 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5333 | const llvm::Type * SrcTy = src->getType(); |
| 5334 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5335 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5336 | assert(Size <= 8 && "does not support size > 8"); |
| 5337 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5338 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5339 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5340 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5341 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5342 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5343 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5344 | src, dst, "weakassign"); |
| 5345 | return; |
| 5346 | } |
| 5347 | |
| 5348 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 5349 | /// object: objc_read_weak (id *src) |
| 5350 | /// |
| 5351 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
| 5352 | CodeGen::CodeGenFunction &CGF, |
| 5353 | llvm::Value *AddrWeakObj) |
| 5354 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5355 | const llvm::Type* DestTy = |
| 5356 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5357 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 5358 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5359 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5360 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5361 | return read_weak; |
| 5362 | } |
| 5363 | |
| 5364 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 5365 | /// objc_assign_weak (id src, id *dst) |
| 5366 | /// |
| 5367 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 5368 | llvm::Value *src, llvm::Value *dst) |
| 5369 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5370 | const llvm::Type * SrcTy = src->getType(); |
| 5371 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5372 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5373 | assert(Size <= 8 && "does not support size > 8"); |
| 5374 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5375 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5376 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5377 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5378 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5379 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 5380 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5381 | src, dst, "weakassign"); |
| 5382 | return; |
| 5383 | } |
| 5384 | |
| 5385 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 5386 | /// objc_assign_global (id src, id *dst) |
| 5387 | /// |
| 5388 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 5389 | llvm::Value *src, llvm::Value *dst) |
| 5390 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5391 | const llvm::Type * SrcTy = src->getType(); |
| 5392 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5393 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5394 | assert(Size <= 8 && "does not support size > 8"); |
| 5395 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5396 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5397 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5398 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5399 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5400 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5401 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5402 | src, dst, "globalassign"); |
| 5403 | return; |
| 5404 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5405 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5406 | void |
| 5407 | CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 5408 | const Stmt &S) { |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5409 | bool isTry = isa<ObjCAtTryStmt>(S); |
| 5410 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 5411 | llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5412 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5413 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5414 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5415 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
| 5416 | |
| 5417 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 5418 | // evaluation of the expression must occur before we enter the |
| 5419 | // @synchronized. We can safely avoid a temp here because jumps into |
| 5420 | // @synchronized are illegal & this will dominate uses. |
| 5421 | llvm::Value *SyncArg = 0; |
| 5422 | if (!isTry) { |
| 5423 | SyncArg = |
| 5424 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 5425 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5426 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5427 | } |
| 5428 | |
| 5429 | // Push an EH context entry, used for handling rethrows and jumps |
| 5430 | // through finally. |
| 5431 | CGF.PushCleanupBlock(FinallyBlock); |
| 5432 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5433 | CGF.setInvokeDest(TryHandler); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5434 | |
| 5435 | CGF.EmitBlock(TryBlock); |
| 5436 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 5437 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
| 5438 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5439 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5440 | // Emit the exception handler. |
| 5441 | |
| 5442 | CGF.EmitBlock(TryHandler); |
| 5443 | |
| 5444 | llvm::Value *llvm_eh_exception = |
| 5445 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
| 5446 | llvm::Value *llvm_eh_selector_i64 = |
| 5447 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64); |
| 5448 | llvm::Value *llvm_eh_typeid_for_i64 = |
| 5449 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64); |
| 5450 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5451 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow"); |
| 5452 | |
| 5453 | llvm::SmallVector<llvm::Value*, 8> SelectorArgs; |
| 5454 | SelectorArgs.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5455 | SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5456 | |
| 5457 | // Construct the lists of (type, catch body) to handle. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5458 | llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5459 | bool HasCatchAll = false; |
| 5460 | if (isTry) { |
| 5461 | if (const ObjCAtCatchStmt* CatchStmt = |
| 5462 | cast<ObjCAtTryStmt>(S).getCatchStmts()) { |
| 5463 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5464 | const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5465 | Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody())); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5466 | |
| 5467 | // catch(...) always matches. |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5468 | if (!CatchDecl) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5469 | // Use i8* null here to signal this is a catch all, not a cleanup. |
| 5470 | llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 5471 | SelectorArgs.push_back(Null); |
| 5472 | HasCatchAll = true; |
| 5473 | break; |
| 5474 | } |
| 5475 | |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5476 | if (CGF.getContext().isObjCIdType(CatchDecl->getType()) || |
| 5477 | CatchDecl->getType()->isObjCQualifiedIdType()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5478 | llvm::Value *IDEHType = |
| 5479 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 5480 | if (!IDEHType) |
| 5481 | IDEHType = |
| 5482 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5483 | llvm::GlobalValue::ExternalLinkage, |
| 5484 | 0, "OBJC_EHTYPE_id", &CGM.getModule()); |
| 5485 | SelectorArgs.push_back(IDEHType); |
| 5486 | HasCatchAll = true; |
| 5487 | break; |
| 5488 | } |
| 5489 | |
| 5490 | // All other types should be Objective-C interface pointer types. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5491 | const PointerType *PT = CatchDecl->getType()->getAsPointerType(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5492 | assert(PT && "Invalid @catch type."); |
| 5493 | const ObjCInterfaceType *IT = |
| 5494 | PT->getPointeeType()->getAsObjCInterfaceType(); |
| 5495 | assert(IT && "Invalid @catch type."); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5496 | llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5497 | SelectorArgs.push_back(EHType); |
| 5498 | } |
| 5499 | } |
| 5500 | } |
| 5501 | |
| 5502 | // We use a cleanup unless there was already a catch all. |
| 5503 | if (!HasCatchAll) { |
| 5504 | SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5505 | Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5506 | } |
| 5507 | |
| 5508 | llvm::Value *Selector = |
| 5509 | CGF.Builder.CreateCall(llvm_eh_selector_i64, |
| 5510 | SelectorArgs.begin(), SelectorArgs.end(), |
| 5511 | "selector"); |
| 5512 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5513 | const ParmVarDecl *CatchParam = Handlers[i].first; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5514 | const Stmt *CatchBody = Handlers[i].second; |
| 5515 | |
| 5516 | llvm::BasicBlock *Next = 0; |
| 5517 | |
| 5518 | // The last handler always matches. |
| 5519 | if (i + 1 != e) { |
| 5520 | assert(CatchParam && "Only last handler can be a catch all."); |
| 5521 | |
| 5522 | llvm::BasicBlock *Match = CGF.createBasicBlock("match"); |
| 5523 | Next = CGF.createBasicBlock("catch.next"); |
| 5524 | llvm::Value *Id = |
| 5525 | CGF.Builder.CreateCall(llvm_eh_typeid_for_i64, |
| 5526 | CGF.Builder.CreateBitCast(SelectorArgs[i+2], |
| 5527 | ObjCTypes.Int8PtrTy)); |
| 5528 | CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id), |
| 5529 | Match, Next); |
| 5530 | |
| 5531 | CGF.EmitBlock(Match); |
| 5532 | } |
| 5533 | |
| 5534 | if (CatchBody) { |
| 5535 | llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end"); |
| 5536 | llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler"); |
| 5537 | |
| 5538 | // Cleanups must call objc_end_catch. |
| 5539 | // |
| 5540 | // FIXME: It seems incorrect for objc_begin_catch to be inside |
| 5541 | // this context, but this matches gcc. |
| 5542 | CGF.PushCleanupBlock(MatchEnd); |
| 5543 | CGF.setInvokeDest(MatchHandler); |
| 5544 | |
| 5545 | llvm::Value *ExcObject = |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5546 | CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5547 | |
| 5548 | // Bind the catch parameter if it exists. |
| 5549 | if (CatchParam) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5550 | ExcObject = |
| 5551 | CGF.Builder.CreateBitCast(ExcObject, |
| 5552 | CGF.ConvertType(CatchParam->getType())); |
| 5553 | // CatchParam is a ParmVarDecl because of the grammar |
| 5554 | // construction used to handle this, but for codegen purposes |
| 5555 | // we treat this as a local decl. |
| 5556 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
| 5557 | CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5558 | } |
| 5559 | |
| 5560 | CGF.ObjCEHValueStack.push_back(ExcObject); |
| 5561 | CGF.EmitStmt(CatchBody); |
| 5562 | CGF.ObjCEHValueStack.pop_back(); |
| 5563 | |
| 5564 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5565 | |
| 5566 | CGF.EmitBlock(MatchHandler); |
| 5567 | |
| 5568 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5569 | // We are required to emit this call to satisfy LLVM, even |
| 5570 | // though we don't use the result. |
| 5571 | llvm::SmallVector<llvm::Value*, 8> Args; |
| 5572 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5573 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5574 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5575 | 0)); |
| 5576 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5577 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5578 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5579 | |
| 5580 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5581 | |
| 5582 | CGF.EmitBlock(MatchEnd); |
| 5583 | |
| 5584 | // Unfortunately, we also have to generate another EH frame here |
| 5585 | // in case this throws. |
| 5586 | llvm::BasicBlock *MatchEndHandler = |
| 5587 | CGF.createBasicBlock("match.end.handler"); |
| 5588 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5589 | CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5590 | Cont, MatchEndHandler, |
| 5591 | Args.begin(), Args.begin()); |
| 5592 | |
| 5593 | CGF.EmitBlock(Cont); |
| 5594 | if (Info.SwitchBlock) |
| 5595 | CGF.EmitBlock(Info.SwitchBlock); |
| 5596 | if (Info.EndBlock) |
| 5597 | CGF.EmitBlock(Info.EndBlock); |
| 5598 | |
| 5599 | CGF.EmitBlock(MatchEndHandler); |
| 5600 | Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5601 | // We are required to emit this call to satisfy LLVM, even |
| 5602 | // though we don't use the result. |
| 5603 | Args.clear(); |
| 5604 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5605 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5606 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5607 | 0)); |
| 5608 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5609 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5610 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5611 | |
| 5612 | if (Next) |
| 5613 | CGF.EmitBlock(Next); |
| 5614 | } else { |
| 5615 | assert(!Next && "catchup should be last handler."); |
| 5616 | |
| 5617 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5618 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5619 | } |
| 5620 | } |
| 5621 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5622 | // Pop the cleanup entry, the @finally is outside this cleanup |
| 5623 | // scope. |
| 5624 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5625 | CGF.setInvokeDest(PrevLandingPad); |
| 5626 | |
| 5627 | CGF.EmitBlock(FinallyBlock); |
| 5628 | |
| 5629 | if (isTry) { |
| 5630 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 5631 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 5632 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 5633 | } else { |
| 5634 | // Emit 'objc_sync_exit(expr)' as finally's sole statement for |
| 5635 | // @synchronized. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5636 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5637 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5638 | |
| 5639 | if (Info.SwitchBlock) |
| 5640 | CGF.EmitBlock(Info.SwitchBlock); |
| 5641 | if (Info.EndBlock) |
| 5642 | CGF.EmitBlock(Info.EndBlock); |
| 5643 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5644 | // Branch around the rethrow code. |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5645 | CGF.EmitBranch(FinallyEnd); |
| 5646 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5647 | CGF.EmitBlock(FinallyRethrow); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5648 | CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5649 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5650 | CGF.Builder.CreateUnreachable(); |
| 5651 | |
| 5652 | CGF.EmitBlock(FinallyEnd); |
| 5653 | } |
| 5654 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5655 | /// EmitThrowStmt - Generate code for a throw statement. |
| 5656 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 5657 | const ObjCAtThrowStmt &S) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5658 | llvm::Value *Exception; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5659 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5660 | Exception = CGF.EmitScalarExpr(ThrowExpr); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5661 | } else { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5662 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
| 5663 | "Unexpected rethrow outside @catch block."); |
| 5664 | Exception = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5665 | } |
| 5666 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5667 | llvm::Value *ExceptionAsObject = |
| 5668 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 5669 | llvm::BasicBlock *InvokeDest = CGF.getInvokeDest(); |
| 5670 | if (InvokeDest) { |
| 5671 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5672 | CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5673 | Cont, InvokeDest, |
| 5674 | &ExceptionAsObject, &ExceptionAsObject + 1); |
| 5675 | CGF.EmitBlock(Cont); |
| 5676 | } else |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5677 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5678 | CGF.Builder.CreateUnreachable(); |
| 5679 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5680 | // Clear the insertion point to indicate we are in unreachable code. |
| 5681 | CGF.Builder.ClearInsertionPoint(); |
| 5682 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5683 | |
| 5684 | llvm::Value * |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5685 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 5686 | bool ForDefinition) { |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5687 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5688 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5689 | // If we don't need a definition, return the entry if found or check |
| 5690 | // if we use an external reference. |
| 5691 | if (!ForDefinition) { |
| 5692 | if (Entry) |
| 5693 | return Entry; |
Daniel Dunbar | 7e075cb | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 5694 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5695 | // If this type (or a super class) has the __objc_exception__ |
| 5696 | // attribute, emit an external reference. |
| 5697 | if (hasObjCExceptionAttribute(ID)) |
| 5698 | return Entry = |
| 5699 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5700 | llvm::GlobalValue::ExternalLinkage, |
| 5701 | 0, |
| 5702 | (std::string("OBJC_EHTYPE_$_") + |
| 5703 | ID->getIdentifier()->getName()), |
| 5704 | &CGM.getModule()); |
| 5705 | } |
| 5706 | |
| 5707 | // Otherwise we need to either make a new entry or fill in the |
| 5708 | // initializer. |
| 5709 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5710 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5711 | std::string VTableName = "objc_ehtype_vtable"; |
| 5712 | llvm::GlobalVariable *VTableGV = |
| 5713 | CGM.getModule().getGlobalVariable(VTableName); |
| 5714 | if (!VTableGV) |
| 5715 | VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false, |
| 5716 | llvm::GlobalValue::ExternalLinkage, |
| 5717 | 0, VTableName, &CGM.getModule()); |
| 5718 | |
| 5719 | llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2); |
| 5720 | |
| 5721 | std::vector<llvm::Constant*> Values(3); |
| 5722 | Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1); |
| 5723 | Values[1] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5724 | Values[2] = GetClassGlobal(ClassName); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5725 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
| 5726 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5727 | if (Entry) { |
| 5728 | Entry->setInitializer(Init); |
| 5729 | } else { |
| 5730 | Entry = new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5731 | llvm::GlobalValue::WeakAnyLinkage, |
| 5732 | Init, |
| 5733 | (std::string("OBJC_EHTYPE_$_") + |
| 5734 | ID->getIdentifier()->getName()), |
| 5735 | &CGM.getModule()); |
| 5736 | } |
| 5737 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 5738 | if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden) |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5739 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5740 | Entry->setAlignment(8); |
| 5741 | |
| 5742 | if (ForDefinition) { |
| 5743 | Entry->setSection("__DATA,__objc_const"); |
| 5744 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 5745 | } else { |
| 5746 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 5747 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5748 | |
| 5749 | return Entry; |
| 5750 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5751 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5752 | /* *** */ |
| 5753 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5754 | CodeGen::CGObjCRuntime * |
| 5755 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 5756 | return new CGObjCMac(CGM); |
| 5757 | } |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5758 | |
| 5759 | CodeGen::CGObjCRuntime * |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5760 | CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5761 | return new CGObjCNonFragileABIMac(CGM); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5762 | } |