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) { |
| 46 | const ObjCInterfaceDecl *Super = OID->getSuperClass(); |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 47 | |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 48 | // FIXME: The index here is closely tied to how |
| 49 | // ASTContext::getObjCLayout is implemented. This should be fixed to |
| 50 | // get the information from the layout directly. |
| 51 | Index = 0; |
| 52 | for (ObjCInterfaceDecl::ivar_iterator IVI = OID->ivar_begin(), |
| 53 | IVE = OID->ivar_end(); IVI != IVE; ++IVI, ++Index) |
| 54 | if (OIVD == *IVI) |
| 55 | return OID; |
| 56 | |
| 57 | // Also look in synthesized ivars. |
| 58 | for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(Context), |
| 59 | E = OID->prop_end(Context); I != E; ++I) { |
| 60 | if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl()) { |
| 61 | if (OIVD == Ivar) |
| 62 | return OID; |
| 63 | ++Index; |
| 64 | } |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 67 | // Otherwise check in the super class. |
| 68 | if (Super) |
| 69 | return FindIvarInterface(Context, Super, OIVD, Index); |
| 70 | |
| 71 | return 0; |
Daniel Dunbar | a243578 | 2009-04-22 12:00:04 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 74 | static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM, |
| 75 | const ObjCInterfaceDecl *OID, |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 76 | const ObjCImplementationDecl *ID, |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 77 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 78 | unsigned Index; |
| 79 | const ObjCInterfaceDecl *Container = |
| 80 | FindIvarInterface(CGM.getContext(), OID, Ivar, Index); |
| 81 | assert(Container && "Unable to find ivar container"); |
| 82 | |
| 83 | // If we know have an implementation (and the ivar is in it) then |
| 84 | // look up in the implementation layout. |
| 85 | const ASTRecordLayout *RL; |
| 86 | if (ID && ID->getClassInterface() == Container) |
| 87 | RL = &CGM.getContext().getASTObjCImplementationLayout(ID); |
| 88 | else |
| 89 | RL = &CGM.getContext().getASTObjCInterfaceLayout(Container); |
| 90 | return RL->getFieldOffset(Index); |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM, |
| 94 | const ObjCInterfaceDecl *OID, |
| 95 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 96 | return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8; |
| 97 | } |
| 98 | |
| 99 | uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM, |
| 100 | const ObjCImplementationDecl *OID, |
| 101 | const ObjCIvarDecl *Ivar) { |
| 102 | return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8; |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF, |
| 106 | const ObjCInterfaceDecl *OID, |
| 107 | llvm::Value *BaseValue, |
| 108 | const ObjCIvarDecl *Ivar, |
| 109 | unsigned CVRQualifiers, |
| 110 | llvm::Value *Offset) { |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 111 | // Compute (type*) ( (char *) BaseValue + Offset) |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 112 | llvm::Type *I8Ptr = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 113 | QualType IvarTy = Ivar->getType(); |
| 114 | const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 115 | llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 116 | V = CGF.Builder.CreateGEP(V, Offset, "add.ptr"); |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 117 | V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy)); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 118 | |
| 119 | if (Ivar->isBitField()) { |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 120 | // We need to compute the bit offset for the bit-field, the offset |
| 121 | // is to the byte. Note, there is a subtle invariant here: we can |
| 122 | // only call this routine on non-sythesized ivars but we may be |
| 123 | // called for synthesized ivars. However, a synthesized ivar can |
| 124 | // never be a bit-field so this is safe. |
| 125 | uint64_t BitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar) % 8; |
| 126 | |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 127 | uint64_t BitFieldSize = |
| 128 | Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue(); |
| 129 | return LValue::MakeBitfield(V, BitOffset, BitFieldSize, |
Daniel Dunbar | e38df86 | 2009-05-03 07:52:00 +0000 | [diff] [blame] | 130 | IvarTy->isSignedIntegerType(), |
| 131 | IvarTy.getCVRQualifiers()|CVRQualifiers); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 134 | LValue LV = LValue::MakeAddr(V, IvarTy.getCVRQualifiers()|CVRQualifiers, |
| 135 | CGF.CGM.getContext().getObjCGCAttrKind(IvarTy)); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 136 | LValue::SetObjCIvar(LV, true); |
| 137 | return LV; |
| 138 | } |
| 139 | |
| 140 | /// |
| 141 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 142 | namespace { |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 143 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 144 | typedef std::vector<llvm::Constant*> ConstantVector; |
| 145 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 146 | // FIXME: We should find a nicer way to make the labels for |
| 147 | // metadata, string concatenation is lame. |
| 148 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 149 | class ObjCCommonTypesHelper { |
| 150 | protected: |
| 151 | CodeGen::CodeGenModule &CGM; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 152 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 153 | public: |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 154 | const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 155 | const llvm::Type *Int8PtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 156 | |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 157 | /// ObjectPtrTy - LLVM type for object handles (typeof(id)) |
| 158 | const llvm::Type *ObjectPtrTy; |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 159 | |
| 160 | /// PtrObjectPtrTy - LLVM type for id * |
| 161 | const llvm::Type *PtrObjectPtrTy; |
| 162 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 163 | /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL)) |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 164 | const llvm::Type *SelectorPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 165 | /// ProtocolPtrTy - LLVM type for external protocol handles |
| 166 | /// (typeof(Protocol)) |
| 167 | const llvm::Type *ExternalProtocolPtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 168 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 169 | // SuperCTy - clang type for struct objc_super. |
| 170 | QualType SuperCTy; |
| 171 | // SuperPtrCTy - clang type for struct objc_super *. |
| 172 | QualType SuperPtrCTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 173 | |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 174 | /// SuperTy - LLVM type for struct objc_super. |
| 175 | const llvm::StructType *SuperTy; |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 176 | /// SuperPtrTy - LLVM type for struct objc_super *. |
| 177 | const llvm::Type *SuperPtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 178 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 179 | /// PropertyTy - LLVM type for struct objc_property (struct _prop_t |
| 180 | /// in GCC parlance). |
| 181 | const llvm::StructType *PropertyTy; |
| 182 | |
| 183 | /// PropertyListTy - LLVM type for struct objc_property_list |
| 184 | /// (_prop_list_t in GCC parlance). |
| 185 | const llvm::StructType *PropertyListTy; |
| 186 | /// PropertyListPtrTy - LLVM type for struct objc_property_list*. |
| 187 | const llvm::Type *PropertyListPtrTy; |
| 188 | |
| 189 | // MethodTy - LLVM type for struct objc_method. |
| 190 | const llvm::StructType *MethodTy; |
| 191 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 192 | /// CacheTy - LLVM type for struct objc_cache. |
| 193 | const llvm::Type *CacheTy; |
| 194 | /// CachePtrTy - LLVM type for struct objc_cache *. |
| 195 | const llvm::Type *CachePtrTy; |
| 196 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 197 | llvm::Constant *getGetPropertyFn() { |
| 198 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 199 | ASTContext &Ctx = CGM.getContext(); |
| 200 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
| 201 | llvm::SmallVector<QualType,16> Params; |
| 202 | QualType IdType = Ctx.getObjCIdType(); |
| 203 | QualType SelType = Ctx.getObjCSelType(); |
| 204 | Params.push_back(IdType); |
| 205 | Params.push_back(SelType); |
| 206 | Params.push_back(Ctx.LongTy); |
| 207 | Params.push_back(Ctx.BoolTy); |
| 208 | const llvm::FunctionType *FTy = |
| 209 | Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false); |
| 210 | return CGM.CreateRuntimeFunction(FTy, "objc_getProperty"); |
| 211 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 212 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 213 | llvm::Constant *getSetPropertyFn() { |
| 214 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 215 | ASTContext &Ctx = CGM.getContext(); |
| 216 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
| 217 | llvm::SmallVector<QualType,16> Params; |
| 218 | QualType IdType = Ctx.getObjCIdType(); |
| 219 | QualType SelType = Ctx.getObjCSelType(); |
| 220 | Params.push_back(IdType); |
| 221 | Params.push_back(SelType); |
| 222 | Params.push_back(Ctx.LongTy); |
| 223 | Params.push_back(IdType); |
| 224 | Params.push_back(Ctx.BoolTy); |
| 225 | Params.push_back(Ctx.BoolTy); |
| 226 | const llvm::FunctionType *FTy = |
| 227 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
| 228 | return CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); |
| 229 | } |
| 230 | |
| 231 | llvm::Constant *getEnumerationMutationFn() { |
| 232 | // void objc_enumerationMutation (id) |
| 233 | std::vector<const llvm::Type*> Args; |
| 234 | Args.push_back(ObjectPtrTy); |
| 235 | llvm::FunctionType *FTy = |
| 236 | llvm::FunctionType::get(llvm::Type::VoidTy, Args, false); |
| 237 | return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation"); |
| 238 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 239 | |
| 240 | /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 241 | llvm::Constant *getGcReadWeakFn() { |
| 242 | // id objc_read_weak (id *) |
| 243 | std::vector<const llvm::Type*> Args; |
| 244 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 245 | llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 246 | return CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
| 247 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 248 | |
| 249 | /// GcAssignWeakFn -- LLVM objc_assign_weak function. |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 250 | llvm::Constant *getGcAssignWeakFn() { |
| 251 | // id objc_assign_weak (id, id *) |
| 252 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 253 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 254 | llvm::FunctionType *FTy = |
| 255 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 256 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 257 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 258 | |
| 259 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 260 | llvm::Constant *getGcAssignGlobalFn() { |
| 261 | // id objc_assign_global(id, id *) |
| 262 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 263 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 264 | llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 265 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 266 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 267 | |
| 268 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 269 | llvm::Constant *getGcAssignIvarFn() { |
| 270 | // id objc_assign_ivar(id, id *) |
| 271 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 272 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 273 | llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 274 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 275 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 276 | |
| 277 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 278 | llvm::Constant *getGcAssignStrongCastFn() { |
| 279 | // id objc_assign_global(id, id *) |
| 280 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 281 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 282 | llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 283 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); |
| 284 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 285 | |
| 286 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 287 | llvm::Constant *getExceptionThrowFn() { |
| 288 | // void objc_exception_throw(id) |
| 289 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 290 | llvm::FunctionType *FTy = |
| 291 | llvm::FunctionType::get(llvm::Type::VoidTy, Args, false); |
| 292 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
| 293 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 294 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 295 | /// SyncEnterFn - LLVM object_sync_enter function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 296 | llvm::Constant *getSyncEnterFn() { |
| 297 | // void objc_sync_enter (id) |
| 298 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 299 | llvm::FunctionType *FTy = |
| 300 | llvm::FunctionType::get(llvm::Type::VoidTy, Args, false); |
| 301 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 302 | } |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 303 | |
| 304 | /// SyncExitFn - LLVM object_sync_exit function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 305 | llvm::Constant *getSyncExitFn() { |
| 306 | // void objc_sync_exit (id) |
| 307 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 308 | llvm::FunctionType *FTy = |
| 309 | llvm::FunctionType::get(llvm::Type::VoidTy, Args, false); |
| 310 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
| 311 | } |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 312 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 313 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
| 314 | ~ObjCCommonTypesHelper(){} |
| 315 | }; |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 316 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 317 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 318 | /// construction of varies types used during ObjC generation. |
| 319 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
| 320 | private: |
| 321 | |
Chris Lattner | 4176b0c | 2009-04-22 02:32:31 +0000 | [diff] [blame] | 322 | llvm::Constant *getMessageSendFn() { |
| 323 | // id objc_msgSend (id, SEL, ...) |
| 324 | std::vector<const llvm::Type*> Params; |
| 325 | Params.push_back(ObjectPtrTy); |
| 326 | Params.push_back(SelectorPtrTy); |
| 327 | return |
| 328 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 329 | Params, true), |
| 330 | "objc_msgSend"); |
| 331 | } |
| 332 | |
| 333 | llvm::Constant *getMessageSendStretFn() { |
| 334 | // id objc_msgSend_stret (id, SEL, ...) |
| 335 | std::vector<const llvm::Type*> Params; |
| 336 | Params.push_back(ObjectPtrTy); |
| 337 | Params.push_back(SelectorPtrTy); |
| 338 | return |
| 339 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 340 | Params, true), |
| 341 | "objc_msgSend_stret"); |
| 342 | |
| 343 | } |
| 344 | |
| 345 | llvm::Constant *getMessageSendFpretFn() { |
| 346 | // FIXME: This should be long double on x86_64? |
| 347 | // [double | long double] objc_msgSend_fpret(id self, SEL op, ...) |
| 348 | std::vector<const llvm::Type*> Params; |
| 349 | Params.push_back(ObjectPtrTy); |
| 350 | Params.push_back(SelectorPtrTy); |
| 351 | return |
| 352 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy, |
| 353 | Params, |
| 354 | true), |
| 355 | "objc_msgSend_fpret"); |
| 356 | |
| 357 | } |
| 358 | |
| 359 | llvm::Constant *getMessageSendSuperFn() { |
| 360 | // id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
| 361 | std::vector<const llvm::Type*> Params; |
| 362 | Params.push_back(SuperPtrTy); |
| 363 | Params.push_back(SelectorPtrTy); |
| 364 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 365 | Params, true), |
| 366 | "objc_msgSendSuper"); |
| 367 | } |
| 368 | llvm::Constant *getMessageSendSuperStretFn() { |
| 369 | // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super, |
| 370 | // SEL op, ...) |
| 371 | std::vector<const llvm::Type*> Params; |
| 372 | Params.push_back(Int8PtrTy); |
| 373 | Params.push_back(SuperPtrTy); |
| 374 | Params.push_back(SelectorPtrTy); |
| 375 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 376 | Params, true), |
| 377 | "objc_msgSendSuper_stret"); |
| 378 | } |
| 379 | |
| 380 | llvm::Constant *getMessageSendSuperFpretFn() { |
| 381 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 382 | return getMessageSendSuperFn(); |
| 383 | } |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 384 | |
| 385 | public: |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 386 | /// SymtabTy - LLVM type for struct objc_symtab. |
| 387 | const llvm::StructType *SymtabTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 388 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
| 389 | const llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 390 | /// ModuleTy - LLVM type for struct objc_module. |
| 391 | const llvm::StructType *ModuleTy; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 392 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 393 | /// ProtocolTy - LLVM type for struct objc_protocol. |
| 394 | const llvm::StructType *ProtocolTy; |
| 395 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
| 396 | const llvm::Type *ProtocolPtrTy; |
| 397 | /// ProtocolExtensionTy - LLVM type for struct |
| 398 | /// objc_protocol_extension. |
| 399 | const llvm::StructType *ProtocolExtensionTy; |
| 400 | /// ProtocolExtensionTy - LLVM type for struct |
| 401 | /// objc_protocol_extension *. |
| 402 | const llvm::Type *ProtocolExtensionPtrTy; |
| 403 | /// MethodDescriptionTy - LLVM type for struct |
| 404 | /// objc_method_description. |
| 405 | const llvm::StructType *MethodDescriptionTy; |
| 406 | /// MethodDescriptionListTy - LLVM type for struct |
| 407 | /// objc_method_description_list. |
| 408 | const llvm::StructType *MethodDescriptionListTy; |
| 409 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 410 | /// objc_method_description_list *. |
| 411 | const llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 412 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
| 413 | const llvm::Type *ProtocolListTy; |
| 414 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
| 415 | const llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 416 | /// CategoryTy - LLVM type for struct objc_category. |
| 417 | const llvm::StructType *CategoryTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 418 | /// ClassTy - LLVM type for struct objc_class. |
| 419 | const llvm::StructType *ClassTy; |
| 420 | /// ClassPtrTy - LLVM type for struct objc_class *. |
| 421 | const llvm::Type *ClassPtrTy; |
| 422 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
| 423 | const llvm::StructType *ClassExtensionTy; |
| 424 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
| 425 | const llvm::Type *ClassExtensionPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 426 | // IvarTy - LLVM type for struct objc_ivar. |
| 427 | const llvm::StructType *IvarTy; |
| 428 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
| 429 | const llvm::Type *IvarListTy; |
| 430 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
| 431 | const llvm::Type *IvarListPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 432 | /// MethodListTy - LLVM type for struct objc_method_list. |
| 433 | const llvm::Type *MethodListTy; |
| 434 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
| 435 | const llvm::Type *MethodListPtrTy; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 436 | |
| 437 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
| 438 | const llvm::Type *ExceptionDataTy; |
| 439 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 440 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 441 | llvm::Constant *getExceptionTryEnterFn() { |
| 442 | std::vector<const llvm::Type*> Params; |
| 443 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 444 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 445 | Params, false), |
| 446 | "objc_exception_try_enter"); |
| 447 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 448 | |
| 449 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 450 | llvm::Constant *getExceptionTryExitFn() { |
| 451 | std::vector<const llvm::Type*> Params; |
| 452 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 453 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 454 | Params, false), |
| 455 | "objc_exception_try_exit"); |
| 456 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 457 | |
| 458 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 459 | llvm::Constant *getExceptionExtractFn() { |
| 460 | std::vector<const llvm::Type*> Params; |
| 461 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 462 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 463 | Params, false), |
| 464 | "objc_exception_extract"); |
| 465 | |
| 466 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 467 | |
| 468 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 469 | llvm::Constant *getExceptionMatchFn() { |
| 470 | std::vector<const llvm::Type*> Params; |
| 471 | Params.push_back(ClassPtrTy); |
| 472 | Params.push_back(ObjectPtrTy); |
| 473 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 474 | Params, false), |
| 475 | "objc_exception_match"); |
| 476 | |
| 477 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 478 | |
| 479 | /// SetJmpFn - LLVM _setjmp function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 480 | llvm::Constant *getSetJmpFn() { |
| 481 | std::vector<const llvm::Type*> Params; |
| 482 | Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty)); |
| 483 | return |
| 484 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 485 | Params, false), |
| 486 | "_setjmp"); |
| 487 | |
| 488 | } |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 489 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 490 | public: |
| 491 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 492 | ~ObjCTypesHelper() {} |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 493 | |
| 494 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 495 | llvm::Constant *getSendFn(bool IsSuper) { |
Chris Lattner | 4176b0c | 2009-04-22 02:32:31 +0000 | [diff] [blame] | 496 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFn(); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 499 | llvm::Constant *getSendStretFn(bool IsSuper) { |
Chris Lattner | 4176b0c | 2009-04-22 02:32:31 +0000 | [diff] [blame] | 500 | return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn(); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 503 | llvm::Constant *getSendFpretFn(bool IsSuper) { |
Chris Lattner | 4176b0c | 2009-04-22 02:32:31 +0000 | [diff] [blame] | 504 | return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn(); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 505 | } |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 506 | }; |
| 507 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 508 | /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 509 | /// modern abi |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 510 | class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 511 | public: |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 512 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 513 | // MethodListnfABITy - LLVM for struct _method_list_t |
| 514 | const llvm::StructType *MethodListnfABITy; |
| 515 | |
| 516 | // MethodListnfABIPtrTy - LLVM for struct _method_list_t* |
| 517 | const llvm::Type *MethodListnfABIPtrTy; |
| 518 | |
| 519 | // ProtocolnfABITy = LLVM for struct _protocol_t |
| 520 | const llvm::StructType *ProtocolnfABITy; |
| 521 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 522 | // ProtocolnfABIPtrTy = LLVM for struct _protocol_t* |
| 523 | const llvm::Type *ProtocolnfABIPtrTy; |
| 524 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 525 | // ProtocolListnfABITy - LLVM for struct _objc_protocol_list |
| 526 | const llvm::StructType *ProtocolListnfABITy; |
| 527 | |
| 528 | // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list* |
| 529 | const llvm::Type *ProtocolListnfABIPtrTy; |
| 530 | |
| 531 | // ClassnfABITy - LLVM for struct _class_t |
| 532 | const llvm::StructType *ClassnfABITy; |
| 533 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 534 | // ClassnfABIPtrTy - LLVM for struct _class_t* |
| 535 | const llvm::Type *ClassnfABIPtrTy; |
| 536 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 537 | // IvarnfABITy - LLVM for struct _ivar_t |
| 538 | const llvm::StructType *IvarnfABITy; |
| 539 | |
| 540 | // IvarListnfABITy - LLVM for struct _ivar_list_t |
| 541 | const llvm::StructType *IvarListnfABITy; |
| 542 | |
| 543 | // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t* |
| 544 | const llvm::Type *IvarListnfABIPtrTy; |
| 545 | |
| 546 | // ClassRonfABITy - LLVM for struct _class_ro_t |
| 547 | const llvm::StructType *ClassRonfABITy; |
| 548 | |
| 549 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 550 | const llvm::Type *ImpnfABITy; |
| 551 | |
| 552 | // CategorynfABITy - LLVM for struct _category_t |
| 553 | const llvm::StructType *CategorynfABITy; |
| 554 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 555 | // New types for nonfragile abi messaging. |
| 556 | |
| 557 | // MessageRefTy - LLVM for: |
| 558 | // struct _message_ref_t { |
| 559 | // IMP messenger; |
| 560 | // SEL name; |
| 561 | // }; |
| 562 | const llvm::StructType *MessageRefTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 563 | // MessageRefCTy - clang type for struct _message_ref_t |
| 564 | QualType MessageRefCTy; |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 565 | |
| 566 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 567 | const llvm::Type *MessageRefPtrTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 568 | // MessageRefCPtrTy - clang type for struct _message_ref_t* |
| 569 | QualType MessageRefCPtrTy; |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 570 | |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 571 | // MessengerTy - Type of the messenger (shown as IMP above) |
| 572 | const llvm::FunctionType *MessengerTy; |
| 573 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 574 | // SuperMessageRefTy - LLVM for: |
| 575 | // struct _super_message_ref_t { |
| 576 | // SUPER_IMP messenger; |
| 577 | // SEL name; |
| 578 | // }; |
| 579 | const llvm::StructType *SuperMessageRefTy; |
| 580 | |
| 581 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 582 | const llvm::Type *SuperMessageRefPtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 583 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 584 | llvm::Constant *getMessageSendFixupFn() { |
| 585 | // id objc_msgSend_fixup(id, struct message_ref_t*, ...) |
| 586 | std::vector<const llvm::Type*> Params; |
| 587 | Params.push_back(ObjectPtrTy); |
| 588 | Params.push_back(MessageRefPtrTy); |
| 589 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 590 | Params, true), |
| 591 | "objc_msgSend_fixup"); |
| 592 | } |
| 593 | |
| 594 | llvm::Constant *getMessageSendFpretFixupFn() { |
| 595 | // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...) |
| 596 | std::vector<const llvm::Type*> Params; |
| 597 | Params.push_back(ObjectPtrTy); |
| 598 | Params.push_back(MessageRefPtrTy); |
| 599 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 600 | Params, true), |
| 601 | "objc_msgSend_fpret_fixup"); |
| 602 | } |
| 603 | |
| 604 | llvm::Constant *getMessageSendStretFixupFn() { |
| 605 | // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...) |
| 606 | std::vector<const llvm::Type*> Params; |
| 607 | Params.push_back(ObjectPtrTy); |
| 608 | Params.push_back(MessageRefPtrTy); |
| 609 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 610 | Params, true), |
| 611 | "objc_msgSend_stret_fixup"); |
| 612 | } |
| 613 | |
| 614 | llvm::Constant *getMessageSendIdFixupFn() { |
| 615 | // id objc_msgSendId_fixup(id, struct message_ref_t*, ...) |
| 616 | std::vector<const llvm::Type*> Params; |
| 617 | Params.push_back(ObjectPtrTy); |
| 618 | Params.push_back(MessageRefPtrTy); |
| 619 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 620 | Params, true), |
| 621 | "objc_msgSendId_fixup"); |
| 622 | } |
| 623 | |
| 624 | llvm::Constant *getMessageSendIdStretFixupFn() { |
| 625 | // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...) |
| 626 | std::vector<const llvm::Type*> Params; |
| 627 | Params.push_back(ObjectPtrTy); |
| 628 | Params.push_back(MessageRefPtrTy); |
| 629 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 630 | Params, true), |
| 631 | "objc_msgSendId_stret_fixup"); |
| 632 | } |
| 633 | llvm::Constant *getMessageSendSuper2FixupFn() { |
| 634 | // id objc_msgSendSuper2_fixup (struct objc_super *, |
| 635 | // struct _super_message_ref_t*, ...) |
| 636 | std::vector<const llvm::Type*> Params; |
| 637 | Params.push_back(SuperPtrTy); |
| 638 | Params.push_back(SuperMessageRefPtrTy); |
| 639 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 640 | Params, true), |
| 641 | "objc_msgSendSuper2_fixup"); |
| 642 | } |
| 643 | |
| 644 | llvm::Constant *getMessageSendSuper2StretFixupFn() { |
| 645 | // id objc_msgSendSuper2_stret_fixup(struct objc_super *, |
| 646 | // struct _super_message_ref_t*, ...) |
| 647 | std::vector<const llvm::Type*> Params; |
| 648 | Params.push_back(SuperPtrTy); |
| 649 | Params.push_back(SuperMessageRefPtrTy); |
| 650 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 651 | Params, true), |
| 652 | "objc_msgSendSuper2_stret_fixup"); |
| 653 | } |
| 654 | |
| 655 | |
| 656 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 657 | /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C |
| 658 | /// exception personality function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 659 | llvm::Value *getEHPersonalityPtr() { |
| 660 | llvm::Constant *Personality = |
| 661 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 662 | std::vector<const llvm::Type*>(), |
| 663 | true), |
| 664 | "__objc_personality_v0"); |
| 665 | return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy); |
| 666 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 667 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 668 | llvm::Constant *getUnwindResumeOrRethrowFn() { |
| 669 | std::vector<const llvm::Type*> Params; |
| 670 | Params.push_back(Int8PtrTy); |
| 671 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 672 | Params, false), |
| 673 | "_Unwind_Resume_or_Rethrow"); |
| 674 | } |
| 675 | |
| 676 | llvm::Constant *getObjCEndCatchFn() { |
| 677 | std::vector<const llvm::Type*> Params; |
| 678 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 679 | Params, false), |
| 680 | "objc_end_catch"); |
| 681 | |
| 682 | } |
| 683 | |
| 684 | llvm::Constant *getObjCBeginCatchFn() { |
| 685 | std::vector<const llvm::Type*> Params; |
| 686 | Params.push_back(Int8PtrTy); |
| 687 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy, |
| 688 | Params, false), |
| 689 | "objc_begin_catch"); |
| 690 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 691 | |
| 692 | const llvm::StructType *EHTypeTy; |
| 693 | const llvm::Type *EHTypePtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 694 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 695 | ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); |
| 696 | ~ObjCNonFragileABITypesHelper(){} |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 697 | }; |
| 698 | |
| 699 | class CGObjCCommonMac : public CodeGen::CGObjCRuntime { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 700 | public: |
| 701 | // FIXME - accessibility |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 702 | class GC_IVAR { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 703 | public: |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 704 | unsigned ivar_bytepos; |
| 705 | unsigned ivar_size; |
| 706 | GC_IVAR(unsigned bytepos = 0, unsigned size = 0) |
| 707 | : ivar_bytepos(bytepos), ivar_size(size) {} |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 708 | |
| 709 | // Allow sorting based on byte pos. |
| 710 | bool operator<(const GC_IVAR &b) const { |
| 711 | return ivar_bytepos < b.ivar_bytepos; |
| 712 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 713 | }; |
| 714 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 715 | class SKIP_SCAN { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 716 | public: |
| 717 | unsigned skip; |
| 718 | unsigned scan; |
| 719 | SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0) |
| 720 | : skip(_skip), scan(_scan) {} |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 721 | }; |
| 722 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 723 | protected: |
| 724 | CodeGen::CodeGenModule &CGM; |
| 725 | // FIXME! May not be needing this after all. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 726 | unsigned ObjCABI; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 727 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 728 | // gc ivar layout bitmap calculation helper caches. |
| 729 | llvm::SmallVector<GC_IVAR, 16> SkipIvars; |
| 730 | llvm::SmallVector<GC_IVAR, 16> IvarsInfo; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 731 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 732 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 733 | /// DefinedSymbols and FinishModule(). |
| 734 | std::set<IdentifierInfo*> LazySymbols; |
| 735 | |
| 736 | /// DefinedSymbols - External symbols which are defined by this |
| 737 | /// module. The symbols in this list and LazySymbols are used to add |
| 738 | /// special linker symbols which ensure that Objective-C modules are |
| 739 | /// linked properly. |
| 740 | std::set<IdentifierInfo*> DefinedSymbols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 741 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 742 | /// ClassNames - uniqued class names. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 743 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 744 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 745 | /// MethodVarNames - uniqued method variable names. |
| 746 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 747 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 748 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 749 | /// a StringMap here because have no other unique reference. |
| 750 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 751 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 752 | /// MethodDefinitions - map of methods which have been defined in |
| 753 | /// this translation unit. |
| 754 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 755 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 756 | /// PropertyNames - uniqued method variable names. |
| 757 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 758 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 759 | /// ClassReferences - uniqued class references. |
| 760 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 761 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 762 | /// SelectorReferences - uniqued selector references. |
| 763 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 764 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 765 | /// Protocols - Protocols for which an objc_protocol structure has |
| 766 | /// been emitted. Forward declarations are handled by creating an |
| 767 | /// empty structure whose initializer is filled in when/if defined. |
| 768 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 769 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 770 | /// DefinedProtocols - Protocols which have actually been |
| 771 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 772 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 773 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 774 | /// DefinedClasses - List of defined classes. |
| 775 | std::vector<llvm::GlobalValue*> DefinedClasses; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 776 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 777 | /// DefinedCategories - List of defined categories. |
| 778 | std::vector<llvm::GlobalValue*> DefinedCategories; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 779 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 780 | /// UsedGlobals - List of globals to pack into the llvm.used metadata |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 781 | /// to prevent them from being clobbered. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 782 | std::vector<llvm::GlobalVariable*> UsedGlobals; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 783 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 784 | /// GetNameForMethod - Return a name for the given method. |
| 785 | /// \param[out] NameOut - The return value. |
| 786 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 787 | const ObjCContainerDecl *CD, |
| 788 | std::string &NameOut); |
| 789 | |
| 790 | /// GetMethodVarName - Return a unique constant for the given |
| 791 | /// selector's name. The return value has type char *. |
| 792 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 793 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
| 794 | llvm::Constant *GetMethodVarName(const std::string &Name); |
| 795 | |
| 796 | /// GetMethodVarType - Return a unique constant for the given |
| 797 | /// selector's name. The return value has type char *. |
| 798 | |
| 799 | // FIXME: This is a horrible name. |
| 800 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 801 | llvm::Constant *GetMethodVarType(const FieldDecl *D); |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 802 | |
| 803 | /// GetPropertyName - Return a unique constant for the given |
| 804 | /// name. The return value has type char *. |
| 805 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
| 806 | |
| 807 | // FIXME: This can be dropped once string functions are unified. |
| 808 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 809 | const Decl *Container); |
| 810 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 811 | /// GetClassName - Return a unique constant for the given selector's |
| 812 | /// name. The return value has type char *. |
| 813 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
| 814 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 815 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 816 | /// implementation for the __strong or __weak case. |
| 817 | /// |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 818 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
| 819 | bool ForStrongLayout); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 820 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 821 | void BuildAggrIvarRecordLayout(const RecordType *RT, |
| 822 | unsigned int BytePos, bool ForStrongLayout, |
| 823 | bool &HasUnion); |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 824 | void BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 825 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 826 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 827 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 828 | unsigned int BytePos, bool ForStrongLayout, |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 829 | bool &HasUnion); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 830 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 831 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 832 | /// ivar layout bitmap. |
| 833 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 834 | const ObjCCommonTypesHelper &ObjCTypes); |
| 835 | |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 836 | /// EmitPropertyList - Emit the given property list. The return |
| 837 | /// value has type PropertyListPtrTy. |
| 838 | llvm::Constant *EmitPropertyList(const std::string &Name, |
| 839 | const Decl *Container, |
| 840 | const ObjCContainerDecl *OCD, |
| 841 | const ObjCCommonTypesHelper &ObjCTypes); |
| 842 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 843 | /// GetProtocolRef - Return a reference to the internal protocol |
| 844 | /// description, creating an empty one if it has not been |
| 845 | /// defined. The return value has type ProtocolPtrTy. |
| 846 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 847 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 848 | /// CreateMetadataVar - Create a global variable with internal |
| 849 | /// linkage for use by the Objective-C runtime. |
| 850 | /// |
| 851 | /// This is a convenience wrapper which not only creates the |
| 852 | /// variable, but also sets the section and alignment and adds the |
| 853 | /// global to the UsedGlobals list. |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 854 | /// |
| 855 | /// \param Name - The variable name. |
| 856 | /// \param Init - The variable initializer; this is also used to |
| 857 | /// define the type of the variable. |
| 858 | /// \param Section - The section the variable should go into, or 0. |
| 859 | /// \param Align - The alignment for the variable, or 0. |
| 860 | /// \param AddToUsed - Whether the variable should be added to |
Daniel Dunbar | c158306 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 861 | /// "llvm.used". |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 862 | llvm::GlobalVariable *CreateMetadataVar(const std::string &Name, |
| 863 | llvm::Constant *Init, |
| 864 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 865 | unsigned Align, |
| 866 | bool AddToUsed); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 867 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 868 | /// GetNamedIvarList - Return the list of ivars in the interface |
| 869 | /// itself (not including super classes and not including unnamed |
| 870 | /// bitfields). |
| 871 | /// |
| 872 | /// For the non-fragile ABI, this also includes synthesized property |
| 873 | /// ivars. |
| 874 | void GetNamedIvarList(const ObjCInterfaceDecl *OID, |
| 875 | llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const; |
| 876 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 877 | public: |
| 878 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm) |
| 879 | { } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 880 | |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 881 | virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 882 | |
| 883 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 884 | const ObjCContainerDecl *CD=0); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 885 | |
| 886 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
| 887 | |
| 888 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 889 | /// declaration, emitting it if necessary. The return value has type |
| 890 | /// ProtocolPtrTy. |
| 891 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
| 892 | |
| 893 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 894 | /// object for the given declaration, emitting it if needed. These |
| 895 | /// forward references will be filled in with empty bodies if no |
| 896 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 897 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 898 | }; |
| 899 | |
| 900 | class CGObjCMac : public CGObjCCommonMac { |
| 901 | private: |
| 902 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 903 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 904 | /// level information. |
| 905 | void EmitImageInfo(); |
| 906 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 907 | /// EmitModuleInfo - Another marker encoding module level |
| 908 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 909 | void EmitModuleInfo(); |
| 910 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 911 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 912 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 913 | llvm::Constant *EmitModuleSymbols(); |
| 914 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 915 | /// FinishModule - Write out global data structures at the end of |
| 916 | /// processing a translation unit. |
| 917 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 918 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 919 | /// EmitClassExtension - Generate the class extension structure used |
| 920 | /// to store the weak ivar layout and properties. The return value |
| 921 | /// has type ClassExtensionPtrTy. |
| 922 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 923 | |
| 924 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 925 | /// for the given class. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 926 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 927 | const ObjCInterfaceDecl *ID); |
| 928 | |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 929 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 930 | QualType ResultType, |
| 931 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 932 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 933 | QualType Arg0Ty, |
| 934 | bool IsSuper, |
| 935 | const CallArgList &CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 936 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 937 | /// EmitIvarList - Emit the ivar list for the given |
| 938 | /// implementation. If ForClass is true the list of class ivars |
| 939 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 940 | /// interface ivars will be emitted. The return value has type |
| 941 | /// IvarListPtrTy. |
| 942 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 943 | bool ForClass); |
| 944 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 945 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 946 | /// for the metaclass of the given interface. The return value has |
| 947 | /// type ClassPtrTy. |
| 948 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 949 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 950 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 951 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 952 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 953 | llvm::Constant *Protocols, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 954 | const ConstantVector &Methods); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 955 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 956 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 957 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 958 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 959 | |
| 960 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 961 | /// implementation. The return value has type MethodListPtrTy. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 962 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 963 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 964 | const ConstantVector &Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 965 | |
| 966 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 967 | /// method declarations. |
| 968 | /// - TypeName: The name for the type containing the methods. |
| 969 | /// - IsProtocol: True iff these methods are for a protocol. |
| 970 | /// - ClassMethds: True iff these are class methods. |
| 971 | /// - Required: When true, only "required" methods are |
| 972 | /// listed. Similarly, when false only "optional" methods are |
| 973 | /// listed. For classes this should always be true. |
| 974 | /// - begin, end: The method list to output. |
| 975 | /// |
| 976 | /// The return value has type MethodDescriptionListPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 977 | llvm::Constant *EmitMethodDescList(const std::string &Name, |
| 978 | const char *Section, |
| 979 | const ConstantVector &Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 980 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 981 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 982 | /// declaration, emitting it if necessary. The return value has type |
| 983 | /// ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 984 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 985 | |
| 986 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 987 | /// object for the given declaration, emitting it if needed. These |
| 988 | /// forward references will be filled in with empty bodies if no |
| 989 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 990 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 991 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 992 | /// EmitProtocolExtension - Generate the protocol extension |
| 993 | /// structure used to store optional instance and class methods, and |
| 994 | /// protocol properties. The return value has type |
| 995 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 996 | llvm::Constant * |
| 997 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 998 | const ConstantVector &OptInstanceMethods, |
| 999 | const ConstantVector &OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1000 | |
| 1001 | /// EmitProtocolList - Generate the list of referenced |
| 1002 | /// protocols. The return value has type ProtocolListPtrTy. |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1003 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 1004 | ObjCProtocolDecl::protocol_iterator begin, |
| 1005 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1006 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1007 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1008 | /// for the given selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1009 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1010 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1011 | public: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1012 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1013 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1014 | virtual llvm::Function *ModuleInitFunction(); |
| 1015 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1016 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1017 | QualType ResultType, |
| 1018 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1019 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1020 | bool IsClassMessage, |
| 1021 | const CallArgList &CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1022 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1023 | virtual CodeGen::RValue |
| 1024 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1025 | QualType ResultType, |
| 1026 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1027 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1028 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1029 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1030 | bool IsClassMessage, |
| 1031 | const CallArgList &CallArgs); |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1032 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1033 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1034 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1035 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1036 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1037 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1038 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1039 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1040 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1041 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1042 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1043 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1044 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1045 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 1046 | virtual llvm::Constant *GetPropertySetFunction(); |
| 1047 | virtual llvm::Constant *EnumerationMutationFunction(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1048 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 1049 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1050 | const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1051 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 1052 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1053 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 1054 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1055 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 1056 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1057 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 1058 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 1059 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 1060 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1061 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 1062 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 1063 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1064 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1065 | QualType ObjectTy, |
| 1066 | llvm::Value *BaseValue, |
| 1067 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1068 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1069 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1070 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1071 | const ObjCIvarDecl *Ivar); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1072 | }; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1073 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1074 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1075 | private: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1076 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1077 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 1078 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1079 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1080 | /// SuperClassReferences - uniqued super class references. |
| 1081 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; |
| 1082 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1083 | /// MetaClassReferences - uniqued meta class references. |
| 1084 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1085 | |
| 1086 | /// EHTypeReferences - uniqued class ehtype references. |
| 1087 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1088 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1089 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 1090 | /// processing a translation unit. |
| 1091 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1092 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 1093 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 1094 | unsigned InstanceStart, |
| 1095 | unsigned InstanceSize, |
| 1096 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1097 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
| 1098 | llvm::Constant *IsAGV, |
| 1099 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 1100 | llvm::Constant *ClassRoGV, |
| 1101 | bool HiddenVisibility); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1102 | |
| 1103 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
| 1104 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 1105 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
| 1106 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1107 | /// EmitMethodList - Emit the method list for the given |
| 1108 | /// implementation. The return value has type MethodListnfABITy. |
| 1109 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 1110 | const char *Section, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 1111 | const ConstantVector &Methods); |
| 1112 | /// EmitIvarList - Emit the ivar list for the given |
| 1113 | /// implementation. If ForClass is true the list of class ivars |
| 1114 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1115 | /// interface ivars will be emitted. The return value has type |
| 1116 | /// IvarListnfABIPtrTy. |
| 1117 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 1118 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1119 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 1120 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 1121 | unsigned long int offset); |
| 1122 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1123 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1124 | /// declaration, emitting it if necessary. The return value has type |
| 1125 | /// ProtocolPtrTy. |
| 1126 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
| 1127 | |
| 1128 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1129 | /// object for the given declaration, emitting it if needed. These |
| 1130 | /// forward references will be filled in with empty bodies if no |
| 1131 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1132 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
| 1133 | |
| 1134 | /// EmitProtocolList - Generate the list of referenced |
| 1135 | /// protocols. The return value has type ProtocolListPtrTy. |
| 1136 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 1137 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1138 | ObjCProtocolDecl::protocol_iterator end); |
| 1139 | |
| 1140 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1141 | QualType ResultType, |
| 1142 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 1143 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1144 | QualType Arg0Ty, |
| 1145 | bool IsSuper, |
| 1146 | const CallArgList &CallArgs); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 1147 | |
| 1148 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1149 | /// class of the given name. |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 1150 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name); |
| 1151 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1152 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1153 | /// for the given class reference. |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1154 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1155 | const ObjCInterfaceDecl *ID); |
| 1156 | |
| 1157 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1158 | /// for the given super class reference. |
| 1159 | llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder, |
| 1160 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1161 | |
| 1162 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 1163 | /// meta-data |
| 1164 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
| 1165 | const ObjCInterfaceDecl *ID); |
| 1166 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1167 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 1168 | /// the given ivar. |
| 1169 | /// |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 1170 | llvm::GlobalVariable * ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1171 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1172 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1173 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1174 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1175 | /// for the given selector. |
| 1176 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1177 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1178 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1179 | /// interface. The return value has type EHTypePtrTy. |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1180 | llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 1181 | bool ForDefinition); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1182 | |
| 1183 | const char *getMetaclassSymbolPrefix() const { |
| 1184 | return "OBJC_METACLASS_$_"; |
| 1185 | } |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1186 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1187 | const char *getClassSymbolPrefix() const { |
| 1188 | return "OBJC_CLASS_$_"; |
| 1189 | } |
| 1190 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 1191 | void GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1192 | uint32_t &InstanceStart, |
| 1193 | uint32_t &InstanceSize); |
| 1194 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1195 | public: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1196 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1197 | // FIXME. All stubs for now! |
| 1198 | virtual llvm::Function *ModuleInitFunction(); |
| 1199 | |
| 1200 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1201 | QualType ResultType, |
| 1202 | Selector Sel, |
| 1203 | llvm::Value *Receiver, |
| 1204 | bool IsClassMessage, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1205 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1206 | |
| 1207 | virtual CodeGen::RValue |
| 1208 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 1209 | QualType ResultType, |
| 1210 | Selector Sel, |
| 1211 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1212 | bool isCategoryImpl, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1213 | llvm::Value *Receiver, |
| 1214 | bool IsClassMessage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1215 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1216 | |
| 1217 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1218 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1219 | |
| 1220 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel) |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1221 | { return EmitSelector(Builder, Sel); } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1222 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 1223 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1224 | |
| 1225 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1226 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 1227 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1228 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1229 | virtual llvm::Constant *GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1230 | return ObjCTypes.getGetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1231 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1232 | virtual llvm::Constant *GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1233 | return ObjCTypes.getSetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1234 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1235 | virtual llvm::Constant *EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1236 | return ObjCTypes.getEnumerationMutationFn(); |
Daniel Dunbar | 28ed084 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 1237 | } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1238 | |
| 1239 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 1240 | const Stmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1241 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 1242 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1243 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1244 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1245 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1246 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1247 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1248 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1249 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1250 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1251 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1252 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1253 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1254 | QualType ObjectTy, |
| 1255 | llvm::Value *BaseValue, |
| 1256 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1257 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1258 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1259 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1260 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1261 | }; |
| 1262 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1263 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1264 | |
| 1265 | /* *** Helper Functions *** */ |
| 1266 | |
| 1267 | /// getConstantGEP() - Help routine to construct simple GEPs. |
| 1268 | static llvm::Constant *getConstantGEP(llvm::Constant *C, |
| 1269 | unsigned idx0, |
| 1270 | unsigned idx1) { |
| 1271 | llvm::Value *Idxs[] = { |
| 1272 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0), |
| 1273 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1) |
| 1274 | }; |
| 1275 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2); |
| 1276 | } |
| 1277 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1278 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 1279 | /// class has the __objc_exception__ attribute. |
| 1280 | static bool hasObjCExceptionAttribute(const ObjCInterfaceDecl *OID) { |
Daniel Dunbar | b11fa0d | 2009-04-13 21:08:27 +0000 | [diff] [blame] | 1281 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1282 | return true; |
| 1283 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
| 1284 | return hasObjCExceptionAttribute(Super); |
| 1285 | return false; |
| 1286 | } |
| 1287 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1288 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1289 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1290 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
| 1291 | ObjCTypes(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1292 | { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1293 | ObjCABI = 1; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1294 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1297 | /// GetClass - Return a reference to the class for the given interface |
| 1298 | /// decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1299 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1300 | const ObjCInterfaceDecl *ID) { |
| 1301 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1305 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 1306 | return EmitSelector(Builder, Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1309 | /// Generate a constant CFString object. |
| 1310 | /* |
| 1311 | struct __builtin_CFString { |
| 1312 | const int *isa; // point to __CFConstantStringClassReference |
| 1313 | int flags; |
| 1314 | const char *str; |
| 1315 | long length; |
| 1316 | }; |
| 1317 | */ |
| 1318 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1319 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 1320 | const ObjCStringLiteral *SL) { |
Steve Naroff | 8d4141f | 2009-04-01 13:55:36 +0000 | [diff] [blame] | 1321 | return CGM.GetAddrOfConstantCFString(SL->getString()); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | /// Generates a message send where the super is the receiver. This is |
| 1325 | /// a message send to self with special delivery semantics indicating |
| 1326 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1327 | CodeGen::RValue |
| 1328 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1329 | QualType ResultType, |
| 1330 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1331 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1332 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1333 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1334 | bool IsClassMessage, |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1335 | const CodeGen::CallArgList &CallArgs) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1336 | // Create and init a super structure; this is a (receiver, class) |
| 1337 | // pair we will pass to objc_msgSendSuper. |
| 1338 | llvm::Value *ObjCSuper = |
| 1339 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 1340 | llvm::Value *ReceiverAsObject = |
| 1341 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 1342 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 1343 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1344 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1345 | // If this is a class message the metaclass is passed as the target. |
| 1346 | llvm::Value *Target; |
| 1347 | if (IsClassMessage) { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1348 | if (isCategoryImpl) { |
| 1349 | // Message sent to 'super' in a class method defined in a category |
| 1350 | // implementation requires an odd treatment. |
| 1351 | // If we are in a class method, we must retrieve the |
| 1352 | // _metaclass_ for the current class, pointed at by |
| 1353 | // the class's "isa" pointer. The following assumes that |
| 1354 | // isa" is the first ivar in a class (which it must be). |
| 1355 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1356 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 1357 | Target = CGF.Builder.CreateLoad(Target); |
| 1358 | } |
| 1359 | else { |
| 1360 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 1361 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 1362 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 1363 | Target = Super; |
| 1364 | } |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1365 | } else { |
| 1366 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1367 | } |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1368 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 1369 | // and ObjCTypes types. |
| 1370 | const llvm::Type *ClassTy = |
| 1371 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1372 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1373 | CGF.Builder.CreateStore(Target, |
| 1374 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 1375 | |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1376 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1377 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 1378 | true, CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1379 | } |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1380 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1381 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1382 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1383 | QualType ResultType, |
| 1384 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1385 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1386 | bool IsClassMessage, |
| 1387 | const CallArgList &CallArgs) { |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1388 | return EmitMessageSend(CGF, ResultType, Sel, |
Fariborz Jahanian | d019d96 | 2009-04-24 21:07:43 +0000 | [diff] [blame] | 1389 | Receiver, CGF.getContext().getObjCIdType(), |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1390 | false, CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1394 | QualType ResultType, |
| 1395 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1396 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1397 | QualType Arg0Ty, |
| 1398 | bool IsSuper, |
| 1399 | const CallArgList &CallArgs) { |
| 1400 | CallArgList ActualArgs; |
Fariborz Jahanian | d019d96 | 2009-04-24 21:07:43 +0000 | [diff] [blame] | 1401 | if (!IsSuper) |
| 1402 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1403 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 1404 | ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder, |
| 1405 | Sel)), |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1406 | CGF.getContext().getObjCSelType())); |
| 1407 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1408 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1409 | CodeGenTypes &Types = CGM.getTypes(); |
| 1410 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); |
Fariborz Jahanian | 65257ca | 2009-04-29 22:47:27 +0000 | [diff] [blame] | 1411 | // FIXME. vararg flag must be true when this API is used for 64bit code gen. |
| 1412 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1413 | |
| 1414 | llvm::Constant *Fn; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1415 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1416 | Fn = ObjCTypes.getSendStretFn(IsSuper); |
| 1417 | } else if (ResultType->isFloatingType()) { |
| 1418 | // FIXME: Sadly, this is wrong. This actually depends on the |
| 1419 | // architecture. This happens to be right for x86-32 though. |
| 1420 | Fn = ObjCTypes.getSendFpretFn(IsSuper); |
| 1421 | } else { |
| 1422 | Fn = ObjCTypes.getSendFn(IsSuper); |
| 1423 | } |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 1424 | Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy)); |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1425 | return CGF.EmitCall(FnInfo, Fn, ActualArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1428 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1429 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1430 | // FIXME: I don't understand why gcc generates this, or where it is |
| 1431 | // resolved. Investigate. Its also wasteful to look this up over and |
| 1432 | // over. |
| 1433 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1434 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1435 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
| 1436 | ObjCTypes.ExternalProtocolPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1439 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1440 | // FIXME: We shouldn't need this, the protocol decl should contain |
| 1441 | // enough information to tell us whether this was a declaration or a |
| 1442 | // definition. |
| 1443 | DefinedProtocols.insert(PD->getIdentifier()); |
| 1444 | |
| 1445 | // If we have generated a forward reference to this protocol, emit |
| 1446 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 1447 | // emitted. |
| 1448 | if (Protocols.count(PD->getIdentifier())) |
| 1449 | GetOrEmitProtocol(PD); |
| 1450 | } |
| 1451 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1452 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1453 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 1454 | return GetOrEmitProtocol(PD); |
| 1455 | return GetOrEmitProtocolRef(PD); |
| 1456 | } |
| 1457 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1458 | /* |
| 1459 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 1460 | struct _objc_protocol { |
| 1461 | struct _objc_protocol_extension *isa; |
| 1462 | char *protocol_name; |
| 1463 | struct _objc_protocol_list *protocol_list; |
| 1464 | struct _objc__method_prototype_list *instance_methods; |
| 1465 | struct _objc__method_prototype_list *class_methods |
| 1466 | }; |
| 1467 | |
| 1468 | See EmitProtocolExtension(). |
| 1469 | */ |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1470 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
| 1471 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1472 | |
| 1473 | // Early exit if a defining object has already been generated. |
| 1474 | if (Entry && Entry->hasInitializer()) |
| 1475 | return Entry; |
| 1476 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1477 | // FIXME: I don't understand why gcc generates this, or where it is |
| 1478 | // resolved. Investigate. Its also wasteful to look this up over and |
| 1479 | // over. |
| 1480 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1481 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1482 | const char *ProtocolName = PD->getNameAsCString(); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1483 | |
| 1484 | // Construct method lists. |
| 1485 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1486 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1487 | for (ObjCProtocolDecl::instmeth_iterator |
| 1488 | i = PD->instmeth_begin(CGM.getContext()), |
| 1489 | e = PD->instmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1490 | ObjCMethodDecl *MD = *i; |
| 1491 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1492 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1493 | OptInstanceMethods.push_back(C); |
| 1494 | } else { |
| 1495 | InstanceMethods.push_back(C); |
| 1496 | } |
| 1497 | } |
| 1498 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1499 | for (ObjCProtocolDecl::classmeth_iterator |
| 1500 | i = PD->classmeth_begin(CGM.getContext()), |
| 1501 | e = PD->classmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1502 | ObjCMethodDecl *MD = *i; |
| 1503 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1504 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1505 | OptClassMethods.push_back(C); |
| 1506 | } else { |
| 1507 | ClassMethods.push_back(C); |
| 1508 | } |
| 1509 | } |
| 1510 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1511 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1512 | Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1513 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1514 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1515 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1516 | PD->protocol_begin(), |
| 1517 | PD->protocol_end()); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1518 | Values[3] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1519 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" |
| 1520 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1521 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1522 | InstanceMethods); |
| 1523 | Values[4] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1524 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" |
| 1525 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1526 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1527 | ClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1528 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 1529 | Values); |
| 1530 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1531 | if (Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1532 | // Already created, fix the linkage and update the initializer. |
| 1533 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1534 | Entry->setInitializer(Init); |
| 1535 | } else { |
| 1536 | Entry = |
| 1537 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
| 1538 | llvm::GlobalValue::InternalLinkage, |
| 1539 | Init, |
| 1540 | std::string("\01L_OBJC_PROTOCOL_")+ProtocolName, |
| 1541 | &CGM.getModule()); |
| 1542 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1543 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1544 | UsedGlobals.push_back(Entry); |
| 1545 | // FIXME: Is this necessary? Why only for protocol? |
| 1546 | Entry->setAlignment(4); |
| 1547 | } |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1548 | |
| 1549 | return Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1552 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1553 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1554 | |
| 1555 | if (!Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1556 | // We use the initializer as a marker of whether this is a forward |
| 1557 | // reference or not. At module finalization we add the empty |
| 1558 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1559 | Entry = |
| 1560 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1561 | llvm::GlobalValue::ExternalLinkage, |
| 1562 | 0, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1563 | "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1564 | &CGM.getModule()); |
| 1565 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1566 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1567 | UsedGlobals.push_back(Entry); |
| 1568 | // FIXME: Is this necessary? Why only for protocol? |
| 1569 | Entry->setAlignment(4); |
| 1570 | } |
| 1571 | |
| 1572 | return Entry; |
| 1573 | } |
| 1574 | |
| 1575 | /* |
| 1576 | struct _objc_protocol_extension { |
| 1577 | uint32_t size; |
| 1578 | struct objc_method_description_list *optional_instance_methods; |
| 1579 | struct objc_method_description_list *optional_class_methods; |
| 1580 | struct objc_property_list *instance_properties; |
| 1581 | }; |
| 1582 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1583 | llvm::Constant * |
| 1584 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1585 | const ConstantVector &OptInstanceMethods, |
| 1586 | const ConstantVector &OptClassMethods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1587 | uint64_t Size = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1588 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1589 | std::vector<llvm::Constant*> Values(4); |
| 1590 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1591 | Values[1] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1592 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
| 1593 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1594 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1595 | OptInstanceMethods); |
| 1596 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1597 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" |
| 1598 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1599 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1600 | OptClassMethods); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1601 | Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + |
| 1602 | PD->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1603 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1604 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1605 | // Return null if no extension bits are used. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1606 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
| 1607 | Values[3]->isNullValue()) |
| 1608 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 1609 | |
| 1610 | llvm::Constant *Init = |
| 1611 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1612 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1613 | // No special section, but goes in llvm.used |
| 1614 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(), |
| 1615 | Init, |
| 1616 | 0, 0, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
| 1619 | /* |
| 1620 | struct objc_protocol_list { |
| 1621 | struct objc_protocol_list *next; |
| 1622 | long count; |
| 1623 | Protocol *list[]; |
| 1624 | }; |
| 1625 | */ |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1626 | llvm::Constant * |
| 1627 | CGObjCMac::EmitProtocolList(const std::string &Name, |
| 1628 | ObjCProtocolDecl::protocol_iterator begin, |
| 1629 | ObjCProtocolDecl::protocol_iterator end) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1630 | std::vector<llvm::Constant*> ProtocolRefs; |
| 1631 | |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1632 | for (; begin != end; ++begin) |
| 1633 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1634 | |
| 1635 | // Just return null for empty protocol lists |
| 1636 | if (ProtocolRefs.empty()) |
| 1637 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1638 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1639 | // This list is null terminated. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1640 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
| 1641 | |
| 1642 | std::vector<llvm::Constant*> Values(3); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1643 | // This field is only used by the runtime. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1644 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1645 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 1646 | Values[2] = |
| 1647 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 1648 | ProtocolRefs.size()), |
| 1649 | ProtocolRefs); |
| 1650 | |
| 1651 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1652 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1653 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1654 | 4, false); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1655 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
| 1656 | } |
| 1657 | |
| 1658 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1659 | struct _objc_property { |
| 1660 | const char * const name; |
| 1661 | const char * const attributes; |
| 1662 | }; |
| 1663 | |
| 1664 | struct _objc_property_list { |
| 1665 | uint32_t entsize; // sizeof (struct _objc_property) |
| 1666 | uint32_t prop_count; |
| 1667 | struct _objc_property[prop_count]; |
| 1668 | }; |
| 1669 | */ |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1670 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name, |
| 1671 | const Decl *Container, |
| 1672 | const ObjCContainerDecl *OCD, |
| 1673 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1674 | std::vector<llvm::Constant*> Properties, Prop(2); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1675 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(CGM.getContext()), |
| 1676 | E = OCD->prop_end(CGM.getContext()); I != E; ++I) { |
Steve Naroff | 93983f8 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 1677 | const ObjCPropertyDecl *PD = *I; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1678 | Prop[0] = GetPropertyName(PD->getIdentifier()); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1679 | Prop[1] = GetPropertyTypeString(PD, Container); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1680 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
| 1681 | Prop)); |
| 1682 | } |
| 1683 | |
| 1684 | // Return null for empty list. |
| 1685 | if (Properties.empty()) |
| 1686 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1687 | |
| 1688 | unsigned PropertySize = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1689 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1690 | std::vector<llvm::Constant*> Values(3); |
| 1691 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 1692 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
| 1693 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
| 1694 | Properties.size()); |
| 1695 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
| 1696 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1697 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1698 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1699 | CreateMetadataVar(Name, Init, |
| 1700 | (ObjCABI == 2) ? "__DATA, __objc_const" : |
| 1701 | "__OBJC,__property,regular,no_dead_strip", |
| 1702 | (ObjCABI == 2) ? 8 : 4, |
| 1703 | true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1704 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1708 | struct objc_method_description_list { |
| 1709 | int count; |
| 1710 | struct objc_method_description list[]; |
| 1711 | }; |
| 1712 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1713 | llvm::Constant * |
| 1714 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 1715 | std::vector<llvm::Constant*> Desc(2); |
| 1716 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1717 | ObjCTypes.SelectorPtrTy); |
| 1718 | Desc[1] = GetMethodVarType(MD); |
| 1719 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
| 1720 | Desc); |
| 1721 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1722 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1723 | llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name, |
| 1724 | const char *Section, |
| 1725 | const ConstantVector &Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1726 | // Return null for empty list. |
| 1727 | if (Methods.empty()) |
| 1728 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 1729 | |
| 1730 | std::vector<llvm::Constant*> Values(2); |
| 1731 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 1732 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
| 1733 | Methods.size()); |
| 1734 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
| 1735 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1736 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1737 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1738 | return llvm::ConstantExpr::getBitCast(GV, |
| 1739 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1742 | /* |
| 1743 | struct _objc_category { |
| 1744 | char *category_name; |
| 1745 | char *class_name; |
| 1746 | struct _objc_method_list *instance_methods; |
| 1747 | struct _objc_method_list *class_methods; |
| 1748 | struct _objc_protocol_list *protocols; |
| 1749 | uint32_t size; // <rdar://4585769> |
| 1750 | struct _objc_property_list *instance_properties; |
| 1751 | }; |
| 1752 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1753 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1754 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1755 | |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1756 | // FIXME: This is poor design, the OCD should have a pointer to the |
| 1757 | // category decl. Additionally, note that Category can be null for |
| 1758 | // the @implementation w/o an @interface case. Sema should just |
| 1759 | // create one for us as it does for @implementation so everyone else |
| 1760 | // can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1761 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1762 | const ObjCCategoryDecl *Category = |
| 1763 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1764 | std::string ExtName(Interface->getNameAsString() + "_" + |
| 1765 | OCD->getNameAsString()); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1766 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1767 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1768 | for (ObjCCategoryImplDecl::instmeth_iterator |
| 1769 | i = OCD->instmeth_begin(CGM.getContext()), |
| 1770 | e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1771 | // Instance methods should always be defined. |
| 1772 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1773 | } |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1774 | for (ObjCCategoryImplDecl::classmeth_iterator |
| 1775 | i = OCD->classmeth_begin(CGM.getContext()), |
| 1776 | e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1777 | // Class methods should always be defined. |
| 1778 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1779 | } |
| 1780 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1781 | std::vector<llvm::Constant*> Values(7); |
| 1782 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 1783 | Values[1] = GetClassName(Interface->getIdentifier()); |
Fariborz Jahanian | 679cd7f | 2009-04-29 20:40:05 +0000 | [diff] [blame] | 1784 | LazySymbols.insert(Interface->getIdentifier()); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1785 | Values[2] = |
| 1786 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") + |
| 1787 | ExtName, |
| 1788 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1789 | InstanceMethods); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1790 | Values[3] = |
| 1791 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName, |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1792 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1793 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1794 | if (Category) { |
| 1795 | Values[4] = |
| 1796 | EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName, |
| 1797 | Category->protocol_begin(), |
| 1798 | Category->protocol_end()); |
| 1799 | } else { |
| 1800 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1801 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1802 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1803 | |
| 1804 | // If there is no category @interface then there can be no properties. |
| 1805 | if (Category) { |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1806 | Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1807 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1808 | } else { |
| 1809 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1810 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1811 | |
| 1812 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
| 1813 | Values); |
| 1814 | |
| 1815 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1816 | CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init, |
| 1817 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1818 | 4, true); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1819 | DefinedCategories.push_back(GV); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1822 | // FIXME: Get from somewhere? |
| 1823 | enum ClassFlags { |
| 1824 | eClassFlags_Factory = 0x00001, |
| 1825 | eClassFlags_Meta = 0x00002, |
| 1826 | // <rdr://5142207> |
| 1827 | eClassFlags_HasCXXStructors = 0x02000, |
| 1828 | eClassFlags_Hidden = 0x20000, |
| 1829 | eClassFlags_ABI2_Hidden = 0x00010, |
| 1830 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 1831 | }; |
| 1832 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1833 | /* |
| 1834 | struct _objc_class { |
| 1835 | Class isa; |
| 1836 | Class super_class; |
| 1837 | const char *name; |
| 1838 | long version; |
| 1839 | long info; |
| 1840 | long instance_size; |
| 1841 | struct _objc_ivar_list *ivars; |
| 1842 | struct _objc_method_list *methods; |
| 1843 | struct _objc_cache *cache; |
| 1844 | struct _objc_protocol_list *protocols; |
| 1845 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 1846 | const char *ivar_layout; |
| 1847 | struct _objc_class_ext *ext; |
| 1848 | }; |
| 1849 | |
| 1850 | See EmitClassExtension(); |
| 1851 | */ |
| 1852 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1853 | DefinedSymbols.insert(ID->getIdentifier()); |
| 1854 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1855 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1856 | // FIXME: Gross |
| 1857 | ObjCInterfaceDecl *Interface = |
| 1858 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1859 | llvm::Constant *Protocols = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1860 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1861 | Interface->protocol_begin(), |
| 1862 | Interface->protocol_end()); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1863 | unsigned Flags = eClassFlags_Factory; |
Daniel Dunbar | 2bebbf0 | 2009-05-03 10:46:44 +0000 | [diff] [blame] | 1864 | unsigned Size = |
| 1865 | CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1866 | |
| 1867 | // FIXME: Set CXX-structors flag. |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1868 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1869 | Flags |= eClassFlags_Hidden; |
| 1870 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1871 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1872 | for (ObjCImplementationDecl::instmeth_iterator |
| 1873 | i = ID->instmeth_begin(CGM.getContext()), |
| 1874 | e = ID->instmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1875 | // Instance methods should always be defined. |
| 1876 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1877 | } |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1878 | for (ObjCImplementationDecl::classmeth_iterator |
| 1879 | i = ID->classmeth_begin(CGM.getContext()), |
| 1880 | e = ID->classmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1881 | // Class methods should always be defined. |
| 1882 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1883 | } |
| 1884 | |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1885 | for (ObjCImplementationDecl::propimpl_iterator |
| 1886 | i = ID->propimpl_begin(CGM.getContext()), |
| 1887 | e = ID->propimpl_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1888 | ObjCPropertyImplDecl *PID = *i; |
| 1889 | |
| 1890 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 1891 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 1892 | |
| 1893 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 1894 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1895 | InstanceMethods.push_back(C); |
| 1896 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 1897 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1898 | InstanceMethods.push_back(C); |
| 1899 | } |
| 1900 | } |
| 1901 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1902 | std::vector<llvm::Constant*> Values(12); |
Daniel Dunbar | 5384b09 | 2009-05-03 08:56:52 +0000 | [diff] [blame] | 1903 | Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1904 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1905 | // Record a reference to the super class. |
| 1906 | LazySymbols.insert(Super->getIdentifier()); |
| 1907 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1908 | Values[ 1] = |
| 1909 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1910 | ObjCTypes.ClassPtrTy); |
| 1911 | } else { |
| 1912 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1913 | } |
| 1914 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1915 | // Version is always 0. |
| 1916 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1917 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1918 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1919 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1920 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1921 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1922 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1923 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1924 | // cache is always NULL. |
| 1925 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1926 | Values[ 9] = Protocols; |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 1927 | Values[10] = BuildIvarLayout(ID, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1928 | Values[11] = EmitClassExtension(ID); |
| 1929 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1930 | Values); |
| 1931 | |
| 1932 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1933 | CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init, |
| 1934 | "__OBJC,__class,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1935 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1936 | DefinedClasses.push_back(GV); |
| 1937 | } |
| 1938 | |
| 1939 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1940 | llvm::Constant *Protocols, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1941 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1942 | unsigned Flags = eClassFlags_Meta; |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1943 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1944 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1945 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1946 | Flags |= eClassFlags_Hidden; |
| 1947 | |
| 1948 | std::vector<llvm::Constant*> Values(12); |
| 1949 | // The isa for the metaclass is the root of the hierarchy. |
| 1950 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 1951 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 1952 | Root = Super; |
| 1953 | Values[ 0] = |
| 1954 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
| 1955 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1956 | // The super class for the metaclass is emitted as the name of the |
| 1957 | // super class. The runtime fixes this up to point to the |
| 1958 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1959 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
| 1960 | Values[ 1] = |
| 1961 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1962 | ObjCTypes.ClassPtrTy); |
| 1963 | } else { |
| 1964 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1965 | } |
| 1966 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1967 | // Version is always 0. |
| 1968 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1969 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1970 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1971 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1972 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1973 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1974 | "__OBJC,__cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1975 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1976 | // cache is always NULL. |
| 1977 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1978 | Values[ 9] = Protocols; |
| 1979 | // ivar_layout for metaclass is always NULL. |
| 1980 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 1981 | // The class extension is always unused for metaclasses. |
| 1982 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 1983 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1984 | Values); |
| 1985 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1986 | std::string Name("\01L_OBJC_METACLASS_"); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1987 | Name += ID->getNameAsCString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1988 | |
| 1989 | // Check for a forward reference. |
| 1990 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 1991 | if (GV) { |
| 1992 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 1993 | "Forward metaclass reference has incorrect type."); |
| 1994 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 1995 | GV->setInitializer(Init); |
| 1996 | } else { |
| 1997 | GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1998 | llvm::GlobalValue::InternalLinkage, |
| 1999 | Init, Name, |
| 2000 | &CGM.getModule()); |
| 2001 | } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2002 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2003 | GV->setAlignment(4); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2004 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2005 | |
| 2006 | return GV; |
| 2007 | } |
| 2008 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2009 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2010 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2011 | |
| 2012 | // FIXME: Should we look these up somewhere other than the |
| 2013 | // module. Its a bit silly since we only generate these while |
| 2014 | // processing an implementation, so exactly one pointer would work |
| 2015 | // if know when we entered/exitted an implementation block. |
| 2016 | |
| 2017 | // Check for an existing forward reference. |
Fariborz Jahanian | b0d2794 | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 2018 | // Previously, metaclass with internal linkage may have been defined. |
| 2019 | // pass 'true' as 2nd argument so it is returned. |
| 2020 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) { |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2021 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2022 | "Forward metaclass reference has incorrect type."); |
| 2023 | return GV; |
| 2024 | } else { |
| 2025 | // Generate as an external reference to keep a consistent |
| 2026 | // module. This will be patched up when we emit the metaclass. |
| 2027 | return new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 2028 | llvm::GlobalValue::ExternalLinkage, |
| 2029 | 0, |
| 2030 | Name, |
| 2031 | &CGM.getModule()); |
| 2032 | } |
| 2033 | } |
| 2034 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2035 | /* |
| 2036 | struct objc_class_ext { |
| 2037 | uint32_t size; |
| 2038 | const char *weak_ivar_layout; |
| 2039 | struct _objc_property_list *properties; |
| 2040 | }; |
| 2041 | */ |
| 2042 | llvm::Constant * |
| 2043 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
| 2044 | uint64_t Size = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 2045 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2046 | |
| 2047 | std::vector<llvm::Constant*> Values(3); |
| 2048 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 2049 | Values[1] = BuildIvarLayout(ID, false); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2050 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2051 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2052 | |
| 2053 | // Return null if no extension bits are used. |
| 2054 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
| 2055 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 2056 | |
| 2057 | llvm::Constant *Init = |
| 2058 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2059 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2060 | Init, "__OBJC,__class_ext,regular,no_dead_strip", |
| 2061 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 2064 | /// getInterfaceDeclForIvar - Get the interface declaration node where |
| 2065 | /// this ivar is declared in. |
| 2066 | /// FIXME. Ideally, this info should be in the ivar node. But currently |
| 2067 | /// it is not and prevailing wisdom is that ASTs should not have more |
| 2068 | /// info than is absolutely needed, even though this info reflects the |
| 2069 | /// source language. |
| 2070 | /// |
| 2071 | static const ObjCInterfaceDecl *getInterfaceDeclForIvar( |
| 2072 | const ObjCInterfaceDecl *OI, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2073 | const ObjCIvarDecl *IVD, |
| 2074 | ASTContext &Context) { |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 2075 | if (!OI) |
| 2076 | return 0; |
| 2077 | assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface"); |
| 2078 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 2079 | E = OI->ivar_end(); I != E; ++I) |
| 2080 | if ((*I)->getIdentifier() == IVD->getIdentifier()) |
| 2081 | return OI; |
Fariborz Jahanian | 5a4b453 | 2009-03-31 17:00:52 +0000 | [diff] [blame] | 2082 | // look into properties. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2083 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context), |
| 2084 | E = OI->prop_end(Context); I != E; ++I) { |
Fariborz Jahanian | 5a4b453 | 2009-03-31 17:00:52 +0000 | [diff] [blame] | 2085 | ObjCPropertyDecl *PDecl = (*I); |
| 2086 | if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl()) |
| 2087 | if (IV->getIdentifier() == IVD->getIdentifier()) |
| 2088 | return OI; |
| 2089 | } |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2090 | return getInterfaceDeclForIvar(OI->getSuperClass(), IVD, Context); |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2093 | /* |
| 2094 | struct objc_ivar { |
| 2095 | char *ivar_name; |
| 2096 | char *ivar_type; |
| 2097 | int ivar_offset; |
| 2098 | }; |
| 2099 | |
| 2100 | struct objc_ivar_list { |
| 2101 | int ivar_count; |
| 2102 | struct objc_ivar list[count]; |
| 2103 | }; |
| 2104 | */ |
| 2105 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2106 | bool ForClass) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2107 | std::vector<llvm::Constant*> Ivars, Ivar(3); |
| 2108 | |
| 2109 | // When emitting the root class GCC emits ivar entries for the |
| 2110 | // actual class structure. It is not clear if we need to follow this |
| 2111 | // behavior; for now lets try and get away with not doing it. If so, |
| 2112 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 2113 | // for the class. |
| 2114 | if (ForClass) |
| 2115 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2116 | |
| 2117 | ObjCInterfaceDecl *OID = |
| 2118 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2119 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 2120 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
| 2121 | GetNamedIvarList(OID, OIvars); |
| 2122 | |
| 2123 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 2124 | ObjCIvarDecl *IVD = OIvars[i]; |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 2125 | Ivar[0] = GetMethodVarName(IVD->getIdentifier()); |
| 2126 | Ivar[1] = GetMethodVarType(IVD); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 2127 | Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2128 | ComputeIvarBaseOffset(CGM, OID, IVD)); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2129 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2130 | } |
| 2131 | |
| 2132 | // Return null for empty list. |
| 2133 | if (Ivars.empty()) |
| 2134 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
| 2135 | |
| 2136 | std::vector<llvm::Constant*> Values(2); |
| 2137 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 2138 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
| 2139 | Ivars.size()); |
| 2140 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
| 2141 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2142 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2143 | llvm::GlobalVariable *GV; |
| 2144 | if (ForClass) |
| 2145 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(), |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2146 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
| 2147 | 4, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2148 | else |
| 2149 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" |
| 2150 | + ID->getNameAsString(), |
| 2151 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2152 | 4, true); |
| 2153 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
| 2156 | /* |
| 2157 | struct objc_method { |
| 2158 | SEL method_name; |
| 2159 | char *method_types; |
| 2160 | void *method; |
| 2161 | }; |
| 2162 | |
| 2163 | struct objc_method_list { |
| 2164 | struct objc_method_list *obsolete; |
| 2165 | int count; |
| 2166 | struct objc_method methods_list[count]; |
| 2167 | }; |
| 2168 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2169 | |
| 2170 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 2171 | /// given method if it has been defined. The result is null if the |
| 2172 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2173 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2174 | // FIXME: Use DenseMap::lookup |
| 2175 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 2176 | if (!Fn) |
| 2177 | return 0; |
| 2178 | |
| 2179 | std::vector<llvm::Constant*> Method(3); |
| 2180 | Method[0] = |
| 2181 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 2182 | ObjCTypes.SelectorPtrTy); |
| 2183 | Method[1] = GetMethodVarType(MD); |
| 2184 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 2185 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 2186 | } |
| 2187 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2188 | llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name, |
| 2189 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2190 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2191 | // Return null for empty list. |
| 2192 | if (Methods.empty()) |
| 2193 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
| 2194 | |
| 2195 | std::vector<llvm::Constant*> Values(3); |
| 2196 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 2197 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 2198 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 2199 | Methods.size()); |
| 2200 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 2201 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2202 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2203 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2204 | return llvm::ConstantExpr::getBitCast(GV, |
| 2205 | ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2206 | } |
| 2207 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 2208 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 2209 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2210 | std::string Name; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 2211 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2212 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2213 | CodeGenTypes &Types = CGM.getTypes(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2214 | const llvm::FunctionType *MethodTy = |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2215 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2216 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2217 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2218 | llvm::GlobalValue::InternalLinkage, |
| 2219 | Name, |
| 2220 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2221 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2222 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2223 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2224 | } |
| 2225 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2226 | llvm::GlobalVariable * |
| 2227 | CGObjCCommonMac::CreateMetadataVar(const std::string &Name, |
| 2228 | llvm::Constant *Init, |
| 2229 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2230 | unsigned Align, |
| 2231 | bool AddToUsed) { |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2232 | const llvm::Type *Ty = Init->getType(); |
| 2233 | llvm::GlobalVariable *GV = |
| 2234 | new llvm::GlobalVariable(Ty, false, |
| 2235 | llvm::GlobalValue::InternalLinkage, |
| 2236 | Init, |
| 2237 | Name, |
| 2238 | &CGM.getModule()); |
| 2239 | if (Section) |
| 2240 | GV->setSection(Section); |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2241 | if (Align) |
| 2242 | GV->setAlignment(Align); |
| 2243 | if (AddToUsed) |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2244 | UsedGlobals.push_back(GV); |
| 2245 | return GV; |
| 2246 | } |
| 2247 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2248 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2249 | // Abuse this interface function as a place to finalize. |
| 2250 | FinishModule(); |
| 2251 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2252 | return NULL; |
| 2253 | } |
| 2254 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2255 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2256 | return ObjCTypes.getGetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2257 | } |
| 2258 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2259 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2260 | return ObjCTypes.getSetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2261 | } |
| 2262 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2263 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2264 | return ObjCTypes.getEnumerationMutationFn(); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2267 | /* |
| 2268 | |
| 2269 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 2270 | -- |
| 2271 | |
| 2272 | The basic framework for a @try-catch-finally is as follows: |
| 2273 | { |
| 2274 | objc_exception_data d; |
| 2275 | id _rethrow = null; |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2276 | bool _call_try_exit = true; |
| 2277 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2278 | objc_exception_try_enter(&d); |
| 2279 | if (!setjmp(d.jmp_buf)) { |
| 2280 | ... try body ... |
| 2281 | } else { |
| 2282 | // exception path |
| 2283 | id _caught = objc_exception_extract(&d); |
| 2284 | |
| 2285 | // enter new try scope for handlers |
| 2286 | if (!setjmp(d.jmp_buf)) { |
| 2287 | ... match exception and execute catch blocks ... |
| 2288 | |
| 2289 | // fell off end, rethrow. |
| 2290 | _rethrow = _caught; |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2291 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2292 | } else { |
| 2293 | // exception in catch block |
| 2294 | _rethrow = objc_exception_extract(&d); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2295 | _call_try_exit = false; |
| 2296 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2297 | } |
| 2298 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2299 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2300 | |
| 2301 | finally: |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2302 | if (_call_try_exit) |
| 2303 | objc_exception_try_exit(&d); |
| 2304 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2305 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2306 | ... dispatch to finally destination ... |
| 2307 | |
| 2308 | finally_rethrow: |
| 2309 | objc_exception_throw(_rethrow); |
| 2310 | |
| 2311 | finally_end: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2312 | } |
| 2313 | |
| 2314 | This framework differs slightly from the one gcc uses, in that gcc |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2315 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 2316 | and if the object should be rethrown. This breaks in the face of |
| 2317 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2318 | |
| 2319 | We specialize this framework for a few particular circumstances: |
| 2320 | |
| 2321 | - If there are no catch blocks, then we avoid emitting the second |
| 2322 | exception handling context. |
| 2323 | |
| 2324 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 2325 | e)) we avoid emitting the code to rethrow an uncaught exception. |
| 2326 | |
| 2327 | - FIXME: If there is no @finally block we can do a few more |
| 2328 | simplifications. |
| 2329 | |
| 2330 | Rethrows and Jumps-Through-Finally |
| 2331 | -- |
| 2332 | |
| 2333 | Support for implicit rethrows and jumping through the finally block is |
| 2334 | handled by storing the current exception-handling context in |
| 2335 | ObjCEHStack. |
| 2336 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2337 | In order to implement proper @finally semantics, we support one basic |
| 2338 | mechanism for jumping through the finally block to an arbitrary |
| 2339 | destination. Constructs which generate exits from a @try or @catch |
| 2340 | block use this mechanism to implement the proper semantics by chaining |
| 2341 | jumps, as necessary. |
| 2342 | |
| 2343 | This mechanism works like the one used for indirect goto: we |
| 2344 | arbitrarily assign an ID to each destination and store the ID for the |
| 2345 | destination in a variable prior to entering the finally block. At the |
| 2346 | end of the finally block we simply create a switch to the proper |
| 2347 | destination. |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2348 | |
| 2349 | Code gen for @synchronized(expr) stmt; |
| 2350 | Effectively generating code for: |
| 2351 | objc_sync_enter(expr); |
| 2352 | @try stmt @finally { objc_sync_exit(expr); } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2353 | */ |
| 2354 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2355 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 2356 | const Stmt &S) { |
| 2357 | bool isTry = isa<ObjCAtTryStmt>(S); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2358 | // Create various blocks we refer to for handling @finally. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2359 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2360 | llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit"); |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2361 | llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit"); |
| 2362 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
| 2363 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2364 | |
| 2365 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 2366 | // evaluation of the expression must occur before we enter the |
| 2367 | // @synchronized. We can safely avoid a temp here because jumps into |
| 2368 | // @synchronized are illegal & this will dominate uses. |
| 2369 | llvm::Value *SyncArg = 0; |
| 2370 | if (!isTry) { |
| 2371 | SyncArg = |
| 2372 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 2373 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 2374 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2375 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2376 | |
| 2377 | // Push an EH context entry, used for handling rethrows and jumps |
| 2378 | // through finally. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2379 | CGF.PushCleanupBlock(FinallyBlock); |
| 2380 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2381 | CGF.ObjCEHValueStack.push_back(0); |
| 2382 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2383 | // Allocate memory for the exception data and rethrow pointer. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2384 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 2385 | "exceptiondata.ptr"); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2386 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy, |
| 2387 | "_rethrow"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2388 | llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty, |
| 2389 | "_call_try_exit"); |
| 2390 | CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr); |
| 2391 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2392 | // Enter a new try block and call setjmp. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2393 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2394 | llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0, |
| 2395 | "jmpbufarray"); |
| 2396 | JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2397 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2398 | JmpBufPtr, "result"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2399 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2400 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 2401 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2402 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2403 | TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2404 | |
| 2405 | // Emit the @try block. |
| 2406 | CGF.EmitBlock(TryBlock); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2407 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 2408 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2409 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2410 | |
| 2411 | // Emit the "exception in @try" block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2412 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2413 | |
| 2414 | // Retrieve the exception object. We may emit multiple blocks but |
| 2415 | // nothing can cross this so the value is already in SSA form. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2416 | llvm::Value *Caught = |
| 2417 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2418 | ExceptionData, "caught"); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2419 | CGF.ObjCEHValueStack.back() = Caught; |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2420 | if (!isTry) |
| 2421 | { |
| 2422 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2423 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2424 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2425 | } |
| 2426 | else if (const ObjCAtCatchStmt* CatchStmt = |
| 2427 | cast<ObjCAtTryStmt>(S).getCatchStmts()) |
| 2428 | { |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2429 | // Enter a new exception try block (in case a @catch block throws |
| 2430 | // an exception). |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2431 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2432 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2433 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2434 | JmpBufPtr, "result"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2435 | llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2436 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2437 | llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch"); |
| 2438 | llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler"); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2439 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2440 | |
| 2441 | CGF.EmitBlock(CatchBlock); |
| 2442 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2443 | // Handle catch list. As a special case we check if everything is |
| 2444 | // matched and avoid generating code for falling off the end if |
| 2445 | // so. |
| 2446 | bool AllMatched = false; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2447 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2448 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2449 | |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2450 | const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2451 | const PointerType *PT = 0; |
| 2452 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2453 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2454 | if (!CatchParam) { |
| 2455 | AllMatched = true; |
| 2456 | } else { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2457 | PT = CatchParam->getType()->getAsPointerType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2458 | |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 2459 | // catch(id e) always matches. |
| 2460 | // FIXME: For the time being we also match id<X>; this should |
| 2461 | // be rejected by Sema instead. |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2462 | if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) || |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2463 | CatchParam->getType()->isObjCQualifiedIdType()) |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2464 | AllMatched = true; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2465 | } |
| 2466 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2467 | if (AllMatched) { |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2468 | if (CatchParam) { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2469 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2470 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2471 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2472 | } |
Anders Carlsson | 1452f55 | 2008-09-11 08:21:54 +0000 | [diff] [blame] | 2473 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2474 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2475 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2476 | break; |
| 2477 | } |
| 2478 | |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2479 | assert(PT && "Unexpected non-pointer type in @catch"); |
| 2480 | QualType T = PT->getPointeeType(); |
Anders Carlsson | 4b7ff6e | 2008-09-11 06:35:14 +0000 | [diff] [blame] | 2481 | const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2482 | assert(ObjCType && "Catch parameter must have Objective-C type!"); |
| 2483 | |
| 2484 | // Check if the @catch block matches the exception object. |
| 2485 | llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl()); |
| 2486 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2487 | llvm::Value *Match = |
| 2488 | CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(), |
| 2489 | Class, Caught, "match"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2490 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2491 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2492 | |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2493 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2494 | MatchedBlock, NextCatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2495 | |
| 2496 | // Emit the @catch block. |
| 2497 | CGF.EmitBlock(MatchedBlock); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2498 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2499 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2500 | |
| 2501 | llvm::Value *Tmp = |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2502 | CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()), |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2503 | "tmp"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2504 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2505 | |
| 2506 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2507 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2508 | |
| 2509 | CGF.EmitBlock(NextCatchBlock); |
| 2510 | } |
| 2511 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2512 | if (!AllMatched) { |
| 2513 | // None of the handlers caught the exception, so store it to be |
| 2514 | // rethrown at the end of the @finally block. |
| 2515 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2516 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2517 | } |
| 2518 | |
| 2519 | // Emit the exception handler for the @catch blocks. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2520 | CGF.EmitBlock(CatchHandler); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2521 | CGF.Builder.CreateStore( |
| 2522 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2523 | ExceptionData), |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2524 | RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2525 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2526 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2527 | } else { |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2528 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2529 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2530 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2531 | } |
| 2532 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2533 | // Pop the exception-handling stack entry. It is important to do |
| 2534 | // this now, because the code in the @finally block is not in this |
| 2535 | // context. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2536 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 2537 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2538 | CGF.ObjCEHValueStack.pop_back(); |
| 2539 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2540 | // Emit the @finally block. |
| 2541 | CGF.EmitBlock(FinallyBlock); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2542 | llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp"); |
| 2543 | |
| 2544 | CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit); |
| 2545 | |
| 2546 | CGF.EmitBlock(FinallyExit); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2547 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2548 | |
| 2549 | CGF.EmitBlock(FinallyNoExit); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2550 | if (isTry) { |
| 2551 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 2552 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 2553 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2554 | } else { |
| 2555 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 2556 | // @synchronized. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2557 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg); |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 2558 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2559 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2560 | // Emit the switch block |
| 2561 | if (Info.SwitchBlock) |
| 2562 | CGF.EmitBlock(Info.SwitchBlock); |
| 2563 | if (Info.EndBlock) |
| 2564 | CGF.EmitBlock(Info.EndBlock); |
| 2565 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2566 | CGF.EmitBlock(FinallyRethrow); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2567 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2568 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2569 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2570 | |
| 2571 | CGF.EmitBlock(FinallyEnd); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2572 | } |
| 2573 | |
| 2574 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2575 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2576 | llvm::Value *ExceptionAsObject; |
| 2577 | |
| 2578 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 2579 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
| 2580 | ExceptionAsObject = |
| 2581 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 2582 | } else { |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2583 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2584 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2585 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2586 | } |
| 2587 | |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2588 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2589 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2590 | |
| 2591 | // Clear the insertion point to indicate we are in unreachable code. |
| 2592 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2593 | } |
| 2594 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2595 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2596 | /// object: objc_read_weak (id *src) |
| 2597 | /// |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2598 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2599 | llvm::Value *AddrWeakObj) |
| 2600 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2601 | const llvm::Type* DestTy = |
| 2602 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2603 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2604 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2605 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2606 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2607 | return read_weak; |
| 2608 | } |
| 2609 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2610 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 2611 | /// objc_assign_weak (id src, id *dst) |
| 2612 | /// |
| 2613 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 2614 | llvm::Value *src, llvm::Value *dst) |
| 2615 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2616 | const llvm::Type * SrcTy = src->getType(); |
| 2617 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2618 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2619 | assert(Size <= 8 && "does not support size > 8"); |
| 2620 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2621 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2622 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2623 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2624 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2625 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 2626 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2627 | src, dst, "weakassign"); |
| 2628 | return; |
| 2629 | } |
| 2630 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2631 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 2632 | /// objc_assign_global (id src, id *dst) |
| 2633 | /// |
| 2634 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 2635 | llvm::Value *src, llvm::Value *dst) |
| 2636 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2637 | const llvm::Type * SrcTy = src->getType(); |
| 2638 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2639 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2640 | assert(Size <= 8 && "does not support size > 8"); |
| 2641 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2642 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2643 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2644 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2645 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2646 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2647 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2648 | src, dst, "globalassign"); |
| 2649 | return; |
| 2650 | } |
| 2651 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2652 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 2653 | /// objc_assign_ivar (id src, id *dst) |
| 2654 | /// |
| 2655 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 2656 | llvm::Value *src, llvm::Value *dst) |
| 2657 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2658 | const llvm::Type * SrcTy = src->getType(); |
| 2659 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2660 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2661 | assert(Size <= 8 && "does not support size > 8"); |
| 2662 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2663 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2664 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2665 | } |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2666 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2667 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2668 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(), |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2669 | src, dst, "assignivar"); |
| 2670 | return; |
| 2671 | } |
| 2672 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2673 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 2674 | /// objc_assign_strongCast (id src, id *dst) |
| 2675 | /// |
| 2676 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 2677 | llvm::Value *src, llvm::Value *dst) |
| 2678 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2679 | const llvm::Type * SrcTy = src->getType(); |
| 2680 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2681 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2682 | assert(Size <= 8 && "does not support size > 8"); |
| 2683 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2684 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2685 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2686 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2687 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2688 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2689 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2690 | src, dst, "weakassign"); |
| 2691 | return; |
| 2692 | } |
| 2693 | |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2694 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 2695 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2696 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 2697 | QualType ObjectTy, |
| 2698 | llvm::Value *BaseValue, |
| 2699 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2700 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 2701 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2702 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 2703 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2706 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 2707 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2708 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2709 | uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2710 | return llvm::ConstantInt::get( |
| 2711 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 2712 | Offset); |
| 2713 | } |
| 2714 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2715 | /* *** Private Interface *** */ |
| 2716 | |
| 2717 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 2718 | /// level information. |
| 2719 | /// |
| 2720 | /// See: <rdr://4810609&4810587&4810587> |
| 2721 | /// struct IMAGE_INFO { |
| 2722 | /// unsigned version; |
| 2723 | /// unsigned flags; |
| 2724 | /// }; |
| 2725 | enum ImageInfoFlags { |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2726 | eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what |
| 2727 | // this implies. |
| 2728 | eImageInfo_GarbageCollected = (1 << 1), |
| 2729 | eImageInfo_GCOnly = (1 << 2), |
| 2730 | eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set. |
| 2731 | |
| 2732 | // A flag indicating that the module has no instances of an |
| 2733 | // @synthesize of a superclass variable. <rdar://problem/6803242> |
| 2734 | eImageInfo_CorrectedSynthesize = (1 << 4) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2735 | }; |
| 2736 | |
| 2737 | void CGObjCMac::EmitImageInfo() { |
| 2738 | unsigned version = 0; // Version is unused? |
| 2739 | unsigned flags = 0; |
| 2740 | |
| 2741 | // FIXME: Fix and continue? |
| 2742 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 2743 | flags |= eImageInfo_GarbageCollected; |
| 2744 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 2745 | flags |= eImageInfo_GCOnly; |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2746 | |
| 2747 | // We never allow @synthesize of a superclass property. |
| 2748 | flags |= eImageInfo_CorrectedSynthesize; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2749 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2750 | // Emitted as int[2]; |
| 2751 | llvm::Constant *values[2] = { |
| 2752 | llvm::ConstantInt::get(llvm::Type::Int32Ty, version), |
| 2753 | llvm::ConstantInt::get(llvm::Type::Int32Ty, flags) |
| 2754 | }; |
| 2755 | llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2756 | |
| 2757 | const char *Section; |
| 2758 | if (ObjCABI == 1) |
| 2759 | Section = "__OBJC, __image_info,regular"; |
| 2760 | else |
| 2761 | Section = "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2762 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2763 | CreateMetadataVar("\01L_OBJC_IMAGE_INFO", |
| 2764 | llvm::ConstantArray::get(AT, values, 2), |
| 2765 | Section, |
| 2766 | 0, |
| 2767 | true); |
| 2768 | GV->setConstant(true); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2769 | } |
| 2770 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2771 | |
| 2772 | // struct objc_module { |
| 2773 | // unsigned long version; |
| 2774 | // unsigned long size; |
| 2775 | // const char *name; |
| 2776 | // Symtab symtab; |
| 2777 | // }; |
| 2778 | |
| 2779 | // FIXME: Get from somewhere |
| 2780 | static const int ModuleVersion = 7; |
| 2781 | |
| 2782 | void CGObjCMac::EmitModuleInfo() { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 2783 | uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2784 | |
| 2785 | std::vector<llvm::Constant*> Values(4); |
| 2786 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion); |
| 2787 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2788 | // This used to be the filename, now it is unused. <rdr://4327263> |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2789 | Values[2] = GetClassName(&CGM.getContext().Idents.get("")); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2790 | Values[3] = EmitModuleSymbols(); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2791 | CreateMetadataVar("\01L_OBJC_MODULES", |
| 2792 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
| 2793 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2794 | 4, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2795 | } |
| 2796 | |
| 2797 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2798 | unsigned NumClasses = DefinedClasses.size(); |
| 2799 | unsigned NumCategories = DefinedCategories.size(); |
| 2800 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2801 | // Return null if no symbols were defined. |
| 2802 | if (!NumClasses && !NumCategories) |
| 2803 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
| 2804 | |
| 2805 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2806 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2807 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
| 2808 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 2809 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
| 2810 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2811 | // The runtime expects exactly the list of defined classes followed |
| 2812 | // by the list of defined categories, in a single array. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2813 | std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2814 | for (unsigned i=0; i<NumClasses; i++) |
| 2815 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 2816 | ObjCTypes.Int8PtrTy); |
| 2817 | for (unsigned i=0; i<NumCategories; i++) |
| 2818 | Symbols[NumClasses + i] = |
| 2819 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 2820 | ObjCTypes.Int8PtrTy); |
| 2821 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2822 | Values[4] = |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2823 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2824 | NumClasses + NumCategories), |
| 2825 | Symbols); |
| 2826 | |
| 2827 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2828 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2829 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2830 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 2831 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2832 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2833 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
| 2834 | } |
| 2835 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2836 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2837 | const ObjCInterfaceDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2838 | LazySymbols.insert(ID->getIdentifier()); |
| 2839 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2840 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 2841 | |
| 2842 | if (!Entry) { |
| 2843 | llvm::Constant *Casted = |
| 2844 | llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()), |
| 2845 | ObjCTypes.ClassPtrTy); |
| 2846 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2847 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 2848 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2849 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2850 | } |
| 2851 | |
| 2852 | return Builder.CreateLoad(Entry, false, "tmp"); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2853 | } |
| 2854 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2855 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2856 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 2857 | |
| 2858 | if (!Entry) { |
| 2859 | llvm::Constant *Casted = |
| 2860 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 2861 | ObjCTypes.SelectorPtrTy); |
| 2862 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2863 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 2864 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2865 | 4, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2866 | } |
| 2867 | |
| 2868 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 2869 | } |
| 2870 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 2871 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2872 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2873 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2874 | if (!Entry) |
| 2875 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 2876 | llvm::ConstantArray::get(Ident->getName()), |
| 2877 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 2878 | 1, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2879 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2880 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2881 | } |
| 2882 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 2883 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 2884 | /// ivar layout bitmap. |
| 2885 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
| 2886 | const ObjCCommonTypesHelper &ObjCTypes) { |
| 2887 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 2888 | } |
| 2889 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 2890 | static QualType::GCAttrTypes GetGCAttrTypeForType(ASTContext &Ctx, |
| 2891 | QualType FQT) { |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 2892 | if (FQT.isObjCGCStrong()) |
| 2893 | return QualType::Strong; |
| 2894 | |
| 2895 | if (FQT.isObjCGCWeak()) |
| 2896 | return QualType::Weak; |
| 2897 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 2898 | if (Ctx.isObjCObjectPointerType(FQT)) |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 2899 | return QualType::Strong; |
| 2900 | |
| 2901 | if (const PointerType *PT = FQT->getAsPointerType()) |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 2902 | return GetGCAttrTypeForType(Ctx, PT->getPointeeType()); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 2903 | |
| 2904 | return QualType::GCNone; |
| 2905 | } |
| 2906 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 2907 | void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT, |
| 2908 | unsigned int BytePos, |
| 2909 | bool ForStrongLayout, |
| 2910 | bool &HasUnion) { |
| 2911 | const RecordDecl *RD = RT->getDecl(); |
| 2912 | // FIXME - Use iterator. |
| 2913 | llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(CGM.getContext()), |
| 2914 | RD->field_end(CGM.getContext())); |
| 2915 | const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0)); |
| 2916 | const llvm::StructLayout *RecLayout = |
| 2917 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); |
| 2918 | |
| 2919 | BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos, |
| 2920 | ForStrongLayout, HasUnion); |
| 2921 | } |
| 2922 | |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 2923 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2924 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2925 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2926 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2927 | unsigned int BytePos, bool ForStrongLayout, |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 2928 | bool &HasUnion) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2929 | bool IsUnion = (RD && RD->isUnion()); |
| 2930 | uint64_t MaxUnionIvarSize = 0; |
| 2931 | uint64_t MaxSkippedUnionIvarSize = 0; |
| 2932 | FieldDecl *MaxField = 0; |
| 2933 | FieldDecl *MaxSkippedField = 0; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2934 | FieldDecl *LastFieldBitfield = 0; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 2935 | uint64_t MaxFieldOffset = 0; |
| 2936 | uint64_t MaxSkippedFieldOffset = 0; |
| 2937 | uint64_t LastBitfieldOffset = 0; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2938 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2939 | if (RecFields.empty()) |
| 2940 | return; |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2941 | unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0); |
| 2942 | unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth(); |
| 2943 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2944 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2945 | FieldDecl *Field = RecFields[i]; |
Daniel Dunbar | e05cc98 | 2009-05-03 23:35:23 +0000 | [diff] [blame^] | 2946 | uint64_t FieldOffset; |
| 2947 | if (RD) |
| 2948 | FieldOffset = |
| 2949 | Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); |
| 2950 | else |
| 2951 | FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)); |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 2952 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2953 | // Skip over unnamed or bitfields |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2954 | if (!Field->getIdentifier() || Field->isBitField()) { |
| 2955 | LastFieldBitfield = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 2956 | LastBitfieldOffset = FieldOffset; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2957 | continue; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2958 | } |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 2959 | |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2960 | LastFieldBitfield = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2961 | QualType FQT = Field->getType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2962 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2963 | if (FQT->isUnionType()) |
| 2964 | HasUnion = true; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2965 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 2966 | BuildAggrIvarRecordLayout(FQT->getAsRecordType(), |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 2967 | BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 2968 | ForStrongLayout, HasUnion); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2969 | continue; |
| 2970 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2971 | |
| 2972 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2973 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 2974 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2975 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 2976 | assert(CArray && "only array with known element size is supported"); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2977 | FQT = CArray->getElementType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2978 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 2979 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 2980 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2981 | ElCount *= CArray->getSize().getZExtValue(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2982 | FQT = CArray->getElementType(); |
| 2983 | } |
| 2984 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2985 | assert(!FQT->isUnionType() && |
| 2986 | "layout for array of unions not supported"); |
| 2987 | if (FQT->isRecordType()) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 2988 | int OldIndex = IvarsInfo.size() - 1; |
| 2989 | int OldSkIndex = SkipIvars.size() -1; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2990 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2991 | const RecordType *RT = FQT->getAsRecordType(); |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 2992 | BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 2993 | ForStrongLayout, HasUnion); |
| 2994 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2995 | // Replicate layout information for each array element. Note that |
| 2996 | // one element is already done. |
| 2997 | uint64_t ElIx = 1; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 2998 | for (int FirstIndex = IvarsInfo.size() - 1, |
| 2999 | FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3000 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3001 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 3002 | IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx, |
| 3003 | IvarsInfo[i].ivar_size)); |
| 3004 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) |
| 3005 | SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx, |
| 3006 | SkipIvars[i].ivar_size)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3007 | } |
| 3008 | continue; |
| 3009 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3010 | } |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3011 | // At this point, we are done with Record/Union and array there of. |
| 3012 | // For other arrays we are down to its element type. |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3013 | QualType::GCAttrTypes GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3014 | |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3015 | unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType()); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3016 | if ((ForStrongLayout && GCAttr == QualType::Strong) |
| 3017 | || (!ForStrongLayout && GCAttr == QualType::Weak)) { |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3018 | if (IsUnion) { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3019 | uint64_t UnionIvarSize = FieldSize / WordSizeInBits; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3020 | if (UnionIvarSize > MaxUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3021 | MaxUnionIvarSize = UnionIvarSize; |
| 3022 | MaxField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3023 | MaxFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3024 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3025 | } else { |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3026 | IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3027 | FieldSize / WordSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3028 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3029 | } else if ((ForStrongLayout && |
| 3030 | (GCAttr == QualType::GCNone || GCAttr == QualType::Weak)) |
| 3031 | || (!ForStrongLayout && GCAttr != QualType::Weak)) { |
| 3032 | if (IsUnion) { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3033 | // FIXME: Why the asymmetry? We divide by word size in bits on |
| 3034 | // other side. |
| 3035 | uint64_t UnionIvarSize = FieldSize; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3036 | if (UnionIvarSize > MaxSkippedUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3037 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 3038 | MaxSkippedField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3039 | MaxSkippedFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3040 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3041 | } else { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3042 | // FIXME: Why the asymmetry, we divide by byte size in bits here? |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3043 | SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3044 | FieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3045 | } |
| 3046 | } |
| 3047 | } |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3048 | |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3049 | if (LastFieldBitfield) { |
| 3050 | // Last field was a bitfield. Must update skip info. |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3051 | Expr *BitWidth = LastFieldBitfield->getBitWidth(); |
| 3052 | uint64_t BitFieldSize = |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 3053 | BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue(); |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3054 | GC_IVAR skivar; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3055 | skivar.ivar_bytepos = BytePos + LastBitfieldOffset; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3056 | skivar.ivar_size = (BitFieldSize / ByteSizeInBits) |
| 3057 | + ((BitFieldSize % ByteSizeInBits) != 0); |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3058 | SkipIvars.push_back(skivar); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3059 | } |
| 3060 | |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3061 | if (MaxField) |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3062 | IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3063 | MaxUnionIvarSize)); |
| 3064 | if (MaxSkippedField) |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3065 | SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3066 | MaxSkippedUnionIvarSize)); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3067 | } |
| 3068 | |
| 3069 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 3070 | /// implementation for the __strong or __weak case. |
| 3071 | /// The layout map displays which words in ivar list must be skipped |
| 3072 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 3073 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
| 3074 | /// of words to skip and right nibble is count of words to scan. So, each |
| 3075 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
| 3076 | /// represented by a 0x00 byte which also ends the string. |
| 3077 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 3078 | /// - id, Class |
| 3079 | /// - object * |
| 3080 | /// - __strong anything |
| 3081 | /// |
| 3082 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 3083 | /// - __weak anything |
| 3084 | /// |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3085 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3086 | const ObjCImplementationDecl *OMD, |
| 3087 | bool ForStrongLayout) { |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3088 | bool hasUnion = false; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3089 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3090 | unsigned int WordsToScan, WordsToSkip; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3091 | const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 3092 | if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) |
| 3093 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3094 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3095 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3096 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3097 | CGM.getContext().CollectObjCIvars(OI, RecFields); |
| 3098 | if (RecFields.empty()) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3099 | return llvm::Constant::getNullValue(PtrTy); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3100 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3101 | SkipIvars.clear(); |
| 3102 | IvarsInfo.clear(); |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 3103 | |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 3104 | BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion); |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3105 | if (IvarsInfo.empty()) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3106 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3107 | |
| 3108 | // Sort on byte position in case we encounterred a union nested in |
| 3109 | // the ivar list. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3110 | if (hasUnion && !IvarsInfo.empty()) |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 3111 | std::sort(IvarsInfo.begin(), IvarsInfo.end()); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3112 | if (hasUnion && !SkipIvars.empty()) |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 3113 | std::sort(SkipIvars.begin(), SkipIvars.end()); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3114 | |
| 3115 | // Build the string of skip/scan nibbles |
Fariborz Jahanian | 8c2f2d1 | 2009-04-24 17:15:27 +0000 | [diff] [blame] | 3116 | llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3117 | unsigned int WordSize = |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3118 | CGM.getTypes().getTargetData().getTypePaddedSize(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3119 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 3120 | WordsToSkip = 0; |
| 3121 | WordsToScan = IvarsInfo[0].ivar_size; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3122 | } else { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3123 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 3124 | WordsToScan = IvarsInfo[0].ivar_size; |
| 3125 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3126 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3127 | unsigned int TailPrevGCObjC = |
| 3128 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3129 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3130 | // consecutive 'scanned' object pointers. |
| 3131 | WordsToScan += IvarsInfo[i].ivar_size; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3132 | } else { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3133 | // Skip over 'gc'able object pointer which lay over each other. |
| 3134 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 3135 | continue; |
| 3136 | // Must skip over 1 or more words. We save current skip/scan values |
| 3137 | // and start a new pair. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3138 | SKIP_SCAN SkScan; |
| 3139 | SkScan.skip = WordsToSkip; |
| 3140 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3141 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3142 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3143 | // Skip the hole. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3144 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 3145 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3146 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3147 | WordsToSkip = 0; |
| 3148 | WordsToScan = IvarsInfo[i].ivar_size; |
| 3149 | } |
| 3150 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3151 | if (WordsToScan > 0) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3152 | SKIP_SCAN SkScan; |
| 3153 | SkScan.skip = WordsToSkip; |
| 3154 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3155 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3156 | } |
| 3157 | |
| 3158 | bool BytesSkipped = false; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3159 | if (!SkipIvars.empty()) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3160 | unsigned int LastIndex = SkipIvars.size()-1; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3161 | int LastByteSkipped = |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3162 | SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size; |
| 3163 | LastIndex = IvarsInfo.size()-1; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3164 | int LastByteScanned = |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3165 | IvarsInfo[LastIndex].ivar_bytepos + |
| 3166 | IvarsInfo[LastIndex].ivar_size * WordSize; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3167 | BytesSkipped = (LastByteSkipped > LastByteScanned); |
| 3168 | // 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] | 3169 | if (BytesSkipped) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3170 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3171 | SKIP_SCAN SkScan; |
| 3172 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
| 3173 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3174 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3175 | } |
| 3176 | } |
| 3177 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 3178 | // as 0xMN. |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3179 | int SkipScan = SkipScanIvars.size()-1; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3180 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3181 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 3182 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 3183 | // 0xM0 followed by 0x0N detected. |
| 3184 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 3185 | for (int j = i+1; j < SkipScan; j++) |
| 3186 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 3187 | --SkipScan; |
| 3188 | } |
| 3189 | } |
| 3190 | |
| 3191 | // Generate the string. |
| 3192 | std::string BitMap; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3193 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3194 | unsigned char byte; |
| 3195 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 3196 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 3197 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 3198 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
| 3199 | |
| 3200 | if (skip_small > 0 || skip_big > 0) |
| 3201 | BytesSkipped = true; |
| 3202 | // first skip big. |
| 3203 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 3204 | BitMap += (unsigned char)(0xf0); |
| 3205 | |
| 3206 | // next (skip small, scan) |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3207 | if (skip_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3208 | byte = skip_small << 4; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3209 | if (scan_big > 0) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3210 | byte |= 0xf; |
| 3211 | --scan_big; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3212 | } else if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3213 | byte |= scan_small; |
| 3214 | scan_small = 0; |
| 3215 | } |
| 3216 | BitMap += byte; |
| 3217 | } |
| 3218 | // next scan big |
| 3219 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 3220 | BitMap += (unsigned char)(0x0f); |
| 3221 | // last scan small |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3222 | if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3223 | byte = scan_small; |
| 3224 | BitMap += byte; |
| 3225 | } |
| 3226 | } |
| 3227 | // null terminate string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3228 | unsigned char zero = 0; |
| 3229 | BitMap += zero; |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3230 | |
| 3231 | if (CGM.getLangOptions().ObjCGCBitmapPrint) { |
| 3232 | printf("\n%s ivar layout for class '%s': ", |
| 3233 | ForStrongLayout ? "strong" : "weak", |
| 3234 | OMD->getClassInterface()->getNameAsCString()); |
| 3235 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
| 3236 | for (unsigned i = 0; i < BitMap.size(); i++) |
| 3237 | if (!(s[i] & 0xf0)) |
| 3238 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3239 | else |
| 3240 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3241 | printf("\n"); |
| 3242 | } |
| 3243 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3244 | // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as |
| 3245 | // final layout. |
| 3246 | if (ForStrongLayout && !BytesSkipped) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3247 | return llvm::Constant::getNullValue(PtrTy); |
| 3248 | llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 3249 | llvm::ConstantArray::get(BitMap.c_str()), |
| 3250 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3251 | 1, true); |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3252 | return getConstantGEP(Entry, 0, 0); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3253 | } |
| 3254 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3255 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3256 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 3257 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3258 | // FIXME: Avoid std::string copying. |
| 3259 | if (!Entry) |
| 3260 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
| 3261 | llvm::ConstantArray::get(Sel.getAsString()), |
| 3262 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3263 | 1, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3264 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3265 | return getConstantGEP(Entry, 0, 0); |
| 3266 | } |
| 3267 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3268 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3269 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3270 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 3271 | } |
| 3272 | |
| 3273 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3274 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3275 | return GetMethodVarName(&CGM.getContext().Idents.get(Name)); |
| 3276 | } |
| 3277 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 3278 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3279 | std::string TypeStr; |
| 3280 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 3281 | |
| 3282 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3283 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3284 | if (!Entry) |
| 3285 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3286 | llvm::ConstantArray::get(TypeStr), |
| 3287 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3288 | 1, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3289 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3290 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3291 | } |
| 3292 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3293 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3294 | std::string TypeStr; |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3295 | CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D), |
| 3296 | TypeStr); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3297 | |
| 3298 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 3299 | |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3300 | if (!Entry) |
| 3301 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3302 | llvm::ConstantArray::get(TypeStr), |
| 3303 | "__TEXT,__cstring,cstring_literals", |
| 3304 | 1, true); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3305 | |
| 3306 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3309 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3310 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3311 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
| 3312 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3313 | if (!Entry) |
| 3314 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
| 3315 | llvm::ConstantArray::get(Ident->getName()), |
| 3316 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3317 | 1, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3318 | |
| 3319 | return getConstantGEP(Entry, 0, 0); |
| 3320 | } |
| 3321 | |
| 3322 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3323 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3324 | llvm::Constant * |
| 3325 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 3326 | const Decl *Container) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3327 | std::string TypeStr; |
| 3328 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3329 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 3330 | } |
| 3331 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3332 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
| 3333 | const ObjCContainerDecl *CD, |
| 3334 | std::string &NameOut) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 3335 | NameOut = '\01'; |
| 3336 | NameOut += (D->isInstanceMethod() ? '-' : '+'); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3337 | NameOut += '['; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 3338 | assert (CD && "Missing container decl in GetNameForMethod"); |
| 3339 | NameOut += CD->getNameAsString(); |
Fariborz Jahanian | 1e9aef3 | 2009-04-16 18:34:20 +0000 | [diff] [blame] | 3340 | if (const ObjCCategoryImplDecl *CID = |
| 3341 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) { |
| 3342 | NameOut += '('; |
| 3343 | NameOut += CID->getNameAsString(); |
| 3344 | NameOut+= ')'; |
| 3345 | } |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3346 | NameOut += ' '; |
| 3347 | NameOut += D->getSelector().getAsString(); |
| 3348 | NameOut += ']'; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3349 | } |
| 3350 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3351 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3352 | EmitModuleInfo(); |
| 3353 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3354 | // Emit the dummy bodies for any protocols which were referenced but |
| 3355 | // never defined. |
| 3356 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
| 3357 | i = Protocols.begin(), e = Protocols.end(); i != e; ++i) { |
| 3358 | if (i->second->hasInitializer()) |
| 3359 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3360 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3361 | std::vector<llvm::Constant*> Values(5); |
| 3362 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 3363 | Values[1] = GetClassName(i->first); |
| 3364 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 3365 | Values[3] = Values[4] = |
| 3366 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 3367 | i->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 3368 | i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 3369 | Values)); |
| 3370 | } |
| 3371 | |
| 3372 | std::vector<llvm::Constant*> Used; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3373 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3374 | e = UsedGlobals.end(); i != e; ++i) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3375 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3376 | } |
| 3377 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3378 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3379 | llvm::GlobalValue *GV = |
| 3380 | new llvm::GlobalVariable(AT, false, |
| 3381 | llvm::GlobalValue::AppendingLinkage, |
| 3382 | llvm::ConstantArray::get(AT, Used), |
| 3383 | "llvm.used", |
| 3384 | &CGM.getModule()); |
| 3385 | |
| 3386 | GV->setSection("llvm.metadata"); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3387 | |
| 3388 | // Add assembler directives to add lazy undefined symbol references |
| 3389 | // for classes which are referenced but not defined. This is |
| 3390 | // important for correct linker interaction. |
| 3391 | |
| 3392 | // FIXME: Uh, this isn't particularly portable. |
| 3393 | std::stringstream s; |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3394 | |
| 3395 | if (!CGM.getModule().getModuleInlineAsm().empty()) |
| 3396 | s << "\n"; |
| 3397 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3398 | for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(), |
| 3399 | e = LazySymbols.end(); i != e; ++i) { |
| 3400 | s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n"; |
| 3401 | } |
| 3402 | for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(), |
| 3403 | e = DefinedSymbols.end(); i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3404 | s << "\t.objc_class_name_" << (*i)->getName() << "=0\n" |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3405 | << "\t.globl .objc_class_name_" << (*i)->getName() << "\n"; |
| 3406 | } |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3407 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3408 | CGM.getModule().appendModuleInlineAsm(s.str()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3409 | } |
| 3410 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3411 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3412 | : CGObjCCommonMac(cgm), |
| 3413 | ObjCTypes(cgm) |
| 3414 | { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3415 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3416 | ObjCABI = 2; |
| 3417 | } |
| 3418 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3419 | /* *** */ |
| 3420 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3421 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3422 | : CGM(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3423 | { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3424 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3425 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3426 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3427 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3428 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3429 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3430 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3431 | Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 3432 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3433 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 3434 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3435 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3436 | |
| 3437 | // FIXME: It would be nice to unify this with the opaque type, so |
| 3438 | // that the IR comes out a bit cleaner. |
| 3439 | const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 3440 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3441 | |
| 3442 | // I'm not sure I like this. The implicit coordination is a bit |
| 3443 | // gross. We should solve this in a reasonable fashion because this |
| 3444 | // is a pretty common task (match some runtime data structure with |
| 3445 | // an LLVM data structure). |
| 3446 | |
| 3447 | // FIXME: This is leaked. |
| 3448 | // FIXME: Merge with rewriter code? |
| 3449 | |
| 3450 | // struct _objc_super { |
| 3451 | // id self; |
| 3452 | // Class cls; |
| 3453 | // } |
| 3454 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3455 | SourceLocation(), |
| 3456 | &Ctx.Idents.get("_objc_super")); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3457 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3458 | Ctx.getObjCIdType(), 0, false)); |
| 3459 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3460 | Ctx.getObjCClassType(), 0, false)); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3461 | RD->completeDefinition(Ctx); |
| 3462 | |
| 3463 | SuperCTy = Ctx.getTagDeclType(RD); |
| 3464 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
| 3465 | |
| 3466 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3467 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 3468 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3469 | // struct _prop_t { |
| 3470 | // char *name; |
| 3471 | // char *attributes; |
| 3472 | // } |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 3473 | PropertyTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, NULL); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3474 | CGM.getModule().addTypeName("struct._prop_t", |
| 3475 | PropertyTy); |
| 3476 | |
| 3477 | // struct _prop_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3478 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3479 | // uint32_t count_of_properties; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3480 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3481 | // } |
| 3482 | PropertyListTy = llvm::StructType::get(IntTy, |
| 3483 | IntTy, |
| 3484 | llvm::ArrayType::get(PropertyTy, 0), |
| 3485 | NULL); |
| 3486 | CGM.getModule().addTypeName("struct._prop_list_t", |
| 3487 | PropertyListTy); |
| 3488 | // struct _prop_list_t * |
| 3489 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
| 3490 | |
| 3491 | // struct _objc_method { |
| 3492 | // SEL _cmd; |
| 3493 | // char *method_type; |
| 3494 | // char *_imp; |
| 3495 | // } |
| 3496 | MethodTy = llvm::StructType::get(SelectorPtrTy, |
| 3497 | Int8PtrTy, |
| 3498 | Int8PtrTy, |
| 3499 | NULL); |
| 3500 | CGM.getModule().addTypeName("struct._objc_method", MethodTy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3501 | |
| 3502 | // struct _objc_cache * |
| 3503 | CacheTy = llvm::OpaqueType::get(); |
| 3504 | CGM.getModule().addTypeName("struct._objc_cache", CacheTy); |
| 3505 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3506 | } |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3507 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3508 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3509 | : ObjCCommonTypesHelper(cgm) |
| 3510 | { |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3511 | // struct _objc_method_description { |
| 3512 | // SEL name; |
| 3513 | // char *types; |
| 3514 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3515 | MethodDescriptionTy = |
| 3516 | llvm::StructType::get(SelectorPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3517 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3518 | NULL); |
| 3519 | CGM.getModule().addTypeName("struct._objc_method_description", |
| 3520 | MethodDescriptionTy); |
| 3521 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3522 | // struct _objc_method_description_list { |
| 3523 | // int count; |
| 3524 | // struct _objc_method_description[1]; |
| 3525 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3526 | MethodDescriptionListTy = |
| 3527 | llvm::StructType::get(IntTy, |
| 3528 | llvm::ArrayType::get(MethodDescriptionTy, 0), |
| 3529 | NULL); |
| 3530 | CGM.getModule().addTypeName("struct._objc_method_description_list", |
| 3531 | MethodDescriptionListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3532 | |
| 3533 | // struct _objc_method_description_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3534 | MethodDescriptionListPtrTy = |
| 3535 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
| 3536 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3537 | // Protocol description structures |
| 3538 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3539 | // struct _objc_protocol_extension { |
| 3540 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 3541 | // struct _objc_method_description_list *optional_instance_methods; |
| 3542 | // struct _objc_method_description_list *optional_class_methods; |
| 3543 | // struct _objc_property_list *instance_properties; |
| 3544 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3545 | ProtocolExtensionTy = |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3546 | llvm::StructType::get(IntTy, |
| 3547 | MethodDescriptionListPtrTy, |
| 3548 | MethodDescriptionListPtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3549 | PropertyListPtrTy, |
| 3550 | NULL); |
| 3551 | CGM.getModule().addTypeName("struct._objc_protocol_extension", |
| 3552 | ProtocolExtensionTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3553 | |
| 3554 | // struct _objc_protocol_extension * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3555 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
| 3556 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3557 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3558 | |
| 3559 | llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(); |
| 3560 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3561 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3562 | const llvm::Type *T = |
| 3563 | llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3564 | LongTy, |
| 3565 | llvm::ArrayType::get(ProtocolTyHolder, 0), |
| 3566 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3567 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T); |
| 3568 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3569 | // struct _objc_protocol { |
| 3570 | // struct _objc_protocol_extension *isa; |
| 3571 | // char *protocol_name; |
| 3572 | // struct _objc_protocol **_objc_protocol_list; |
| 3573 | // struct _objc_method_description_list *instance_methods; |
| 3574 | // struct _objc_method_description_list *class_methods; |
| 3575 | // } |
| 3576 | T = llvm::StructType::get(ProtocolExtensionPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3577 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3578 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3579 | MethodDescriptionListPtrTy, |
| 3580 | MethodDescriptionListPtrTy, |
| 3581 | NULL); |
| 3582 | cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T); |
| 3583 | |
| 3584 | ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get()); |
| 3585 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3586 | ProtocolListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3587 | // struct _objc_protocol_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3588 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
| 3589 | |
| 3590 | ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get()); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3591 | CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3592 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3593 | |
| 3594 | // Class description structures |
| 3595 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3596 | // struct _objc_ivar { |
| 3597 | // char *ivar_name; |
| 3598 | // char *ivar_type; |
| 3599 | // int ivar_offset; |
| 3600 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3601 | IvarTy = llvm::StructType::get(Int8PtrTy, |
| 3602 | Int8PtrTy, |
| 3603 | IntTy, |
| 3604 | NULL); |
| 3605 | CGM.getModule().addTypeName("struct._objc_ivar", IvarTy); |
| 3606 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3607 | // struct _objc_ivar_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3608 | IvarListTy = llvm::OpaqueType::get(); |
| 3609 | CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy); |
| 3610 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
| 3611 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3612 | // struct _objc_method_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3613 | MethodListTy = llvm::OpaqueType::get(); |
| 3614 | CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy); |
| 3615 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
| 3616 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3617 | // struct _objc_class_extension * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3618 | ClassExtensionTy = |
| 3619 | llvm::StructType::get(IntTy, |
| 3620 | Int8PtrTy, |
| 3621 | PropertyListPtrTy, |
| 3622 | NULL); |
| 3623 | CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy); |
| 3624 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
| 3625 | |
| 3626 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3627 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3628 | // struct _objc_class { |
| 3629 | // Class isa; |
| 3630 | // Class super_class; |
| 3631 | // char *name; |
| 3632 | // long version; |
| 3633 | // long info; |
| 3634 | // long instance_size; |
| 3635 | // struct _objc_ivar_list *ivars; |
| 3636 | // struct _objc_method_list *methods; |
| 3637 | // struct _objc_cache *cache; |
| 3638 | // struct _objc_protocol_list *protocols; |
| 3639 | // char *ivar_layout; |
| 3640 | // struct _objc_class_ext *ext; |
| 3641 | // }; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3642 | T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3643 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3644 | Int8PtrTy, |
| 3645 | LongTy, |
| 3646 | LongTy, |
| 3647 | LongTy, |
| 3648 | IvarListPtrTy, |
| 3649 | MethodListPtrTy, |
| 3650 | CachePtrTy, |
| 3651 | ProtocolListPtrTy, |
| 3652 | Int8PtrTy, |
| 3653 | ClassExtensionPtrTy, |
| 3654 | NULL); |
| 3655 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T); |
| 3656 | |
| 3657 | ClassTy = cast<llvm::StructType>(ClassTyHolder.get()); |
| 3658 | CGM.getModule().addTypeName("struct._objc_class", ClassTy); |
| 3659 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
| 3660 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3661 | // struct _objc_category { |
| 3662 | // char *category_name; |
| 3663 | // char *class_name; |
| 3664 | // struct _objc_method_list *instance_method; |
| 3665 | // struct _objc_method_list *class_method; |
| 3666 | // uint32_t size; // sizeof(struct _objc_category) |
| 3667 | // struct _objc_property_list *instance_properties;// category's @property |
| 3668 | // } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3669 | CategoryTy = llvm::StructType::get(Int8PtrTy, |
| 3670 | Int8PtrTy, |
| 3671 | MethodListPtrTy, |
| 3672 | MethodListPtrTy, |
| 3673 | ProtocolListPtrTy, |
| 3674 | IntTy, |
| 3675 | PropertyListPtrTy, |
| 3676 | NULL); |
| 3677 | CGM.getModule().addTypeName("struct._objc_category", CategoryTy); |
| 3678 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3679 | // Global metadata structures |
| 3680 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3681 | // struct _objc_symtab { |
| 3682 | // long sel_ref_cnt; |
| 3683 | // SEL *refs; |
| 3684 | // short cls_def_cnt; |
| 3685 | // short cat_def_cnt; |
| 3686 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 3687 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3688 | SymtabTy = llvm::StructType::get(LongTy, |
| 3689 | SelectorPtrTy, |
| 3690 | ShortTy, |
| 3691 | ShortTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3692 | llvm::ArrayType::get(Int8PtrTy, 0), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3693 | NULL); |
| 3694 | CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy); |
| 3695 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
| 3696 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3697 | // struct _objc_module { |
| 3698 | // long version; |
| 3699 | // long size; // sizeof(struct _objc_module) |
| 3700 | // char *name; |
| 3701 | // struct _objc_symtab* symtab; |
| 3702 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3703 | ModuleTy = |
| 3704 | llvm::StructType::get(LongTy, |
| 3705 | LongTy, |
| 3706 | Int8PtrTy, |
| 3707 | SymtabPtrTy, |
| 3708 | NULL); |
| 3709 | CGM.getModule().addTypeName("struct._objc_module", ModuleTy); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3710 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 3711 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3712 | // FIXME: This is the size of the setjmp buffer and should be |
| 3713 | // target specific. 18 is what's used on 32-bit X86. |
| 3714 | uint64_t SetJmpBufferSize = 18; |
| 3715 | |
| 3716 | // Exceptions |
| 3717 | const llvm::Type *StackPtrTy = |
Daniel Dunbar | 1000491 | 2008-09-27 06:32:25 +0000 | [diff] [blame] | 3718 | llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3719 | |
| 3720 | ExceptionDataTy = |
| 3721 | llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty, |
| 3722 | SetJmpBufferSize), |
| 3723 | StackPtrTy, NULL); |
| 3724 | CGM.getModule().addTypeName("struct._objc_exception_data", |
| 3725 | ExceptionDataTy); |
| 3726 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3727 | } |
| 3728 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3729 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3730 | : ObjCCommonTypesHelper(cgm) |
| 3731 | { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3732 | // struct _method_list_t { |
| 3733 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 3734 | // uint32_t method_count; |
| 3735 | // struct _objc_method method_list[method_count]; |
| 3736 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3737 | MethodListnfABITy = llvm::StructType::get(IntTy, |
| 3738 | IntTy, |
| 3739 | llvm::ArrayType::get(MethodTy, 0), |
| 3740 | NULL); |
| 3741 | CGM.getModule().addTypeName("struct.__method_list_t", |
| 3742 | MethodListnfABITy); |
| 3743 | // struct method_list_t * |
| 3744 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3745 | |
| 3746 | // struct _protocol_t { |
| 3747 | // id isa; // NULL |
| 3748 | // const char * const protocol_name; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3749 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3750 | // const struct method_list_t * const instance_methods; |
| 3751 | // const struct method_list_t * const class_methods; |
| 3752 | // const struct method_list_t *optionalInstanceMethods; |
| 3753 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3754 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3755 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 3756 | // const uint32_t flags; // = 0 |
| 3757 | // } |
| 3758 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3759 | // Holder for struct _protocol_list_t * |
| 3760 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3761 | |
| 3762 | ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy, |
| 3763 | Int8PtrTy, |
| 3764 | llvm::PointerType::getUnqual( |
| 3765 | ProtocolListTyHolder), |
| 3766 | MethodListnfABIPtrTy, |
| 3767 | MethodListnfABIPtrTy, |
| 3768 | MethodListnfABIPtrTy, |
| 3769 | MethodListnfABIPtrTy, |
| 3770 | PropertyListPtrTy, |
| 3771 | IntTy, |
| 3772 | IntTy, |
| 3773 | NULL); |
| 3774 | CGM.getModule().addTypeName("struct._protocol_t", |
| 3775 | ProtocolnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3776 | |
| 3777 | // struct _protocol_t* |
| 3778 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3779 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3780 | // struct _protocol_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3781 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3782 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3783 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3784 | ProtocolListnfABITy = llvm::StructType::get(LongTy, |
| 3785 | llvm::ArrayType::get( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3786 | ProtocolnfABIPtrTy, 0), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3787 | NULL); |
| 3788 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3789 | ProtocolListnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3790 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo( |
| 3791 | ProtocolListnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3792 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3793 | // struct _objc_protocol_list* |
| 3794 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3795 | |
| 3796 | // struct _ivar_t { |
| 3797 | // unsigned long int *offset; // pointer to ivar offset location |
| 3798 | // char *name; |
| 3799 | // char *type; |
| 3800 | // uint32_t alignment; |
| 3801 | // uint32_t size; |
| 3802 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3803 | IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy), |
| 3804 | Int8PtrTy, |
| 3805 | Int8PtrTy, |
| 3806 | IntTy, |
| 3807 | IntTy, |
| 3808 | NULL); |
| 3809 | CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy); |
| 3810 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3811 | // struct _ivar_list_t { |
| 3812 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 3813 | // uint32 count; |
| 3814 | // struct _iver_t list[count]; |
| 3815 | // } |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3816 | IvarListnfABITy = llvm::StructType::get(IntTy, |
| 3817 | IntTy, |
| 3818 | llvm::ArrayType::get( |
| 3819 | IvarnfABITy, 0), |
| 3820 | NULL); |
| 3821 | CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy); |
| 3822 | |
| 3823 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3824 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3825 | // struct _class_ro_t { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3826 | // uint32_t const flags; |
| 3827 | // uint32_t const instanceStart; |
| 3828 | // uint32_t const instanceSize; |
| 3829 | // uint32_t const reserved; // only when building for 64bit targets |
| 3830 | // const uint8_t * const ivarLayout; |
| 3831 | // const char *const name; |
| 3832 | // const struct _method_list_t * const baseMethods; |
| 3833 | // const struct _objc_protocol_list *const baseProtocols; |
| 3834 | // const struct _ivar_list_t *const ivars; |
| 3835 | // const uint8_t * const weakIvarLayout; |
| 3836 | // const struct _prop_list_t * const properties; |
| 3837 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3838 | |
| 3839 | // FIXME. Add 'reserved' field in 64bit abi mode! |
| 3840 | ClassRonfABITy = llvm::StructType::get(IntTy, |
| 3841 | IntTy, |
| 3842 | IntTy, |
| 3843 | Int8PtrTy, |
| 3844 | Int8PtrTy, |
| 3845 | MethodListnfABIPtrTy, |
| 3846 | ProtocolListnfABIPtrTy, |
| 3847 | IvarListnfABIPtrTy, |
| 3848 | Int8PtrTy, |
| 3849 | PropertyListPtrTy, |
| 3850 | NULL); |
| 3851 | CGM.getModule().addTypeName("struct._class_ro_t", |
| 3852 | ClassRonfABITy); |
| 3853 | |
| 3854 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 3855 | std::vector<const llvm::Type*> Params; |
| 3856 | Params.push_back(ObjectPtrTy); |
| 3857 | Params.push_back(SelectorPtrTy); |
| 3858 | ImpnfABITy = llvm::PointerType::getUnqual( |
| 3859 | llvm::FunctionType::get(ObjectPtrTy, Params, false)); |
| 3860 | |
| 3861 | // struct _class_t { |
| 3862 | // struct _class_t *isa; |
| 3863 | // struct _class_t * const superclass; |
| 3864 | // void *cache; |
| 3865 | // IMP *vtable; |
| 3866 | // struct class_ro_t *ro; |
| 3867 | // } |
| 3868 | |
| 3869 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3870 | ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3871 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3872 | CachePtrTy, |
| 3873 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 3874 | llvm::PointerType::getUnqual( |
| 3875 | ClassRonfABITy), |
| 3876 | NULL); |
| 3877 | CGM.getModule().addTypeName("struct._class_t", ClassnfABITy); |
| 3878 | |
| 3879 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo( |
| 3880 | ClassnfABITy); |
| 3881 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3882 | // LLVM for struct _class_t * |
| 3883 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
| 3884 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3885 | // struct _category_t { |
| 3886 | // const char * const name; |
| 3887 | // struct _class_t *const cls; |
| 3888 | // const struct _method_list_t * const instance_methods; |
| 3889 | // const struct _method_list_t * const class_methods; |
| 3890 | // const struct _protocol_list_t * const protocols; |
| 3891 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3892 | // } |
| 3893 | CategorynfABITy = llvm::StructType::get(Int8PtrTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3894 | ClassnfABIPtrTy, |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3895 | MethodListnfABIPtrTy, |
| 3896 | MethodListnfABIPtrTy, |
| 3897 | ProtocolListnfABIPtrTy, |
| 3898 | PropertyListPtrTy, |
| 3899 | NULL); |
| 3900 | CGM.getModule().addTypeName("struct._category_t", CategorynfABITy); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3901 | |
| 3902 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3903 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3904 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3905 | |
| 3906 | // MessageRefTy - LLVM for: |
| 3907 | // struct _message_ref_t { |
| 3908 | // IMP messenger; |
| 3909 | // SEL name; |
| 3910 | // }; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3911 | |
| 3912 | // First the clang type for struct _message_ref_t |
| 3913 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3914 | SourceLocation(), |
| 3915 | &Ctx.Idents.get("_message_ref_t")); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3916 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3917 | Ctx.VoidPtrTy, 0, false)); |
| 3918 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3919 | Ctx.getObjCSelType(), 0, false)); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3920 | RD->completeDefinition(Ctx); |
| 3921 | |
| 3922 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 3923 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 3924 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3925 | |
| 3926 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 3927 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
| 3928 | |
| 3929 | // SuperMessageRefTy - LLVM for: |
| 3930 | // struct _super_message_ref_t { |
| 3931 | // SUPER_IMP messenger; |
| 3932 | // SEL name; |
| 3933 | // }; |
| 3934 | SuperMessageRefTy = llvm::StructType::get(ImpnfABITy, |
| 3935 | SelectorPtrTy, |
| 3936 | NULL); |
| 3937 | CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy); |
| 3938 | |
| 3939 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 3940 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
| 3941 | |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 3942 | |
| 3943 | // struct objc_typeinfo { |
| 3944 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 3945 | // const char* name; // c++ typeinfo string |
| 3946 | // Class cls; |
| 3947 | // }; |
| 3948 | EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy), |
| 3949 | Int8PtrTy, |
| 3950 | ClassnfABIPtrTy, |
| 3951 | NULL); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 3952 | CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 3953 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3954 | } |
| 3955 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3956 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
| 3957 | FinishNonFragileABIModule(); |
| 3958 | |
| 3959 | return NULL; |
| 3960 | } |
| 3961 | |
| 3962 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 3963 | // nonfragile abi has no module definition. |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 3964 | |
| 3965 | // Build list of all implemented classe addresses in array |
| 3966 | // L_OBJC_LABEL_CLASS_$. |
| 3967 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$ |
| 3968 | // list of 'nonlazy' implementations (defined as those with a +load{} |
| 3969 | // method!!). |
| 3970 | unsigned NumClasses = DefinedClasses.size(); |
| 3971 | if (NumClasses) { |
| 3972 | std::vector<llvm::Constant*> Symbols(NumClasses); |
| 3973 | for (unsigned i=0; i<NumClasses; i++) |
| 3974 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 3975 | ObjCTypes.Int8PtrTy); |
| 3976 | llvm::Constant* Init = |
| 3977 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 3978 | NumClasses), |
| 3979 | Symbols); |
| 3980 | |
| 3981 | llvm::GlobalVariable *GV = |
| 3982 | new llvm::GlobalVariable(Init->getType(), false, |
| 3983 | llvm::GlobalValue::InternalLinkage, |
| 3984 | Init, |
| 3985 | "\01L_OBJC_LABEL_CLASS_$", |
| 3986 | &CGM.getModule()); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 3987 | GV->setAlignment(8); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 3988 | GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip"); |
| 3989 | UsedGlobals.push_back(GV); |
| 3990 | } |
| 3991 | |
| 3992 | // Build list of all implemented category addresses in array |
| 3993 | // L_OBJC_LABEL_CATEGORY_$. |
| 3994 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$ |
| 3995 | // list of 'nonlazy' category implementations (defined as those with a +load{} |
| 3996 | // method!!). |
| 3997 | unsigned NumCategory = DefinedCategories.size(); |
| 3998 | if (NumCategory) { |
| 3999 | std::vector<llvm::Constant*> Symbols(NumCategory); |
| 4000 | for (unsigned i=0; i<NumCategory; i++) |
| 4001 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 4002 | ObjCTypes.Int8PtrTy); |
| 4003 | llvm::Constant* Init = |
| 4004 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 4005 | NumCategory), |
| 4006 | Symbols); |
| 4007 | |
| 4008 | llvm::GlobalVariable *GV = |
| 4009 | new llvm::GlobalVariable(Init->getType(), false, |
| 4010 | llvm::GlobalValue::InternalLinkage, |
| 4011 | Init, |
| 4012 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 4013 | &CGM.getModule()); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 4014 | GV->setAlignment(8); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4015 | GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip"); |
| 4016 | UsedGlobals.push_back(GV); |
| 4017 | } |
| 4018 | |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4019 | // static int L_OBJC_IMAGE_INFO[2] = { 0, flags }; |
| 4020 | // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0 |
| 4021 | std::vector<llvm::Constant*> Values(2); |
| 4022 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0); |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4023 | unsigned int flags = 0; |
Fariborz Jahanian | 66a5c2c | 2009-02-24 23:34:44 +0000 | [diff] [blame] | 4024 | // FIXME: Fix and continue? |
| 4025 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 4026 | flags |= eImageInfo_GarbageCollected; |
| 4027 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 4028 | flags |= eImageInfo_GCOnly; |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4029 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4030 | llvm::Constant* Init = llvm::ConstantArray::get( |
| 4031 | llvm::ArrayType::get(ObjCTypes.IntTy, 2), |
| 4032 | Values); |
| 4033 | llvm::GlobalVariable *IMGV = |
| 4034 | new llvm::GlobalVariable(Init->getType(), false, |
| 4035 | llvm::GlobalValue::InternalLinkage, |
| 4036 | Init, |
| 4037 | "\01L_OBJC_IMAGE_INFO", |
| 4038 | &CGM.getModule()); |
| 4039 | IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); |
Daniel Dunbar | 325f758 | 2009-04-23 08:03:21 +0000 | [diff] [blame] | 4040 | IMGV->setConstant(true); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4041 | UsedGlobals.push_back(IMGV); |
| 4042 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4043 | std::vector<llvm::Constant*> Used; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4044 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4045 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
| 4046 | e = UsedGlobals.end(); i != e; ++i) { |
| 4047 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
| 4048 | } |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4049 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4050 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
| 4051 | llvm::GlobalValue *GV = |
| 4052 | new llvm::GlobalVariable(AT, false, |
| 4053 | llvm::GlobalValue::AppendingLinkage, |
| 4054 | llvm::ConstantArray::get(AT, Used), |
| 4055 | "llvm.used", |
| 4056 | &CGM.getModule()); |
| 4057 | |
| 4058 | GV->setSection("llvm.metadata"); |
| 4059 | |
| 4060 | } |
| 4061 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4062 | // Metadata flags |
| 4063 | enum MetaDataDlags { |
| 4064 | CLS = 0x0, |
| 4065 | CLS_META = 0x1, |
| 4066 | CLS_ROOT = 0x2, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4067 | OBJC2_CLS_HIDDEN = 0x10, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4068 | CLS_EXCEPTION = 0x20 |
| 4069 | }; |
| 4070 | /// BuildClassRoTInitializer - generate meta-data for: |
| 4071 | /// struct _class_ro_t { |
| 4072 | /// uint32_t const flags; |
| 4073 | /// uint32_t const instanceStart; |
| 4074 | /// uint32_t const instanceSize; |
| 4075 | /// uint32_t const reserved; // only when building for 64bit targets |
| 4076 | /// const uint8_t * const ivarLayout; |
| 4077 | /// const char *const name; |
| 4078 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4079 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4080 | /// const struct _ivar_list_t *const ivars; |
| 4081 | /// const uint8_t * const weakIvarLayout; |
| 4082 | /// const struct _prop_list_t * const properties; |
| 4083 | /// } |
| 4084 | /// |
| 4085 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
| 4086 | unsigned flags, |
| 4087 | unsigned InstanceStart, |
| 4088 | unsigned InstanceSize, |
| 4089 | const ObjCImplementationDecl *ID) { |
| 4090 | std::string ClassName = ID->getNameAsString(); |
| 4091 | std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets! |
| 4092 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 4093 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 4094 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
| 4095 | // FIXME. For 64bit targets add 0 here. |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 4096 | Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4097 | : BuildIvarLayout(ID, true); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4098 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4099 | // const struct _method_list_t * const baseMethods; |
| 4100 | std::vector<llvm::Constant*> Methods; |
| 4101 | std::string MethodListName("\01l_OBJC_$_"); |
| 4102 | if (flags & CLS_META) { |
| 4103 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4104 | for (ObjCImplementationDecl::classmeth_iterator |
| 4105 | i = ID->classmeth_begin(CGM.getContext()), |
| 4106 | e = ID->classmeth_end(CGM.getContext()); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4107 | // Class methods should always be defined. |
| 4108 | Methods.push_back(GetMethodConstant(*i)); |
| 4109 | } |
| 4110 | } else { |
| 4111 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4112 | for (ObjCImplementationDecl::instmeth_iterator |
| 4113 | i = ID->instmeth_begin(CGM.getContext()), |
| 4114 | e = ID->instmeth_end(CGM.getContext()); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4115 | // Instance methods should always be defined. |
| 4116 | Methods.push_back(GetMethodConstant(*i)); |
| 4117 | } |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4118 | for (ObjCImplementationDecl::propimpl_iterator |
| 4119 | i = ID->propimpl_begin(CGM.getContext()), |
| 4120 | e = ID->propimpl_end(CGM.getContext()); i != e; ++i) { |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4121 | ObjCPropertyImplDecl *PID = *i; |
| 4122 | |
| 4123 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 4124 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 4125 | |
| 4126 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 4127 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4128 | Methods.push_back(C); |
| 4129 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 4130 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4131 | Methods.push_back(C); |
| 4132 | } |
| 4133 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4134 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4135 | Values[ 5] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4136 | "__DATA, __objc_const", Methods); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4137 | |
| 4138 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4139 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
| 4140 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
| 4141 | + OID->getNameAsString(), |
| 4142 | OID->protocol_begin(), |
| 4143 | OID->protocol_end()); |
| 4144 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4145 | if (flags & CLS_META) |
| 4146 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4147 | else |
| 4148 | Values[ 7] = EmitIvarList(ID); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 4149 | Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4150 | : BuildIvarLayout(ID, false); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4151 | if (flags & CLS_META) |
| 4152 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4153 | else |
| 4154 | Values[ 9] = |
| 4155 | EmitPropertyList( |
| 4156 | "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
| 4157 | ID, ID->getClassInterface(), ObjCTypes); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4158 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
| 4159 | Values); |
| 4160 | llvm::GlobalVariable *CLASS_RO_GV = |
| 4161 | new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false, |
| 4162 | llvm::GlobalValue::InternalLinkage, |
| 4163 | Init, |
| 4164 | (flags & CLS_META) ? |
| 4165 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 4166 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName, |
| 4167 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4168 | CLASS_RO_GV->setAlignment( |
| 4169 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4170 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4171 | return CLASS_RO_GV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4172 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4173 | } |
| 4174 | |
| 4175 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 4176 | /// for the given ClassName for: |
| 4177 | /// struct _class_t { |
| 4178 | /// struct _class_t *isa; |
| 4179 | /// struct _class_t * const superclass; |
| 4180 | /// void *cache; |
| 4181 | /// IMP *vtable; |
| 4182 | /// struct class_ro_t *ro; |
| 4183 | /// } |
| 4184 | /// |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4185 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
| 4186 | std::string &ClassName, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4187 | llvm::Constant *IsAGV, |
| 4188 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4189 | llvm::Constant *ClassRoGV, |
| 4190 | bool HiddenVisibility) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4191 | std::vector<llvm::Constant*> Values(5); |
| 4192 | Values[0] = IsAGV; |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4193 | Values[1] = SuperClassGV |
| 4194 | ? SuperClassGV |
| 4195 | : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4196 | Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar |
| 4197 | Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar |
| 4198 | Values[4] = ClassRoGV; // &CLASS_RO_GV |
| 4199 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
| 4200 | Values); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4201 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 4202 | GV->setInitializer(Init); |
Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 4203 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4204 | GV->setAlignment( |
| 4205 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4206 | if (HiddenVisibility) |
| 4207 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4208 | return GV; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4209 | } |
| 4210 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4211 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4212 | uint32_t &InstanceStart, |
| 4213 | uint32_t &InstanceSize) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4214 | // Find first and last (non-padding) ivars in this interface. |
| 4215 | |
| 4216 | // FIXME: Use iterator. |
| 4217 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4218 | GetNamedIvarList(OID->getClassInterface(), OIvars); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4219 | |
| 4220 | if (OIvars.empty()) { |
| 4221 | InstanceStart = InstanceSize = 0; |
| 4222 | return; |
Daniel Dunbar | d4ae6c0 | 2009-04-22 04:39:47 +0000 | [diff] [blame] | 4223 | } |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4224 | |
| 4225 | const ObjCIvarDecl *First = OIvars.front(); |
| 4226 | const ObjCIvarDecl *Last = OIvars.back(); |
| 4227 | |
| 4228 | InstanceStart = ComputeIvarBaseOffset(CGM, OID, First); |
| 4229 | const llvm::Type *FieldTy = |
| 4230 | CGM.getTypes().ConvertTypeForMem(Last->getType()); |
| 4231 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 4232 | // FIXME. This breaks compatibility with llvm-gcc-4.2 (but makes it compatible |
| 4233 | // with gcc-4.2). We postpone this for now. |
| 4234 | #if 0 |
| 4235 | if (Last->isBitField()) { |
| 4236 | Expr *BitWidth = Last->getBitWidth(); |
| 4237 | uint64_t BitFieldSize = |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 4238 | BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue(); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 4239 | Size = (BitFieldSize / 8) + ((BitFieldSize % 8) != 0); |
| 4240 | } |
| 4241 | #endif |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4242 | InstanceSize = ComputeIvarBaseOffset(CGM, OID, Last) + Size; |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4243 | } |
| 4244 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4245 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 4246 | std::string ClassName = ID->getNameAsString(); |
| 4247 | if (!ObjCEmptyCacheVar) { |
| 4248 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4249 | ObjCTypes.CacheTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4250 | false, |
| 4251 | llvm::GlobalValue::ExternalLinkage, |
| 4252 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4253 | "_objc_empty_cache", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4254 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4255 | |
| 4256 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4257 | ObjCTypes.ImpnfABITy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4258 | false, |
| 4259 | llvm::GlobalValue::ExternalLinkage, |
| 4260 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4261 | "_objc_empty_vtable", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4262 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4263 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4264 | assert(ID->getClassInterface() && |
| 4265 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 4266 | // FIXME: Is this correct (that meta class size is never computed)? |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4267 | uint32_t InstanceStart = |
| 4268 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy); |
| 4269 | uint32_t InstanceSize = InstanceStart; |
| 4270 | uint32_t flags = CLS_META; |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4271 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 4272 | std::string ObjCClassName(getClassSymbolPrefix()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4273 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4274 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4275 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4276 | bool classIsHidden = |
| 4277 | CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4278 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4279 | flags |= OBJC2_CLS_HIDDEN; |
| 4280 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4281 | // class is root |
| 4282 | flags |= CLS_ROOT; |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4283 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4284 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4285 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4286 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4287 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 4288 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 4289 | Root = Super; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4290 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4291 | // work on super class metadata symbol. |
| 4292 | std::string SuperClassName = |
| 4293 | ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4294 | SuperClassGV = GetClassGlobal(SuperClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4295 | } |
| 4296 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 4297 | InstanceStart, |
| 4298 | InstanceSize,ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4299 | std::string TClassName = ObjCMetaClassName + ClassName; |
| 4300 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4301 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 4302 | classIsHidden); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4303 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4304 | // Metadata for the class |
| 4305 | flags = CLS; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4306 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4307 | flags |= OBJC2_CLS_HIDDEN; |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4308 | |
| 4309 | if (hasObjCExceptionAttribute(ID->getClassInterface())) |
| 4310 | flags |= CLS_EXCEPTION; |
| 4311 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4312 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4313 | flags |= CLS_ROOT; |
| 4314 | SuperClassGV = 0; |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 4315 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4316 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4317 | std::string RootClassName = |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4318 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4319 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4320 | } |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4321 | GetClassSizeInfo(ID, InstanceStart, InstanceSize); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4322 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4323 | InstanceStart, |
| 4324 | InstanceSize, |
| 4325 | ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4326 | |
| 4327 | TClassName = ObjCClassName + ClassName; |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4328 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4329 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 4330 | classIsHidden); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4331 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4332 | |
| 4333 | // Force the definition of the EHType if necessary. |
| 4334 | if (flags & CLS_EXCEPTION) |
| 4335 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4336 | } |
| 4337 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4338 | /// GenerateProtocolRef - This routine is called to generate code for |
| 4339 | /// a protocol reference expression; as in: |
| 4340 | /// @code |
| 4341 | /// @protocol(Proto1); |
| 4342 | /// @endcode |
| 4343 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 4344 | /// which will hold address of the protocol meta-data. |
| 4345 | /// |
| 4346 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
| 4347 | const ObjCProtocolDecl *PD) { |
| 4348 | |
Fariborz Jahanian | 960cd06 | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 4349 | // This routine is called for @protocol only. So, we must build definition |
| 4350 | // of protocol's meta-data (not a reference to it!) |
| 4351 | // |
| 4352 | llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4353 | ObjCTypes.ExternalProtocolPtrTy); |
| 4354 | |
| 4355 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
| 4356 | ProtocolName += PD->getNameAsCString(); |
| 4357 | |
| 4358 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 4359 | if (PTGV) |
| 4360 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4361 | PTGV = new llvm::GlobalVariable( |
| 4362 | Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4363 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4364 | Init, |
| 4365 | ProtocolName, |
| 4366 | &CGM.getModule()); |
| 4367 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 4368 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4369 | UsedGlobals.push_back(PTGV); |
| 4370 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4371 | } |
| 4372 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4373 | /// GenerateCategory - Build metadata for a category implementation. |
| 4374 | /// struct _category_t { |
| 4375 | /// const char * const name; |
| 4376 | /// struct _class_t *const cls; |
| 4377 | /// const struct _method_list_t * const instance_methods; |
| 4378 | /// const struct _method_list_t * const class_methods; |
| 4379 | /// const struct _protocol_list_t * const protocols; |
| 4380 | /// const struct _prop_list_t * const properties; |
| 4381 | /// } |
| 4382 | /// |
| 4383 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) |
| 4384 | { |
| 4385 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4386 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
| 4387 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4388 | "_$_" + OCD->getNameAsString()); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4389 | std::string ExtClassName(getClassSymbolPrefix() + |
| 4390 | Interface->getNameAsString()); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4391 | |
| 4392 | std::vector<llvm::Constant*> Values(6); |
| 4393 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 4394 | // meta-class entry symbol |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4395 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4396 | Values[1] = ClassGV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4397 | std::vector<llvm::Constant*> Methods; |
| 4398 | std::string MethodListName(Prefix); |
| 4399 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
| 4400 | "_$_" + OCD->getNameAsString(); |
| 4401 | |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4402 | for (ObjCCategoryImplDecl::instmeth_iterator |
| 4403 | i = OCD->instmeth_begin(CGM.getContext()), |
| 4404 | e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4405 | // Instance methods should always be defined. |
| 4406 | Methods.push_back(GetMethodConstant(*i)); |
| 4407 | } |
| 4408 | |
| 4409 | Values[2] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4410 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4411 | Methods); |
| 4412 | |
| 4413 | MethodListName = Prefix; |
| 4414 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 4415 | OCD->getNameAsString(); |
| 4416 | Methods.clear(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4417 | for (ObjCCategoryImplDecl::classmeth_iterator |
| 4418 | i = OCD->classmeth_begin(CGM.getContext()), |
| 4419 | e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4420 | // Class methods should always be defined. |
| 4421 | Methods.push_back(GetMethodConstant(*i)); |
| 4422 | } |
| 4423 | |
| 4424 | Values[3] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4425 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4426 | Methods); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4427 | const ObjCCategoryDecl *Category = |
| 4428 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4429 | if (Category) { |
| 4430 | std::string ExtName(Interface->getNameAsString() + "_$_" + |
| 4431 | OCD->getNameAsString()); |
| 4432 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
| 4433 | + Interface->getNameAsString() + "_$_" |
| 4434 | + Category->getNameAsString(), |
| 4435 | Category->protocol_begin(), |
| 4436 | Category->protocol_end()); |
| 4437 | Values[5] = |
| 4438 | EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
| 4439 | OCD, Category, ObjCTypes); |
| 4440 | } |
| 4441 | else { |
| 4442 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4443 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4444 | } |
| 4445 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4446 | llvm::Constant *Init = |
| 4447 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
| 4448 | Values); |
| 4449 | llvm::GlobalVariable *GCATV |
| 4450 | = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy, |
| 4451 | false, |
| 4452 | llvm::GlobalValue::InternalLinkage, |
| 4453 | Init, |
| 4454 | ExtCatName, |
| 4455 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4456 | GCATV->setAlignment( |
| 4457 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4458 | GCATV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4459 | UsedGlobals.push_back(GCATV); |
| 4460 | DefinedCategories.push_back(GCATV); |
| 4461 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4462 | |
| 4463 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 4464 | /// given method if it has been defined. The result is null if the |
| 4465 | /// method has not been defined. The return value has type MethodPtrTy. |
| 4466 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
| 4467 | const ObjCMethodDecl *MD) { |
| 4468 | // FIXME: Use DenseMap::lookup |
| 4469 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 4470 | if (!Fn) |
| 4471 | return 0; |
| 4472 | |
| 4473 | std::vector<llvm::Constant*> Method(3); |
| 4474 | Method[0] = |
| 4475 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4476 | ObjCTypes.SelectorPtrTy); |
| 4477 | Method[1] = GetMethodVarType(MD); |
| 4478 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 4479 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 4480 | } |
| 4481 | |
| 4482 | /// EmitMethodList - Build meta-data for method declarations |
| 4483 | /// struct _method_list_t { |
| 4484 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 4485 | /// uint32_t method_count; |
| 4486 | /// struct _objc_method method_list[method_count]; |
| 4487 | /// } |
| 4488 | /// |
| 4489 | llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList( |
| 4490 | const std::string &Name, |
| 4491 | const char *Section, |
| 4492 | const ConstantVector &Methods) { |
| 4493 | // Return null for empty list. |
| 4494 | if (Methods.empty()) |
| 4495 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
| 4496 | |
| 4497 | std::vector<llvm::Constant*> Values(3); |
| 4498 | // sizeof(struct _objc_method) |
| 4499 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy); |
| 4500 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4501 | // method_count |
| 4502 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 4503 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 4504 | Methods.size()); |
| 4505 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 4506 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4507 | |
| 4508 | llvm::GlobalVariable *GV = |
| 4509 | new llvm::GlobalVariable(Init->getType(), false, |
| 4510 | llvm::GlobalValue::InternalLinkage, |
| 4511 | Init, |
| 4512 | Name, |
| 4513 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4514 | GV->setAlignment( |
| 4515 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4516 | GV->setSection(Section); |
| 4517 | UsedGlobals.push_back(GV); |
| 4518 | return llvm::ConstantExpr::getBitCast(GV, |
| 4519 | ObjCTypes.MethodListnfABIPtrTy); |
| 4520 | } |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4521 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4522 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 4523 | /// the given ivar. |
| 4524 | /// |
| 4525 | llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 4526 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4527 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 4528 | std::string Name = "OBJC_IVAR_$_" + |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4529 | getInterfaceDeclForIvar(ID, Ivar, CGM.getContext())->getNameAsString() + |
| 4530 | '.' + Ivar->getNameAsString(); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4531 | llvm::GlobalVariable *IvarOffsetGV = |
| 4532 | CGM.getModule().getGlobalVariable(Name); |
| 4533 | if (!IvarOffsetGV) |
| 4534 | IvarOffsetGV = |
| 4535 | new llvm::GlobalVariable(ObjCTypes.LongTy, |
| 4536 | false, |
| 4537 | llvm::GlobalValue::ExternalLinkage, |
| 4538 | 0, |
| 4539 | Name, |
| 4540 | &CGM.getModule()); |
| 4541 | return IvarOffsetGV; |
| 4542 | } |
| 4543 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4544 | llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar( |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4545 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4546 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4547 | unsigned long int Offset) { |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4548 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
| 4549 | IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy, |
| 4550 | Offset)); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4551 | IvarOffsetGV->setAlignment( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4552 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy)); |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4553 | |
| 4554 | // FIXME: This matches gcc, but shouldn't the visibility be set on |
| 4555 | // the use as well (i.e., in ObjCIvarOffsetVariable). |
| 4556 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 4557 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
| 4558 | CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden) |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4559 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4560 | else |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4561 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4562 | IvarOffsetGV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4563 | return IvarOffsetGV; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4564 | } |
| 4565 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4566 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 4567 | /// implementation. The return value has type |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4568 | /// IvarListnfABIPtrTy. |
| 4569 | /// struct _ivar_t { |
| 4570 | /// unsigned long int *offset; // pointer to ivar offset location |
| 4571 | /// char *name; |
| 4572 | /// char *type; |
| 4573 | /// uint32_t alignment; |
| 4574 | /// uint32_t size; |
| 4575 | /// } |
| 4576 | /// struct _ivar_list_t { |
| 4577 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 4578 | /// uint32 count; |
| 4579 | /// struct _iver_t list[count]; |
| 4580 | /// } |
| 4581 | /// |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4582 | |
| 4583 | void CGObjCCommonMac::GetNamedIvarList(const ObjCInterfaceDecl *OID, |
| 4584 | llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const { |
| 4585 | for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), |
| 4586 | E = OID->ivar_end(); I != E; ++I) { |
| 4587 | // Ignore unnamed bit-fields. |
| 4588 | if (!(*I)->getDeclName()) |
| 4589 | continue; |
| 4590 | |
| 4591 | Res.push_back(*I); |
| 4592 | } |
| 4593 | |
| 4594 | for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(CGM.getContext()), |
| 4595 | E = OID->prop_end(CGM.getContext()); I != E; ++I) |
| 4596 | if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl()) |
| 4597 | Res.push_back(IV); |
| 4598 | } |
| 4599 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4600 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
| 4601 | const ObjCImplementationDecl *ID) { |
| 4602 | |
| 4603 | std::vector<llvm::Constant*> Ivars, Ivar(5); |
| 4604 | |
| 4605 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4606 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
| 4607 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4608 | // FIXME. Consolidate this with similar code in GenerateClass. |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 4609 | |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4610 | // Collect declared and synthesized ivars in a small vector. |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4611 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4612 | GetNamedIvarList(OID, OIvars); |
Fariborz Jahanian | 99eee36 | 2009-04-01 19:37:34 +0000 | [diff] [blame] | 4613 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4614 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 4615 | ObjCIvarDecl *IVD = OIvars[i]; |
Daniel Dunbar | 3eec8aa | 2009-04-20 05:53:40 +0000 | [diff] [blame] | 4616 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD, |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4617 | ComputeIvarBaseOffset(CGM, ID, IVD)); |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4618 | Ivar[1] = GetMethodVarName(IVD->getIdentifier()); |
| 4619 | Ivar[2] = GetMethodVarType(IVD); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4620 | const llvm::Type *FieldTy = |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4621 | CGM.getTypes().ConvertTypeForMem(IVD->getType()); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4622 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
| 4623 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4624 | IVD->getType().getTypePtr()) >> 3; |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4625 | Align = llvm::Log2_32(Align); |
| 4626 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4627 | // NOTE. Size of a bitfield does not match gcc's, because of the |
| 4628 | // way bitfields are treated special in each. But I am told that |
| 4629 | // 'size' for bitfield ivars is ignored by the runtime so it does |
| 4630 | // not matter. If it matters, there is enough info to get the |
| 4631 | // bitfield right! |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4632 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4633 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
| 4634 | } |
| 4635 | // Return null for empty list. |
| 4636 | if (Ivars.empty()) |
| 4637 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4638 | std::vector<llvm::Constant*> Values(3); |
| 4639 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy); |
| 4640 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4641 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 4642 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
| 4643 | Ivars.size()); |
| 4644 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
| 4645 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4646 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 4647 | llvm::GlobalVariable *GV = |
| 4648 | new llvm::GlobalVariable(Init->getType(), false, |
| 4649 | llvm::GlobalValue::InternalLinkage, |
| 4650 | Init, |
| 4651 | Prefix + OID->getNameAsString(), |
| 4652 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4653 | GV->setAlignment( |
| 4654 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4655 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4656 | |
| 4657 | UsedGlobals.push_back(GV); |
| 4658 | return llvm::ConstantExpr::getBitCast(GV, |
| 4659 | ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4660 | } |
| 4661 | |
| 4662 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
| 4663 | const ObjCProtocolDecl *PD) { |
| 4664 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4665 | |
| 4666 | if (!Entry) { |
| 4667 | // We use the initializer as a marker of whether this is a forward |
| 4668 | // reference or not. At module finalization we add the empty |
| 4669 | // contents for protocols which were referenced but never defined. |
| 4670 | Entry = |
| 4671 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
| 4672 | llvm::GlobalValue::ExternalLinkage, |
| 4673 | 0, |
| 4674 | "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(), |
| 4675 | &CGM.getModule()); |
| 4676 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 4677 | UsedGlobals.push_back(Entry); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4678 | } |
| 4679 | |
| 4680 | return Entry; |
| 4681 | } |
| 4682 | |
| 4683 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 4684 | /// @code |
| 4685 | /// struct _protocol_t { |
| 4686 | /// id isa; // NULL |
| 4687 | /// const char * const protocol_name; |
| 4688 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 4689 | /// const struct method_list_t * const instance_methods; |
| 4690 | /// const struct method_list_t * const class_methods; |
| 4691 | /// const struct method_list_t *optionalInstanceMethods; |
| 4692 | /// const struct method_list_t *optionalClassMethods; |
| 4693 | /// const struct _prop_list_t * properties; |
| 4694 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 4695 | /// const uint32_t flags; // = 0 |
| 4696 | /// } |
| 4697 | /// @endcode |
| 4698 | /// |
| 4699 | |
| 4700 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
| 4701 | const ObjCProtocolDecl *PD) { |
| 4702 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4703 | |
| 4704 | // Early exit if a defining object has already been generated. |
| 4705 | if (Entry && Entry->hasInitializer()) |
| 4706 | return Entry; |
| 4707 | |
| 4708 | const char *ProtocolName = PD->getNameAsCString(); |
| 4709 | |
| 4710 | // Construct method lists. |
| 4711 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 4712 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4713 | for (ObjCProtocolDecl::instmeth_iterator |
| 4714 | i = PD->instmeth_begin(CGM.getContext()), |
| 4715 | e = PD->instmeth_end(CGM.getContext()); |
| 4716 | i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4717 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4718 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4719 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4720 | OptInstanceMethods.push_back(C); |
| 4721 | } else { |
| 4722 | InstanceMethods.push_back(C); |
| 4723 | } |
| 4724 | } |
| 4725 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4726 | for (ObjCProtocolDecl::classmeth_iterator |
| 4727 | i = PD->classmeth_begin(CGM.getContext()), |
| 4728 | e = PD->classmeth_end(CGM.getContext()); |
| 4729 | i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4730 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4731 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4732 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4733 | OptClassMethods.push_back(C); |
| 4734 | } else { |
| 4735 | ClassMethods.push_back(C); |
| 4736 | } |
| 4737 | } |
| 4738 | |
| 4739 | std::vector<llvm::Constant*> Values(10); |
| 4740 | // isa is NULL |
| 4741 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
| 4742 | Values[1] = GetClassName(PD->getIdentifier()); |
| 4743 | Values[2] = EmitProtocolList( |
| 4744 | "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(), |
| 4745 | PD->protocol_begin(), |
| 4746 | PD->protocol_end()); |
| 4747 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4748 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4749 | + PD->getNameAsString(), |
| 4750 | "__DATA, __objc_const", |
| 4751 | InstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4752 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4753 | + PD->getNameAsString(), |
| 4754 | "__DATA, __objc_const", |
| 4755 | ClassMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4756 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4757 | + PD->getNameAsString(), |
| 4758 | "__DATA, __objc_const", |
| 4759 | OptInstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4760 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4761 | + PD->getNameAsString(), |
| 4762 | "__DATA, __objc_const", |
| 4763 | OptClassMethods); |
| 4764 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(), |
| 4765 | 0, PD, ObjCTypes); |
| 4766 | uint32_t Size = |
| 4767 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy); |
| 4768 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4769 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
| 4770 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
| 4771 | Values); |
| 4772 | |
| 4773 | if (Entry) { |
| 4774 | // Already created, fix the linkage and update the initializer. |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4775 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4776 | Entry->setInitializer(Init); |
| 4777 | } else { |
| 4778 | Entry = |
| 4779 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4780 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4781 | Init, |
| 4782 | std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName, |
| 4783 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4784 | Entry->setAlignment( |
| 4785 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4786 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4787 | } |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4788 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4789 | |
| 4790 | // Use this protocol meta-data to build protocol list table in section |
| 4791 | // __DATA, __objc_protolist |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4792 | llvm::GlobalVariable *PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4793 | ObjCTypes.ProtocolnfABIPtrTy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4794 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4795 | Entry, |
| 4796 | std::string("\01l_OBJC_LABEL_PROTOCOL_$_") |
| 4797 | +ProtocolName, |
| 4798 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4799 | PTGV->setAlignment( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4800 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 4801 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4802 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4803 | UsedGlobals.push_back(PTGV); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4804 | return Entry; |
| 4805 | } |
| 4806 | |
| 4807 | /// EmitProtocolList - Generate protocol list meta-data: |
| 4808 | /// @code |
| 4809 | /// struct _protocol_list_t { |
| 4810 | /// long protocol_count; // Note, this is 32/64 bit |
| 4811 | /// struct _protocol_t[protocol_count]; |
| 4812 | /// } |
| 4813 | /// @endcode |
| 4814 | /// |
| 4815 | llvm::Constant * |
| 4816 | CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name, |
| 4817 | ObjCProtocolDecl::protocol_iterator begin, |
| 4818 | ObjCProtocolDecl::protocol_iterator end) { |
| 4819 | std::vector<llvm::Constant*> ProtocolRefs; |
| 4820 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4821 | // Just return null for empty protocol lists |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4822 | if (begin == end) |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4823 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4824 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4825 | // FIXME: We shouldn't need to do this lookup here, should we? |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4826 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
| 4827 | if (GV) |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4828 | return llvm::ConstantExpr::getBitCast(GV, |
| 4829 | ObjCTypes.ProtocolListnfABIPtrTy); |
| 4830 | |
| 4831 | for (; begin != end; ++begin) |
| 4832 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 4833 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4834 | // This list is null terminated. |
| 4835 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4836 | ObjCTypes.ProtocolnfABIPtrTy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4837 | |
| 4838 | std::vector<llvm::Constant*> Values(2); |
| 4839 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 4840 | Values[1] = |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4841 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4842 | ProtocolRefs.size()), |
| 4843 | ProtocolRefs); |
| 4844 | |
| 4845 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4846 | GV = new llvm::GlobalVariable(Init->getType(), false, |
| 4847 | llvm::GlobalValue::InternalLinkage, |
| 4848 | Init, |
| 4849 | Name, |
| 4850 | &CGM.getModule()); |
| 4851 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4852 | GV->setAlignment( |
| 4853 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4854 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4855 | return llvm::ConstantExpr::getBitCast(GV, |
| 4856 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4857 | } |
| 4858 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4859 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 4860 | /// struct _objc_method { |
| 4861 | /// SEL _cmd; |
| 4862 | /// char *method_type; |
| 4863 | /// char *_imp; |
| 4864 | /// } |
| 4865 | |
| 4866 | llvm::Constant * |
| 4867 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 4868 | std::vector<llvm::Constant*> Desc(3); |
| 4869 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4870 | ObjCTypes.SelectorPtrTy); |
| 4871 | Desc[1] = GetMethodVarType(MD); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4872 | // Protocol methods have no implementation. So, this entry is always NULL. |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4873 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 4874 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
| 4875 | } |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4876 | |
| 4877 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 4878 | /// This code gen. amounts to generating code for: |
| 4879 | /// @code |
| 4880 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 4881 | /// @encode |
| 4882 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4883 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4884 | CodeGen::CodeGenFunction &CGF, |
| 4885 | QualType ObjectTy, |
| 4886 | llvm::Value *BaseValue, |
| 4887 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4888 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 4889 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4890 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 4891 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4892 | } |
| 4893 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4894 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
| 4895 | CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 4896 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4897 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 4898 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar), |
| 4899 | false, "ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4900 | } |
| 4901 | |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4902 | CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend( |
| 4903 | CodeGen::CodeGenFunction &CGF, |
| 4904 | QualType ResultType, |
| 4905 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4906 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4907 | QualType Arg0Ty, |
| 4908 | bool IsSuper, |
| 4909 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4910 | // FIXME. Even though IsSuper is passes. This function doese not |
| 4911 | // handle calls to 'super' receivers. |
| 4912 | CodeGenTypes &Types = CGM.getTypes(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4913 | llvm::Value *Arg0 = Receiver; |
| 4914 | if (!IsSuper) |
| 4915 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4916 | |
| 4917 | // Find the message function name. |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 4918 | // FIXME. This is too much work to get the ABI-specific result type |
| 4919 | // needed to find the message name. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4920 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, |
| 4921 | llvm::SmallVector<QualType, 16>()); |
Fariborz Jahanian | 70b51c7 | 2009-04-30 23:08:58 +0000 | [diff] [blame] | 4922 | llvm::Constant *Fn = 0; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4923 | std::string Name("\01l_"); |
| 4924 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4925 | #if 0 |
| 4926 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4927 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4928 | Fn = ObjCTypes.getMessageSendIdStretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4929 | // FIXME. Is there a better way of getting these names. |
| 4930 | // They are available in RuntimeFunctions vector pair. |
| 4931 | Name += "objc_msgSendId_stret_fixup"; |
| 4932 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4933 | else |
| 4934 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4935 | if (IsSuper) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4936 | Fn = ObjCTypes.getMessageSendSuper2StretFixupFn(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4937 | Name += "objc_msgSendSuper2_stret_fixup"; |
| 4938 | } |
| 4939 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4940 | { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4941 | Fn = ObjCTypes.getMessageSendStretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4942 | Name += "objc_msgSend_stret_fixup"; |
| 4943 | } |
| 4944 | } |
Fariborz Jahanian | 5b2bad0 | 2009-04-30 16:31:11 +0000 | [diff] [blame] | 4945 | else if (!IsSuper && ResultType->isFloatingType()) { |
| 4946 | if (const BuiltinType *BT = ResultType->getAsBuiltinType()) { |
| 4947 | BuiltinType::Kind k = BT->getKind(); |
| 4948 | if (k == BuiltinType::LongDouble) { |
| 4949 | Fn = ObjCTypes.getMessageSendFpretFixupFn(); |
| 4950 | Name += "objc_msgSend_fpret_fixup"; |
| 4951 | } |
| 4952 | else { |
| 4953 | Fn = ObjCTypes.getMessageSendFixupFn(); |
| 4954 | Name += "objc_msgSend_fixup"; |
| 4955 | } |
| 4956 | } |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4957 | } |
| 4958 | else { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4959 | #if 0 |
| 4960 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4961 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4962 | Fn = ObjCTypes.getMessageSendIdFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4963 | Name += "objc_msgSendId_fixup"; |
| 4964 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4965 | else |
| 4966 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4967 | if (IsSuper) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4968 | Fn = ObjCTypes.getMessageSendSuper2FixupFn(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4969 | Name += "objc_msgSendSuper2_fixup"; |
| 4970 | } |
| 4971 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4972 | { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4973 | Fn = ObjCTypes.getMessageSendFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4974 | Name += "objc_msgSend_fixup"; |
| 4975 | } |
| 4976 | } |
Fariborz Jahanian | 70b51c7 | 2009-04-30 23:08:58 +0000 | [diff] [blame] | 4977 | assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4978 | Name += '_'; |
| 4979 | std::string SelName(Sel.getAsString()); |
| 4980 | // Replace all ':' in selector name with '_' ouch! |
| 4981 | for(unsigned i = 0; i < SelName.size(); i++) |
| 4982 | if (SelName[i] == ':') |
| 4983 | SelName[i] = '_'; |
| 4984 | Name += SelName; |
| 4985 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 4986 | if (!GV) { |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 4987 | // Build message ref table entry. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4988 | std::vector<llvm::Constant*> Values(2); |
| 4989 | Values[0] = Fn; |
| 4990 | Values[1] = GetMethodVarName(Sel); |
| 4991 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4992 | GV = new llvm::GlobalVariable(Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4993 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4994 | Init, |
| 4995 | Name, |
| 4996 | &CGM.getModule()); |
| 4997 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | f59c1a6 | 2009-04-15 19:04:46 +0000 | [diff] [blame] | 4998 | GV->setAlignment(16); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4999 | GV->setSection("__DATA, __objc_msgrefs, coalesced"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5000 | } |
| 5001 | llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5002 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5003 | CallArgList ActualArgs; |
| 5004 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 5005 | ActualArgs.push_back(std::make_pair(RValue::get(Arg1), |
| 5006 | ObjCTypes.MessageRefCPtrTy)); |
| 5007 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5008 | const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs); |
| 5009 | llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0); |
| 5010 | Callee = CGF.Builder.CreateLoad(Callee); |
Fariborz Jahanian | 3ab75bd | 2009-02-14 21:25:36 +0000 | [diff] [blame] | 5011 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5012 | Callee = CGF.Builder.CreateBitCast(Callee, |
| 5013 | llvm::PointerType::getUnqual(FTy)); |
| 5014 | return CGF.EmitCall(FnInfo1, Callee, ActualArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5015 | } |
| 5016 | |
| 5017 | /// Generate code for a message send expression in the nonfragile abi. |
| 5018 | CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend( |
| 5019 | CodeGen::CodeGenFunction &CGF, |
| 5020 | QualType ResultType, |
| 5021 | Selector Sel, |
| 5022 | llvm::Value *Receiver, |
| 5023 | bool IsClassMessage, |
| 5024 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5025 | return EmitMessageSend(CGF, ResultType, Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5026 | Receiver, CGF.getContext().getObjCIdType(), |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5027 | false, CallArgs); |
| 5028 | } |
| 5029 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5030 | llvm::GlobalVariable * |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5031 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5032 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5033 | |
Daniel Dunbar | dfff230 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 5034 | if (!GV) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5035 | GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 5036 | llvm::GlobalValue::ExternalLinkage, |
| 5037 | 0, Name, &CGM.getModule()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5038 | } |
| 5039 | |
| 5040 | return GV; |
| 5041 | } |
| 5042 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5043 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5044 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5045 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 5046 | |
| 5047 | if (!Entry) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5048 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5049 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5050 | Entry = |
| 5051 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5052 | llvm::GlobalValue::InternalLinkage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5053 | ClassGV, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5054 | "\01L_OBJC_CLASSLIST_REFERENCES_$_", |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5055 | &CGM.getModule()); |
| 5056 | Entry->setAlignment( |
| 5057 | CGM.getTargetData().getPrefTypeAlignment( |
| 5058 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5059 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
| 5060 | UsedGlobals.push_back(Entry); |
| 5061 | } |
| 5062 | |
| 5063 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5064 | } |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5065 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5066 | llvm::Value * |
| 5067 | CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder, |
| 5068 | const ObjCInterfaceDecl *ID) { |
| 5069 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
| 5070 | |
| 5071 | if (!Entry) { |
| 5072 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5073 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
| 5074 | Entry = |
| 5075 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5076 | llvm::GlobalValue::InternalLinkage, |
| 5077 | ClassGV, |
| 5078 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 5079 | &CGM.getModule()); |
| 5080 | Entry->setAlignment( |
| 5081 | CGM.getTargetData().getPrefTypeAlignment( |
| 5082 | ObjCTypes.ClassnfABIPtrTy)); |
| 5083 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5084 | UsedGlobals.push_back(Entry); |
| 5085 | } |
| 5086 | |
| 5087 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5088 | } |
| 5089 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5090 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 5091 | /// meta-data |
| 5092 | /// |
| 5093 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 5094 | const ObjCInterfaceDecl *ID) { |
| 5095 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 5096 | if (Entry) |
| 5097 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5098 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5099 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5100 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5101 | Entry = |
| 5102 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5103 | llvm::GlobalValue::InternalLinkage, |
| 5104 | MetaClassGV, |
| 5105 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 5106 | &CGM.getModule()); |
| 5107 | Entry->setAlignment( |
| 5108 | CGM.getTargetData().getPrefTypeAlignment( |
| 5109 | ObjCTypes.ClassnfABIPtrTy)); |
| 5110 | |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5111 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5112 | UsedGlobals.push_back(Entry); |
| 5113 | |
| 5114 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5115 | } |
| 5116 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5117 | /// GetClass - Return a reference to the class for the given interface |
| 5118 | /// decl. |
| 5119 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 5120 | const ObjCInterfaceDecl *ID) { |
| 5121 | return EmitClassRef(Builder, ID); |
| 5122 | } |
| 5123 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5124 | /// Generates a message send where the super is the receiver. This is |
| 5125 | /// a message send to self with special delivery semantics indicating |
| 5126 | /// which class's method should be called. |
| 5127 | CodeGen::RValue |
| 5128 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 5129 | QualType ResultType, |
| 5130 | Selector Sel, |
| 5131 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5132 | bool isCategoryImpl, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5133 | llvm::Value *Receiver, |
| 5134 | bool IsClassMessage, |
| 5135 | const CodeGen::CallArgList &CallArgs) { |
| 5136 | // ... |
| 5137 | // Create and init a super structure; this is a (receiver, class) |
| 5138 | // pair we will pass to objc_msgSendSuper. |
| 5139 | llvm::Value *ObjCSuper = |
| 5140 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 5141 | |
| 5142 | llvm::Value *ReceiverAsObject = |
| 5143 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 5144 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 5145 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
| 5146 | |
| 5147 | // 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] | 5148 | llvm::Value *Target; |
| 5149 | if (IsClassMessage) { |
| 5150 | if (isCategoryImpl) { |
| 5151 | // Message sent to "super' in a class method defined in |
| 5152 | // a category implementation. |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5153 | Target = EmitClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5154 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 5155 | Target = CGF.Builder.CreateLoad(Target); |
| 5156 | } |
| 5157 | else |
| 5158 | Target = EmitMetaClassRef(CGF.Builder, Class); |
| 5159 | } |
| 5160 | else |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5161 | Target = EmitSuperClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5162 | |
| 5163 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 5164 | // and ObjCTypes types. |
| 5165 | const llvm::Type *ClassTy = |
| 5166 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 5167 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 5168 | CGF.Builder.CreateStore(Target, |
| 5169 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 5170 | |
| 5171 | return EmitMessageSend(CGF, ResultType, Sel, |
| 5172 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 5173 | true, CallArgs); |
| 5174 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5175 | |
| 5176 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
| 5177 | Selector Sel) { |
| 5178 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 5179 | |
| 5180 | if (!Entry) { |
| 5181 | llvm::Constant *Casted = |
| 5182 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 5183 | ObjCTypes.SelectorPtrTy); |
| 5184 | Entry = |
| 5185 | new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false, |
| 5186 | llvm::GlobalValue::InternalLinkage, |
| 5187 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_", |
| 5188 | &CGM.getModule()); |
| 5189 | Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip"); |
| 5190 | UsedGlobals.push_back(Entry); |
| 5191 | } |
| 5192 | |
| 5193 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5194 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5195 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 5196 | /// objc_assign_ivar (id src, id *dst) |
| 5197 | /// |
| 5198 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 5199 | llvm::Value *src, llvm::Value *dst) |
| 5200 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5201 | const llvm::Type * SrcTy = src->getType(); |
| 5202 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5203 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5204 | assert(Size <= 8 && "does not support size > 8"); |
| 5205 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5206 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5207 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5208 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5209 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5210 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5211 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5212 | src, dst, "assignivar"); |
| 5213 | return; |
| 5214 | } |
| 5215 | |
| 5216 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 5217 | /// objc_assign_strongCast (id src, id *dst) |
| 5218 | /// |
| 5219 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
| 5220 | CodeGen::CodeGenFunction &CGF, |
| 5221 | llvm::Value *src, llvm::Value *dst) |
| 5222 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5223 | const llvm::Type * SrcTy = src->getType(); |
| 5224 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5225 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5226 | assert(Size <= 8 && "does not support size > 8"); |
| 5227 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5228 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5229 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5230 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5231 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5232 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5233 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5234 | src, dst, "weakassign"); |
| 5235 | return; |
| 5236 | } |
| 5237 | |
| 5238 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 5239 | /// object: objc_read_weak (id *src) |
| 5240 | /// |
| 5241 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
| 5242 | CodeGen::CodeGenFunction &CGF, |
| 5243 | llvm::Value *AddrWeakObj) |
| 5244 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5245 | const llvm::Type* DestTy = |
| 5246 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5247 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 5248 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5249 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5250 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5251 | return read_weak; |
| 5252 | } |
| 5253 | |
| 5254 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 5255 | /// objc_assign_weak (id src, id *dst) |
| 5256 | /// |
| 5257 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 5258 | llvm::Value *src, llvm::Value *dst) |
| 5259 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5260 | const llvm::Type * SrcTy = src->getType(); |
| 5261 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5262 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5263 | assert(Size <= 8 && "does not support size > 8"); |
| 5264 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5265 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5266 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5267 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5268 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5269 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 5270 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5271 | src, dst, "weakassign"); |
| 5272 | return; |
| 5273 | } |
| 5274 | |
| 5275 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 5276 | /// objc_assign_global (id src, id *dst) |
| 5277 | /// |
| 5278 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 5279 | llvm::Value *src, llvm::Value *dst) |
| 5280 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5281 | const llvm::Type * SrcTy = src->getType(); |
| 5282 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5283 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5284 | assert(Size <= 8 && "does not support size > 8"); |
| 5285 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5286 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5287 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5288 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5289 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5290 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5291 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5292 | src, dst, "globalassign"); |
| 5293 | return; |
| 5294 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5295 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5296 | void |
| 5297 | CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 5298 | const Stmt &S) { |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5299 | bool isTry = isa<ObjCAtTryStmt>(S); |
| 5300 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 5301 | llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5302 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5303 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5304 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5305 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
| 5306 | |
| 5307 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 5308 | // evaluation of the expression must occur before we enter the |
| 5309 | // @synchronized. We can safely avoid a temp here because jumps into |
| 5310 | // @synchronized are illegal & this will dominate uses. |
| 5311 | llvm::Value *SyncArg = 0; |
| 5312 | if (!isTry) { |
| 5313 | SyncArg = |
| 5314 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 5315 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5316 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5317 | } |
| 5318 | |
| 5319 | // Push an EH context entry, used for handling rethrows and jumps |
| 5320 | // through finally. |
| 5321 | CGF.PushCleanupBlock(FinallyBlock); |
| 5322 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5323 | CGF.setInvokeDest(TryHandler); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5324 | |
| 5325 | CGF.EmitBlock(TryBlock); |
| 5326 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 5327 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
| 5328 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5329 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5330 | // Emit the exception handler. |
| 5331 | |
| 5332 | CGF.EmitBlock(TryHandler); |
| 5333 | |
| 5334 | llvm::Value *llvm_eh_exception = |
| 5335 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
| 5336 | llvm::Value *llvm_eh_selector_i64 = |
| 5337 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64); |
| 5338 | llvm::Value *llvm_eh_typeid_for_i64 = |
| 5339 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64); |
| 5340 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5341 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow"); |
| 5342 | |
| 5343 | llvm::SmallVector<llvm::Value*, 8> SelectorArgs; |
| 5344 | SelectorArgs.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5345 | SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5346 | |
| 5347 | // Construct the lists of (type, catch body) to handle. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5348 | llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5349 | bool HasCatchAll = false; |
| 5350 | if (isTry) { |
| 5351 | if (const ObjCAtCatchStmt* CatchStmt = |
| 5352 | cast<ObjCAtTryStmt>(S).getCatchStmts()) { |
| 5353 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5354 | const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5355 | Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody())); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5356 | |
| 5357 | // catch(...) always matches. |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5358 | if (!CatchDecl) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5359 | // Use i8* null here to signal this is a catch all, not a cleanup. |
| 5360 | llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 5361 | SelectorArgs.push_back(Null); |
| 5362 | HasCatchAll = true; |
| 5363 | break; |
| 5364 | } |
| 5365 | |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5366 | if (CGF.getContext().isObjCIdType(CatchDecl->getType()) || |
| 5367 | CatchDecl->getType()->isObjCQualifiedIdType()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5368 | llvm::Value *IDEHType = |
| 5369 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 5370 | if (!IDEHType) |
| 5371 | IDEHType = |
| 5372 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5373 | llvm::GlobalValue::ExternalLinkage, |
| 5374 | 0, "OBJC_EHTYPE_id", &CGM.getModule()); |
| 5375 | SelectorArgs.push_back(IDEHType); |
| 5376 | HasCatchAll = true; |
| 5377 | break; |
| 5378 | } |
| 5379 | |
| 5380 | // All other types should be Objective-C interface pointer types. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5381 | const PointerType *PT = CatchDecl->getType()->getAsPointerType(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5382 | assert(PT && "Invalid @catch type."); |
| 5383 | const ObjCInterfaceType *IT = |
| 5384 | PT->getPointeeType()->getAsObjCInterfaceType(); |
| 5385 | assert(IT && "Invalid @catch type."); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5386 | llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5387 | SelectorArgs.push_back(EHType); |
| 5388 | } |
| 5389 | } |
| 5390 | } |
| 5391 | |
| 5392 | // We use a cleanup unless there was already a catch all. |
| 5393 | if (!HasCatchAll) { |
| 5394 | SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5395 | Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5396 | } |
| 5397 | |
| 5398 | llvm::Value *Selector = |
| 5399 | CGF.Builder.CreateCall(llvm_eh_selector_i64, |
| 5400 | SelectorArgs.begin(), SelectorArgs.end(), |
| 5401 | "selector"); |
| 5402 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5403 | const ParmVarDecl *CatchParam = Handlers[i].first; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5404 | const Stmt *CatchBody = Handlers[i].second; |
| 5405 | |
| 5406 | llvm::BasicBlock *Next = 0; |
| 5407 | |
| 5408 | // The last handler always matches. |
| 5409 | if (i + 1 != e) { |
| 5410 | assert(CatchParam && "Only last handler can be a catch all."); |
| 5411 | |
| 5412 | llvm::BasicBlock *Match = CGF.createBasicBlock("match"); |
| 5413 | Next = CGF.createBasicBlock("catch.next"); |
| 5414 | llvm::Value *Id = |
| 5415 | CGF.Builder.CreateCall(llvm_eh_typeid_for_i64, |
| 5416 | CGF.Builder.CreateBitCast(SelectorArgs[i+2], |
| 5417 | ObjCTypes.Int8PtrTy)); |
| 5418 | CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id), |
| 5419 | Match, Next); |
| 5420 | |
| 5421 | CGF.EmitBlock(Match); |
| 5422 | } |
| 5423 | |
| 5424 | if (CatchBody) { |
| 5425 | llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end"); |
| 5426 | llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler"); |
| 5427 | |
| 5428 | // Cleanups must call objc_end_catch. |
| 5429 | // |
| 5430 | // FIXME: It seems incorrect for objc_begin_catch to be inside |
| 5431 | // this context, but this matches gcc. |
| 5432 | CGF.PushCleanupBlock(MatchEnd); |
| 5433 | CGF.setInvokeDest(MatchHandler); |
| 5434 | |
| 5435 | llvm::Value *ExcObject = |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5436 | CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5437 | |
| 5438 | // Bind the catch parameter if it exists. |
| 5439 | if (CatchParam) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5440 | ExcObject = |
| 5441 | CGF.Builder.CreateBitCast(ExcObject, |
| 5442 | CGF.ConvertType(CatchParam->getType())); |
| 5443 | // CatchParam is a ParmVarDecl because of the grammar |
| 5444 | // construction used to handle this, but for codegen purposes |
| 5445 | // we treat this as a local decl. |
| 5446 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
| 5447 | CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5448 | } |
| 5449 | |
| 5450 | CGF.ObjCEHValueStack.push_back(ExcObject); |
| 5451 | CGF.EmitStmt(CatchBody); |
| 5452 | CGF.ObjCEHValueStack.pop_back(); |
| 5453 | |
| 5454 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5455 | |
| 5456 | CGF.EmitBlock(MatchHandler); |
| 5457 | |
| 5458 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5459 | // We are required to emit this call to satisfy LLVM, even |
| 5460 | // though we don't use the result. |
| 5461 | llvm::SmallVector<llvm::Value*, 8> Args; |
| 5462 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5463 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5464 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5465 | 0)); |
| 5466 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5467 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5468 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5469 | |
| 5470 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5471 | |
| 5472 | CGF.EmitBlock(MatchEnd); |
| 5473 | |
| 5474 | // Unfortunately, we also have to generate another EH frame here |
| 5475 | // in case this throws. |
| 5476 | llvm::BasicBlock *MatchEndHandler = |
| 5477 | CGF.createBasicBlock("match.end.handler"); |
| 5478 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5479 | CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5480 | Cont, MatchEndHandler, |
| 5481 | Args.begin(), Args.begin()); |
| 5482 | |
| 5483 | CGF.EmitBlock(Cont); |
| 5484 | if (Info.SwitchBlock) |
| 5485 | CGF.EmitBlock(Info.SwitchBlock); |
| 5486 | if (Info.EndBlock) |
| 5487 | CGF.EmitBlock(Info.EndBlock); |
| 5488 | |
| 5489 | CGF.EmitBlock(MatchEndHandler); |
| 5490 | Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5491 | // We are required to emit this call to satisfy LLVM, even |
| 5492 | // though we don't use the result. |
| 5493 | Args.clear(); |
| 5494 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5495 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5496 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5497 | 0)); |
| 5498 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5499 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5500 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5501 | |
| 5502 | if (Next) |
| 5503 | CGF.EmitBlock(Next); |
| 5504 | } else { |
| 5505 | assert(!Next && "catchup should be last handler."); |
| 5506 | |
| 5507 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5508 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5509 | } |
| 5510 | } |
| 5511 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5512 | // Pop the cleanup entry, the @finally is outside this cleanup |
| 5513 | // scope. |
| 5514 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5515 | CGF.setInvokeDest(PrevLandingPad); |
| 5516 | |
| 5517 | CGF.EmitBlock(FinallyBlock); |
| 5518 | |
| 5519 | if (isTry) { |
| 5520 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 5521 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 5522 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 5523 | } else { |
| 5524 | // Emit 'objc_sync_exit(expr)' as finally's sole statement for |
| 5525 | // @synchronized. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5526 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5527 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5528 | |
| 5529 | if (Info.SwitchBlock) |
| 5530 | CGF.EmitBlock(Info.SwitchBlock); |
| 5531 | if (Info.EndBlock) |
| 5532 | CGF.EmitBlock(Info.EndBlock); |
| 5533 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5534 | // Branch around the rethrow code. |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5535 | CGF.EmitBranch(FinallyEnd); |
| 5536 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5537 | CGF.EmitBlock(FinallyRethrow); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5538 | CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5539 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5540 | CGF.Builder.CreateUnreachable(); |
| 5541 | |
| 5542 | CGF.EmitBlock(FinallyEnd); |
| 5543 | } |
| 5544 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5545 | /// EmitThrowStmt - Generate code for a throw statement. |
| 5546 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 5547 | const ObjCAtThrowStmt &S) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5548 | llvm::Value *Exception; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5549 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5550 | Exception = CGF.EmitScalarExpr(ThrowExpr); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5551 | } else { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5552 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
| 5553 | "Unexpected rethrow outside @catch block."); |
| 5554 | Exception = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5555 | } |
| 5556 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5557 | llvm::Value *ExceptionAsObject = |
| 5558 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 5559 | llvm::BasicBlock *InvokeDest = CGF.getInvokeDest(); |
| 5560 | if (InvokeDest) { |
| 5561 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5562 | CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5563 | Cont, InvokeDest, |
| 5564 | &ExceptionAsObject, &ExceptionAsObject + 1); |
| 5565 | CGF.EmitBlock(Cont); |
| 5566 | } else |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5567 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5568 | CGF.Builder.CreateUnreachable(); |
| 5569 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5570 | // Clear the insertion point to indicate we are in unreachable code. |
| 5571 | CGF.Builder.ClearInsertionPoint(); |
| 5572 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5573 | |
| 5574 | llvm::Value * |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5575 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 5576 | bool ForDefinition) { |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5577 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5578 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5579 | // If we don't need a definition, return the entry if found or check |
| 5580 | // if we use an external reference. |
| 5581 | if (!ForDefinition) { |
| 5582 | if (Entry) |
| 5583 | return Entry; |
Daniel Dunbar | 7e075cb | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 5584 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5585 | // If this type (or a super class) has the __objc_exception__ |
| 5586 | // attribute, emit an external reference. |
| 5587 | if (hasObjCExceptionAttribute(ID)) |
| 5588 | return Entry = |
| 5589 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5590 | llvm::GlobalValue::ExternalLinkage, |
| 5591 | 0, |
| 5592 | (std::string("OBJC_EHTYPE_$_") + |
| 5593 | ID->getIdentifier()->getName()), |
| 5594 | &CGM.getModule()); |
| 5595 | } |
| 5596 | |
| 5597 | // Otherwise we need to either make a new entry or fill in the |
| 5598 | // initializer. |
| 5599 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5600 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5601 | std::string VTableName = "objc_ehtype_vtable"; |
| 5602 | llvm::GlobalVariable *VTableGV = |
| 5603 | CGM.getModule().getGlobalVariable(VTableName); |
| 5604 | if (!VTableGV) |
| 5605 | VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false, |
| 5606 | llvm::GlobalValue::ExternalLinkage, |
| 5607 | 0, VTableName, &CGM.getModule()); |
| 5608 | |
| 5609 | llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2); |
| 5610 | |
| 5611 | std::vector<llvm::Constant*> Values(3); |
| 5612 | Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1); |
| 5613 | Values[1] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5614 | Values[2] = GetClassGlobal(ClassName); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5615 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
| 5616 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5617 | if (Entry) { |
| 5618 | Entry->setInitializer(Init); |
| 5619 | } else { |
| 5620 | Entry = new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5621 | llvm::GlobalValue::WeakAnyLinkage, |
| 5622 | Init, |
| 5623 | (std::string("OBJC_EHTYPE_$_") + |
| 5624 | ID->getIdentifier()->getName()), |
| 5625 | &CGM.getModule()); |
| 5626 | } |
| 5627 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 5628 | if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden) |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5629 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5630 | Entry->setAlignment(8); |
| 5631 | |
| 5632 | if (ForDefinition) { |
| 5633 | Entry->setSection("__DATA,__objc_const"); |
| 5634 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 5635 | } else { |
| 5636 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 5637 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5638 | |
| 5639 | return Entry; |
| 5640 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5641 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5642 | /* *** */ |
| 5643 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5644 | CodeGen::CGObjCRuntime * |
| 5645 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 5646 | return new CGObjCMac(CGM); |
| 5647 | } |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5648 | |
| 5649 | CodeGen::CGObjCRuntime * |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5650 | CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5651 | return new CGObjCNonFragileABIMac(CGM); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5652 | } |