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 | |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 848 | /// GetFieldBaseOffset - return's field byte offset. |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 849 | uint64_t GetFieldBaseOffset(const ObjCImplementationDecl *OI, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 850 | const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 851 | const FieldDecl *Field); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 852 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 853 | /// CreateMetadataVar - Create a global variable with internal |
| 854 | /// linkage for use by the Objective-C runtime. |
| 855 | /// |
| 856 | /// This is a convenience wrapper which not only creates the |
| 857 | /// variable, but also sets the section and alignment and adds the |
| 858 | /// global to the UsedGlobals list. |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 859 | /// |
| 860 | /// \param Name - The variable name. |
| 861 | /// \param Init - The variable initializer; this is also used to |
| 862 | /// define the type of the variable. |
| 863 | /// \param Section - The section the variable should go into, or 0. |
| 864 | /// \param Align - The alignment for the variable, or 0. |
| 865 | /// \param AddToUsed - Whether the variable should be added to |
Daniel Dunbar | c158306 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 866 | /// "llvm.used". |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 867 | llvm::GlobalVariable *CreateMetadataVar(const std::string &Name, |
| 868 | llvm::Constant *Init, |
| 869 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 870 | unsigned Align, |
| 871 | bool AddToUsed); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 872 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 873 | /// GetNamedIvarList - Return the list of ivars in the interface |
| 874 | /// itself (not including super classes and not including unnamed |
| 875 | /// bitfields). |
| 876 | /// |
| 877 | /// For the non-fragile ABI, this also includes synthesized property |
| 878 | /// ivars. |
| 879 | void GetNamedIvarList(const ObjCInterfaceDecl *OID, |
| 880 | llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const; |
| 881 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 882 | public: |
| 883 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm) |
| 884 | { } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 885 | |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 886 | virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 887 | |
| 888 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 889 | const ObjCContainerDecl *CD=0); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 890 | |
| 891 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
| 892 | |
| 893 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 894 | /// declaration, emitting it if necessary. The return value has type |
| 895 | /// ProtocolPtrTy. |
| 896 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
| 897 | |
| 898 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 899 | /// object for the given declaration, emitting it if needed. These |
| 900 | /// forward references will be filled in with empty bodies if no |
| 901 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 902 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 903 | }; |
| 904 | |
| 905 | class CGObjCMac : public CGObjCCommonMac { |
| 906 | private: |
| 907 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 908 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 909 | /// level information. |
| 910 | void EmitImageInfo(); |
| 911 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 912 | /// EmitModuleInfo - Another marker encoding module level |
| 913 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 914 | void EmitModuleInfo(); |
| 915 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 916 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 917 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 918 | llvm::Constant *EmitModuleSymbols(); |
| 919 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 920 | /// FinishModule - Write out global data structures at the end of |
| 921 | /// processing a translation unit. |
| 922 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 923 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 924 | /// EmitClassExtension - Generate the class extension structure used |
| 925 | /// to store the weak ivar layout and properties. The return value |
| 926 | /// has type ClassExtensionPtrTy. |
| 927 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 928 | |
| 929 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 930 | /// for the given class. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 931 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 932 | const ObjCInterfaceDecl *ID); |
| 933 | |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 934 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 935 | QualType ResultType, |
| 936 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 937 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 938 | QualType Arg0Ty, |
| 939 | bool IsSuper, |
| 940 | const CallArgList &CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 941 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 942 | /// EmitIvarList - Emit the ivar list for the given |
| 943 | /// implementation. If ForClass is true the list of class ivars |
| 944 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 945 | /// interface ivars will be emitted. The return value has type |
| 946 | /// IvarListPtrTy. |
| 947 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 948 | bool ForClass); |
| 949 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 950 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 951 | /// for the metaclass of the given interface. The return value has |
| 952 | /// type ClassPtrTy. |
| 953 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 954 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 955 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 956 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 957 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 958 | llvm::Constant *Protocols, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 959 | const ConstantVector &Methods); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 960 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 961 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 962 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 963 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 964 | |
| 965 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 966 | /// implementation. The return value has type MethodListPtrTy. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 967 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 968 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 969 | const ConstantVector &Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 970 | |
| 971 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 972 | /// method declarations. |
| 973 | /// - TypeName: The name for the type containing the methods. |
| 974 | /// - IsProtocol: True iff these methods are for a protocol. |
| 975 | /// - ClassMethds: True iff these are class methods. |
| 976 | /// - Required: When true, only "required" methods are |
| 977 | /// listed. Similarly, when false only "optional" methods are |
| 978 | /// listed. For classes this should always be true. |
| 979 | /// - begin, end: The method list to output. |
| 980 | /// |
| 981 | /// The return value has type MethodDescriptionListPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 982 | llvm::Constant *EmitMethodDescList(const std::string &Name, |
| 983 | const char *Section, |
| 984 | const ConstantVector &Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 985 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 986 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 987 | /// declaration, emitting it if necessary. The return value has type |
| 988 | /// ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 989 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 990 | |
| 991 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 992 | /// object for the given declaration, emitting it if needed. These |
| 993 | /// forward references will be filled in with empty bodies if no |
| 994 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 995 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 996 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 997 | /// EmitProtocolExtension - Generate the protocol extension |
| 998 | /// structure used to store optional instance and class methods, and |
| 999 | /// protocol properties. The return value has type |
| 1000 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1001 | llvm::Constant * |
| 1002 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1003 | const ConstantVector &OptInstanceMethods, |
| 1004 | const ConstantVector &OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1005 | |
| 1006 | /// EmitProtocolList - Generate the list of referenced |
| 1007 | /// protocols. The return value has type ProtocolListPtrTy. |
Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1008 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 1009 | ObjCProtocolDecl::protocol_iterator begin, |
| 1010 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1011 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1012 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1013 | /// for the given selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1014 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1015 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1016 | public: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1017 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1018 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1019 | virtual llvm::Function *ModuleInitFunction(); |
| 1020 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1021 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1022 | QualType ResultType, |
| 1023 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1024 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1025 | bool IsClassMessage, |
| 1026 | const CallArgList &CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1027 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1028 | virtual CodeGen::RValue |
| 1029 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1030 | QualType ResultType, |
| 1031 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1032 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1033 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1034 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1035 | bool IsClassMessage, |
| 1036 | const CallArgList &CallArgs); |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1037 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1038 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1039 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1040 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1041 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1042 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1043 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1044 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1045 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1046 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1047 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1048 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1049 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1050 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 1051 | virtual llvm::Constant *GetPropertySetFunction(); |
| 1052 | virtual llvm::Constant *EnumerationMutationFunction(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1053 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 1054 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1055 | const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1056 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 1057 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1058 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 1059 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1060 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 1061 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1062 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 1063 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 1064 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 1065 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1066 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 1067 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 1068 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1069 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1070 | QualType ObjectTy, |
| 1071 | llvm::Value *BaseValue, |
| 1072 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1073 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1074 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1075 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1076 | const ObjCIvarDecl *Ivar); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1077 | }; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1078 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1079 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1080 | private: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1081 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1082 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 1083 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1084 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1085 | /// SuperClassReferences - uniqued super class references. |
| 1086 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; |
| 1087 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1088 | /// MetaClassReferences - uniqued meta class references. |
| 1089 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1090 | |
| 1091 | /// EHTypeReferences - uniqued class ehtype references. |
| 1092 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1093 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1094 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 1095 | /// processing a translation unit. |
| 1096 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1097 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 1098 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 1099 | unsigned InstanceStart, |
| 1100 | unsigned InstanceSize, |
| 1101 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1102 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
| 1103 | llvm::Constant *IsAGV, |
| 1104 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 1105 | llvm::Constant *ClassRoGV, |
| 1106 | bool HiddenVisibility); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1107 | |
| 1108 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
| 1109 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 1110 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
| 1111 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1112 | /// EmitMethodList - Emit the method list for the given |
| 1113 | /// implementation. The return value has type MethodListnfABITy. |
| 1114 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 1115 | const char *Section, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 1116 | const ConstantVector &Methods); |
| 1117 | /// EmitIvarList - Emit the ivar list for the given |
| 1118 | /// implementation. If ForClass is true the list of class ivars |
| 1119 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1120 | /// interface ivars will be emitted. The return value has type |
| 1121 | /// IvarListnfABIPtrTy. |
| 1122 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 1123 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1124 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 1125 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 1126 | unsigned long int offset); |
| 1127 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1128 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1129 | /// declaration, emitting it if necessary. The return value has type |
| 1130 | /// ProtocolPtrTy. |
| 1131 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
| 1132 | |
| 1133 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1134 | /// object for the given declaration, emitting it if needed. These |
| 1135 | /// forward references will be filled in with empty bodies if no |
| 1136 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1137 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
| 1138 | |
| 1139 | /// EmitProtocolList - Generate the list of referenced |
| 1140 | /// protocols. The return value has type ProtocolListPtrTy. |
| 1141 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 1142 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1143 | ObjCProtocolDecl::protocol_iterator end); |
| 1144 | |
| 1145 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1146 | QualType ResultType, |
| 1147 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 1148 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1149 | QualType Arg0Ty, |
| 1150 | bool IsSuper, |
| 1151 | const CallArgList &CallArgs); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 1152 | |
| 1153 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1154 | /// class of the given name. |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 1155 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name); |
| 1156 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1157 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1158 | /// for the given class reference. |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1159 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1160 | const ObjCInterfaceDecl *ID); |
| 1161 | |
| 1162 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1163 | /// for the given super class reference. |
| 1164 | llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder, |
| 1165 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1166 | |
| 1167 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 1168 | /// meta-data |
| 1169 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
| 1170 | const ObjCInterfaceDecl *ID); |
| 1171 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1172 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 1173 | /// the given ivar. |
| 1174 | /// |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 1175 | llvm::GlobalVariable * ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1176 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1177 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1178 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1179 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1180 | /// for the given selector. |
| 1181 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1182 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1183 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1184 | /// interface. The return value has type EHTypePtrTy. |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1185 | llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 1186 | bool ForDefinition); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1187 | |
| 1188 | const char *getMetaclassSymbolPrefix() const { |
| 1189 | return "OBJC_METACLASS_$_"; |
| 1190 | } |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1191 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1192 | const char *getClassSymbolPrefix() const { |
| 1193 | return "OBJC_CLASS_$_"; |
| 1194 | } |
| 1195 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 1196 | void GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1197 | uint32_t &InstanceStart, |
| 1198 | uint32_t &InstanceSize); |
| 1199 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1200 | public: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1201 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1202 | // FIXME. All stubs for now! |
| 1203 | virtual llvm::Function *ModuleInitFunction(); |
| 1204 | |
| 1205 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1206 | QualType ResultType, |
| 1207 | Selector Sel, |
| 1208 | llvm::Value *Receiver, |
| 1209 | bool IsClassMessage, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1210 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1211 | |
| 1212 | virtual CodeGen::RValue |
| 1213 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 1214 | QualType ResultType, |
| 1215 | Selector Sel, |
| 1216 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1217 | bool isCategoryImpl, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1218 | llvm::Value *Receiver, |
| 1219 | bool IsClassMessage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1220 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1221 | |
| 1222 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1223 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1224 | |
| 1225 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel) |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1226 | { return EmitSelector(Builder, Sel); } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1227 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 1228 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1229 | |
| 1230 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1231 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 1232 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1233 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1234 | virtual llvm::Constant *GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1235 | return ObjCTypes.getGetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1236 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1237 | virtual llvm::Constant *GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1238 | return ObjCTypes.getSetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1239 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1240 | virtual llvm::Constant *EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1241 | return ObjCTypes.getEnumerationMutationFn(); |
Daniel Dunbar | 28ed084 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 1242 | } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1243 | |
| 1244 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 1245 | const Stmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1246 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 1247 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1248 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1249 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1250 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1251 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1252 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1253 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1254 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1255 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1256 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1257 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1258 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1259 | QualType ObjectTy, |
| 1260 | llvm::Value *BaseValue, |
| 1261 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1262 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1263 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1264 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1265 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1266 | }; |
| 1267 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1268 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1269 | |
| 1270 | /* *** Helper Functions *** */ |
| 1271 | |
| 1272 | /// getConstantGEP() - Help routine to construct simple GEPs. |
| 1273 | static llvm::Constant *getConstantGEP(llvm::Constant *C, |
| 1274 | unsigned idx0, |
| 1275 | unsigned idx1) { |
| 1276 | llvm::Value *Idxs[] = { |
| 1277 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0), |
| 1278 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1) |
| 1279 | }; |
| 1280 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2); |
| 1281 | } |
| 1282 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1283 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 1284 | /// class has the __objc_exception__ attribute. |
| 1285 | static bool hasObjCExceptionAttribute(const ObjCInterfaceDecl *OID) { |
Daniel Dunbar | b11fa0d | 2009-04-13 21:08:27 +0000 | [diff] [blame] | 1286 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1287 | return true; |
| 1288 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
| 1289 | return hasObjCExceptionAttribute(Super); |
| 1290 | return false; |
| 1291 | } |
| 1292 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1293 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1294 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1295 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
| 1296 | ObjCTypes(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1297 | { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1298 | ObjCABI = 1; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1299 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1302 | /// GetClass - Return a reference to the class for the given interface |
| 1303 | /// decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1304 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1305 | const ObjCInterfaceDecl *ID) { |
| 1306 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1310 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 1311 | return EmitSelector(Builder, Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1314 | /// Generate a constant CFString object. |
| 1315 | /* |
| 1316 | struct __builtin_CFString { |
| 1317 | const int *isa; // point to __CFConstantStringClassReference |
| 1318 | int flags; |
| 1319 | const char *str; |
| 1320 | long length; |
| 1321 | }; |
| 1322 | */ |
| 1323 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1324 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 1325 | const ObjCStringLiteral *SL) { |
Steve Naroff | 8d4141f | 2009-04-01 13:55:36 +0000 | [diff] [blame] | 1326 | return CGM.GetAddrOfConstantCFString(SL->getString()); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | /// Generates a message send where the super is the receiver. This is |
| 1330 | /// a message send to self with special delivery semantics indicating |
| 1331 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1332 | CodeGen::RValue |
| 1333 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1334 | QualType ResultType, |
| 1335 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1336 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1337 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1338 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1339 | bool IsClassMessage, |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1340 | const CodeGen::CallArgList &CallArgs) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1341 | // Create and init a super structure; this is a (receiver, class) |
| 1342 | // pair we will pass to objc_msgSendSuper. |
| 1343 | llvm::Value *ObjCSuper = |
| 1344 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 1345 | llvm::Value *ReceiverAsObject = |
| 1346 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 1347 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 1348 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1349 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1350 | // If this is a class message the metaclass is passed as the target. |
| 1351 | llvm::Value *Target; |
| 1352 | if (IsClassMessage) { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1353 | if (isCategoryImpl) { |
| 1354 | // Message sent to 'super' in a class method defined in a category |
| 1355 | // implementation requires an odd treatment. |
| 1356 | // If we are in a class method, we must retrieve the |
| 1357 | // _metaclass_ for the current class, pointed at by |
| 1358 | // the class's "isa" pointer. The following assumes that |
| 1359 | // isa" is the first ivar in a class (which it must be). |
| 1360 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1361 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 1362 | Target = CGF.Builder.CreateLoad(Target); |
| 1363 | } |
| 1364 | else { |
| 1365 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 1366 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 1367 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 1368 | Target = Super; |
| 1369 | } |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1370 | } else { |
| 1371 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1372 | } |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1373 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 1374 | // and ObjCTypes types. |
| 1375 | const llvm::Type *ClassTy = |
| 1376 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1377 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1378 | CGF.Builder.CreateStore(Target, |
| 1379 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 1380 | |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1381 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1382 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 1383 | true, CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1384 | } |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1385 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1386 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1387 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1388 | QualType ResultType, |
| 1389 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1390 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1391 | bool IsClassMessage, |
| 1392 | const CallArgList &CallArgs) { |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1393 | return EmitMessageSend(CGF, ResultType, Sel, |
Fariborz Jahanian | d019d96 | 2009-04-24 21:07:43 +0000 | [diff] [blame] | 1394 | Receiver, CGF.getContext().getObjCIdType(), |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1395 | false, CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
| 1398 | CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1399 | QualType ResultType, |
| 1400 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1401 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1402 | QualType Arg0Ty, |
| 1403 | bool IsSuper, |
| 1404 | const CallArgList &CallArgs) { |
| 1405 | CallArgList ActualArgs; |
Fariborz Jahanian | d019d96 | 2009-04-24 21:07:43 +0000 | [diff] [blame] | 1406 | if (!IsSuper) |
| 1407 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1408 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 1409 | ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder, |
| 1410 | Sel)), |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1411 | CGF.getContext().getObjCSelType())); |
| 1412 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1413 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1414 | CodeGenTypes &Types = CGM.getTypes(); |
| 1415 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); |
Fariborz Jahanian | 65257ca | 2009-04-29 22:47:27 +0000 | [diff] [blame] | 1416 | // FIXME. vararg flag must be true when this API is used for 64bit code gen. |
| 1417 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1418 | |
| 1419 | llvm::Constant *Fn; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1420 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1421 | Fn = ObjCTypes.getSendStretFn(IsSuper); |
| 1422 | } else if (ResultType->isFloatingType()) { |
| 1423 | // FIXME: Sadly, this is wrong. This actually depends on the |
| 1424 | // architecture. This happens to be right for x86-32 though. |
| 1425 | Fn = ObjCTypes.getSendFpretFn(IsSuper); |
| 1426 | } else { |
| 1427 | Fn = ObjCTypes.getSendFn(IsSuper); |
| 1428 | } |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 1429 | Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy)); |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1430 | return CGF.EmitCall(FnInfo, Fn, ActualArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1433 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1434 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1435 | // FIXME: I don't understand why gcc generates this, or where it is |
| 1436 | // resolved. Investigate. Its also wasteful to look this up over and |
| 1437 | // over. |
| 1438 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1439 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1440 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
| 1441 | ObjCTypes.ExternalProtocolPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1444 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1445 | // FIXME: We shouldn't need this, the protocol decl should contain |
| 1446 | // enough information to tell us whether this was a declaration or a |
| 1447 | // definition. |
| 1448 | DefinedProtocols.insert(PD->getIdentifier()); |
| 1449 | |
| 1450 | // If we have generated a forward reference to this protocol, emit |
| 1451 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 1452 | // emitted. |
| 1453 | if (Protocols.count(PD->getIdentifier())) |
| 1454 | GetOrEmitProtocol(PD); |
| 1455 | } |
| 1456 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1457 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1458 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 1459 | return GetOrEmitProtocol(PD); |
| 1460 | return GetOrEmitProtocolRef(PD); |
| 1461 | } |
| 1462 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1463 | /* |
| 1464 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 1465 | struct _objc_protocol { |
| 1466 | struct _objc_protocol_extension *isa; |
| 1467 | char *protocol_name; |
| 1468 | struct _objc_protocol_list *protocol_list; |
| 1469 | struct _objc__method_prototype_list *instance_methods; |
| 1470 | struct _objc__method_prototype_list *class_methods |
| 1471 | }; |
| 1472 | |
| 1473 | See EmitProtocolExtension(). |
| 1474 | */ |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1475 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
| 1476 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1477 | |
| 1478 | // Early exit if a defining object has already been generated. |
| 1479 | if (Entry && Entry->hasInitializer()) |
| 1480 | return Entry; |
| 1481 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1482 | // FIXME: I don't understand why gcc generates this, or where it is |
| 1483 | // resolved. Investigate. Its also wasteful to look this up over and |
| 1484 | // over. |
| 1485 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1486 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1487 | const char *ProtocolName = PD->getNameAsCString(); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1488 | |
| 1489 | // Construct method lists. |
| 1490 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1491 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1492 | for (ObjCProtocolDecl::instmeth_iterator |
| 1493 | i = PD->instmeth_begin(CGM.getContext()), |
| 1494 | e = PD->instmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1495 | ObjCMethodDecl *MD = *i; |
| 1496 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1497 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1498 | OptInstanceMethods.push_back(C); |
| 1499 | } else { |
| 1500 | InstanceMethods.push_back(C); |
| 1501 | } |
| 1502 | } |
| 1503 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1504 | for (ObjCProtocolDecl::classmeth_iterator |
| 1505 | i = PD->classmeth_begin(CGM.getContext()), |
| 1506 | e = PD->classmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1507 | ObjCMethodDecl *MD = *i; |
| 1508 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1509 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1510 | OptClassMethods.push_back(C); |
| 1511 | } else { |
| 1512 | ClassMethods.push_back(C); |
| 1513 | } |
| 1514 | } |
| 1515 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1516 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1517 | Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1518 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1519 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1520 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(), |
Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1521 | PD->protocol_begin(), |
| 1522 | PD->protocol_end()); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1523 | Values[3] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1524 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" |
| 1525 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1526 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1527 | InstanceMethods); |
| 1528 | Values[4] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1529 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" |
| 1530 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1531 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1532 | ClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1533 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 1534 | Values); |
| 1535 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1536 | if (Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1537 | // Already created, fix the linkage and update the initializer. |
| 1538 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1539 | Entry->setInitializer(Init); |
| 1540 | } else { |
| 1541 | Entry = |
| 1542 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
| 1543 | llvm::GlobalValue::InternalLinkage, |
| 1544 | Init, |
| 1545 | std::string("\01L_OBJC_PROTOCOL_")+ProtocolName, |
| 1546 | &CGM.getModule()); |
| 1547 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1548 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1549 | UsedGlobals.push_back(Entry); |
| 1550 | // FIXME: Is this necessary? Why only for protocol? |
| 1551 | Entry->setAlignment(4); |
| 1552 | } |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1553 | |
| 1554 | return Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1557 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1558 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1559 | |
| 1560 | if (!Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1561 | // We use the initializer as a marker of whether this is a forward |
| 1562 | // reference or not. At module finalization we add the empty |
| 1563 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1564 | Entry = |
| 1565 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1566 | llvm::GlobalValue::ExternalLinkage, |
| 1567 | 0, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1568 | "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1569 | &CGM.getModule()); |
| 1570 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1571 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1572 | UsedGlobals.push_back(Entry); |
| 1573 | // FIXME: Is this necessary? Why only for protocol? |
| 1574 | Entry->setAlignment(4); |
| 1575 | } |
| 1576 | |
| 1577 | return Entry; |
| 1578 | } |
| 1579 | |
| 1580 | /* |
| 1581 | struct _objc_protocol_extension { |
| 1582 | uint32_t size; |
| 1583 | struct objc_method_description_list *optional_instance_methods; |
| 1584 | struct objc_method_description_list *optional_class_methods; |
| 1585 | struct objc_property_list *instance_properties; |
| 1586 | }; |
| 1587 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1588 | llvm::Constant * |
| 1589 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1590 | const ConstantVector &OptInstanceMethods, |
| 1591 | const ConstantVector &OptClassMethods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1592 | uint64_t Size = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1593 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1594 | std::vector<llvm::Constant*> Values(4); |
| 1595 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1596 | Values[1] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1597 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
| 1598 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1599 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1600 | OptInstanceMethods); |
| 1601 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1602 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" |
| 1603 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1604 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1605 | OptClassMethods); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1606 | Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + |
| 1607 | PD->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1608 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1609 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1610 | // Return null if no extension bits are used. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1611 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
| 1612 | Values[3]->isNullValue()) |
| 1613 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 1614 | |
| 1615 | llvm::Constant *Init = |
| 1616 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1617 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1618 | // No special section, but goes in llvm.used |
| 1619 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(), |
| 1620 | Init, |
| 1621 | 0, 0, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | /* |
| 1625 | struct objc_protocol_list { |
| 1626 | struct objc_protocol_list *next; |
| 1627 | long count; |
| 1628 | Protocol *list[]; |
| 1629 | }; |
| 1630 | */ |
Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1631 | llvm::Constant * |
| 1632 | CGObjCMac::EmitProtocolList(const std::string &Name, |
| 1633 | ObjCProtocolDecl::protocol_iterator begin, |
| 1634 | ObjCProtocolDecl::protocol_iterator end) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1635 | std::vector<llvm::Constant*> ProtocolRefs; |
| 1636 | |
Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1637 | for (; begin != end; ++begin) |
| 1638 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1639 | |
| 1640 | // Just return null for empty protocol lists |
| 1641 | if (ProtocolRefs.empty()) |
| 1642 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1643 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1644 | // This list is null terminated. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1645 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
| 1646 | |
| 1647 | std::vector<llvm::Constant*> Values(3); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1648 | // This field is only used by the runtime. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1649 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1650 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 1651 | Values[2] = |
| 1652 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 1653 | ProtocolRefs.size()), |
| 1654 | ProtocolRefs); |
| 1655 | |
| 1656 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1657 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1658 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1659 | 4, false); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1660 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
| 1661 | } |
| 1662 | |
| 1663 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1664 | struct _objc_property { |
| 1665 | const char * const name; |
| 1666 | const char * const attributes; |
| 1667 | }; |
| 1668 | |
| 1669 | struct _objc_property_list { |
| 1670 | uint32_t entsize; // sizeof (struct _objc_property) |
| 1671 | uint32_t prop_count; |
| 1672 | struct _objc_property[prop_count]; |
| 1673 | }; |
| 1674 | */ |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1675 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name, |
| 1676 | const Decl *Container, |
| 1677 | const ObjCContainerDecl *OCD, |
| 1678 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1679 | std::vector<llvm::Constant*> Properties, Prop(2); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1680 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(CGM.getContext()), |
| 1681 | E = OCD->prop_end(CGM.getContext()); I != E; ++I) { |
Steve Naroff | 93983f8 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 1682 | const ObjCPropertyDecl *PD = *I; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1683 | Prop[0] = GetPropertyName(PD->getIdentifier()); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1684 | Prop[1] = GetPropertyTypeString(PD, Container); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1685 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
| 1686 | Prop)); |
| 1687 | } |
| 1688 | |
| 1689 | // Return null for empty list. |
| 1690 | if (Properties.empty()) |
| 1691 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1692 | |
| 1693 | unsigned PropertySize = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1694 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1695 | std::vector<llvm::Constant*> Values(3); |
| 1696 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 1697 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
| 1698 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
| 1699 | Properties.size()); |
| 1700 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
| 1701 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1702 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1703 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1704 | CreateMetadataVar(Name, Init, |
| 1705 | (ObjCABI == 2) ? "__DATA, __objc_const" : |
| 1706 | "__OBJC,__property,regular,no_dead_strip", |
| 1707 | (ObjCABI == 2) ? 8 : 4, |
| 1708 | true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1709 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1713 | struct objc_method_description_list { |
| 1714 | int count; |
| 1715 | struct objc_method_description list[]; |
| 1716 | }; |
| 1717 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1718 | llvm::Constant * |
| 1719 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 1720 | std::vector<llvm::Constant*> Desc(2); |
| 1721 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1722 | ObjCTypes.SelectorPtrTy); |
| 1723 | Desc[1] = GetMethodVarType(MD); |
| 1724 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
| 1725 | Desc); |
| 1726 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1727 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1728 | llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name, |
| 1729 | const char *Section, |
| 1730 | const ConstantVector &Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1731 | // Return null for empty list. |
| 1732 | if (Methods.empty()) |
| 1733 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 1734 | |
| 1735 | std::vector<llvm::Constant*> Values(2); |
| 1736 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 1737 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
| 1738 | Methods.size()); |
| 1739 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
| 1740 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1741 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1742 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1743 | return llvm::ConstantExpr::getBitCast(GV, |
| 1744 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1747 | /* |
| 1748 | struct _objc_category { |
| 1749 | char *category_name; |
| 1750 | char *class_name; |
| 1751 | struct _objc_method_list *instance_methods; |
| 1752 | struct _objc_method_list *class_methods; |
| 1753 | struct _objc_protocol_list *protocols; |
| 1754 | uint32_t size; // <rdar://4585769> |
| 1755 | struct _objc_property_list *instance_properties; |
| 1756 | }; |
| 1757 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1758 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1759 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1760 | |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1761 | // FIXME: This is poor design, the OCD should have a pointer to the |
| 1762 | // category decl. Additionally, note that Category can be null for |
| 1763 | // the @implementation w/o an @interface case. Sema should just |
| 1764 | // create one for us as it does for @implementation so everyone else |
| 1765 | // can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1766 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1767 | const ObjCCategoryDecl *Category = |
| 1768 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1769 | std::string ExtName(Interface->getNameAsString() + "_" + |
| 1770 | OCD->getNameAsString()); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1771 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1772 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1773 | for (ObjCCategoryImplDecl::instmeth_iterator |
| 1774 | i = OCD->instmeth_begin(CGM.getContext()), |
| 1775 | e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1776 | // Instance methods should always be defined. |
| 1777 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1778 | } |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1779 | for (ObjCCategoryImplDecl::classmeth_iterator |
| 1780 | i = OCD->classmeth_begin(CGM.getContext()), |
| 1781 | e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1782 | // Class methods should always be defined. |
| 1783 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1784 | } |
| 1785 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1786 | std::vector<llvm::Constant*> Values(7); |
| 1787 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 1788 | Values[1] = GetClassName(Interface->getIdentifier()); |
Fariborz Jahanian | 679cd7f | 2009-04-29 20:40:05 +0000 | [diff] [blame] | 1789 | LazySymbols.insert(Interface->getIdentifier()); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1790 | Values[2] = |
| 1791 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") + |
| 1792 | ExtName, |
| 1793 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1794 | InstanceMethods); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1795 | Values[3] = |
| 1796 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName, |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1797 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1798 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1799 | if (Category) { |
| 1800 | Values[4] = |
| 1801 | EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName, |
| 1802 | Category->protocol_begin(), |
| 1803 | Category->protocol_end()); |
| 1804 | } else { |
| 1805 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1806 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1807 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1808 | |
| 1809 | // If there is no category @interface then there can be no properties. |
| 1810 | if (Category) { |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1811 | Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1812 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1813 | } else { |
| 1814 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1815 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1816 | |
| 1817 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
| 1818 | Values); |
| 1819 | |
| 1820 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1821 | CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init, |
| 1822 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1823 | 4, true); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1824 | DefinedCategories.push_back(GV); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1827 | // FIXME: Get from somewhere? |
| 1828 | enum ClassFlags { |
| 1829 | eClassFlags_Factory = 0x00001, |
| 1830 | eClassFlags_Meta = 0x00002, |
| 1831 | // <rdr://5142207> |
| 1832 | eClassFlags_HasCXXStructors = 0x02000, |
| 1833 | eClassFlags_Hidden = 0x20000, |
| 1834 | eClassFlags_ABI2_Hidden = 0x00010, |
| 1835 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 1836 | }; |
| 1837 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1838 | /* |
| 1839 | struct _objc_class { |
| 1840 | Class isa; |
| 1841 | Class super_class; |
| 1842 | const char *name; |
| 1843 | long version; |
| 1844 | long info; |
| 1845 | long instance_size; |
| 1846 | struct _objc_ivar_list *ivars; |
| 1847 | struct _objc_method_list *methods; |
| 1848 | struct _objc_cache *cache; |
| 1849 | struct _objc_protocol_list *protocols; |
| 1850 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 1851 | const char *ivar_layout; |
| 1852 | struct _objc_class_ext *ext; |
| 1853 | }; |
| 1854 | |
| 1855 | See EmitClassExtension(); |
| 1856 | */ |
| 1857 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1858 | DefinedSymbols.insert(ID->getIdentifier()); |
| 1859 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1860 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1861 | // FIXME: Gross |
| 1862 | ObjCInterfaceDecl *Interface = |
| 1863 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1864 | llvm::Constant *Protocols = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1865 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(), |
Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1866 | Interface->protocol_begin(), |
| 1867 | Interface->protocol_end()); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1868 | unsigned Flags = eClassFlags_Factory; |
Daniel Dunbar | 2bebbf0 | 2009-05-03 10:46:44 +0000 | [diff] [blame] | 1869 | unsigned Size = |
| 1870 | CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1871 | |
| 1872 | // FIXME: Set CXX-structors flag. |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1873 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1874 | Flags |= eClassFlags_Hidden; |
| 1875 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1876 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1877 | for (ObjCImplementationDecl::instmeth_iterator |
| 1878 | i = ID->instmeth_begin(CGM.getContext()), |
| 1879 | e = ID->instmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1880 | // Instance methods should always be defined. |
| 1881 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1882 | } |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1883 | for (ObjCImplementationDecl::classmeth_iterator |
| 1884 | i = ID->classmeth_begin(CGM.getContext()), |
| 1885 | e = ID->classmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1886 | // Class methods should always be defined. |
| 1887 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1888 | } |
| 1889 | |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1890 | for (ObjCImplementationDecl::propimpl_iterator |
| 1891 | i = ID->propimpl_begin(CGM.getContext()), |
| 1892 | e = ID->propimpl_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1893 | ObjCPropertyImplDecl *PID = *i; |
| 1894 | |
| 1895 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 1896 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 1897 | |
| 1898 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 1899 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1900 | InstanceMethods.push_back(C); |
| 1901 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 1902 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1903 | InstanceMethods.push_back(C); |
| 1904 | } |
| 1905 | } |
| 1906 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1907 | std::vector<llvm::Constant*> Values(12); |
Daniel Dunbar | 5384b09 | 2009-05-03 08:56:52 +0000 | [diff] [blame] | 1908 | Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1909 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1910 | // Record a reference to the super class. |
| 1911 | LazySymbols.insert(Super->getIdentifier()); |
| 1912 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1913 | Values[ 1] = |
| 1914 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1915 | ObjCTypes.ClassPtrTy); |
| 1916 | } else { |
| 1917 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1918 | } |
| 1919 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1920 | // Version is always 0. |
| 1921 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1922 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1923 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1924 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1925 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1926 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1927 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1928 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1929 | // cache is always NULL. |
| 1930 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1931 | Values[ 9] = Protocols; |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 1932 | Values[10] = BuildIvarLayout(ID, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1933 | Values[11] = EmitClassExtension(ID); |
| 1934 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1935 | Values); |
| 1936 | |
| 1937 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1938 | CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init, |
| 1939 | "__OBJC,__class,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1940 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1941 | DefinedClasses.push_back(GV); |
| 1942 | } |
| 1943 | |
| 1944 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1945 | llvm::Constant *Protocols, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1946 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1947 | unsigned Flags = eClassFlags_Meta; |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1948 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1949 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1950 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1951 | Flags |= eClassFlags_Hidden; |
| 1952 | |
| 1953 | std::vector<llvm::Constant*> Values(12); |
| 1954 | // The isa for the metaclass is the root of the hierarchy. |
| 1955 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 1956 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 1957 | Root = Super; |
| 1958 | Values[ 0] = |
| 1959 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
| 1960 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1961 | // The super class for the metaclass is emitted as the name of the |
| 1962 | // super class. The runtime fixes this up to point to the |
| 1963 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1964 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
| 1965 | Values[ 1] = |
| 1966 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1967 | ObjCTypes.ClassPtrTy); |
| 1968 | } else { |
| 1969 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1970 | } |
| 1971 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1972 | // Version is always 0. |
| 1973 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1974 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1975 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1976 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1977 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1978 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1979 | "__OBJC,__cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1980 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1981 | // cache is always NULL. |
| 1982 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1983 | Values[ 9] = Protocols; |
| 1984 | // ivar_layout for metaclass is always NULL. |
| 1985 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 1986 | // The class extension is always unused for metaclasses. |
| 1987 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 1988 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1989 | Values); |
| 1990 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1991 | std::string Name("\01L_OBJC_METACLASS_"); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1992 | Name += ID->getNameAsCString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1993 | |
| 1994 | // Check for a forward reference. |
| 1995 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 1996 | if (GV) { |
| 1997 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 1998 | "Forward metaclass reference has incorrect type."); |
| 1999 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 2000 | GV->setInitializer(Init); |
| 2001 | } else { |
| 2002 | GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 2003 | llvm::GlobalValue::InternalLinkage, |
| 2004 | Init, Name, |
| 2005 | &CGM.getModule()); |
| 2006 | } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2007 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2008 | GV->setAlignment(4); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2009 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2010 | |
| 2011 | return GV; |
| 2012 | } |
| 2013 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2014 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2015 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2016 | |
| 2017 | // FIXME: Should we look these up somewhere other than the |
| 2018 | // module. Its a bit silly since we only generate these while |
| 2019 | // processing an implementation, so exactly one pointer would work |
| 2020 | // if know when we entered/exitted an implementation block. |
| 2021 | |
| 2022 | // Check for an existing forward reference. |
Fariborz Jahanian | b0d2794 | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 2023 | // Previously, metaclass with internal linkage may have been defined. |
| 2024 | // pass 'true' as 2nd argument so it is returned. |
| 2025 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) { |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2026 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2027 | "Forward metaclass reference has incorrect type."); |
| 2028 | return GV; |
| 2029 | } else { |
| 2030 | // Generate as an external reference to keep a consistent |
| 2031 | // module. This will be patched up when we emit the metaclass. |
| 2032 | return new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 2033 | llvm::GlobalValue::ExternalLinkage, |
| 2034 | 0, |
| 2035 | Name, |
| 2036 | &CGM.getModule()); |
| 2037 | } |
| 2038 | } |
| 2039 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2040 | /* |
| 2041 | struct objc_class_ext { |
| 2042 | uint32_t size; |
| 2043 | const char *weak_ivar_layout; |
| 2044 | struct _objc_property_list *properties; |
| 2045 | }; |
| 2046 | */ |
| 2047 | llvm::Constant * |
| 2048 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
| 2049 | uint64_t Size = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 2050 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2051 | |
| 2052 | std::vector<llvm::Constant*> Values(3); |
| 2053 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 2054 | Values[1] = BuildIvarLayout(ID, false); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2055 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2056 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2057 | |
| 2058 | // Return null if no extension bits are used. |
| 2059 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
| 2060 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 2061 | |
| 2062 | llvm::Constant *Init = |
| 2063 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2064 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2065 | Init, "__OBJC,__class_ext,regular,no_dead_strip", |
| 2066 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 2069 | /// getInterfaceDeclForIvar - Get the interface declaration node where |
| 2070 | /// this ivar is declared in. |
| 2071 | /// FIXME. Ideally, this info should be in the ivar node. But currently |
| 2072 | /// it is not and prevailing wisdom is that ASTs should not have more |
| 2073 | /// info than is absolutely needed, even though this info reflects the |
| 2074 | /// source language. |
| 2075 | /// |
| 2076 | static const ObjCInterfaceDecl *getInterfaceDeclForIvar( |
| 2077 | const ObjCInterfaceDecl *OI, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2078 | const ObjCIvarDecl *IVD, |
| 2079 | ASTContext &Context) { |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 2080 | if (!OI) |
| 2081 | return 0; |
| 2082 | assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface"); |
| 2083 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 2084 | E = OI->ivar_end(); I != E; ++I) |
| 2085 | if ((*I)->getIdentifier() == IVD->getIdentifier()) |
| 2086 | return OI; |
Fariborz Jahanian | 5a4b453 | 2009-03-31 17:00:52 +0000 | [diff] [blame] | 2087 | // look into properties. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2088 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context), |
| 2089 | E = OI->prop_end(Context); I != E; ++I) { |
Fariborz Jahanian | 5a4b453 | 2009-03-31 17:00:52 +0000 | [diff] [blame] | 2090 | ObjCPropertyDecl *PDecl = (*I); |
| 2091 | if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl()) |
| 2092 | if (IV->getIdentifier() == IVD->getIdentifier()) |
| 2093 | return OI; |
| 2094 | } |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2095 | return getInterfaceDeclForIvar(OI->getSuperClass(), IVD, Context); |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2098 | /* |
| 2099 | struct objc_ivar { |
| 2100 | char *ivar_name; |
| 2101 | char *ivar_type; |
| 2102 | int ivar_offset; |
| 2103 | }; |
| 2104 | |
| 2105 | struct objc_ivar_list { |
| 2106 | int ivar_count; |
| 2107 | struct objc_ivar list[count]; |
| 2108 | }; |
| 2109 | */ |
| 2110 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2111 | bool ForClass) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2112 | std::vector<llvm::Constant*> Ivars, Ivar(3); |
| 2113 | |
| 2114 | // When emitting the root class GCC emits ivar entries for the |
| 2115 | // actual class structure. It is not clear if we need to follow this |
| 2116 | // behavior; for now lets try and get away with not doing it. If so, |
| 2117 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 2118 | // for the class. |
| 2119 | if (ForClass) |
| 2120 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2121 | |
| 2122 | ObjCInterfaceDecl *OID = |
| 2123 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2124 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 2125 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
| 2126 | GetNamedIvarList(OID, OIvars); |
| 2127 | |
| 2128 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 2129 | ObjCIvarDecl *IVD = OIvars[i]; |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 2130 | Ivar[0] = GetMethodVarName(IVD->getIdentifier()); |
| 2131 | Ivar[1] = GetMethodVarType(IVD); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 2132 | Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2133 | ComputeIvarBaseOffset(CGM, OID, IVD)); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2134 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
| 2137 | // Return null for empty list. |
| 2138 | if (Ivars.empty()) |
| 2139 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
| 2140 | |
| 2141 | std::vector<llvm::Constant*> Values(2); |
| 2142 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 2143 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
| 2144 | Ivars.size()); |
| 2145 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
| 2146 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2147 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2148 | llvm::GlobalVariable *GV; |
| 2149 | if (ForClass) |
| 2150 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(), |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2151 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
| 2152 | 4, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2153 | else |
| 2154 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" |
| 2155 | + ID->getNameAsString(), |
| 2156 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2157 | 4, true); |
| 2158 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2159 | } |
| 2160 | |
| 2161 | /* |
| 2162 | struct objc_method { |
| 2163 | SEL method_name; |
| 2164 | char *method_types; |
| 2165 | void *method; |
| 2166 | }; |
| 2167 | |
| 2168 | struct objc_method_list { |
| 2169 | struct objc_method_list *obsolete; |
| 2170 | int count; |
| 2171 | struct objc_method methods_list[count]; |
| 2172 | }; |
| 2173 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2174 | |
| 2175 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 2176 | /// given method if it has been defined. The result is null if the |
| 2177 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2178 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2179 | // FIXME: Use DenseMap::lookup |
| 2180 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 2181 | if (!Fn) |
| 2182 | return 0; |
| 2183 | |
| 2184 | std::vector<llvm::Constant*> Method(3); |
| 2185 | Method[0] = |
| 2186 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 2187 | ObjCTypes.SelectorPtrTy); |
| 2188 | Method[1] = GetMethodVarType(MD); |
| 2189 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 2190 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 2191 | } |
| 2192 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2193 | llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name, |
| 2194 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2195 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2196 | // Return null for empty list. |
| 2197 | if (Methods.empty()) |
| 2198 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
| 2199 | |
| 2200 | std::vector<llvm::Constant*> Values(3); |
| 2201 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 2202 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 2203 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 2204 | Methods.size()); |
| 2205 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 2206 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2207 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2208 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2209 | return llvm::ConstantExpr::getBitCast(GV, |
| 2210 | ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 2213 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 2214 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2215 | std::string Name; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 2216 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2217 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2218 | CodeGenTypes &Types = CGM.getTypes(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2219 | const llvm::FunctionType *MethodTy = |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2220 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2221 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2222 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2223 | llvm::GlobalValue::InternalLinkage, |
| 2224 | Name, |
| 2225 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2226 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2227 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2228 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
Daniel Dunbar | 48fa064 | 2009-04-19 02:03:42 +0000 | [diff] [blame] | 2231 | /// GetFieldBaseOffset - return the field's byte offset. |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 2232 | uint64_t CGObjCCommonMac::GetFieldBaseOffset(const ObjCImplementationDecl *OI, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2233 | const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 2234 | const FieldDecl *Field) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2235 | // Is this a C struct? |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 2236 | if (!OI) |
| 2237 | return Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2238 | return ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2241 | llvm::GlobalVariable * |
| 2242 | CGObjCCommonMac::CreateMetadataVar(const std::string &Name, |
| 2243 | llvm::Constant *Init, |
| 2244 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2245 | unsigned Align, |
| 2246 | bool AddToUsed) { |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2247 | const llvm::Type *Ty = Init->getType(); |
| 2248 | llvm::GlobalVariable *GV = |
| 2249 | new llvm::GlobalVariable(Ty, false, |
| 2250 | llvm::GlobalValue::InternalLinkage, |
| 2251 | Init, |
| 2252 | Name, |
| 2253 | &CGM.getModule()); |
| 2254 | if (Section) |
| 2255 | GV->setSection(Section); |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2256 | if (Align) |
| 2257 | GV->setAlignment(Align); |
| 2258 | if (AddToUsed) |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2259 | UsedGlobals.push_back(GV); |
| 2260 | return GV; |
| 2261 | } |
| 2262 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2263 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2264 | // Abuse this interface function as a place to finalize. |
| 2265 | FinishModule(); |
| 2266 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2267 | return NULL; |
| 2268 | } |
| 2269 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2270 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2271 | return ObjCTypes.getGetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2274 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2275 | return ObjCTypes.getSetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2278 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2279 | return ObjCTypes.getEnumerationMutationFn(); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2282 | /* |
| 2283 | |
| 2284 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 2285 | -- |
| 2286 | |
| 2287 | The basic framework for a @try-catch-finally is as follows: |
| 2288 | { |
| 2289 | objc_exception_data d; |
| 2290 | id _rethrow = null; |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2291 | bool _call_try_exit = true; |
| 2292 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2293 | objc_exception_try_enter(&d); |
| 2294 | if (!setjmp(d.jmp_buf)) { |
| 2295 | ... try body ... |
| 2296 | } else { |
| 2297 | // exception path |
| 2298 | id _caught = objc_exception_extract(&d); |
| 2299 | |
| 2300 | // enter new try scope for handlers |
| 2301 | if (!setjmp(d.jmp_buf)) { |
| 2302 | ... match exception and execute catch blocks ... |
| 2303 | |
| 2304 | // fell off end, rethrow. |
| 2305 | _rethrow = _caught; |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2306 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2307 | } else { |
| 2308 | // exception in catch block |
| 2309 | _rethrow = objc_exception_extract(&d); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2310 | _call_try_exit = false; |
| 2311 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2312 | } |
| 2313 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2314 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2315 | |
| 2316 | finally: |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2317 | if (_call_try_exit) |
| 2318 | objc_exception_try_exit(&d); |
| 2319 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2320 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2321 | ... dispatch to finally destination ... |
| 2322 | |
| 2323 | finally_rethrow: |
| 2324 | objc_exception_throw(_rethrow); |
| 2325 | |
| 2326 | finally_end: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
| 2329 | This framework differs slightly from the one gcc uses, in that gcc |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2330 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 2331 | and if the object should be rethrown. This breaks in the face of |
| 2332 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2333 | |
| 2334 | We specialize this framework for a few particular circumstances: |
| 2335 | |
| 2336 | - If there are no catch blocks, then we avoid emitting the second |
| 2337 | exception handling context. |
| 2338 | |
| 2339 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 2340 | e)) we avoid emitting the code to rethrow an uncaught exception. |
| 2341 | |
| 2342 | - FIXME: If there is no @finally block we can do a few more |
| 2343 | simplifications. |
| 2344 | |
| 2345 | Rethrows and Jumps-Through-Finally |
| 2346 | -- |
| 2347 | |
| 2348 | Support for implicit rethrows and jumping through the finally block is |
| 2349 | handled by storing the current exception-handling context in |
| 2350 | ObjCEHStack. |
| 2351 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2352 | In order to implement proper @finally semantics, we support one basic |
| 2353 | mechanism for jumping through the finally block to an arbitrary |
| 2354 | destination. Constructs which generate exits from a @try or @catch |
| 2355 | block use this mechanism to implement the proper semantics by chaining |
| 2356 | jumps, as necessary. |
| 2357 | |
| 2358 | This mechanism works like the one used for indirect goto: we |
| 2359 | arbitrarily assign an ID to each destination and store the ID for the |
| 2360 | destination in a variable prior to entering the finally block. At the |
| 2361 | end of the finally block we simply create a switch to the proper |
| 2362 | destination. |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2363 | |
| 2364 | Code gen for @synchronized(expr) stmt; |
| 2365 | Effectively generating code for: |
| 2366 | objc_sync_enter(expr); |
| 2367 | @try stmt @finally { objc_sync_exit(expr); } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2368 | */ |
| 2369 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2370 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 2371 | const Stmt &S) { |
| 2372 | bool isTry = isa<ObjCAtTryStmt>(S); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2373 | // Create various blocks we refer to for handling @finally. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2374 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2375 | llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit"); |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2376 | llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit"); |
| 2377 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
| 2378 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2379 | |
| 2380 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 2381 | // evaluation of the expression must occur before we enter the |
| 2382 | // @synchronized. We can safely avoid a temp here because jumps into |
| 2383 | // @synchronized are illegal & this will dominate uses. |
| 2384 | llvm::Value *SyncArg = 0; |
| 2385 | if (!isTry) { |
| 2386 | SyncArg = |
| 2387 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 2388 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 2389 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2390 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2391 | |
| 2392 | // Push an EH context entry, used for handling rethrows and jumps |
| 2393 | // through finally. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2394 | CGF.PushCleanupBlock(FinallyBlock); |
| 2395 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2396 | CGF.ObjCEHValueStack.push_back(0); |
| 2397 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2398 | // Allocate memory for the exception data and rethrow pointer. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2399 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 2400 | "exceptiondata.ptr"); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2401 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy, |
| 2402 | "_rethrow"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2403 | llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty, |
| 2404 | "_call_try_exit"); |
| 2405 | CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr); |
| 2406 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2407 | // Enter a new try block and call setjmp. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2408 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2409 | llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0, |
| 2410 | "jmpbufarray"); |
| 2411 | JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2412 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2413 | JmpBufPtr, "result"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2414 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2415 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 2416 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2417 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2418 | TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2419 | |
| 2420 | // Emit the @try block. |
| 2421 | CGF.EmitBlock(TryBlock); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2422 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 2423 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2424 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2425 | |
| 2426 | // Emit the "exception in @try" block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2427 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2428 | |
| 2429 | // Retrieve the exception object. We may emit multiple blocks but |
| 2430 | // nothing can cross this so the value is already in SSA form. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2431 | llvm::Value *Caught = |
| 2432 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2433 | ExceptionData, "caught"); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2434 | CGF.ObjCEHValueStack.back() = Caught; |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2435 | if (!isTry) |
| 2436 | { |
| 2437 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2438 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2439 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2440 | } |
| 2441 | else if (const ObjCAtCatchStmt* CatchStmt = |
| 2442 | cast<ObjCAtTryStmt>(S).getCatchStmts()) |
| 2443 | { |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2444 | // Enter a new exception try block (in case a @catch block throws |
| 2445 | // an exception). |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2446 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2447 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2448 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2449 | JmpBufPtr, "result"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2450 | llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2451 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2452 | llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch"); |
| 2453 | llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler"); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2454 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2455 | |
| 2456 | CGF.EmitBlock(CatchBlock); |
| 2457 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2458 | // Handle catch list. As a special case we check if everything is |
| 2459 | // matched and avoid generating code for falling off the end if |
| 2460 | // so. |
| 2461 | bool AllMatched = false; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2462 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2463 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2464 | |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2465 | const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2466 | const PointerType *PT = 0; |
| 2467 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2468 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2469 | if (!CatchParam) { |
| 2470 | AllMatched = true; |
| 2471 | } else { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2472 | PT = CatchParam->getType()->getAsPointerType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2473 | |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 2474 | // catch(id e) always matches. |
| 2475 | // FIXME: For the time being we also match id<X>; this should |
| 2476 | // be rejected by Sema instead. |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2477 | if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) || |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2478 | CatchParam->getType()->isObjCQualifiedIdType()) |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2479 | AllMatched = true; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2482 | if (AllMatched) { |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2483 | if (CatchParam) { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2484 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2485 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2486 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2487 | } |
Anders Carlsson | 1452f55 | 2008-09-11 08:21:54 +0000 | [diff] [blame] | 2488 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2489 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2490 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2491 | break; |
| 2492 | } |
| 2493 | |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2494 | assert(PT && "Unexpected non-pointer type in @catch"); |
| 2495 | QualType T = PT->getPointeeType(); |
Anders Carlsson | 4b7ff6e | 2008-09-11 06:35:14 +0000 | [diff] [blame] | 2496 | const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2497 | assert(ObjCType && "Catch parameter must have Objective-C type!"); |
| 2498 | |
| 2499 | // Check if the @catch block matches the exception object. |
| 2500 | llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl()); |
| 2501 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2502 | llvm::Value *Match = |
| 2503 | CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(), |
| 2504 | Class, Caught, "match"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2505 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2506 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2507 | |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2508 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2509 | MatchedBlock, NextCatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2510 | |
| 2511 | // Emit the @catch block. |
| 2512 | CGF.EmitBlock(MatchedBlock); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2513 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2514 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2515 | |
| 2516 | llvm::Value *Tmp = |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2517 | CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()), |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2518 | "tmp"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2519 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2520 | |
| 2521 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2522 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2523 | |
| 2524 | CGF.EmitBlock(NextCatchBlock); |
| 2525 | } |
| 2526 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2527 | if (!AllMatched) { |
| 2528 | // None of the handlers caught the exception, so store it to be |
| 2529 | // rethrown at the end of the @finally block. |
| 2530 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2531 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
| 2534 | // Emit the exception handler for the @catch blocks. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2535 | CGF.EmitBlock(CatchHandler); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2536 | CGF.Builder.CreateStore( |
| 2537 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2538 | ExceptionData), |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2539 | RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2540 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2541 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2542 | } else { |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2543 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2544 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2545 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2548 | // Pop the exception-handling stack entry. It is important to do |
| 2549 | // this now, because the code in the @finally block is not in this |
| 2550 | // context. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2551 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 2552 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2553 | CGF.ObjCEHValueStack.pop_back(); |
| 2554 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2555 | // Emit the @finally block. |
| 2556 | CGF.EmitBlock(FinallyBlock); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2557 | llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp"); |
| 2558 | |
| 2559 | CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit); |
| 2560 | |
| 2561 | CGF.EmitBlock(FinallyExit); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2562 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2563 | |
| 2564 | CGF.EmitBlock(FinallyNoExit); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2565 | if (isTry) { |
| 2566 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 2567 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 2568 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2569 | } else { |
| 2570 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 2571 | // @synchronized. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2572 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg); |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 2573 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2574 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2575 | // Emit the switch block |
| 2576 | if (Info.SwitchBlock) |
| 2577 | CGF.EmitBlock(Info.SwitchBlock); |
| 2578 | if (Info.EndBlock) |
| 2579 | CGF.EmitBlock(Info.EndBlock); |
| 2580 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2581 | CGF.EmitBlock(FinallyRethrow); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2582 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2583 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2584 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2585 | |
| 2586 | CGF.EmitBlock(FinallyEnd); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2587 | } |
| 2588 | |
| 2589 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2590 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2591 | llvm::Value *ExceptionAsObject; |
| 2592 | |
| 2593 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 2594 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
| 2595 | ExceptionAsObject = |
| 2596 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 2597 | } else { |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2598 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2599 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2600 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2601 | } |
| 2602 | |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2603 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2604 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2605 | |
| 2606 | // Clear the insertion point to indicate we are in unreachable code. |
| 2607 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2608 | } |
| 2609 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2610 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2611 | /// object: objc_read_weak (id *src) |
| 2612 | /// |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2613 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2614 | llvm::Value *AddrWeakObj) |
| 2615 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2616 | const llvm::Type* DestTy = |
| 2617 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2618 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2619 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2620 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2621 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2622 | return read_weak; |
| 2623 | } |
| 2624 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2625 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 2626 | /// objc_assign_weak (id src, id *dst) |
| 2627 | /// |
| 2628 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 2629 | llvm::Value *src, llvm::Value *dst) |
| 2630 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2631 | const llvm::Type * SrcTy = src->getType(); |
| 2632 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2633 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2634 | assert(Size <= 8 && "does not support size > 8"); |
| 2635 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2636 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2637 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2638 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2639 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2640 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 2641 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2642 | src, dst, "weakassign"); |
| 2643 | return; |
| 2644 | } |
| 2645 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2646 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 2647 | /// objc_assign_global (id src, id *dst) |
| 2648 | /// |
| 2649 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 2650 | llvm::Value *src, llvm::Value *dst) |
| 2651 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2652 | const llvm::Type * SrcTy = src->getType(); |
| 2653 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2654 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2655 | assert(Size <= 8 && "does not support size > 8"); |
| 2656 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2657 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2658 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2659 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2660 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2661 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2662 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2663 | src, dst, "globalassign"); |
| 2664 | return; |
| 2665 | } |
| 2666 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2667 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 2668 | /// objc_assign_ivar (id src, id *dst) |
| 2669 | /// |
| 2670 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 2671 | llvm::Value *src, llvm::Value *dst) |
| 2672 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2673 | const llvm::Type * SrcTy = src->getType(); |
| 2674 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2675 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2676 | assert(Size <= 8 && "does not support size > 8"); |
| 2677 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2678 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2679 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2680 | } |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2681 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2682 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2683 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(), |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2684 | src, dst, "assignivar"); |
| 2685 | return; |
| 2686 | } |
| 2687 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2688 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 2689 | /// objc_assign_strongCast (id src, id *dst) |
| 2690 | /// |
| 2691 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 2692 | llvm::Value *src, llvm::Value *dst) |
| 2693 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2694 | const llvm::Type * SrcTy = src->getType(); |
| 2695 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2696 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2697 | assert(Size <= 8 && "does not support size > 8"); |
| 2698 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2699 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2700 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2701 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2702 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2703 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2704 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2705 | src, dst, "weakassign"); |
| 2706 | return; |
| 2707 | } |
| 2708 | |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2709 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 2710 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2711 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 2712 | QualType ObjectTy, |
| 2713 | llvm::Value *BaseValue, |
| 2714 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2715 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 2716 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2717 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 2718 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2719 | } |
| 2720 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2721 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 2722 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2723 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2724 | uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2725 | return llvm::ConstantInt::get( |
| 2726 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 2727 | Offset); |
| 2728 | } |
| 2729 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2730 | /* *** Private Interface *** */ |
| 2731 | |
| 2732 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 2733 | /// level information. |
| 2734 | /// |
| 2735 | /// See: <rdr://4810609&4810587&4810587> |
| 2736 | /// struct IMAGE_INFO { |
| 2737 | /// unsigned version; |
| 2738 | /// unsigned flags; |
| 2739 | /// }; |
| 2740 | enum ImageInfoFlags { |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2741 | eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what |
| 2742 | // this implies. |
| 2743 | eImageInfo_GarbageCollected = (1 << 1), |
| 2744 | eImageInfo_GCOnly = (1 << 2), |
| 2745 | eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set. |
| 2746 | |
| 2747 | // A flag indicating that the module has no instances of an |
| 2748 | // @synthesize of a superclass variable. <rdar://problem/6803242> |
| 2749 | eImageInfo_CorrectedSynthesize = (1 << 4) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2750 | }; |
| 2751 | |
| 2752 | void CGObjCMac::EmitImageInfo() { |
| 2753 | unsigned version = 0; // Version is unused? |
| 2754 | unsigned flags = 0; |
| 2755 | |
| 2756 | // FIXME: Fix and continue? |
| 2757 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 2758 | flags |= eImageInfo_GarbageCollected; |
| 2759 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 2760 | flags |= eImageInfo_GCOnly; |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2761 | |
| 2762 | // We never allow @synthesize of a superclass property. |
| 2763 | flags |= eImageInfo_CorrectedSynthesize; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2764 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2765 | // Emitted as int[2]; |
| 2766 | llvm::Constant *values[2] = { |
| 2767 | llvm::ConstantInt::get(llvm::Type::Int32Ty, version), |
| 2768 | llvm::ConstantInt::get(llvm::Type::Int32Ty, flags) |
| 2769 | }; |
| 2770 | llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2771 | |
| 2772 | const char *Section; |
| 2773 | if (ObjCABI == 1) |
| 2774 | Section = "__OBJC, __image_info,regular"; |
| 2775 | else |
| 2776 | Section = "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2777 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2778 | CreateMetadataVar("\01L_OBJC_IMAGE_INFO", |
| 2779 | llvm::ConstantArray::get(AT, values, 2), |
| 2780 | Section, |
| 2781 | 0, |
| 2782 | true); |
| 2783 | GV->setConstant(true); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2784 | } |
| 2785 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2786 | |
| 2787 | // struct objc_module { |
| 2788 | // unsigned long version; |
| 2789 | // unsigned long size; |
| 2790 | // const char *name; |
| 2791 | // Symtab symtab; |
| 2792 | // }; |
| 2793 | |
| 2794 | // FIXME: Get from somewhere |
| 2795 | static const int ModuleVersion = 7; |
| 2796 | |
| 2797 | void CGObjCMac::EmitModuleInfo() { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 2798 | uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2799 | |
| 2800 | std::vector<llvm::Constant*> Values(4); |
| 2801 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion); |
| 2802 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2803 | // This used to be the filename, now it is unused. <rdr://4327263> |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2804 | Values[2] = GetClassName(&CGM.getContext().Idents.get("")); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2805 | Values[3] = EmitModuleSymbols(); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2806 | CreateMetadataVar("\01L_OBJC_MODULES", |
| 2807 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
| 2808 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2809 | 4, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
| 2812 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2813 | unsigned NumClasses = DefinedClasses.size(); |
| 2814 | unsigned NumCategories = DefinedCategories.size(); |
| 2815 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2816 | // Return null if no symbols were defined. |
| 2817 | if (!NumClasses && !NumCategories) |
| 2818 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
| 2819 | |
| 2820 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2821 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2822 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
| 2823 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 2824 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
| 2825 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2826 | // The runtime expects exactly the list of defined classes followed |
| 2827 | // by the list of defined categories, in a single array. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2828 | std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2829 | for (unsigned i=0; i<NumClasses; i++) |
| 2830 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 2831 | ObjCTypes.Int8PtrTy); |
| 2832 | for (unsigned i=0; i<NumCategories; i++) |
| 2833 | Symbols[NumClasses + i] = |
| 2834 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 2835 | ObjCTypes.Int8PtrTy); |
| 2836 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2837 | Values[4] = |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2838 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2839 | NumClasses + NumCategories), |
| 2840 | Symbols); |
| 2841 | |
| 2842 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2843 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2844 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2845 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 2846 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2847 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2848 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
| 2849 | } |
| 2850 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2851 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2852 | const ObjCInterfaceDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2853 | LazySymbols.insert(ID->getIdentifier()); |
| 2854 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2855 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 2856 | |
| 2857 | if (!Entry) { |
| 2858 | llvm::Constant *Casted = |
| 2859 | llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()), |
| 2860 | ObjCTypes.ClassPtrTy); |
| 2861 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2862 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 2863 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2864 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2865 | } |
| 2866 | |
| 2867 | return Builder.CreateLoad(Entry, false, "tmp"); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2868 | } |
| 2869 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2870 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2871 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 2872 | |
| 2873 | if (!Entry) { |
| 2874 | llvm::Constant *Casted = |
| 2875 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 2876 | ObjCTypes.SelectorPtrTy); |
| 2877 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2878 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 2879 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2880 | 4, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2881 | } |
| 2882 | |
| 2883 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 2884 | } |
| 2885 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 2886 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2887 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2888 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2889 | if (!Entry) |
| 2890 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 2891 | llvm::ConstantArray::get(Ident->getName()), |
| 2892 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 2893 | 1, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2894 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2895 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2896 | } |
| 2897 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 2898 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 2899 | /// ivar layout bitmap. |
| 2900 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
| 2901 | const ObjCCommonTypesHelper &ObjCTypes) { |
| 2902 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 2903 | } |
| 2904 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 2905 | static QualType::GCAttrTypes GetGCAttrTypeForType(ASTContext &Ctx, |
| 2906 | QualType FQT) { |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 2907 | if (FQT.isObjCGCStrong()) |
| 2908 | return QualType::Strong; |
| 2909 | |
| 2910 | if (FQT.isObjCGCWeak()) |
| 2911 | return QualType::Weak; |
| 2912 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 2913 | if (Ctx.isObjCObjectPointerType(FQT)) |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 2914 | return QualType::Strong; |
| 2915 | |
| 2916 | if (const PointerType *PT = FQT->getAsPointerType()) |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 2917 | return GetGCAttrTypeForType(Ctx, PT->getPointeeType()); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 2918 | |
| 2919 | return QualType::GCNone; |
| 2920 | } |
| 2921 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 2922 | void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT, |
| 2923 | unsigned int BytePos, |
| 2924 | bool ForStrongLayout, |
| 2925 | bool &HasUnion) { |
| 2926 | const RecordDecl *RD = RT->getDecl(); |
| 2927 | // FIXME - Use iterator. |
| 2928 | llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(CGM.getContext()), |
| 2929 | RD->field_end(CGM.getContext())); |
| 2930 | const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0)); |
| 2931 | const llvm::StructLayout *RecLayout = |
| 2932 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); |
| 2933 | |
| 2934 | BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos, |
| 2935 | ForStrongLayout, HasUnion); |
| 2936 | } |
| 2937 | |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 2938 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2939 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2940 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2941 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2942 | unsigned int BytePos, bool ForStrongLayout, |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 2943 | bool &HasUnion) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2944 | bool IsUnion = (RD && RD->isUnion()); |
| 2945 | uint64_t MaxUnionIvarSize = 0; |
| 2946 | uint64_t MaxSkippedUnionIvarSize = 0; |
| 2947 | FieldDecl *MaxField = 0; |
| 2948 | FieldDecl *MaxSkippedField = 0; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2949 | FieldDecl *LastFieldBitfield = 0; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame^] | 2950 | uint64_t MaxFieldOffset = 0; |
| 2951 | uint64_t MaxSkippedFieldOffset = 0; |
| 2952 | uint64_t LastBitfieldOffset = 0; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2953 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2954 | if (RecFields.empty()) |
| 2955 | return; |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2956 | unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0); |
| 2957 | unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth(); |
| 2958 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2959 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2960 | FieldDecl *Field = RecFields[i]; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame^] | 2961 | unsigned FieldOffset = GetFieldBaseOffset(OI, Layout, Field); |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 2962 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2963 | // Skip over unnamed or bitfields |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2964 | if (!Field->getIdentifier() || Field->isBitField()) { |
| 2965 | LastFieldBitfield = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame^] | 2966 | LastBitfieldOffset = FieldOffset; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2967 | continue; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2968 | } |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 2969 | |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2970 | LastFieldBitfield = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2971 | QualType FQT = Field->getType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2972 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2973 | if (FQT->isUnionType()) |
| 2974 | HasUnion = true; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2975 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 2976 | BuildAggrIvarRecordLayout(FQT->getAsRecordType(), |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 2977 | BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 2978 | ForStrongLayout, HasUnion); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2979 | continue; |
| 2980 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2981 | |
| 2982 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2983 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 2984 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2985 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 2986 | assert(CArray && "only array with known element size is supported"); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2987 | FQT = CArray->getElementType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2988 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 2989 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 2990 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2991 | ElCount *= CArray->getSize().getZExtValue(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2992 | FQT = CArray->getElementType(); |
| 2993 | } |
| 2994 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2995 | assert(!FQT->isUnionType() && |
| 2996 | "layout for array of unions not supported"); |
| 2997 | if (FQT->isRecordType()) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 2998 | int OldIndex = IvarsInfo.size() - 1; |
| 2999 | int OldSkIndex = SkipIvars.size() -1; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3000 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3001 | const RecordType *RT = FQT->getAsRecordType(); |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3002 | BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3003 | ForStrongLayout, HasUnion); |
| 3004 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3005 | // Replicate layout information for each array element. Note that |
| 3006 | // one element is already done. |
| 3007 | uint64_t ElIx = 1; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3008 | for (int FirstIndex = IvarsInfo.size() - 1, |
| 3009 | FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3010 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3011 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 3012 | IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx, |
| 3013 | IvarsInfo[i].ivar_size)); |
| 3014 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) |
| 3015 | SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx, |
| 3016 | SkipIvars[i].ivar_size)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3017 | } |
| 3018 | continue; |
| 3019 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3020 | } |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3021 | // At this point, we are done with Record/Union and array there of. |
| 3022 | // For other arrays we are down to its element type. |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3023 | QualType::GCAttrTypes GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3024 | |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3025 | unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType()); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3026 | if ((ForStrongLayout && GCAttr == QualType::Strong) |
| 3027 | || (!ForStrongLayout && GCAttr == QualType::Weak)) { |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3028 | if (IsUnion) { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3029 | uint64_t UnionIvarSize = FieldSize / WordSizeInBits; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3030 | if (UnionIvarSize > MaxUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3031 | MaxUnionIvarSize = UnionIvarSize; |
| 3032 | MaxField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame^] | 3033 | MaxFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3034 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3035 | } else { |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3036 | IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3037 | FieldSize / WordSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3038 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3039 | } else if ((ForStrongLayout && |
| 3040 | (GCAttr == QualType::GCNone || GCAttr == QualType::Weak)) |
| 3041 | || (!ForStrongLayout && GCAttr != QualType::Weak)) { |
| 3042 | if (IsUnion) { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3043 | // FIXME: Why the asymmetry? We divide by word size in bits on |
| 3044 | // other side. |
| 3045 | uint64_t UnionIvarSize = FieldSize; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3046 | if (UnionIvarSize > MaxSkippedUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3047 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 3048 | MaxSkippedField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame^] | 3049 | MaxSkippedFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3050 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3051 | } else { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3052 | // FIXME: Why the asymmetry, we divide by byte size in bits here? |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3053 | SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3054 | FieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3055 | } |
| 3056 | } |
| 3057 | } |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3058 | |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3059 | if (LastFieldBitfield) { |
| 3060 | // Last field was a bitfield. Must update skip info. |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3061 | Expr *BitWidth = LastFieldBitfield->getBitWidth(); |
| 3062 | uint64_t BitFieldSize = |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 3063 | BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue(); |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3064 | GC_IVAR skivar; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame^] | 3065 | skivar.ivar_bytepos = BytePos + LastBitfieldOffset; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3066 | skivar.ivar_size = (BitFieldSize / ByteSizeInBits) |
| 3067 | + ((BitFieldSize % ByteSizeInBits) != 0); |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3068 | SkipIvars.push_back(skivar); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3069 | } |
| 3070 | |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3071 | if (MaxField) |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame^] | 3072 | IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3073 | MaxUnionIvarSize)); |
| 3074 | if (MaxSkippedField) |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame^] | 3075 | SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3076 | MaxSkippedUnionIvarSize)); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3077 | } |
| 3078 | |
| 3079 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 3080 | /// implementation for the __strong or __weak case. |
| 3081 | /// The layout map displays which words in ivar list must be skipped |
| 3082 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 3083 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
| 3084 | /// of words to skip and right nibble is count of words to scan. So, each |
| 3085 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
| 3086 | /// represented by a 0x00 byte which also ends the string. |
| 3087 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 3088 | /// - id, Class |
| 3089 | /// - object * |
| 3090 | /// - __strong anything |
| 3091 | /// |
| 3092 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 3093 | /// - __weak anything |
| 3094 | /// |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3095 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3096 | const ObjCImplementationDecl *OMD, |
| 3097 | bool ForStrongLayout) { |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3098 | bool hasUnion = false; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3099 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3100 | unsigned int WordsToScan, WordsToSkip; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3101 | const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 3102 | if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) |
| 3103 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3104 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3105 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3106 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3107 | CGM.getContext().CollectObjCIvars(OI, RecFields); |
| 3108 | if (RecFields.empty()) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3109 | return llvm::Constant::getNullValue(PtrTy); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3110 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3111 | SkipIvars.clear(); |
| 3112 | IvarsInfo.clear(); |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 3113 | |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 3114 | BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion); |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3115 | if (IvarsInfo.empty()) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3116 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3117 | |
| 3118 | // Sort on byte position in case we encounterred a union nested in |
| 3119 | // the ivar list. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3120 | if (hasUnion && !IvarsInfo.empty()) |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 3121 | std::sort(IvarsInfo.begin(), IvarsInfo.end()); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3122 | if (hasUnion && !SkipIvars.empty()) |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 3123 | std::sort(SkipIvars.begin(), SkipIvars.end()); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3124 | |
| 3125 | // Build the string of skip/scan nibbles |
Fariborz Jahanian | 8c2f2d1 | 2009-04-24 17:15:27 +0000 | [diff] [blame] | 3126 | llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3127 | unsigned int WordSize = |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3128 | CGM.getTypes().getTargetData().getTypePaddedSize(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3129 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 3130 | WordsToSkip = 0; |
| 3131 | WordsToScan = IvarsInfo[0].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 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 3134 | WordsToScan = IvarsInfo[0].ivar_size; |
| 3135 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3136 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3137 | unsigned int TailPrevGCObjC = |
| 3138 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3139 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3140 | // consecutive 'scanned' object pointers. |
| 3141 | WordsToScan += IvarsInfo[i].ivar_size; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3142 | } else { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3143 | // Skip over 'gc'able object pointer which lay over each other. |
| 3144 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 3145 | continue; |
| 3146 | // Must skip over 1 or more words. We save current skip/scan values |
| 3147 | // and start a new pair. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3148 | SKIP_SCAN SkScan; |
| 3149 | SkScan.skip = WordsToSkip; |
| 3150 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3151 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3152 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3153 | // Skip the hole. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3154 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 3155 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3156 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3157 | WordsToSkip = 0; |
| 3158 | WordsToScan = IvarsInfo[i].ivar_size; |
| 3159 | } |
| 3160 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3161 | if (WordsToScan > 0) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3162 | SKIP_SCAN SkScan; |
| 3163 | SkScan.skip = WordsToSkip; |
| 3164 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3165 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3166 | } |
| 3167 | |
| 3168 | bool BytesSkipped = false; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3169 | if (!SkipIvars.empty()) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3170 | unsigned int LastIndex = SkipIvars.size()-1; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3171 | int LastByteSkipped = |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3172 | SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size; |
| 3173 | LastIndex = IvarsInfo.size()-1; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3174 | int LastByteScanned = |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3175 | IvarsInfo[LastIndex].ivar_bytepos + |
| 3176 | IvarsInfo[LastIndex].ivar_size * WordSize; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3177 | BytesSkipped = (LastByteSkipped > LastByteScanned); |
| 3178 | // 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] | 3179 | if (BytesSkipped) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3180 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3181 | SKIP_SCAN SkScan; |
| 3182 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
| 3183 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3184 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3185 | } |
| 3186 | } |
| 3187 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 3188 | // as 0xMN. |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3189 | int SkipScan = SkipScanIvars.size()-1; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3190 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3191 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 3192 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 3193 | // 0xM0 followed by 0x0N detected. |
| 3194 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 3195 | for (int j = i+1; j < SkipScan; j++) |
| 3196 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 3197 | --SkipScan; |
| 3198 | } |
| 3199 | } |
| 3200 | |
| 3201 | // Generate the string. |
| 3202 | std::string BitMap; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3203 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3204 | unsigned char byte; |
| 3205 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 3206 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 3207 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 3208 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
| 3209 | |
| 3210 | if (skip_small > 0 || skip_big > 0) |
| 3211 | BytesSkipped = true; |
| 3212 | // first skip big. |
| 3213 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 3214 | BitMap += (unsigned char)(0xf0); |
| 3215 | |
| 3216 | // next (skip small, scan) |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3217 | if (skip_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3218 | byte = skip_small << 4; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3219 | if (scan_big > 0) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3220 | byte |= 0xf; |
| 3221 | --scan_big; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3222 | } else if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3223 | byte |= scan_small; |
| 3224 | scan_small = 0; |
| 3225 | } |
| 3226 | BitMap += byte; |
| 3227 | } |
| 3228 | // next scan big |
| 3229 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 3230 | BitMap += (unsigned char)(0x0f); |
| 3231 | // last scan small |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3232 | if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3233 | byte = scan_small; |
| 3234 | BitMap += byte; |
| 3235 | } |
| 3236 | } |
| 3237 | // null terminate string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3238 | unsigned char zero = 0; |
| 3239 | BitMap += zero; |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3240 | |
| 3241 | if (CGM.getLangOptions().ObjCGCBitmapPrint) { |
| 3242 | printf("\n%s ivar layout for class '%s': ", |
| 3243 | ForStrongLayout ? "strong" : "weak", |
| 3244 | OMD->getClassInterface()->getNameAsCString()); |
| 3245 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
| 3246 | for (unsigned i = 0; i < BitMap.size(); i++) |
| 3247 | if (!(s[i] & 0xf0)) |
| 3248 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3249 | else |
| 3250 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3251 | printf("\n"); |
| 3252 | } |
| 3253 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3254 | // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as |
| 3255 | // final layout. |
| 3256 | if (ForStrongLayout && !BytesSkipped) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3257 | return llvm::Constant::getNullValue(PtrTy); |
| 3258 | llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 3259 | llvm::ConstantArray::get(BitMap.c_str()), |
| 3260 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3261 | 1, true); |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3262 | return getConstantGEP(Entry, 0, 0); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3263 | } |
| 3264 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3265 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3266 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 3267 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3268 | // FIXME: Avoid std::string copying. |
| 3269 | if (!Entry) |
| 3270 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
| 3271 | llvm::ConstantArray::get(Sel.getAsString()), |
| 3272 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3273 | 1, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3274 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3275 | return getConstantGEP(Entry, 0, 0); |
| 3276 | } |
| 3277 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3278 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3279 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3280 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 3281 | } |
| 3282 | |
| 3283 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3284 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3285 | return GetMethodVarName(&CGM.getContext().Idents.get(Name)); |
| 3286 | } |
| 3287 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 3288 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3289 | std::string TypeStr; |
| 3290 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 3291 | |
| 3292 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3293 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3294 | if (!Entry) |
| 3295 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3296 | llvm::ConstantArray::get(TypeStr), |
| 3297 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3298 | 1, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3299 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3300 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3301 | } |
| 3302 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3303 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3304 | std::string TypeStr; |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3305 | CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D), |
| 3306 | TypeStr); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3307 | |
| 3308 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 3309 | |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3310 | if (!Entry) |
| 3311 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3312 | llvm::ConstantArray::get(TypeStr), |
| 3313 | "__TEXT,__cstring,cstring_literals", |
| 3314 | 1, true); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3315 | |
| 3316 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3317 | } |
| 3318 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3319 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3320 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3321 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
| 3322 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3323 | if (!Entry) |
| 3324 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
| 3325 | llvm::ConstantArray::get(Ident->getName()), |
| 3326 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3327 | 1, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3328 | |
| 3329 | return getConstantGEP(Entry, 0, 0); |
| 3330 | } |
| 3331 | |
| 3332 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3333 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3334 | llvm::Constant * |
| 3335 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 3336 | const Decl *Container) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3337 | std::string TypeStr; |
| 3338 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3339 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 3340 | } |
| 3341 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3342 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
| 3343 | const ObjCContainerDecl *CD, |
| 3344 | std::string &NameOut) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 3345 | NameOut = '\01'; |
| 3346 | NameOut += (D->isInstanceMethod() ? '-' : '+'); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3347 | NameOut += '['; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 3348 | assert (CD && "Missing container decl in GetNameForMethod"); |
| 3349 | NameOut += CD->getNameAsString(); |
Fariborz Jahanian | 1e9aef3 | 2009-04-16 18:34:20 +0000 | [diff] [blame] | 3350 | if (const ObjCCategoryImplDecl *CID = |
| 3351 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) { |
| 3352 | NameOut += '('; |
| 3353 | NameOut += CID->getNameAsString(); |
| 3354 | NameOut+= ')'; |
| 3355 | } |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3356 | NameOut += ' '; |
| 3357 | NameOut += D->getSelector().getAsString(); |
| 3358 | NameOut += ']'; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3359 | } |
| 3360 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3361 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3362 | EmitModuleInfo(); |
| 3363 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3364 | // Emit the dummy bodies for any protocols which were referenced but |
| 3365 | // never defined. |
| 3366 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
| 3367 | i = Protocols.begin(), e = Protocols.end(); i != e; ++i) { |
| 3368 | if (i->second->hasInitializer()) |
| 3369 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3370 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3371 | std::vector<llvm::Constant*> Values(5); |
| 3372 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 3373 | Values[1] = GetClassName(i->first); |
| 3374 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 3375 | Values[3] = Values[4] = |
| 3376 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 3377 | i->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 3378 | i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 3379 | Values)); |
| 3380 | } |
| 3381 | |
| 3382 | std::vector<llvm::Constant*> Used; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3383 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3384 | e = UsedGlobals.end(); i != e; ++i) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3385 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3386 | } |
| 3387 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3388 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3389 | llvm::GlobalValue *GV = |
| 3390 | new llvm::GlobalVariable(AT, false, |
| 3391 | llvm::GlobalValue::AppendingLinkage, |
| 3392 | llvm::ConstantArray::get(AT, Used), |
| 3393 | "llvm.used", |
| 3394 | &CGM.getModule()); |
| 3395 | |
| 3396 | GV->setSection("llvm.metadata"); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3397 | |
| 3398 | // Add assembler directives to add lazy undefined symbol references |
| 3399 | // for classes which are referenced but not defined. This is |
| 3400 | // important for correct linker interaction. |
| 3401 | |
| 3402 | // FIXME: Uh, this isn't particularly portable. |
| 3403 | std::stringstream s; |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3404 | |
| 3405 | if (!CGM.getModule().getModuleInlineAsm().empty()) |
| 3406 | s << "\n"; |
| 3407 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3408 | for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(), |
| 3409 | e = LazySymbols.end(); i != e; ++i) { |
| 3410 | s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n"; |
| 3411 | } |
| 3412 | for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(), |
| 3413 | e = DefinedSymbols.end(); i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3414 | s << "\t.objc_class_name_" << (*i)->getName() << "=0\n" |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3415 | << "\t.globl .objc_class_name_" << (*i)->getName() << "\n"; |
| 3416 | } |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3417 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3418 | CGM.getModule().appendModuleInlineAsm(s.str()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3419 | } |
| 3420 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3421 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3422 | : CGObjCCommonMac(cgm), |
| 3423 | ObjCTypes(cgm) |
| 3424 | { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3425 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3426 | ObjCABI = 2; |
| 3427 | } |
| 3428 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3429 | /* *** */ |
| 3430 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3431 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3432 | : CGM(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3433 | { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3434 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3435 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3436 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3437 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3438 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3439 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3440 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3441 | Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 3442 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3443 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 3444 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3445 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3446 | |
| 3447 | // FIXME: It would be nice to unify this with the opaque type, so |
| 3448 | // that the IR comes out a bit cleaner. |
| 3449 | const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 3450 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3451 | |
| 3452 | // I'm not sure I like this. The implicit coordination is a bit |
| 3453 | // gross. We should solve this in a reasonable fashion because this |
| 3454 | // is a pretty common task (match some runtime data structure with |
| 3455 | // an LLVM data structure). |
| 3456 | |
| 3457 | // FIXME: This is leaked. |
| 3458 | // FIXME: Merge with rewriter code? |
| 3459 | |
| 3460 | // struct _objc_super { |
| 3461 | // id self; |
| 3462 | // Class cls; |
| 3463 | // } |
| 3464 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3465 | SourceLocation(), |
| 3466 | &Ctx.Idents.get("_objc_super")); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3467 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3468 | Ctx.getObjCIdType(), 0, false)); |
| 3469 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3470 | Ctx.getObjCClassType(), 0, false)); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3471 | RD->completeDefinition(Ctx); |
| 3472 | |
| 3473 | SuperCTy = Ctx.getTagDeclType(RD); |
| 3474 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
| 3475 | |
| 3476 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3477 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 3478 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3479 | // struct _prop_t { |
| 3480 | // char *name; |
| 3481 | // char *attributes; |
| 3482 | // } |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 3483 | PropertyTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, NULL); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3484 | CGM.getModule().addTypeName("struct._prop_t", |
| 3485 | PropertyTy); |
| 3486 | |
| 3487 | // struct _prop_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3488 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3489 | // uint32_t count_of_properties; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3490 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3491 | // } |
| 3492 | PropertyListTy = llvm::StructType::get(IntTy, |
| 3493 | IntTy, |
| 3494 | llvm::ArrayType::get(PropertyTy, 0), |
| 3495 | NULL); |
| 3496 | CGM.getModule().addTypeName("struct._prop_list_t", |
| 3497 | PropertyListTy); |
| 3498 | // struct _prop_list_t * |
| 3499 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
| 3500 | |
| 3501 | // struct _objc_method { |
| 3502 | // SEL _cmd; |
| 3503 | // char *method_type; |
| 3504 | // char *_imp; |
| 3505 | // } |
| 3506 | MethodTy = llvm::StructType::get(SelectorPtrTy, |
| 3507 | Int8PtrTy, |
| 3508 | Int8PtrTy, |
| 3509 | NULL); |
| 3510 | CGM.getModule().addTypeName("struct._objc_method", MethodTy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3511 | |
| 3512 | // struct _objc_cache * |
| 3513 | CacheTy = llvm::OpaqueType::get(); |
| 3514 | CGM.getModule().addTypeName("struct._objc_cache", CacheTy); |
| 3515 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3516 | } |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3517 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3518 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3519 | : ObjCCommonTypesHelper(cgm) |
| 3520 | { |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3521 | // struct _objc_method_description { |
| 3522 | // SEL name; |
| 3523 | // char *types; |
| 3524 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3525 | MethodDescriptionTy = |
| 3526 | llvm::StructType::get(SelectorPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3527 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3528 | NULL); |
| 3529 | CGM.getModule().addTypeName("struct._objc_method_description", |
| 3530 | MethodDescriptionTy); |
| 3531 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3532 | // struct _objc_method_description_list { |
| 3533 | // int count; |
| 3534 | // struct _objc_method_description[1]; |
| 3535 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3536 | MethodDescriptionListTy = |
| 3537 | llvm::StructType::get(IntTy, |
| 3538 | llvm::ArrayType::get(MethodDescriptionTy, 0), |
| 3539 | NULL); |
| 3540 | CGM.getModule().addTypeName("struct._objc_method_description_list", |
| 3541 | MethodDescriptionListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3542 | |
| 3543 | // struct _objc_method_description_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3544 | MethodDescriptionListPtrTy = |
| 3545 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
| 3546 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3547 | // Protocol description structures |
| 3548 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3549 | // struct _objc_protocol_extension { |
| 3550 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 3551 | // struct _objc_method_description_list *optional_instance_methods; |
| 3552 | // struct _objc_method_description_list *optional_class_methods; |
| 3553 | // struct _objc_property_list *instance_properties; |
| 3554 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3555 | ProtocolExtensionTy = |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3556 | llvm::StructType::get(IntTy, |
| 3557 | MethodDescriptionListPtrTy, |
| 3558 | MethodDescriptionListPtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3559 | PropertyListPtrTy, |
| 3560 | NULL); |
| 3561 | CGM.getModule().addTypeName("struct._objc_protocol_extension", |
| 3562 | ProtocolExtensionTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3563 | |
| 3564 | // struct _objc_protocol_extension * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3565 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
| 3566 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3567 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3568 | |
| 3569 | llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(); |
| 3570 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3571 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3572 | const llvm::Type *T = |
| 3573 | llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3574 | LongTy, |
| 3575 | llvm::ArrayType::get(ProtocolTyHolder, 0), |
| 3576 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3577 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T); |
| 3578 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3579 | // struct _objc_protocol { |
| 3580 | // struct _objc_protocol_extension *isa; |
| 3581 | // char *protocol_name; |
| 3582 | // struct _objc_protocol **_objc_protocol_list; |
| 3583 | // struct _objc_method_description_list *instance_methods; |
| 3584 | // struct _objc_method_description_list *class_methods; |
| 3585 | // } |
| 3586 | T = llvm::StructType::get(ProtocolExtensionPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3587 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3588 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3589 | MethodDescriptionListPtrTy, |
| 3590 | MethodDescriptionListPtrTy, |
| 3591 | NULL); |
| 3592 | cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T); |
| 3593 | |
| 3594 | ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get()); |
| 3595 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3596 | ProtocolListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3597 | // struct _objc_protocol_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3598 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
| 3599 | |
| 3600 | ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get()); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3601 | CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3602 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3603 | |
| 3604 | // Class description structures |
| 3605 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3606 | // struct _objc_ivar { |
| 3607 | // char *ivar_name; |
| 3608 | // char *ivar_type; |
| 3609 | // int ivar_offset; |
| 3610 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3611 | IvarTy = llvm::StructType::get(Int8PtrTy, |
| 3612 | Int8PtrTy, |
| 3613 | IntTy, |
| 3614 | NULL); |
| 3615 | CGM.getModule().addTypeName("struct._objc_ivar", IvarTy); |
| 3616 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3617 | // struct _objc_ivar_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3618 | IvarListTy = llvm::OpaqueType::get(); |
| 3619 | CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy); |
| 3620 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
| 3621 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3622 | // struct _objc_method_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3623 | MethodListTy = llvm::OpaqueType::get(); |
| 3624 | CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy); |
| 3625 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
| 3626 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3627 | // struct _objc_class_extension * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3628 | ClassExtensionTy = |
| 3629 | llvm::StructType::get(IntTy, |
| 3630 | Int8PtrTy, |
| 3631 | PropertyListPtrTy, |
| 3632 | NULL); |
| 3633 | CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy); |
| 3634 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
| 3635 | |
| 3636 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3637 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3638 | // struct _objc_class { |
| 3639 | // Class isa; |
| 3640 | // Class super_class; |
| 3641 | // char *name; |
| 3642 | // long version; |
| 3643 | // long info; |
| 3644 | // long instance_size; |
| 3645 | // struct _objc_ivar_list *ivars; |
| 3646 | // struct _objc_method_list *methods; |
| 3647 | // struct _objc_cache *cache; |
| 3648 | // struct _objc_protocol_list *protocols; |
| 3649 | // char *ivar_layout; |
| 3650 | // struct _objc_class_ext *ext; |
| 3651 | // }; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3652 | T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3653 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3654 | Int8PtrTy, |
| 3655 | LongTy, |
| 3656 | LongTy, |
| 3657 | LongTy, |
| 3658 | IvarListPtrTy, |
| 3659 | MethodListPtrTy, |
| 3660 | CachePtrTy, |
| 3661 | ProtocolListPtrTy, |
| 3662 | Int8PtrTy, |
| 3663 | ClassExtensionPtrTy, |
| 3664 | NULL); |
| 3665 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T); |
| 3666 | |
| 3667 | ClassTy = cast<llvm::StructType>(ClassTyHolder.get()); |
| 3668 | CGM.getModule().addTypeName("struct._objc_class", ClassTy); |
| 3669 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
| 3670 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3671 | // struct _objc_category { |
| 3672 | // char *category_name; |
| 3673 | // char *class_name; |
| 3674 | // struct _objc_method_list *instance_method; |
| 3675 | // struct _objc_method_list *class_method; |
| 3676 | // uint32_t size; // sizeof(struct _objc_category) |
| 3677 | // struct _objc_property_list *instance_properties;// category's @property |
| 3678 | // } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3679 | CategoryTy = llvm::StructType::get(Int8PtrTy, |
| 3680 | Int8PtrTy, |
| 3681 | MethodListPtrTy, |
| 3682 | MethodListPtrTy, |
| 3683 | ProtocolListPtrTy, |
| 3684 | IntTy, |
| 3685 | PropertyListPtrTy, |
| 3686 | NULL); |
| 3687 | CGM.getModule().addTypeName("struct._objc_category", CategoryTy); |
| 3688 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3689 | // Global metadata structures |
| 3690 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3691 | // struct _objc_symtab { |
| 3692 | // long sel_ref_cnt; |
| 3693 | // SEL *refs; |
| 3694 | // short cls_def_cnt; |
| 3695 | // short cat_def_cnt; |
| 3696 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 3697 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3698 | SymtabTy = llvm::StructType::get(LongTy, |
| 3699 | SelectorPtrTy, |
| 3700 | ShortTy, |
| 3701 | ShortTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3702 | llvm::ArrayType::get(Int8PtrTy, 0), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3703 | NULL); |
| 3704 | CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy); |
| 3705 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
| 3706 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3707 | // struct _objc_module { |
| 3708 | // long version; |
| 3709 | // long size; // sizeof(struct _objc_module) |
| 3710 | // char *name; |
| 3711 | // struct _objc_symtab* symtab; |
| 3712 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3713 | ModuleTy = |
| 3714 | llvm::StructType::get(LongTy, |
| 3715 | LongTy, |
| 3716 | Int8PtrTy, |
| 3717 | SymtabPtrTy, |
| 3718 | NULL); |
| 3719 | CGM.getModule().addTypeName("struct._objc_module", ModuleTy); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3720 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 3721 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3722 | // FIXME: This is the size of the setjmp buffer and should be |
| 3723 | // target specific. 18 is what's used on 32-bit X86. |
| 3724 | uint64_t SetJmpBufferSize = 18; |
| 3725 | |
| 3726 | // Exceptions |
| 3727 | const llvm::Type *StackPtrTy = |
Daniel Dunbar | 1000491 | 2008-09-27 06:32:25 +0000 | [diff] [blame] | 3728 | llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3729 | |
| 3730 | ExceptionDataTy = |
| 3731 | llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty, |
| 3732 | SetJmpBufferSize), |
| 3733 | StackPtrTy, NULL); |
| 3734 | CGM.getModule().addTypeName("struct._objc_exception_data", |
| 3735 | ExceptionDataTy); |
| 3736 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3737 | } |
| 3738 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3739 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3740 | : ObjCCommonTypesHelper(cgm) |
| 3741 | { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3742 | // struct _method_list_t { |
| 3743 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 3744 | // uint32_t method_count; |
| 3745 | // struct _objc_method method_list[method_count]; |
| 3746 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3747 | MethodListnfABITy = llvm::StructType::get(IntTy, |
| 3748 | IntTy, |
| 3749 | llvm::ArrayType::get(MethodTy, 0), |
| 3750 | NULL); |
| 3751 | CGM.getModule().addTypeName("struct.__method_list_t", |
| 3752 | MethodListnfABITy); |
| 3753 | // struct method_list_t * |
| 3754 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3755 | |
| 3756 | // struct _protocol_t { |
| 3757 | // id isa; // NULL |
| 3758 | // const char * const protocol_name; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3759 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3760 | // const struct method_list_t * const instance_methods; |
| 3761 | // const struct method_list_t * const class_methods; |
| 3762 | // const struct method_list_t *optionalInstanceMethods; |
| 3763 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3764 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3765 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 3766 | // const uint32_t flags; // = 0 |
| 3767 | // } |
| 3768 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3769 | // Holder for struct _protocol_list_t * |
| 3770 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3771 | |
| 3772 | ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy, |
| 3773 | Int8PtrTy, |
| 3774 | llvm::PointerType::getUnqual( |
| 3775 | ProtocolListTyHolder), |
| 3776 | MethodListnfABIPtrTy, |
| 3777 | MethodListnfABIPtrTy, |
| 3778 | MethodListnfABIPtrTy, |
| 3779 | MethodListnfABIPtrTy, |
| 3780 | PropertyListPtrTy, |
| 3781 | IntTy, |
| 3782 | IntTy, |
| 3783 | NULL); |
| 3784 | CGM.getModule().addTypeName("struct._protocol_t", |
| 3785 | ProtocolnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3786 | |
| 3787 | // struct _protocol_t* |
| 3788 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3789 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3790 | // struct _protocol_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3791 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3792 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3793 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3794 | ProtocolListnfABITy = llvm::StructType::get(LongTy, |
| 3795 | llvm::ArrayType::get( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3796 | ProtocolnfABIPtrTy, 0), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3797 | NULL); |
| 3798 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3799 | ProtocolListnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3800 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo( |
| 3801 | ProtocolListnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3802 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3803 | // struct _objc_protocol_list* |
| 3804 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3805 | |
| 3806 | // struct _ivar_t { |
| 3807 | // unsigned long int *offset; // pointer to ivar offset location |
| 3808 | // char *name; |
| 3809 | // char *type; |
| 3810 | // uint32_t alignment; |
| 3811 | // uint32_t size; |
| 3812 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3813 | IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy), |
| 3814 | Int8PtrTy, |
| 3815 | Int8PtrTy, |
| 3816 | IntTy, |
| 3817 | IntTy, |
| 3818 | NULL); |
| 3819 | CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy); |
| 3820 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3821 | // struct _ivar_list_t { |
| 3822 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 3823 | // uint32 count; |
| 3824 | // struct _iver_t list[count]; |
| 3825 | // } |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3826 | IvarListnfABITy = llvm::StructType::get(IntTy, |
| 3827 | IntTy, |
| 3828 | llvm::ArrayType::get( |
| 3829 | IvarnfABITy, 0), |
| 3830 | NULL); |
| 3831 | CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy); |
| 3832 | |
| 3833 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3834 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3835 | // struct _class_ro_t { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3836 | // uint32_t const flags; |
| 3837 | // uint32_t const instanceStart; |
| 3838 | // uint32_t const instanceSize; |
| 3839 | // uint32_t const reserved; // only when building for 64bit targets |
| 3840 | // const uint8_t * const ivarLayout; |
| 3841 | // const char *const name; |
| 3842 | // const struct _method_list_t * const baseMethods; |
| 3843 | // const struct _objc_protocol_list *const baseProtocols; |
| 3844 | // const struct _ivar_list_t *const ivars; |
| 3845 | // const uint8_t * const weakIvarLayout; |
| 3846 | // const struct _prop_list_t * const properties; |
| 3847 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3848 | |
| 3849 | // FIXME. Add 'reserved' field in 64bit abi mode! |
| 3850 | ClassRonfABITy = llvm::StructType::get(IntTy, |
| 3851 | IntTy, |
| 3852 | IntTy, |
| 3853 | Int8PtrTy, |
| 3854 | Int8PtrTy, |
| 3855 | MethodListnfABIPtrTy, |
| 3856 | ProtocolListnfABIPtrTy, |
| 3857 | IvarListnfABIPtrTy, |
| 3858 | Int8PtrTy, |
| 3859 | PropertyListPtrTy, |
| 3860 | NULL); |
| 3861 | CGM.getModule().addTypeName("struct._class_ro_t", |
| 3862 | ClassRonfABITy); |
| 3863 | |
| 3864 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 3865 | std::vector<const llvm::Type*> Params; |
| 3866 | Params.push_back(ObjectPtrTy); |
| 3867 | Params.push_back(SelectorPtrTy); |
| 3868 | ImpnfABITy = llvm::PointerType::getUnqual( |
| 3869 | llvm::FunctionType::get(ObjectPtrTy, Params, false)); |
| 3870 | |
| 3871 | // struct _class_t { |
| 3872 | // struct _class_t *isa; |
| 3873 | // struct _class_t * const superclass; |
| 3874 | // void *cache; |
| 3875 | // IMP *vtable; |
| 3876 | // struct class_ro_t *ro; |
| 3877 | // } |
| 3878 | |
| 3879 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3880 | ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3881 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3882 | CachePtrTy, |
| 3883 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 3884 | llvm::PointerType::getUnqual( |
| 3885 | ClassRonfABITy), |
| 3886 | NULL); |
| 3887 | CGM.getModule().addTypeName("struct._class_t", ClassnfABITy); |
| 3888 | |
| 3889 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo( |
| 3890 | ClassnfABITy); |
| 3891 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3892 | // LLVM for struct _class_t * |
| 3893 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
| 3894 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3895 | // struct _category_t { |
| 3896 | // const char * const name; |
| 3897 | // struct _class_t *const cls; |
| 3898 | // const struct _method_list_t * const instance_methods; |
| 3899 | // const struct _method_list_t * const class_methods; |
| 3900 | // const struct _protocol_list_t * const protocols; |
| 3901 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3902 | // } |
| 3903 | CategorynfABITy = llvm::StructType::get(Int8PtrTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3904 | ClassnfABIPtrTy, |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3905 | MethodListnfABIPtrTy, |
| 3906 | MethodListnfABIPtrTy, |
| 3907 | ProtocolListnfABIPtrTy, |
| 3908 | PropertyListPtrTy, |
| 3909 | NULL); |
| 3910 | CGM.getModule().addTypeName("struct._category_t", CategorynfABITy); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3911 | |
| 3912 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3913 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3914 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3915 | |
| 3916 | // MessageRefTy - LLVM for: |
| 3917 | // struct _message_ref_t { |
| 3918 | // IMP messenger; |
| 3919 | // SEL name; |
| 3920 | // }; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3921 | |
| 3922 | // First the clang type for struct _message_ref_t |
| 3923 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3924 | SourceLocation(), |
| 3925 | &Ctx.Idents.get("_message_ref_t")); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3926 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3927 | Ctx.VoidPtrTy, 0, false)); |
| 3928 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3929 | Ctx.getObjCSelType(), 0, false)); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3930 | RD->completeDefinition(Ctx); |
| 3931 | |
| 3932 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 3933 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 3934 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3935 | |
| 3936 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 3937 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
| 3938 | |
| 3939 | // SuperMessageRefTy - LLVM for: |
| 3940 | // struct _super_message_ref_t { |
| 3941 | // SUPER_IMP messenger; |
| 3942 | // SEL name; |
| 3943 | // }; |
| 3944 | SuperMessageRefTy = llvm::StructType::get(ImpnfABITy, |
| 3945 | SelectorPtrTy, |
| 3946 | NULL); |
| 3947 | CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy); |
| 3948 | |
| 3949 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 3950 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
| 3951 | |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 3952 | |
| 3953 | // struct objc_typeinfo { |
| 3954 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 3955 | // const char* name; // c++ typeinfo string |
| 3956 | // Class cls; |
| 3957 | // }; |
| 3958 | EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy), |
| 3959 | Int8PtrTy, |
| 3960 | ClassnfABIPtrTy, |
| 3961 | NULL); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 3962 | CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 3963 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3964 | } |
| 3965 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3966 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
| 3967 | FinishNonFragileABIModule(); |
| 3968 | |
| 3969 | return NULL; |
| 3970 | } |
| 3971 | |
| 3972 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 3973 | // nonfragile abi has no module definition. |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 3974 | |
| 3975 | // Build list of all implemented classe addresses in array |
| 3976 | // L_OBJC_LABEL_CLASS_$. |
| 3977 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$ |
| 3978 | // list of 'nonlazy' implementations (defined as those with a +load{} |
| 3979 | // method!!). |
| 3980 | unsigned NumClasses = DefinedClasses.size(); |
| 3981 | if (NumClasses) { |
| 3982 | std::vector<llvm::Constant*> Symbols(NumClasses); |
| 3983 | for (unsigned i=0; i<NumClasses; i++) |
| 3984 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 3985 | ObjCTypes.Int8PtrTy); |
| 3986 | llvm::Constant* Init = |
| 3987 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 3988 | NumClasses), |
| 3989 | Symbols); |
| 3990 | |
| 3991 | llvm::GlobalVariable *GV = |
| 3992 | new llvm::GlobalVariable(Init->getType(), false, |
| 3993 | llvm::GlobalValue::InternalLinkage, |
| 3994 | Init, |
| 3995 | "\01L_OBJC_LABEL_CLASS_$", |
| 3996 | &CGM.getModule()); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 3997 | GV->setAlignment(8); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 3998 | GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip"); |
| 3999 | UsedGlobals.push_back(GV); |
| 4000 | } |
| 4001 | |
| 4002 | // Build list of all implemented category addresses in array |
| 4003 | // L_OBJC_LABEL_CATEGORY_$. |
| 4004 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$ |
| 4005 | // list of 'nonlazy' category implementations (defined as those with a +load{} |
| 4006 | // method!!). |
| 4007 | unsigned NumCategory = DefinedCategories.size(); |
| 4008 | if (NumCategory) { |
| 4009 | std::vector<llvm::Constant*> Symbols(NumCategory); |
| 4010 | for (unsigned i=0; i<NumCategory; i++) |
| 4011 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 4012 | ObjCTypes.Int8PtrTy); |
| 4013 | llvm::Constant* Init = |
| 4014 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 4015 | NumCategory), |
| 4016 | Symbols); |
| 4017 | |
| 4018 | llvm::GlobalVariable *GV = |
| 4019 | new llvm::GlobalVariable(Init->getType(), false, |
| 4020 | llvm::GlobalValue::InternalLinkage, |
| 4021 | Init, |
| 4022 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 4023 | &CGM.getModule()); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 4024 | GV->setAlignment(8); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4025 | GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip"); |
| 4026 | UsedGlobals.push_back(GV); |
| 4027 | } |
| 4028 | |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4029 | // static int L_OBJC_IMAGE_INFO[2] = { 0, flags }; |
| 4030 | // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0 |
| 4031 | std::vector<llvm::Constant*> Values(2); |
| 4032 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0); |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4033 | unsigned int flags = 0; |
Fariborz Jahanian | 66a5c2c | 2009-02-24 23:34:44 +0000 | [diff] [blame] | 4034 | // FIXME: Fix and continue? |
| 4035 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 4036 | flags |= eImageInfo_GarbageCollected; |
| 4037 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 4038 | flags |= eImageInfo_GCOnly; |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4039 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4040 | llvm::Constant* Init = llvm::ConstantArray::get( |
| 4041 | llvm::ArrayType::get(ObjCTypes.IntTy, 2), |
| 4042 | Values); |
| 4043 | llvm::GlobalVariable *IMGV = |
| 4044 | new llvm::GlobalVariable(Init->getType(), false, |
| 4045 | llvm::GlobalValue::InternalLinkage, |
| 4046 | Init, |
| 4047 | "\01L_OBJC_IMAGE_INFO", |
| 4048 | &CGM.getModule()); |
| 4049 | IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); |
Daniel Dunbar | 325f758 | 2009-04-23 08:03:21 +0000 | [diff] [blame] | 4050 | IMGV->setConstant(true); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4051 | UsedGlobals.push_back(IMGV); |
| 4052 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4053 | std::vector<llvm::Constant*> Used; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4054 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4055 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
| 4056 | e = UsedGlobals.end(); i != e; ++i) { |
| 4057 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
| 4058 | } |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4059 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4060 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
| 4061 | llvm::GlobalValue *GV = |
| 4062 | new llvm::GlobalVariable(AT, false, |
| 4063 | llvm::GlobalValue::AppendingLinkage, |
| 4064 | llvm::ConstantArray::get(AT, Used), |
| 4065 | "llvm.used", |
| 4066 | &CGM.getModule()); |
| 4067 | |
| 4068 | GV->setSection("llvm.metadata"); |
| 4069 | |
| 4070 | } |
| 4071 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4072 | // Metadata flags |
| 4073 | enum MetaDataDlags { |
| 4074 | CLS = 0x0, |
| 4075 | CLS_META = 0x1, |
| 4076 | CLS_ROOT = 0x2, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4077 | OBJC2_CLS_HIDDEN = 0x10, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4078 | CLS_EXCEPTION = 0x20 |
| 4079 | }; |
| 4080 | /// BuildClassRoTInitializer - generate meta-data for: |
| 4081 | /// struct _class_ro_t { |
| 4082 | /// uint32_t const flags; |
| 4083 | /// uint32_t const instanceStart; |
| 4084 | /// uint32_t const instanceSize; |
| 4085 | /// uint32_t const reserved; // only when building for 64bit targets |
| 4086 | /// const uint8_t * const ivarLayout; |
| 4087 | /// const char *const name; |
| 4088 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4089 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4090 | /// const struct _ivar_list_t *const ivars; |
| 4091 | /// const uint8_t * const weakIvarLayout; |
| 4092 | /// const struct _prop_list_t * const properties; |
| 4093 | /// } |
| 4094 | /// |
| 4095 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
| 4096 | unsigned flags, |
| 4097 | unsigned InstanceStart, |
| 4098 | unsigned InstanceSize, |
| 4099 | const ObjCImplementationDecl *ID) { |
| 4100 | std::string ClassName = ID->getNameAsString(); |
| 4101 | std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets! |
| 4102 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 4103 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 4104 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
| 4105 | // FIXME. For 64bit targets add 0 here. |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 4106 | Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4107 | : BuildIvarLayout(ID, true); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4108 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4109 | // const struct _method_list_t * const baseMethods; |
| 4110 | std::vector<llvm::Constant*> Methods; |
| 4111 | std::string MethodListName("\01l_OBJC_$_"); |
| 4112 | if (flags & CLS_META) { |
| 4113 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4114 | for (ObjCImplementationDecl::classmeth_iterator |
| 4115 | i = ID->classmeth_begin(CGM.getContext()), |
| 4116 | e = ID->classmeth_end(CGM.getContext()); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4117 | // Class methods should always be defined. |
| 4118 | Methods.push_back(GetMethodConstant(*i)); |
| 4119 | } |
| 4120 | } else { |
| 4121 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4122 | for (ObjCImplementationDecl::instmeth_iterator |
| 4123 | i = ID->instmeth_begin(CGM.getContext()), |
| 4124 | e = ID->instmeth_end(CGM.getContext()); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4125 | // Instance methods should always be defined. |
| 4126 | Methods.push_back(GetMethodConstant(*i)); |
| 4127 | } |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4128 | for (ObjCImplementationDecl::propimpl_iterator |
| 4129 | i = ID->propimpl_begin(CGM.getContext()), |
| 4130 | e = ID->propimpl_end(CGM.getContext()); i != e; ++i) { |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4131 | ObjCPropertyImplDecl *PID = *i; |
| 4132 | |
| 4133 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 4134 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 4135 | |
| 4136 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 4137 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4138 | Methods.push_back(C); |
| 4139 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 4140 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4141 | Methods.push_back(C); |
| 4142 | } |
| 4143 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4144 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4145 | Values[ 5] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4146 | "__DATA, __objc_const", Methods); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4147 | |
| 4148 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4149 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
| 4150 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
| 4151 | + OID->getNameAsString(), |
| 4152 | OID->protocol_begin(), |
| 4153 | OID->protocol_end()); |
| 4154 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4155 | if (flags & CLS_META) |
| 4156 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4157 | else |
| 4158 | Values[ 7] = EmitIvarList(ID); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 4159 | Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4160 | : BuildIvarLayout(ID, false); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4161 | if (flags & CLS_META) |
| 4162 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4163 | else |
| 4164 | Values[ 9] = |
| 4165 | EmitPropertyList( |
| 4166 | "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
| 4167 | ID, ID->getClassInterface(), ObjCTypes); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4168 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
| 4169 | Values); |
| 4170 | llvm::GlobalVariable *CLASS_RO_GV = |
| 4171 | new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false, |
| 4172 | llvm::GlobalValue::InternalLinkage, |
| 4173 | Init, |
| 4174 | (flags & CLS_META) ? |
| 4175 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 4176 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName, |
| 4177 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4178 | CLASS_RO_GV->setAlignment( |
| 4179 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4180 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4181 | return CLASS_RO_GV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4182 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4183 | } |
| 4184 | |
| 4185 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 4186 | /// for the given ClassName for: |
| 4187 | /// struct _class_t { |
| 4188 | /// struct _class_t *isa; |
| 4189 | /// struct _class_t * const superclass; |
| 4190 | /// void *cache; |
| 4191 | /// IMP *vtable; |
| 4192 | /// struct class_ro_t *ro; |
| 4193 | /// } |
| 4194 | /// |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4195 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
| 4196 | std::string &ClassName, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4197 | llvm::Constant *IsAGV, |
| 4198 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4199 | llvm::Constant *ClassRoGV, |
| 4200 | bool HiddenVisibility) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4201 | std::vector<llvm::Constant*> Values(5); |
| 4202 | Values[0] = IsAGV; |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4203 | Values[1] = SuperClassGV |
| 4204 | ? SuperClassGV |
| 4205 | : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4206 | Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar |
| 4207 | Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar |
| 4208 | Values[4] = ClassRoGV; // &CLASS_RO_GV |
| 4209 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
| 4210 | Values); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4211 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 4212 | GV->setInitializer(Init); |
Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 4213 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4214 | GV->setAlignment( |
| 4215 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4216 | if (HiddenVisibility) |
| 4217 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4218 | return GV; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4219 | } |
| 4220 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4221 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4222 | uint32_t &InstanceStart, |
| 4223 | uint32_t &InstanceSize) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4224 | // Find first and last (non-padding) ivars in this interface. |
| 4225 | |
| 4226 | // FIXME: Use iterator. |
| 4227 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4228 | GetNamedIvarList(OID->getClassInterface(), OIvars); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4229 | |
| 4230 | if (OIvars.empty()) { |
| 4231 | InstanceStart = InstanceSize = 0; |
| 4232 | return; |
Daniel Dunbar | d4ae6c0 | 2009-04-22 04:39:47 +0000 | [diff] [blame] | 4233 | } |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4234 | |
| 4235 | const ObjCIvarDecl *First = OIvars.front(); |
| 4236 | const ObjCIvarDecl *Last = OIvars.back(); |
| 4237 | |
| 4238 | InstanceStart = ComputeIvarBaseOffset(CGM, OID, First); |
| 4239 | const llvm::Type *FieldTy = |
| 4240 | CGM.getTypes().ConvertTypeForMem(Last->getType()); |
| 4241 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 4242 | // FIXME. This breaks compatibility with llvm-gcc-4.2 (but makes it compatible |
| 4243 | // with gcc-4.2). We postpone this for now. |
| 4244 | #if 0 |
| 4245 | if (Last->isBitField()) { |
| 4246 | Expr *BitWidth = Last->getBitWidth(); |
| 4247 | uint64_t BitFieldSize = |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 4248 | BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue(); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 4249 | Size = (BitFieldSize / 8) + ((BitFieldSize % 8) != 0); |
| 4250 | } |
| 4251 | #endif |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4252 | InstanceSize = ComputeIvarBaseOffset(CGM, OID, Last) + Size; |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4253 | } |
| 4254 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4255 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 4256 | std::string ClassName = ID->getNameAsString(); |
| 4257 | if (!ObjCEmptyCacheVar) { |
| 4258 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4259 | ObjCTypes.CacheTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4260 | false, |
| 4261 | llvm::GlobalValue::ExternalLinkage, |
| 4262 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4263 | "_objc_empty_cache", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4264 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4265 | |
| 4266 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4267 | ObjCTypes.ImpnfABITy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4268 | false, |
| 4269 | llvm::GlobalValue::ExternalLinkage, |
| 4270 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4271 | "_objc_empty_vtable", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4272 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4273 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4274 | assert(ID->getClassInterface() && |
| 4275 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 4276 | // FIXME: Is this correct (that meta class size is never computed)? |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4277 | uint32_t InstanceStart = |
| 4278 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy); |
| 4279 | uint32_t InstanceSize = InstanceStart; |
| 4280 | uint32_t flags = CLS_META; |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4281 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 4282 | std::string ObjCClassName(getClassSymbolPrefix()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4283 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4284 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4285 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4286 | bool classIsHidden = |
| 4287 | CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4288 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4289 | flags |= OBJC2_CLS_HIDDEN; |
| 4290 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4291 | // class is root |
| 4292 | flags |= CLS_ROOT; |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4293 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4294 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4295 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4296 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4297 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 4298 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 4299 | Root = Super; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4300 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4301 | // work on super class metadata symbol. |
| 4302 | std::string SuperClassName = |
| 4303 | ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4304 | SuperClassGV = GetClassGlobal(SuperClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4305 | } |
| 4306 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 4307 | InstanceStart, |
| 4308 | InstanceSize,ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4309 | std::string TClassName = ObjCMetaClassName + ClassName; |
| 4310 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4311 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 4312 | classIsHidden); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4313 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4314 | // Metadata for the class |
| 4315 | flags = CLS; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4316 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4317 | flags |= OBJC2_CLS_HIDDEN; |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4318 | |
| 4319 | if (hasObjCExceptionAttribute(ID->getClassInterface())) |
| 4320 | flags |= CLS_EXCEPTION; |
| 4321 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4322 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4323 | flags |= CLS_ROOT; |
| 4324 | SuperClassGV = 0; |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 4325 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4326 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4327 | std::string RootClassName = |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4328 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4329 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4330 | } |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4331 | GetClassSizeInfo(ID, InstanceStart, InstanceSize); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4332 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4333 | InstanceStart, |
| 4334 | InstanceSize, |
| 4335 | ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4336 | |
| 4337 | TClassName = ObjCClassName + ClassName; |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4338 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4339 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 4340 | classIsHidden); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4341 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4342 | |
| 4343 | // Force the definition of the EHType if necessary. |
| 4344 | if (flags & CLS_EXCEPTION) |
| 4345 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4346 | } |
| 4347 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4348 | /// GenerateProtocolRef - This routine is called to generate code for |
| 4349 | /// a protocol reference expression; as in: |
| 4350 | /// @code |
| 4351 | /// @protocol(Proto1); |
| 4352 | /// @endcode |
| 4353 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 4354 | /// which will hold address of the protocol meta-data. |
| 4355 | /// |
| 4356 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
| 4357 | const ObjCProtocolDecl *PD) { |
| 4358 | |
Fariborz Jahanian | 960cd06 | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 4359 | // This routine is called for @protocol only. So, we must build definition |
| 4360 | // of protocol's meta-data (not a reference to it!) |
| 4361 | // |
| 4362 | llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4363 | ObjCTypes.ExternalProtocolPtrTy); |
| 4364 | |
| 4365 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
| 4366 | ProtocolName += PD->getNameAsCString(); |
| 4367 | |
| 4368 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 4369 | if (PTGV) |
| 4370 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4371 | PTGV = new llvm::GlobalVariable( |
| 4372 | Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4373 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4374 | Init, |
| 4375 | ProtocolName, |
| 4376 | &CGM.getModule()); |
| 4377 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 4378 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4379 | UsedGlobals.push_back(PTGV); |
| 4380 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4381 | } |
| 4382 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4383 | /// GenerateCategory - Build metadata for a category implementation. |
| 4384 | /// struct _category_t { |
| 4385 | /// const char * const name; |
| 4386 | /// struct _class_t *const cls; |
| 4387 | /// const struct _method_list_t * const instance_methods; |
| 4388 | /// const struct _method_list_t * const class_methods; |
| 4389 | /// const struct _protocol_list_t * const protocols; |
| 4390 | /// const struct _prop_list_t * const properties; |
| 4391 | /// } |
| 4392 | /// |
| 4393 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) |
| 4394 | { |
| 4395 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4396 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
| 4397 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4398 | "_$_" + OCD->getNameAsString()); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4399 | std::string ExtClassName(getClassSymbolPrefix() + |
| 4400 | Interface->getNameAsString()); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4401 | |
| 4402 | std::vector<llvm::Constant*> Values(6); |
| 4403 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 4404 | // meta-class entry symbol |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4405 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4406 | Values[1] = ClassGV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4407 | std::vector<llvm::Constant*> Methods; |
| 4408 | std::string MethodListName(Prefix); |
| 4409 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
| 4410 | "_$_" + OCD->getNameAsString(); |
| 4411 | |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4412 | for (ObjCCategoryImplDecl::instmeth_iterator |
| 4413 | i = OCD->instmeth_begin(CGM.getContext()), |
| 4414 | e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4415 | // Instance methods should always be defined. |
| 4416 | Methods.push_back(GetMethodConstant(*i)); |
| 4417 | } |
| 4418 | |
| 4419 | Values[2] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4420 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4421 | Methods); |
| 4422 | |
| 4423 | MethodListName = Prefix; |
| 4424 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 4425 | OCD->getNameAsString(); |
| 4426 | Methods.clear(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4427 | for (ObjCCategoryImplDecl::classmeth_iterator |
| 4428 | i = OCD->classmeth_begin(CGM.getContext()), |
| 4429 | e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4430 | // Class methods should always be defined. |
| 4431 | Methods.push_back(GetMethodConstant(*i)); |
| 4432 | } |
| 4433 | |
| 4434 | Values[3] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4435 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4436 | Methods); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4437 | const ObjCCategoryDecl *Category = |
| 4438 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4439 | if (Category) { |
| 4440 | std::string ExtName(Interface->getNameAsString() + "_$_" + |
| 4441 | OCD->getNameAsString()); |
| 4442 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
| 4443 | + Interface->getNameAsString() + "_$_" |
| 4444 | + Category->getNameAsString(), |
| 4445 | Category->protocol_begin(), |
| 4446 | Category->protocol_end()); |
| 4447 | Values[5] = |
| 4448 | EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
| 4449 | OCD, Category, ObjCTypes); |
| 4450 | } |
| 4451 | else { |
| 4452 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4453 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4454 | } |
| 4455 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4456 | llvm::Constant *Init = |
| 4457 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
| 4458 | Values); |
| 4459 | llvm::GlobalVariable *GCATV |
| 4460 | = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy, |
| 4461 | false, |
| 4462 | llvm::GlobalValue::InternalLinkage, |
| 4463 | Init, |
| 4464 | ExtCatName, |
| 4465 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4466 | GCATV->setAlignment( |
| 4467 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4468 | GCATV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4469 | UsedGlobals.push_back(GCATV); |
| 4470 | DefinedCategories.push_back(GCATV); |
| 4471 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4472 | |
| 4473 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 4474 | /// given method if it has been defined. The result is null if the |
| 4475 | /// method has not been defined. The return value has type MethodPtrTy. |
| 4476 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
| 4477 | const ObjCMethodDecl *MD) { |
| 4478 | // FIXME: Use DenseMap::lookup |
| 4479 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 4480 | if (!Fn) |
| 4481 | return 0; |
| 4482 | |
| 4483 | std::vector<llvm::Constant*> Method(3); |
| 4484 | Method[0] = |
| 4485 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4486 | ObjCTypes.SelectorPtrTy); |
| 4487 | Method[1] = GetMethodVarType(MD); |
| 4488 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 4489 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 4490 | } |
| 4491 | |
| 4492 | /// EmitMethodList - Build meta-data for method declarations |
| 4493 | /// struct _method_list_t { |
| 4494 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 4495 | /// uint32_t method_count; |
| 4496 | /// struct _objc_method method_list[method_count]; |
| 4497 | /// } |
| 4498 | /// |
| 4499 | llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList( |
| 4500 | const std::string &Name, |
| 4501 | const char *Section, |
| 4502 | const ConstantVector &Methods) { |
| 4503 | // Return null for empty list. |
| 4504 | if (Methods.empty()) |
| 4505 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
| 4506 | |
| 4507 | std::vector<llvm::Constant*> Values(3); |
| 4508 | // sizeof(struct _objc_method) |
| 4509 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy); |
| 4510 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4511 | // method_count |
| 4512 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 4513 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 4514 | Methods.size()); |
| 4515 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 4516 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4517 | |
| 4518 | llvm::GlobalVariable *GV = |
| 4519 | new llvm::GlobalVariable(Init->getType(), false, |
| 4520 | llvm::GlobalValue::InternalLinkage, |
| 4521 | Init, |
| 4522 | Name, |
| 4523 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4524 | GV->setAlignment( |
| 4525 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4526 | GV->setSection(Section); |
| 4527 | UsedGlobals.push_back(GV); |
| 4528 | return llvm::ConstantExpr::getBitCast(GV, |
| 4529 | ObjCTypes.MethodListnfABIPtrTy); |
| 4530 | } |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4531 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4532 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 4533 | /// the given ivar. |
| 4534 | /// |
| 4535 | llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 4536 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4537 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 4538 | std::string Name = "OBJC_IVAR_$_" + |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4539 | getInterfaceDeclForIvar(ID, Ivar, CGM.getContext())->getNameAsString() + |
| 4540 | '.' + Ivar->getNameAsString(); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4541 | llvm::GlobalVariable *IvarOffsetGV = |
| 4542 | CGM.getModule().getGlobalVariable(Name); |
| 4543 | if (!IvarOffsetGV) |
| 4544 | IvarOffsetGV = |
| 4545 | new llvm::GlobalVariable(ObjCTypes.LongTy, |
| 4546 | false, |
| 4547 | llvm::GlobalValue::ExternalLinkage, |
| 4548 | 0, |
| 4549 | Name, |
| 4550 | &CGM.getModule()); |
| 4551 | return IvarOffsetGV; |
| 4552 | } |
| 4553 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4554 | llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar( |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4555 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4556 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4557 | unsigned long int Offset) { |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4558 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
| 4559 | IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy, |
| 4560 | Offset)); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4561 | IvarOffsetGV->setAlignment( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4562 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy)); |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4563 | |
| 4564 | // FIXME: This matches gcc, but shouldn't the visibility be set on |
| 4565 | // the use as well (i.e., in ObjCIvarOffsetVariable). |
| 4566 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 4567 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
| 4568 | CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden) |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4569 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4570 | else |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4571 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4572 | IvarOffsetGV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4573 | return IvarOffsetGV; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4574 | } |
| 4575 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4576 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 4577 | /// implementation. The return value has type |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4578 | /// IvarListnfABIPtrTy. |
| 4579 | /// struct _ivar_t { |
| 4580 | /// unsigned long int *offset; // pointer to ivar offset location |
| 4581 | /// char *name; |
| 4582 | /// char *type; |
| 4583 | /// uint32_t alignment; |
| 4584 | /// uint32_t size; |
| 4585 | /// } |
| 4586 | /// struct _ivar_list_t { |
| 4587 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 4588 | /// uint32 count; |
| 4589 | /// struct _iver_t list[count]; |
| 4590 | /// } |
| 4591 | /// |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4592 | |
| 4593 | void CGObjCCommonMac::GetNamedIvarList(const ObjCInterfaceDecl *OID, |
| 4594 | llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const { |
| 4595 | for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), |
| 4596 | E = OID->ivar_end(); I != E; ++I) { |
| 4597 | // Ignore unnamed bit-fields. |
| 4598 | if (!(*I)->getDeclName()) |
| 4599 | continue; |
| 4600 | |
| 4601 | Res.push_back(*I); |
| 4602 | } |
| 4603 | |
| 4604 | for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(CGM.getContext()), |
| 4605 | E = OID->prop_end(CGM.getContext()); I != E; ++I) |
| 4606 | if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl()) |
| 4607 | Res.push_back(IV); |
| 4608 | } |
| 4609 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4610 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
| 4611 | const ObjCImplementationDecl *ID) { |
| 4612 | |
| 4613 | std::vector<llvm::Constant*> Ivars, Ivar(5); |
| 4614 | |
| 4615 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4616 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
| 4617 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4618 | // FIXME. Consolidate this with similar code in GenerateClass. |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 4619 | |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4620 | // Collect declared and synthesized ivars in a small vector. |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4621 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4622 | GetNamedIvarList(OID, OIvars); |
Fariborz Jahanian | 99eee36 | 2009-04-01 19:37:34 +0000 | [diff] [blame] | 4623 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4624 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 4625 | ObjCIvarDecl *IVD = OIvars[i]; |
Daniel Dunbar | 3eec8aa | 2009-04-20 05:53:40 +0000 | [diff] [blame] | 4626 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD, |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4627 | ComputeIvarBaseOffset(CGM, ID, IVD)); |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4628 | Ivar[1] = GetMethodVarName(IVD->getIdentifier()); |
| 4629 | Ivar[2] = GetMethodVarType(IVD); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4630 | const llvm::Type *FieldTy = |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4631 | CGM.getTypes().ConvertTypeForMem(IVD->getType()); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4632 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
| 4633 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4634 | IVD->getType().getTypePtr()) >> 3; |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4635 | Align = llvm::Log2_32(Align); |
| 4636 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4637 | // NOTE. Size of a bitfield does not match gcc's, because of the |
| 4638 | // way bitfields are treated special in each. But I am told that |
| 4639 | // 'size' for bitfield ivars is ignored by the runtime so it does |
| 4640 | // not matter. If it matters, there is enough info to get the |
| 4641 | // bitfield right! |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4642 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4643 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
| 4644 | } |
| 4645 | // Return null for empty list. |
| 4646 | if (Ivars.empty()) |
| 4647 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4648 | std::vector<llvm::Constant*> Values(3); |
| 4649 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy); |
| 4650 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4651 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 4652 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
| 4653 | Ivars.size()); |
| 4654 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
| 4655 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4656 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 4657 | llvm::GlobalVariable *GV = |
| 4658 | new llvm::GlobalVariable(Init->getType(), false, |
| 4659 | llvm::GlobalValue::InternalLinkage, |
| 4660 | Init, |
| 4661 | Prefix + OID->getNameAsString(), |
| 4662 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4663 | GV->setAlignment( |
| 4664 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4665 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4666 | |
| 4667 | UsedGlobals.push_back(GV); |
| 4668 | return llvm::ConstantExpr::getBitCast(GV, |
| 4669 | ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4670 | } |
| 4671 | |
| 4672 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
| 4673 | const ObjCProtocolDecl *PD) { |
| 4674 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4675 | |
| 4676 | if (!Entry) { |
| 4677 | // We use the initializer as a marker of whether this is a forward |
| 4678 | // reference or not. At module finalization we add the empty |
| 4679 | // contents for protocols which were referenced but never defined. |
| 4680 | Entry = |
| 4681 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
| 4682 | llvm::GlobalValue::ExternalLinkage, |
| 4683 | 0, |
| 4684 | "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(), |
| 4685 | &CGM.getModule()); |
| 4686 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 4687 | UsedGlobals.push_back(Entry); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4688 | } |
| 4689 | |
| 4690 | return Entry; |
| 4691 | } |
| 4692 | |
| 4693 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 4694 | /// @code |
| 4695 | /// struct _protocol_t { |
| 4696 | /// id isa; // NULL |
| 4697 | /// const char * const protocol_name; |
| 4698 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 4699 | /// const struct method_list_t * const instance_methods; |
| 4700 | /// const struct method_list_t * const class_methods; |
| 4701 | /// const struct method_list_t *optionalInstanceMethods; |
| 4702 | /// const struct method_list_t *optionalClassMethods; |
| 4703 | /// const struct _prop_list_t * properties; |
| 4704 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 4705 | /// const uint32_t flags; // = 0 |
| 4706 | /// } |
| 4707 | /// @endcode |
| 4708 | /// |
| 4709 | |
| 4710 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
| 4711 | const ObjCProtocolDecl *PD) { |
| 4712 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4713 | |
| 4714 | // Early exit if a defining object has already been generated. |
| 4715 | if (Entry && Entry->hasInitializer()) |
| 4716 | return Entry; |
| 4717 | |
| 4718 | const char *ProtocolName = PD->getNameAsCString(); |
| 4719 | |
| 4720 | // Construct method lists. |
| 4721 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 4722 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4723 | for (ObjCProtocolDecl::instmeth_iterator |
| 4724 | i = PD->instmeth_begin(CGM.getContext()), |
| 4725 | e = PD->instmeth_end(CGM.getContext()); |
| 4726 | i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4727 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4728 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4729 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4730 | OptInstanceMethods.push_back(C); |
| 4731 | } else { |
| 4732 | InstanceMethods.push_back(C); |
| 4733 | } |
| 4734 | } |
| 4735 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4736 | for (ObjCProtocolDecl::classmeth_iterator |
| 4737 | i = PD->classmeth_begin(CGM.getContext()), |
| 4738 | e = PD->classmeth_end(CGM.getContext()); |
| 4739 | i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4740 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4741 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4742 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4743 | OptClassMethods.push_back(C); |
| 4744 | } else { |
| 4745 | ClassMethods.push_back(C); |
| 4746 | } |
| 4747 | } |
| 4748 | |
| 4749 | std::vector<llvm::Constant*> Values(10); |
| 4750 | // isa is NULL |
| 4751 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
| 4752 | Values[1] = GetClassName(PD->getIdentifier()); |
| 4753 | Values[2] = EmitProtocolList( |
| 4754 | "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(), |
| 4755 | PD->protocol_begin(), |
| 4756 | PD->protocol_end()); |
| 4757 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4758 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4759 | + PD->getNameAsString(), |
| 4760 | "__DATA, __objc_const", |
| 4761 | InstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4762 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4763 | + PD->getNameAsString(), |
| 4764 | "__DATA, __objc_const", |
| 4765 | ClassMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4766 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4767 | + PD->getNameAsString(), |
| 4768 | "__DATA, __objc_const", |
| 4769 | OptInstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4770 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4771 | + PD->getNameAsString(), |
| 4772 | "__DATA, __objc_const", |
| 4773 | OptClassMethods); |
| 4774 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(), |
| 4775 | 0, PD, ObjCTypes); |
| 4776 | uint32_t Size = |
| 4777 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy); |
| 4778 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4779 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
| 4780 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
| 4781 | Values); |
| 4782 | |
| 4783 | if (Entry) { |
| 4784 | // Already created, fix the linkage and update the initializer. |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4785 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4786 | Entry->setInitializer(Init); |
| 4787 | } else { |
| 4788 | Entry = |
| 4789 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4790 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4791 | Init, |
| 4792 | std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName, |
| 4793 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4794 | Entry->setAlignment( |
| 4795 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4796 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4797 | } |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4798 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4799 | |
| 4800 | // Use this protocol meta-data to build protocol list table in section |
| 4801 | // __DATA, __objc_protolist |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4802 | llvm::GlobalVariable *PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4803 | ObjCTypes.ProtocolnfABIPtrTy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4804 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4805 | Entry, |
| 4806 | std::string("\01l_OBJC_LABEL_PROTOCOL_$_") |
| 4807 | +ProtocolName, |
| 4808 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4809 | PTGV->setAlignment( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4810 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 4811 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4812 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4813 | UsedGlobals.push_back(PTGV); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4814 | return Entry; |
| 4815 | } |
| 4816 | |
| 4817 | /// EmitProtocolList - Generate protocol list meta-data: |
| 4818 | /// @code |
| 4819 | /// struct _protocol_list_t { |
| 4820 | /// long protocol_count; // Note, this is 32/64 bit |
| 4821 | /// struct _protocol_t[protocol_count]; |
| 4822 | /// } |
| 4823 | /// @endcode |
| 4824 | /// |
| 4825 | llvm::Constant * |
| 4826 | CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name, |
| 4827 | ObjCProtocolDecl::protocol_iterator begin, |
| 4828 | ObjCProtocolDecl::protocol_iterator end) { |
| 4829 | std::vector<llvm::Constant*> ProtocolRefs; |
| 4830 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4831 | // Just return null for empty protocol lists |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4832 | if (begin == end) |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4833 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4834 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4835 | // FIXME: We shouldn't need to do this lookup here, should we? |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4836 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
| 4837 | if (GV) |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4838 | return llvm::ConstantExpr::getBitCast(GV, |
| 4839 | ObjCTypes.ProtocolListnfABIPtrTy); |
| 4840 | |
| 4841 | for (; begin != end; ++begin) |
| 4842 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 4843 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4844 | // This list is null terminated. |
| 4845 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4846 | ObjCTypes.ProtocolnfABIPtrTy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4847 | |
| 4848 | std::vector<llvm::Constant*> Values(2); |
| 4849 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 4850 | Values[1] = |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4851 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4852 | ProtocolRefs.size()), |
| 4853 | ProtocolRefs); |
| 4854 | |
| 4855 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4856 | GV = new llvm::GlobalVariable(Init->getType(), false, |
| 4857 | llvm::GlobalValue::InternalLinkage, |
| 4858 | Init, |
| 4859 | Name, |
| 4860 | &CGM.getModule()); |
| 4861 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4862 | GV->setAlignment( |
| 4863 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4864 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4865 | return llvm::ConstantExpr::getBitCast(GV, |
| 4866 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4867 | } |
| 4868 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4869 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 4870 | /// struct _objc_method { |
| 4871 | /// SEL _cmd; |
| 4872 | /// char *method_type; |
| 4873 | /// char *_imp; |
| 4874 | /// } |
| 4875 | |
| 4876 | llvm::Constant * |
| 4877 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 4878 | std::vector<llvm::Constant*> Desc(3); |
| 4879 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4880 | ObjCTypes.SelectorPtrTy); |
| 4881 | Desc[1] = GetMethodVarType(MD); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4882 | // Protocol methods have no implementation. So, this entry is always NULL. |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4883 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 4884 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
| 4885 | } |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4886 | |
| 4887 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 4888 | /// This code gen. amounts to generating code for: |
| 4889 | /// @code |
| 4890 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 4891 | /// @encode |
| 4892 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4893 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4894 | CodeGen::CodeGenFunction &CGF, |
| 4895 | QualType ObjectTy, |
| 4896 | llvm::Value *BaseValue, |
| 4897 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4898 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 4899 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4900 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 4901 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4902 | } |
| 4903 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4904 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
| 4905 | CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 4906 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4907 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 4908 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar), |
| 4909 | false, "ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4910 | } |
| 4911 | |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4912 | CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend( |
| 4913 | CodeGen::CodeGenFunction &CGF, |
| 4914 | QualType ResultType, |
| 4915 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4916 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4917 | QualType Arg0Ty, |
| 4918 | bool IsSuper, |
| 4919 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4920 | // FIXME. Even though IsSuper is passes. This function doese not |
| 4921 | // handle calls to 'super' receivers. |
| 4922 | CodeGenTypes &Types = CGM.getTypes(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4923 | llvm::Value *Arg0 = Receiver; |
| 4924 | if (!IsSuper) |
| 4925 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4926 | |
| 4927 | // Find the message function name. |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 4928 | // FIXME. This is too much work to get the ABI-specific result type |
| 4929 | // needed to find the message name. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4930 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, |
| 4931 | llvm::SmallVector<QualType, 16>()); |
Fariborz Jahanian | 70b51c7 | 2009-04-30 23:08:58 +0000 | [diff] [blame] | 4932 | llvm::Constant *Fn = 0; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4933 | std::string Name("\01l_"); |
| 4934 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4935 | #if 0 |
| 4936 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4937 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4938 | Fn = ObjCTypes.getMessageSendIdStretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4939 | // FIXME. Is there a better way of getting these names. |
| 4940 | // They are available in RuntimeFunctions vector pair. |
| 4941 | Name += "objc_msgSendId_stret_fixup"; |
| 4942 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4943 | else |
| 4944 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4945 | if (IsSuper) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4946 | Fn = ObjCTypes.getMessageSendSuper2StretFixupFn(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4947 | Name += "objc_msgSendSuper2_stret_fixup"; |
| 4948 | } |
| 4949 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4950 | { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4951 | Fn = ObjCTypes.getMessageSendStretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4952 | Name += "objc_msgSend_stret_fixup"; |
| 4953 | } |
| 4954 | } |
Fariborz Jahanian | 5b2bad0 | 2009-04-30 16:31:11 +0000 | [diff] [blame] | 4955 | else if (!IsSuper && ResultType->isFloatingType()) { |
| 4956 | if (const BuiltinType *BT = ResultType->getAsBuiltinType()) { |
| 4957 | BuiltinType::Kind k = BT->getKind(); |
| 4958 | if (k == BuiltinType::LongDouble) { |
| 4959 | Fn = ObjCTypes.getMessageSendFpretFixupFn(); |
| 4960 | Name += "objc_msgSend_fpret_fixup"; |
| 4961 | } |
| 4962 | else { |
| 4963 | Fn = ObjCTypes.getMessageSendFixupFn(); |
| 4964 | Name += "objc_msgSend_fixup"; |
| 4965 | } |
| 4966 | } |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4967 | } |
| 4968 | else { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4969 | #if 0 |
| 4970 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4971 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4972 | Fn = ObjCTypes.getMessageSendIdFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4973 | Name += "objc_msgSendId_fixup"; |
| 4974 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4975 | else |
| 4976 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4977 | if (IsSuper) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4978 | Fn = ObjCTypes.getMessageSendSuper2FixupFn(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4979 | Name += "objc_msgSendSuper2_fixup"; |
| 4980 | } |
| 4981 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4982 | { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4983 | Fn = ObjCTypes.getMessageSendFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4984 | Name += "objc_msgSend_fixup"; |
| 4985 | } |
| 4986 | } |
Fariborz Jahanian | 70b51c7 | 2009-04-30 23:08:58 +0000 | [diff] [blame] | 4987 | assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4988 | Name += '_'; |
| 4989 | std::string SelName(Sel.getAsString()); |
| 4990 | // Replace all ':' in selector name with '_' ouch! |
| 4991 | for(unsigned i = 0; i < SelName.size(); i++) |
| 4992 | if (SelName[i] == ':') |
| 4993 | SelName[i] = '_'; |
| 4994 | Name += SelName; |
| 4995 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 4996 | if (!GV) { |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 4997 | // Build message ref table entry. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4998 | std::vector<llvm::Constant*> Values(2); |
| 4999 | Values[0] = Fn; |
| 5000 | Values[1] = GetMethodVarName(Sel); |
| 5001 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 5002 | GV = new llvm::GlobalVariable(Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 5003 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5004 | Init, |
| 5005 | Name, |
| 5006 | &CGM.getModule()); |
| 5007 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | f59c1a6 | 2009-04-15 19:04:46 +0000 | [diff] [blame] | 5008 | GV->setAlignment(16); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5009 | GV->setSection("__DATA, __objc_msgrefs, coalesced"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5010 | } |
| 5011 | llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5012 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5013 | CallArgList ActualArgs; |
| 5014 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 5015 | ActualArgs.push_back(std::make_pair(RValue::get(Arg1), |
| 5016 | ObjCTypes.MessageRefCPtrTy)); |
| 5017 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5018 | const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs); |
| 5019 | llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0); |
| 5020 | Callee = CGF.Builder.CreateLoad(Callee); |
Fariborz Jahanian | 3ab75bd | 2009-02-14 21:25:36 +0000 | [diff] [blame] | 5021 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5022 | Callee = CGF.Builder.CreateBitCast(Callee, |
| 5023 | llvm::PointerType::getUnqual(FTy)); |
| 5024 | return CGF.EmitCall(FnInfo1, Callee, ActualArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5025 | } |
| 5026 | |
| 5027 | /// Generate code for a message send expression in the nonfragile abi. |
| 5028 | CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend( |
| 5029 | CodeGen::CodeGenFunction &CGF, |
| 5030 | QualType ResultType, |
| 5031 | Selector Sel, |
| 5032 | llvm::Value *Receiver, |
| 5033 | bool IsClassMessage, |
| 5034 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5035 | return EmitMessageSend(CGF, ResultType, Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5036 | Receiver, CGF.getContext().getObjCIdType(), |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5037 | false, CallArgs); |
| 5038 | } |
| 5039 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5040 | llvm::GlobalVariable * |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5041 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5042 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5043 | |
Daniel Dunbar | dfff230 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 5044 | if (!GV) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5045 | GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 5046 | llvm::GlobalValue::ExternalLinkage, |
| 5047 | 0, Name, &CGM.getModule()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5048 | } |
| 5049 | |
| 5050 | return GV; |
| 5051 | } |
| 5052 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5053 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5054 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5055 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 5056 | |
| 5057 | if (!Entry) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5058 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5059 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5060 | Entry = |
| 5061 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5062 | llvm::GlobalValue::InternalLinkage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5063 | ClassGV, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5064 | "\01L_OBJC_CLASSLIST_REFERENCES_$_", |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5065 | &CGM.getModule()); |
| 5066 | Entry->setAlignment( |
| 5067 | CGM.getTargetData().getPrefTypeAlignment( |
| 5068 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5069 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
| 5070 | UsedGlobals.push_back(Entry); |
| 5071 | } |
| 5072 | |
| 5073 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5074 | } |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5075 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5076 | llvm::Value * |
| 5077 | CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder, |
| 5078 | const ObjCInterfaceDecl *ID) { |
| 5079 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
| 5080 | |
| 5081 | if (!Entry) { |
| 5082 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5083 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
| 5084 | Entry = |
| 5085 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5086 | llvm::GlobalValue::InternalLinkage, |
| 5087 | ClassGV, |
| 5088 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 5089 | &CGM.getModule()); |
| 5090 | Entry->setAlignment( |
| 5091 | CGM.getTargetData().getPrefTypeAlignment( |
| 5092 | ObjCTypes.ClassnfABIPtrTy)); |
| 5093 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5094 | UsedGlobals.push_back(Entry); |
| 5095 | } |
| 5096 | |
| 5097 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5098 | } |
| 5099 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5100 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 5101 | /// meta-data |
| 5102 | /// |
| 5103 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 5104 | const ObjCInterfaceDecl *ID) { |
| 5105 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 5106 | if (Entry) |
| 5107 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5108 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5109 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5110 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5111 | Entry = |
| 5112 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5113 | llvm::GlobalValue::InternalLinkage, |
| 5114 | MetaClassGV, |
| 5115 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 5116 | &CGM.getModule()); |
| 5117 | Entry->setAlignment( |
| 5118 | CGM.getTargetData().getPrefTypeAlignment( |
| 5119 | ObjCTypes.ClassnfABIPtrTy)); |
| 5120 | |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5121 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5122 | UsedGlobals.push_back(Entry); |
| 5123 | |
| 5124 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5125 | } |
| 5126 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5127 | /// GetClass - Return a reference to the class for the given interface |
| 5128 | /// decl. |
| 5129 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 5130 | const ObjCInterfaceDecl *ID) { |
| 5131 | return EmitClassRef(Builder, ID); |
| 5132 | } |
| 5133 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5134 | /// Generates a message send where the super is the receiver. This is |
| 5135 | /// a message send to self with special delivery semantics indicating |
| 5136 | /// which class's method should be called. |
| 5137 | CodeGen::RValue |
| 5138 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 5139 | QualType ResultType, |
| 5140 | Selector Sel, |
| 5141 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5142 | bool isCategoryImpl, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5143 | llvm::Value *Receiver, |
| 5144 | bool IsClassMessage, |
| 5145 | const CodeGen::CallArgList &CallArgs) { |
| 5146 | // ... |
| 5147 | // Create and init a super structure; this is a (receiver, class) |
| 5148 | // pair we will pass to objc_msgSendSuper. |
| 5149 | llvm::Value *ObjCSuper = |
| 5150 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 5151 | |
| 5152 | llvm::Value *ReceiverAsObject = |
| 5153 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 5154 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 5155 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
| 5156 | |
| 5157 | // 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] | 5158 | llvm::Value *Target; |
| 5159 | if (IsClassMessage) { |
| 5160 | if (isCategoryImpl) { |
| 5161 | // Message sent to "super' in a class method defined in |
| 5162 | // a category implementation. |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5163 | Target = EmitClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5164 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 5165 | Target = CGF.Builder.CreateLoad(Target); |
| 5166 | } |
| 5167 | else |
| 5168 | Target = EmitMetaClassRef(CGF.Builder, Class); |
| 5169 | } |
| 5170 | else |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5171 | Target = EmitSuperClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5172 | |
| 5173 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 5174 | // and ObjCTypes types. |
| 5175 | const llvm::Type *ClassTy = |
| 5176 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 5177 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 5178 | CGF.Builder.CreateStore(Target, |
| 5179 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 5180 | |
| 5181 | return EmitMessageSend(CGF, ResultType, Sel, |
| 5182 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 5183 | true, CallArgs); |
| 5184 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5185 | |
| 5186 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
| 5187 | Selector Sel) { |
| 5188 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 5189 | |
| 5190 | if (!Entry) { |
| 5191 | llvm::Constant *Casted = |
| 5192 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 5193 | ObjCTypes.SelectorPtrTy); |
| 5194 | Entry = |
| 5195 | new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false, |
| 5196 | llvm::GlobalValue::InternalLinkage, |
| 5197 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_", |
| 5198 | &CGM.getModule()); |
| 5199 | Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip"); |
| 5200 | UsedGlobals.push_back(Entry); |
| 5201 | } |
| 5202 | |
| 5203 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5204 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5205 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 5206 | /// objc_assign_ivar (id src, id *dst) |
| 5207 | /// |
| 5208 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 5209 | llvm::Value *src, llvm::Value *dst) |
| 5210 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5211 | const llvm::Type * SrcTy = src->getType(); |
| 5212 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5213 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5214 | assert(Size <= 8 && "does not support size > 8"); |
| 5215 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5216 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5217 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5218 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5219 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5220 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5221 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5222 | src, dst, "assignivar"); |
| 5223 | return; |
| 5224 | } |
| 5225 | |
| 5226 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 5227 | /// objc_assign_strongCast (id src, id *dst) |
| 5228 | /// |
| 5229 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
| 5230 | CodeGen::CodeGenFunction &CGF, |
| 5231 | llvm::Value *src, llvm::Value *dst) |
| 5232 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5233 | const llvm::Type * SrcTy = src->getType(); |
| 5234 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5235 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5236 | assert(Size <= 8 && "does not support size > 8"); |
| 5237 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5238 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5239 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5240 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5241 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5242 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5243 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5244 | src, dst, "weakassign"); |
| 5245 | return; |
| 5246 | } |
| 5247 | |
| 5248 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 5249 | /// object: objc_read_weak (id *src) |
| 5250 | /// |
| 5251 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
| 5252 | CodeGen::CodeGenFunction &CGF, |
| 5253 | llvm::Value *AddrWeakObj) |
| 5254 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5255 | const llvm::Type* DestTy = |
| 5256 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5257 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 5258 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5259 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5260 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5261 | return read_weak; |
| 5262 | } |
| 5263 | |
| 5264 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 5265 | /// objc_assign_weak (id src, id *dst) |
| 5266 | /// |
| 5267 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 5268 | llvm::Value *src, llvm::Value *dst) |
| 5269 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5270 | const llvm::Type * SrcTy = src->getType(); |
| 5271 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5272 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5273 | assert(Size <= 8 && "does not support size > 8"); |
| 5274 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5275 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5276 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5277 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5278 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5279 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 5280 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5281 | src, dst, "weakassign"); |
| 5282 | return; |
| 5283 | } |
| 5284 | |
| 5285 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 5286 | /// objc_assign_global (id src, id *dst) |
| 5287 | /// |
| 5288 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 5289 | llvm::Value *src, llvm::Value *dst) |
| 5290 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5291 | const llvm::Type * SrcTy = src->getType(); |
| 5292 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5293 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5294 | assert(Size <= 8 && "does not support size > 8"); |
| 5295 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5296 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5297 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5298 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5299 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5300 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5301 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5302 | src, dst, "globalassign"); |
| 5303 | return; |
| 5304 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5305 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5306 | void |
| 5307 | CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 5308 | const Stmt &S) { |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5309 | bool isTry = isa<ObjCAtTryStmt>(S); |
| 5310 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 5311 | llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5312 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5313 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5314 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5315 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
| 5316 | |
| 5317 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 5318 | // evaluation of the expression must occur before we enter the |
| 5319 | // @synchronized. We can safely avoid a temp here because jumps into |
| 5320 | // @synchronized are illegal & this will dominate uses. |
| 5321 | llvm::Value *SyncArg = 0; |
| 5322 | if (!isTry) { |
| 5323 | SyncArg = |
| 5324 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 5325 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5326 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5327 | } |
| 5328 | |
| 5329 | // Push an EH context entry, used for handling rethrows and jumps |
| 5330 | // through finally. |
| 5331 | CGF.PushCleanupBlock(FinallyBlock); |
| 5332 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5333 | CGF.setInvokeDest(TryHandler); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5334 | |
| 5335 | CGF.EmitBlock(TryBlock); |
| 5336 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 5337 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
| 5338 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5339 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5340 | // Emit the exception handler. |
| 5341 | |
| 5342 | CGF.EmitBlock(TryHandler); |
| 5343 | |
| 5344 | llvm::Value *llvm_eh_exception = |
| 5345 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
| 5346 | llvm::Value *llvm_eh_selector_i64 = |
| 5347 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64); |
| 5348 | llvm::Value *llvm_eh_typeid_for_i64 = |
| 5349 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64); |
| 5350 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5351 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow"); |
| 5352 | |
| 5353 | llvm::SmallVector<llvm::Value*, 8> SelectorArgs; |
| 5354 | SelectorArgs.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5355 | SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5356 | |
| 5357 | // Construct the lists of (type, catch body) to handle. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5358 | llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5359 | bool HasCatchAll = false; |
| 5360 | if (isTry) { |
| 5361 | if (const ObjCAtCatchStmt* CatchStmt = |
| 5362 | cast<ObjCAtTryStmt>(S).getCatchStmts()) { |
| 5363 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5364 | const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5365 | Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody())); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5366 | |
| 5367 | // catch(...) always matches. |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5368 | if (!CatchDecl) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5369 | // Use i8* null here to signal this is a catch all, not a cleanup. |
| 5370 | llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 5371 | SelectorArgs.push_back(Null); |
| 5372 | HasCatchAll = true; |
| 5373 | break; |
| 5374 | } |
| 5375 | |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5376 | if (CGF.getContext().isObjCIdType(CatchDecl->getType()) || |
| 5377 | CatchDecl->getType()->isObjCQualifiedIdType()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5378 | llvm::Value *IDEHType = |
| 5379 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 5380 | if (!IDEHType) |
| 5381 | IDEHType = |
| 5382 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5383 | llvm::GlobalValue::ExternalLinkage, |
| 5384 | 0, "OBJC_EHTYPE_id", &CGM.getModule()); |
| 5385 | SelectorArgs.push_back(IDEHType); |
| 5386 | HasCatchAll = true; |
| 5387 | break; |
| 5388 | } |
| 5389 | |
| 5390 | // All other types should be Objective-C interface pointer types. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5391 | const PointerType *PT = CatchDecl->getType()->getAsPointerType(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5392 | assert(PT && "Invalid @catch type."); |
| 5393 | const ObjCInterfaceType *IT = |
| 5394 | PT->getPointeeType()->getAsObjCInterfaceType(); |
| 5395 | assert(IT && "Invalid @catch type."); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5396 | llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5397 | SelectorArgs.push_back(EHType); |
| 5398 | } |
| 5399 | } |
| 5400 | } |
| 5401 | |
| 5402 | // We use a cleanup unless there was already a catch all. |
| 5403 | if (!HasCatchAll) { |
| 5404 | SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5405 | Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5406 | } |
| 5407 | |
| 5408 | llvm::Value *Selector = |
| 5409 | CGF.Builder.CreateCall(llvm_eh_selector_i64, |
| 5410 | SelectorArgs.begin(), SelectorArgs.end(), |
| 5411 | "selector"); |
| 5412 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5413 | const ParmVarDecl *CatchParam = Handlers[i].first; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5414 | const Stmt *CatchBody = Handlers[i].second; |
| 5415 | |
| 5416 | llvm::BasicBlock *Next = 0; |
| 5417 | |
| 5418 | // The last handler always matches. |
| 5419 | if (i + 1 != e) { |
| 5420 | assert(CatchParam && "Only last handler can be a catch all."); |
| 5421 | |
| 5422 | llvm::BasicBlock *Match = CGF.createBasicBlock("match"); |
| 5423 | Next = CGF.createBasicBlock("catch.next"); |
| 5424 | llvm::Value *Id = |
| 5425 | CGF.Builder.CreateCall(llvm_eh_typeid_for_i64, |
| 5426 | CGF.Builder.CreateBitCast(SelectorArgs[i+2], |
| 5427 | ObjCTypes.Int8PtrTy)); |
| 5428 | CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id), |
| 5429 | Match, Next); |
| 5430 | |
| 5431 | CGF.EmitBlock(Match); |
| 5432 | } |
| 5433 | |
| 5434 | if (CatchBody) { |
| 5435 | llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end"); |
| 5436 | llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler"); |
| 5437 | |
| 5438 | // Cleanups must call objc_end_catch. |
| 5439 | // |
| 5440 | // FIXME: It seems incorrect for objc_begin_catch to be inside |
| 5441 | // this context, but this matches gcc. |
| 5442 | CGF.PushCleanupBlock(MatchEnd); |
| 5443 | CGF.setInvokeDest(MatchHandler); |
| 5444 | |
| 5445 | llvm::Value *ExcObject = |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5446 | CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5447 | |
| 5448 | // Bind the catch parameter if it exists. |
| 5449 | if (CatchParam) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5450 | ExcObject = |
| 5451 | CGF.Builder.CreateBitCast(ExcObject, |
| 5452 | CGF.ConvertType(CatchParam->getType())); |
| 5453 | // CatchParam is a ParmVarDecl because of the grammar |
| 5454 | // construction used to handle this, but for codegen purposes |
| 5455 | // we treat this as a local decl. |
| 5456 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
| 5457 | CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5458 | } |
| 5459 | |
| 5460 | CGF.ObjCEHValueStack.push_back(ExcObject); |
| 5461 | CGF.EmitStmt(CatchBody); |
| 5462 | CGF.ObjCEHValueStack.pop_back(); |
| 5463 | |
| 5464 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5465 | |
| 5466 | CGF.EmitBlock(MatchHandler); |
| 5467 | |
| 5468 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5469 | // We are required to emit this call to satisfy LLVM, even |
| 5470 | // though we don't use the result. |
| 5471 | llvm::SmallVector<llvm::Value*, 8> Args; |
| 5472 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5473 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5474 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5475 | 0)); |
| 5476 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5477 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5478 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5479 | |
| 5480 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5481 | |
| 5482 | CGF.EmitBlock(MatchEnd); |
| 5483 | |
| 5484 | // Unfortunately, we also have to generate another EH frame here |
| 5485 | // in case this throws. |
| 5486 | llvm::BasicBlock *MatchEndHandler = |
| 5487 | CGF.createBasicBlock("match.end.handler"); |
| 5488 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5489 | CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5490 | Cont, MatchEndHandler, |
| 5491 | Args.begin(), Args.begin()); |
| 5492 | |
| 5493 | CGF.EmitBlock(Cont); |
| 5494 | if (Info.SwitchBlock) |
| 5495 | CGF.EmitBlock(Info.SwitchBlock); |
| 5496 | if (Info.EndBlock) |
| 5497 | CGF.EmitBlock(Info.EndBlock); |
| 5498 | |
| 5499 | CGF.EmitBlock(MatchEndHandler); |
| 5500 | Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5501 | // We are required to emit this call to satisfy LLVM, even |
| 5502 | // though we don't use the result. |
| 5503 | Args.clear(); |
| 5504 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5505 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5506 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5507 | 0)); |
| 5508 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5509 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5510 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5511 | |
| 5512 | if (Next) |
| 5513 | CGF.EmitBlock(Next); |
| 5514 | } else { |
| 5515 | assert(!Next && "catchup should be last handler."); |
| 5516 | |
| 5517 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5518 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5519 | } |
| 5520 | } |
| 5521 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5522 | // Pop the cleanup entry, the @finally is outside this cleanup |
| 5523 | // scope. |
| 5524 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5525 | CGF.setInvokeDest(PrevLandingPad); |
| 5526 | |
| 5527 | CGF.EmitBlock(FinallyBlock); |
| 5528 | |
| 5529 | if (isTry) { |
| 5530 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 5531 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 5532 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 5533 | } else { |
| 5534 | // Emit 'objc_sync_exit(expr)' as finally's sole statement for |
| 5535 | // @synchronized. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5536 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5537 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5538 | |
| 5539 | if (Info.SwitchBlock) |
| 5540 | CGF.EmitBlock(Info.SwitchBlock); |
| 5541 | if (Info.EndBlock) |
| 5542 | CGF.EmitBlock(Info.EndBlock); |
| 5543 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5544 | // Branch around the rethrow code. |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5545 | CGF.EmitBranch(FinallyEnd); |
| 5546 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5547 | CGF.EmitBlock(FinallyRethrow); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5548 | CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5549 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5550 | CGF.Builder.CreateUnreachable(); |
| 5551 | |
| 5552 | CGF.EmitBlock(FinallyEnd); |
| 5553 | } |
| 5554 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5555 | /// EmitThrowStmt - Generate code for a throw statement. |
| 5556 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 5557 | const ObjCAtThrowStmt &S) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5558 | llvm::Value *Exception; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5559 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5560 | Exception = CGF.EmitScalarExpr(ThrowExpr); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5561 | } else { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5562 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
| 5563 | "Unexpected rethrow outside @catch block."); |
| 5564 | Exception = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5565 | } |
| 5566 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5567 | llvm::Value *ExceptionAsObject = |
| 5568 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 5569 | llvm::BasicBlock *InvokeDest = CGF.getInvokeDest(); |
| 5570 | if (InvokeDest) { |
| 5571 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5572 | CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5573 | Cont, InvokeDest, |
| 5574 | &ExceptionAsObject, &ExceptionAsObject + 1); |
| 5575 | CGF.EmitBlock(Cont); |
| 5576 | } else |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5577 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5578 | CGF.Builder.CreateUnreachable(); |
| 5579 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5580 | // Clear the insertion point to indicate we are in unreachable code. |
| 5581 | CGF.Builder.ClearInsertionPoint(); |
| 5582 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5583 | |
| 5584 | llvm::Value * |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5585 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 5586 | bool ForDefinition) { |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5587 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5588 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5589 | // If we don't need a definition, return the entry if found or check |
| 5590 | // if we use an external reference. |
| 5591 | if (!ForDefinition) { |
| 5592 | if (Entry) |
| 5593 | return Entry; |
Daniel Dunbar | 7e075cb | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 5594 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5595 | // If this type (or a super class) has the __objc_exception__ |
| 5596 | // attribute, emit an external reference. |
| 5597 | if (hasObjCExceptionAttribute(ID)) |
| 5598 | return Entry = |
| 5599 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5600 | llvm::GlobalValue::ExternalLinkage, |
| 5601 | 0, |
| 5602 | (std::string("OBJC_EHTYPE_$_") + |
| 5603 | ID->getIdentifier()->getName()), |
| 5604 | &CGM.getModule()); |
| 5605 | } |
| 5606 | |
| 5607 | // Otherwise we need to either make a new entry or fill in the |
| 5608 | // initializer. |
| 5609 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5610 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5611 | std::string VTableName = "objc_ehtype_vtable"; |
| 5612 | llvm::GlobalVariable *VTableGV = |
| 5613 | CGM.getModule().getGlobalVariable(VTableName); |
| 5614 | if (!VTableGV) |
| 5615 | VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false, |
| 5616 | llvm::GlobalValue::ExternalLinkage, |
| 5617 | 0, VTableName, &CGM.getModule()); |
| 5618 | |
| 5619 | llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2); |
| 5620 | |
| 5621 | std::vector<llvm::Constant*> Values(3); |
| 5622 | Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1); |
| 5623 | Values[1] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5624 | Values[2] = GetClassGlobal(ClassName); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5625 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
| 5626 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5627 | if (Entry) { |
| 5628 | Entry->setInitializer(Init); |
| 5629 | } else { |
| 5630 | Entry = new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5631 | llvm::GlobalValue::WeakAnyLinkage, |
| 5632 | Init, |
| 5633 | (std::string("OBJC_EHTYPE_$_") + |
| 5634 | ID->getIdentifier()->getName()), |
| 5635 | &CGM.getModule()); |
| 5636 | } |
| 5637 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 5638 | if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden) |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5639 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5640 | Entry->setAlignment(8); |
| 5641 | |
| 5642 | if (ForDefinition) { |
| 5643 | Entry->setSection("__DATA,__objc_const"); |
| 5644 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 5645 | } else { |
| 5646 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 5647 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5648 | |
| 5649 | return Entry; |
| 5650 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5651 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5652 | /* *** */ |
| 5653 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5654 | CodeGen::CGObjCRuntime * |
| 5655 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 5656 | return new CGObjCMac(CGM); |
| 5657 | } |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5658 | |
| 5659 | CodeGen::CGObjCRuntime * |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5660 | CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5661 | return new CGObjCNonFragileABIMac(CGM); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5662 | } |