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 | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 21 | #include "clang/Basic/LangOptions.h" |
| 22 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 23 | #include "llvm/Intrinsics.h" |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 24 | #include "llvm/Module.h" |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/DenseSet.h" |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetData.h" |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 27 | #include <sstream> |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace clang; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 30 | using namespace CodeGen; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 31 | |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 32 | // Common CGObjCRuntime functions, these don't belong here, but they |
| 33 | // don't belong in CGObjCRuntime either so we will live with it for |
| 34 | // now. |
| 35 | |
Daniel Dunbar | 84ad77a | 2009-04-22 09:39:34 +0000 | [diff] [blame] | 36 | const llvm::StructType * |
| 37 | CGObjCRuntime::GetConcreteClassStruct(CodeGen::CodeGenModule &CGM, |
| 38 | const ObjCInterfaceDecl *OID) { |
| 39 | assert(!OID->isForwardDecl() && "Invalid interface decl!"); |
Daniel Dunbar | 412f59b | 2009-04-22 10:28:39 +0000 | [diff] [blame] | 40 | const RecordDecl *RD = CGM.getContext().addRecordToClass(OID); |
| 41 | return cast<llvm::StructType>(CGM.getTypes().ConvertTagDeclType(RD)); |
Daniel Dunbar | 84ad77a | 2009-04-22 09:39:34 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Daniel Dunbar | a243578 | 2009-04-22 12:00:04 +0000 | [diff] [blame] | 44 | |
| 45 | /// LookupFieldDeclForIvar - looks up a field decl in the laid out |
| 46 | /// storage which matches this 'ivar'. |
| 47 | /// |
| 48 | static const FieldDecl *LookupFieldDeclForIvar(ASTContext &Context, |
| 49 | const ObjCInterfaceDecl *OID, |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 50 | const ObjCIvarDecl *OIVD, |
| 51 | const ObjCInterfaceDecl *&Found) { |
Daniel Dunbar | a243578 | 2009-04-22 12:00:04 +0000 | [diff] [blame] | 52 | assert(!OID->isForwardDecl() && "Invalid interface decl!"); |
| 53 | const RecordDecl *RecordForDecl = Context.addRecordToClass(OID); |
| 54 | assert(RecordForDecl && "lookupFieldDeclForIvar no storage for class"); |
| 55 | DeclContext::lookup_const_result Lookup = |
| 56 | RecordForDecl->lookup(Context, OIVD->getDeclName()); |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 57 | |
| 58 | if (Lookup.first != Lookup.second) { |
| 59 | Found = OID; |
| 60 | return cast<FieldDecl>(*Lookup.first); |
| 61 | } |
| 62 | |
| 63 | // If lookup failed, try the superclass. |
| 64 | // |
| 65 | // FIXME: This is slow, we shouldn't need to do this. |
| 66 | const ObjCInterfaceDecl *Super = OID->getSuperClass(); |
| 67 | assert(OID && "field decl not found!"); |
| 68 | return LookupFieldDeclForIvar(Context, Super, OIVD, Found); |
Daniel Dunbar | a243578 | 2009-04-22 12:00:04 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 71 | uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM, |
| 72 | const ObjCInterfaceDecl *OID, |
| 73 | const ObjCIvarDecl *Ivar) { |
| 74 | assert(!OID->isForwardDecl() && "Invalid interface decl!"); |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 75 | const ObjCInterfaceDecl *Container; |
| 76 | const FieldDecl *Field = |
| 77 | LookupFieldDeclForIvar(CGM.getContext(), OID, Ivar, Container); |
| 78 | QualType T = CGM.getContext().getObjCInterfaceType(Container); |
| 79 | const llvm::StructType *STy = GetConcreteClassStruct(CGM, Container); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 80 | const llvm::StructLayout *Layout = |
Daniel Dunbar | 84ad77a | 2009-04-22 09:39:34 +0000 | [diff] [blame] | 81 | CGM.getTargetData().getStructLayout(STy); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 82 | if (!Field->isBitField()) |
| 83 | return Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); |
| 84 | |
| 85 | // FIXME. Must be a better way of getting a bitfield base offset. |
| 86 | CodeGenTypes::BitFieldInfo BFI = CGM.getTypes().getBitFieldInfo(Field); |
| 87 | // FIXME: The "field no" for bitfields is something completely |
| 88 | // different; it is the offset in multiples of the base type size! |
| 89 | uint64_t Offset = CGM.getTypes().getLLVMFieldNo(Field); |
| 90 | const llvm::Type *Ty = |
| 91 | CGM.getTypes().ConvertTypeForMemRecursive(Field->getType()); |
| 92 | Offset *= CGM.getTypes().getTargetData().getTypePaddedSizeInBits(Ty); |
| 93 | return (Offset + BFI.Begin) / 8; |
| 94 | } |
| 95 | |
| 96 | LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF, |
| 97 | const ObjCInterfaceDecl *OID, |
| 98 | llvm::Value *BaseValue, |
| 99 | const ObjCIvarDecl *Ivar, |
| 100 | unsigned CVRQualifiers, |
| 101 | llvm::Value *Offset) { |
Daniel Dunbar | 412f59b | 2009-04-22 10:28:39 +0000 | [diff] [blame] | 102 | // Force generation of the codegen information for this structure. |
| 103 | // |
| 104 | // FIXME: Remove once we don't use the bit-field lookup map. |
| 105 | (void) GetConcreteClassStruct(CGF.CGM, OID); |
| 106 | |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 107 | // FIXME: For now, we use an implementation based on just computing |
| 108 | // the offset and calculating things directly. For optimization |
| 109 | // purposes, it would be cleaner to use a GEP on the proper type |
| 110 | // since the structure layout is fixed; however for that we need to |
| 111 | // be able to walk the class chain for an Ivar. |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 112 | const ObjCInterfaceDecl *Container; |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 113 | const FieldDecl *Field = |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 114 | LookupFieldDeclForIvar(CGF.CGM.getContext(), OID, Ivar, Container); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 115 | |
| 116 | // (char *) BaseValue |
| 117 | llvm::Type *I8Ptr = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 118 | llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr); |
| 119 | // (char*)BaseValue + Offset_symbol |
| 120 | V = CGF.Builder.CreateGEP(V, Offset, "add.ptr"); |
| 121 | // (type *)((char*)BaseValue + Offset_symbol) |
| 122 | const llvm::Type *IvarTy = |
| 123 | CGF.CGM.getTypes().ConvertTypeForMem(Ivar->getType()); |
| 124 | llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy); |
| 125 | V = CGF.Builder.CreateBitCast(V, ptrIvarTy); |
| 126 | |
| 127 | if (Ivar->isBitField()) { |
| 128 | QualType FieldTy = Field->getType(); |
| 129 | CodeGenTypes::BitFieldInfo bitFieldInfo = |
| 130 | CGF.CGM.getTypes().getBitFieldInfo(Field); |
| 131 | return LValue::MakeBitfield(V, bitFieldInfo.Begin % 8, bitFieldInfo.Size, |
| 132 | FieldTy->isSignedIntegerType(), |
| 133 | FieldTy.getCVRQualifiers()|CVRQualifiers); |
| 134 | } |
| 135 | |
| 136 | LValue LV = LValue::MakeAddr(V, |
| 137 | Ivar->getType().getCVRQualifiers()|CVRQualifiers, |
| 138 | CGF.CGM.getContext().getObjCGCAttrKind(Ivar->getType())); |
| 139 | LValue::SetObjCIvar(LV, true); |
| 140 | return LV; |
| 141 | } |
| 142 | |
| 143 | /// |
| 144 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 145 | namespace { |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 146 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 147 | typedef std::vector<llvm::Constant*> ConstantVector; |
| 148 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 149 | // FIXME: We should find a nicer way to make the labels for |
| 150 | // metadata, string concatenation is lame. |
| 151 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 152 | class ObjCCommonTypesHelper { |
| 153 | protected: |
| 154 | CodeGen::CodeGenModule &CGM; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 155 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 156 | public: |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 157 | const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 158 | const llvm::Type *Int8PtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 159 | |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 160 | /// ObjectPtrTy - LLVM type for object handles (typeof(id)) |
| 161 | const llvm::Type *ObjectPtrTy; |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 162 | |
| 163 | /// PtrObjectPtrTy - LLVM type for id * |
| 164 | const llvm::Type *PtrObjectPtrTy; |
| 165 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 166 | /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL)) |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 167 | const llvm::Type *SelectorPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 168 | /// ProtocolPtrTy - LLVM type for external protocol handles |
| 169 | /// (typeof(Protocol)) |
| 170 | const llvm::Type *ExternalProtocolPtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 171 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 172 | // SuperCTy - clang type for struct objc_super. |
| 173 | QualType SuperCTy; |
| 174 | // SuperPtrCTy - clang type for struct objc_super *. |
| 175 | QualType SuperPtrCTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 176 | |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 177 | /// SuperTy - LLVM type for struct objc_super. |
| 178 | const llvm::StructType *SuperTy; |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 179 | /// SuperPtrTy - LLVM type for struct objc_super *. |
| 180 | const llvm::Type *SuperPtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 181 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 182 | /// PropertyTy - LLVM type for struct objc_property (struct _prop_t |
| 183 | /// in GCC parlance). |
| 184 | const llvm::StructType *PropertyTy; |
| 185 | |
| 186 | /// PropertyListTy - LLVM type for struct objc_property_list |
| 187 | /// (_prop_list_t in GCC parlance). |
| 188 | const llvm::StructType *PropertyListTy; |
| 189 | /// PropertyListPtrTy - LLVM type for struct objc_property_list*. |
| 190 | const llvm::Type *PropertyListPtrTy; |
| 191 | |
| 192 | // MethodTy - LLVM type for struct objc_method. |
| 193 | const llvm::StructType *MethodTy; |
| 194 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 195 | /// CacheTy - LLVM type for struct objc_cache. |
| 196 | const llvm::Type *CacheTy; |
| 197 | /// CachePtrTy - LLVM type for struct objc_cache *. |
| 198 | const llvm::Type *CachePtrTy; |
| 199 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 200 | llvm::Constant *getGetPropertyFn() { |
| 201 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 202 | ASTContext &Ctx = CGM.getContext(); |
| 203 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
| 204 | llvm::SmallVector<QualType,16> Params; |
| 205 | QualType IdType = Ctx.getObjCIdType(); |
| 206 | QualType SelType = Ctx.getObjCSelType(); |
| 207 | Params.push_back(IdType); |
| 208 | Params.push_back(SelType); |
| 209 | Params.push_back(Ctx.LongTy); |
| 210 | Params.push_back(Ctx.BoolTy); |
| 211 | const llvm::FunctionType *FTy = |
| 212 | Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false); |
| 213 | return CGM.CreateRuntimeFunction(FTy, "objc_getProperty"); |
| 214 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 215 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 216 | llvm::Constant *getSetPropertyFn() { |
| 217 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 218 | ASTContext &Ctx = CGM.getContext(); |
| 219 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
| 220 | llvm::SmallVector<QualType,16> Params; |
| 221 | QualType IdType = Ctx.getObjCIdType(); |
| 222 | QualType SelType = Ctx.getObjCSelType(); |
| 223 | Params.push_back(IdType); |
| 224 | Params.push_back(SelType); |
| 225 | Params.push_back(Ctx.LongTy); |
| 226 | Params.push_back(IdType); |
| 227 | Params.push_back(Ctx.BoolTy); |
| 228 | Params.push_back(Ctx.BoolTy); |
| 229 | const llvm::FunctionType *FTy = |
| 230 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
| 231 | return CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); |
| 232 | } |
| 233 | |
| 234 | llvm::Constant *getEnumerationMutationFn() { |
| 235 | // void objc_enumerationMutation (id) |
| 236 | std::vector<const llvm::Type*> Args; |
| 237 | Args.push_back(ObjectPtrTy); |
| 238 | llvm::FunctionType *FTy = |
| 239 | llvm::FunctionType::get(llvm::Type::VoidTy, Args, false); |
| 240 | return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation"); |
| 241 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 242 | |
| 243 | /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 244 | llvm::Constant *getGcReadWeakFn() { |
| 245 | // id objc_read_weak (id *) |
| 246 | std::vector<const llvm::Type*> Args; |
| 247 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 248 | llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 249 | return CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
| 250 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 251 | |
| 252 | /// GcAssignWeakFn -- LLVM objc_assign_weak function. |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 253 | llvm::Constant *getGcAssignWeakFn() { |
| 254 | // id objc_assign_weak (id, id *) |
| 255 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 256 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 257 | llvm::FunctionType *FTy = |
| 258 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 259 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 260 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 261 | |
| 262 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 263 | llvm::Constant *getGcAssignGlobalFn() { |
| 264 | // id objc_assign_global(id, id *) |
| 265 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 266 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 267 | llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 268 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 269 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 270 | |
| 271 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 272 | llvm::Constant *getGcAssignIvarFn() { |
| 273 | // id objc_assign_ivar(id, id *) |
| 274 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 275 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 276 | llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 277 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 278 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 279 | |
| 280 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 281 | llvm::Constant *getGcAssignStrongCastFn() { |
| 282 | // id objc_assign_global(id, id *) |
| 283 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 284 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 285 | llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 286 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); |
| 287 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 288 | |
| 289 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 290 | llvm::Constant *getExceptionThrowFn() { |
| 291 | // void objc_exception_throw(id) |
| 292 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 293 | llvm::FunctionType *FTy = |
| 294 | llvm::FunctionType::get(llvm::Type::VoidTy, Args, false); |
| 295 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
| 296 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 297 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 298 | /// SyncEnterFn - LLVM object_sync_enter function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 299 | llvm::Constant *getSyncEnterFn() { |
| 300 | // void objc_sync_enter (id) |
| 301 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 302 | llvm::FunctionType *FTy = |
| 303 | llvm::FunctionType::get(llvm::Type::VoidTy, Args, false); |
| 304 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 305 | } |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 306 | |
| 307 | /// SyncExitFn - LLVM object_sync_exit function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 308 | llvm::Constant *getSyncExitFn() { |
| 309 | // void objc_sync_exit (id) |
| 310 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 311 | llvm::FunctionType *FTy = |
| 312 | llvm::FunctionType::get(llvm::Type::VoidTy, Args, false); |
| 313 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
| 314 | } |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 315 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 316 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
| 317 | ~ObjCCommonTypesHelper(){} |
| 318 | }; |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 319 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 320 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 321 | /// construction of varies types used during ObjC generation. |
| 322 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
| 323 | private: |
| 324 | |
Chris Lattner | 4176b0c | 2009-04-22 02:32:31 +0000 | [diff] [blame] | 325 | llvm::Constant *getMessageSendFn() { |
| 326 | // id objc_msgSend (id, SEL, ...) |
| 327 | std::vector<const llvm::Type*> Params; |
| 328 | Params.push_back(ObjectPtrTy); |
| 329 | Params.push_back(SelectorPtrTy); |
| 330 | return |
| 331 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 332 | Params, true), |
| 333 | "objc_msgSend"); |
| 334 | } |
| 335 | |
| 336 | llvm::Constant *getMessageSendStretFn() { |
| 337 | // id objc_msgSend_stret (id, SEL, ...) |
| 338 | std::vector<const llvm::Type*> Params; |
| 339 | Params.push_back(ObjectPtrTy); |
| 340 | Params.push_back(SelectorPtrTy); |
| 341 | return |
| 342 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 343 | Params, true), |
| 344 | "objc_msgSend_stret"); |
| 345 | |
| 346 | } |
| 347 | |
| 348 | llvm::Constant *getMessageSendFpretFn() { |
| 349 | // FIXME: This should be long double on x86_64? |
| 350 | // [double | long double] objc_msgSend_fpret(id self, SEL op, ...) |
| 351 | std::vector<const llvm::Type*> Params; |
| 352 | Params.push_back(ObjectPtrTy); |
| 353 | Params.push_back(SelectorPtrTy); |
| 354 | return |
| 355 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy, |
| 356 | Params, |
| 357 | true), |
| 358 | "objc_msgSend_fpret"); |
| 359 | |
| 360 | } |
| 361 | |
| 362 | llvm::Constant *getMessageSendSuperFn() { |
| 363 | // id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
| 364 | std::vector<const llvm::Type*> Params; |
| 365 | Params.push_back(SuperPtrTy); |
| 366 | Params.push_back(SelectorPtrTy); |
| 367 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 368 | Params, true), |
| 369 | "objc_msgSendSuper"); |
| 370 | } |
| 371 | llvm::Constant *getMessageSendSuperStretFn() { |
| 372 | // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super, |
| 373 | // SEL op, ...) |
| 374 | std::vector<const llvm::Type*> Params; |
| 375 | Params.push_back(Int8PtrTy); |
| 376 | Params.push_back(SuperPtrTy); |
| 377 | Params.push_back(SelectorPtrTy); |
| 378 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 379 | Params, true), |
| 380 | "objc_msgSendSuper_stret"); |
| 381 | } |
| 382 | |
| 383 | llvm::Constant *getMessageSendSuperFpretFn() { |
| 384 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 385 | return getMessageSendSuperFn(); |
| 386 | } |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 387 | |
| 388 | public: |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 389 | /// SymtabTy - LLVM type for struct objc_symtab. |
| 390 | const llvm::StructType *SymtabTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 391 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
| 392 | const llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 393 | /// ModuleTy - LLVM type for struct objc_module. |
| 394 | const llvm::StructType *ModuleTy; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 395 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 396 | /// ProtocolTy - LLVM type for struct objc_protocol. |
| 397 | const llvm::StructType *ProtocolTy; |
| 398 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
| 399 | const llvm::Type *ProtocolPtrTy; |
| 400 | /// ProtocolExtensionTy - LLVM type for struct |
| 401 | /// objc_protocol_extension. |
| 402 | const llvm::StructType *ProtocolExtensionTy; |
| 403 | /// ProtocolExtensionTy - LLVM type for struct |
| 404 | /// objc_protocol_extension *. |
| 405 | const llvm::Type *ProtocolExtensionPtrTy; |
| 406 | /// MethodDescriptionTy - LLVM type for struct |
| 407 | /// objc_method_description. |
| 408 | const llvm::StructType *MethodDescriptionTy; |
| 409 | /// MethodDescriptionListTy - LLVM type for struct |
| 410 | /// objc_method_description_list. |
| 411 | const llvm::StructType *MethodDescriptionListTy; |
| 412 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 413 | /// objc_method_description_list *. |
| 414 | const llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 415 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
| 416 | const llvm::Type *ProtocolListTy; |
| 417 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
| 418 | const llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 419 | /// CategoryTy - LLVM type for struct objc_category. |
| 420 | const llvm::StructType *CategoryTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 421 | /// ClassTy - LLVM type for struct objc_class. |
| 422 | const llvm::StructType *ClassTy; |
| 423 | /// ClassPtrTy - LLVM type for struct objc_class *. |
| 424 | const llvm::Type *ClassPtrTy; |
| 425 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
| 426 | const llvm::StructType *ClassExtensionTy; |
| 427 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
| 428 | const llvm::Type *ClassExtensionPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 429 | // IvarTy - LLVM type for struct objc_ivar. |
| 430 | const llvm::StructType *IvarTy; |
| 431 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
| 432 | const llvm::Type *IvarListTy; |
| 433 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
| 434 | const llvm::Type *IvarListPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 435 | /// MethodListTy - LLVM type for struct objc_method_list. |
| 436 | const llvm::Type *MethodListTy; |
| 437 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
| 438 | const llvm::Type *MethodListPtrTy; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 439 | |
| 440 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
| 441 | const llvm::Type *ExceptionDataTy; |
| 442 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 443 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 444 | llvm::Constant *getExceptionTryEnterFn() { |
| 445 | std::vector<const llvm::Type*> Params; |
| 446 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 447 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 448 | Params, false), |
| 449 | "objc_exception_try_enter"); |
| 450 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 451 | |
| 452 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 453 | llvm::Constant *getExceptionTryExitFn() { |
| 454 | std::vector<const llvm::Type*> Params; |
| 455 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 456 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 457 | Params, false), |
| 458 | "objc_exception_try_exit"); |
| 459 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 460 | |
| 461 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 462 | llvm::Constant *getExceptionExtractFn() { |
| 463 | std::vector<const llvm::Type*> Params; |
| 464 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 465 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 466 | Params, false), |
| 467 | "objc_exception_extract"); |
| 468 | |
| 469 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 470 | |
| 471 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 472 | llvm::Constant *getExceptionMatchFn() { |
| 473 | std::vector<const llvm::Type*> Params; |
| 474 | Params.push_back(ClassPtrTy); |
| 475 | Params.push_back(ObjectPtrTy); |
| 476 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 477 | Params, false), |
| 478 | "objc_exception_match"); |
| 479 | |
| 480 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 481 | |
| 482 | /// SetJmpFn - LLVM _setjmp function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 483 | llvm::Constant *getSetJmpFn() { |
| 484 | std::vector<const llvm::Type*> Params; |
| 485 | Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty)); |
| 486 | return |
| 487 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 488 | Params, false), |
| 489 | "_setjmp"); |
| 490 | |
| 491 | } |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 492 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 493 | public: |
| 494 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 495 | ~ObjCTypesHelper() {} |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 496 | |
| 497 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 498 | llvm::Constant *getSendFn(bool IsSuper) { |
Chris Lattner | 4176b0c | 2009-04-22 02:32:31 +0000 | [diff] [blame] | 499 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFn(); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 502 | llvm::Constant *getSendStretFn(bool IsSuper) { |
Chris Lattner | 4176b0c | 2009-04-22 02:32:31 +0000 | [diff] [blame] | 503 | return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn(); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 506 | llvm::Constant *getSendFpretFn(bool IsSuper) { |
Chris Lattner | 4176b0c | 2009-04-22 02:32:31 +0000 | [diff] [blame] | 507 | return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn(); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 508 | } |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 509 | }; |
| 510 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 511 | /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 512 | /// modern abi |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 513 | class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 514 | public: |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 515 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 516 | // MethodListnfABITy - LLVM for struct _method_list_t |
| 517 | const llvm::StructType *MethodListnfABITy; |
| 518 | |
| 519 | // MethodListnfABIPtrTy - LLVM for struct _method_list_t* |
| 520 | const llvm::Type *MethodListnfABIPtrTy; |
| 521 | |
| 522 | // ProtocolnfABITy = LLVM for struct _protocol_t |
| 523 | const llvm::StructType *ProtocolnfABITy; |
| 524 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 525 | // ProtocolnfABIPtrTy = LLVM for struct _protocol_t* |
| 526 | const llvm::Type *ProtocolnfABIPtrTy; |
| 527 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 528 | // ProtocolListnfABITy - LLVM for struct _objc_protocol_list |
| 529 | const llvm::StructType *ProtocolListnfABITy; |
| 530 | |
| 531 | // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list* |
| 532 | const llvm::Type *ProtocolListnfABIPtrTy; |
| 533 | |
| 534 | // ClassnfABITy - LLVM for struct _class_t |
| 535 | const llvm::StructType *ClassnfABITy; |
| 536 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 537 | // ClassnfABIPtrTy - LLVM for struct _class_t* |
| 538 | const llvm::Type *ClassnfABIPtrTy; |
| 539 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 540 | // IvarnfABITy - LLVM for struct _ivar_t |
| 541 | const llvm::StructType *IvarnfABITy; |
| 542 | |
| 543 | // IvarListnfABITy - LLVM for struct _ivar_list_t |
| 544 | const llvm::StructType *IvarListnfABITy; |
| 545 | |
| 546 | // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t* |
| 547 | const llvm::Type *IvarListnfABIPtrTy; |
| 548 | |
| 549 | // ClassRonfABITy - LLVM for struct _class_ro_t |
| 550 | const llvm::StructType *ClassRonfABITy; |
| 551 | |
| 552 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 553 | const llvm::Type *ImpnfABITy; |
| 554 | |
| 555 | // CategorynfABITy - LLVM for struct _category_t |
| 556 | const llvm::StructType *CategorynfABITy; |
| 557 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 558 | // New types for nonfragile abi messaging. |
| 559 | |
| 560 | // MessageRefTy - LLVM for: |
| 561 | // struct _message_ref_t { |
| 562 | // IMP messenger; |
| 563 | // SEL name; |
| 564 | // }; |
| 565 | const llvm::StructType *MessageRefTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 566 | // MessageRefCTy - clang type for struct _message_ref_t |
| 567 | QualType MessageRefCTy; |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 568 | |
| 569 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 570 | const llvm::Type *MessageRefPtrTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 571 | // MessageRefCPtrTy - clang type for struct _message_ref_t* |
| 572 | QualType MessageRefCPtrTy; |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 573 | |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 574 | // MessengerTy - Type of the messenger (shown as IMP above) |
| 575 | const llvm::FunctionType *MessengerTy; |
| 576 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 577 | // SuperMessageRefTy - LLVM for: |
| 578 | // struct _super_message_ref_t { |
| 579 | // SUPER_IMP messenger; |
| 580 | // SEL name; |
| 581 | // }; |
| 582 | const llvm::StructType *SuperMessageRefTy; |
| 583 | |
| 584 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 585 | const llvm::Type *SuperMessageRefPtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 586 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 587 | llvm::Constant *getMessageSendFixupFn() { |
| 588 | // id objc_msgSend_fixup(id, struct message_ref_t*, ...) |
| 589 | std::vector<const llvm::Type*> Params; |
| 590 | Params.push_back(ObjectPtrTy); |
| 591 | Params.push_back(MessageRefPtrTy); |
| 592 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 593 | Params, true), |
| 594 | "objc_msgSend_fixup"); |
| 595 | } |
| 596 | |
| 597 | llvm::Constant *getMessageSendFpretFixupFn() { |
| 598 | // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...) |
| 599 | std::vector<const llvm::Type*> Params; |
| 600 | Params.push_back(ObjectPtrTy); |
| 601 | Params.push_back(MessageRefPtrTy); |
| 602 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 603 | Params, true), |
| 604 | "objc_msgSend_fpret_fixup"); |
| 605 | } |
| 606 | |
| 607 | llvm::Constant *getMessageSendStretFixupFn() { |
| 608 | // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...) |
| 609 | std::vector<const llvm::Type*> Params; |
| 610 | Params.push_back(ObjectPtrTy); |
| 611 | Params.push_back(MessageRefPtrTy); |
| 612 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 613 | Params, true), |
| 614 | "objc_msgSend_stret_fixup"); |
| 615 | } |
| 616 | |
| 617 | llvm::Constant *getMessageSendIdFixupFn() { |
| 618 | // id objc_msgSendId_fixup(id, struct message_ref_t*, ...) |
| 619 | std::vector<const llvm::Type*> Params; |
| 620 | Params.push_back(ObjectPtrTy); |
| 621 | Params.push_back(MessageRefPtrTy); |
| 622 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 623 | Params, true), |
| 624 | "objc_msgSendId_fixup"); |
| 625 | } |
| 626 | |
| 627 | llvm::Constant *getMessageSendIdStretFixupFn() { |
| 628 | // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...) |
| 629 | std::vector<const llvm::Type*> Params; |
| 630 | Params.push_back(ObjectPtrTy); |
| 631 | Params.push_back(MessageRefPtrTy); |
| 632 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 633 | Params, true), |
| 634 | "objc_msgSendId_stret_fixup"); |
| 635 | } |
| 636 | llvm::Constant *getMessageSendSuper2FixupFn() { |
| 637 | // id objc_msgSendSuper2_fixup (struct objc_super *, |
| 638 | // struct _super_message_ref_t*, ...) |
| 639 | std::vector<const llvm::Type*> Params; |
| 640 | Params.push_back(SuperPtrTy); |
| 641 | Params.push_back(SuperMessageRefPtrTy); |
| 642 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 643 | Params, true), |
| 644 | "objc_msgSendSuper2_fixup"); |
| 645 | } |
| 646 | |
| 647 | llvm::Constant *getMessageSendSuper2StretFixupFn() { |
| 648 | // id objc_msgSendSuper2_stret_fixup(struct objc_super *, |
| 649 | // struct _super_message_ref_t*, ...) |
| 650 | std::vector<const llvm::Type*> Params; |
| 651 | Params.push_back(SuperPtrTy); |
| 652 | Params.push_back(SuperMessageRefPtrTy); |
| 653 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 654 | Params, true), |
| 655 | "objc_msgSendSuper2_stret_fixup"); |
| 656 | } |
| 657 | |
| 658 | |
| 659 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 660 | /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C |
| 661 | /// exception personality function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 662 | llvm::Value *getEHPersonalityPtr() { |
| 663 | llvm::Constant *Personality = |
| 664 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 665 | std::vector<const llvm::Type*>(), |
| 666 | true), |
| 667 | "__objc_personality_v0"); |
| 668 | return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy); |
| 669 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 670 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 671 | llvm::Constant *getUnwindResumeOrRethrowFn() { |
| 672 | std::vector<const llvm::Type*> Params; |
| 673 | Params.push_back(Int8PtrTy); |
| 674 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 675 | Params, false), |
| 676 | "_Unwind_Resume_or_Rethrow"); |
| 677 | } |
| 678 | |
| 679 | llvm::Constant *getObjCEndCatchFn() { |
| 680 | std::vector<const llvm::Type*> Params; |
| 681 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 682 | Params, false), |
| 683 | "objc_end_catch"); |
| 684 | |
| 685 | } |
| 686 | |
| 687 | llvm::Constant *getObjCBeginCatchFn() { |
| 688 | std::vector<const llvm::Type*> Params; |
| 689 | Params.push_back(Int8PtrTy); |
| 690 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy, |
| 691 | Params, false), |
| 692 | "objc_begin_catch"); |
| 693 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 694 | |
| 695 | const llvm::StructType *EHTypeTy; |
| 696 | const llvm::Type *EHTypePtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 697 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 698 | ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); |
| 699 | ~ObjCNonFragileABITypesHelper(){} |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 700 | }; |
| 701 | |
| 702 | class CGObjCCommonMac : public CodeGen::CGObjCRuntime { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 703 | public: |
| 704 | // FIXME - accessibility |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 705 | class GC_IVAR { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 706 | public: |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 707 | unsigned int ivar_bytepos; |
| 708 | unsigned int ivar_size; |
| 709 | GC_IVAR() : ivar_bytepos(0), ivar_size(0) {} |
| 710 | }; |
| 711 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 712 | class SKIP_SCAN { |
| 713 | public: |
| 714 | unsigned int skip; |
| 715 | unsigned int scan; |
| 716 | SKIP_SCAN() : skip(0), scan(0) {} |
| 717 | }; |
| 718 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 719 | protected: |
| 720 | CodeGen::CodeGenModule &CGM; |
| 721 | // FIXME! May not be needing this after all. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 722 | unsigned ObjCABI; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 723 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 724 | // gc ivar layout bitmap calculation helper caches. |
| 725 | llvm::SmallVector<GC_IVAR, 16> SkipIvars; |
| 726 | llvm::SmallVector<GC_IVAR, 16> IvarsInfo; |
| 727 | llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 728 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 729 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 730 | /// DefinedSymbols and FinishModule(). |
| 731 | std::set<IdentifierInfo*> LazySymbols; |
| 732 | |
| 733 | /// DefinedSymbols - External symbols which are defined by this |
| 734 | /// module. The symbols in this list and LazySymbols are used to add |
| 735 | /// special linker symbols which ensure that Objective-C modules are |
| 736 | /// linked properly. |
| 737 | std::set<IdentifierInfo*> DefinedSymbols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 738 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 739 | /// ClassNames - uniqued class names. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 740 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 741 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 742 | /// MethodVarNames - uniqued method variable names. |
| 743 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 744 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 745 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 746 | /// a StringMap here because have no other unique reference. |
| 747 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 748 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 749 | /// MethodDefinitions - map of methods which have been defined in |
| 750 | /// this translation unit. |
| 751 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 752 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 753 | /// PropertyNames - uniqued method variable names. |
| 754 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 755 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 756 | /// ClassReferences - uniqued class references. |
| 757 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 758 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 759 | /// SelectorReferences - uniqued selector references. |
| 760 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 761 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 762 | /// Protocols - Protocols for which an objc_protocol structure has |
| 763 | /// been emitted. Forward declarations are handled by creating an |
| 764 | /// empty structure whose initializer is filled in when/if defined. |
| 765 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 766 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 767 | /// DefinedProtocols - Protocols which have actually been |
| 768 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 769 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 770 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 771 | /// DefinedClasses - List of defined classes. |
| 772 | std::vector<llvm::GlobalValue*> DefinedClasses; |
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 | /// DefinedCategories - List of defined categories. |
| 775 | std::vector<llvm::GlobalValue*> DefinedCategories; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 776 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 777 | /// UsedGlobals - List of globals to pack into the llvm.used metadata |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 778 | /// to prevent them from being clobbered. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 779 | std::vector<llvm::GlobalVariable*> UsedGlobals; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 780 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 781 | /// GetNameForMethod - Return a name for the given method. |
| 782 | /// \param[out] NameOut - The return value. |
| 783 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 784 | const ObjCContainerDecl *CD, |
| 785 | std::string &NameOut); |
| 786 | |
| 787 | /// GetMethodVarName - Return a unique constant for the given |
| 788 | /// selector's name. The return value has type char *. |
| 789 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 790 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
| 791 | llvm::Constant *GetMethodVarName(const std::string &Name); |
| 792 | |
| 793 | /// GetMethodVarType - Return a unique constant for the given |
| 794 | /// selector's name. The return value has type char *. |
| 795 | |
| 796 | // FIXME: This is a horrible name. |
| 797 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 798 | llvm::Constant *GetMethodVarType(const FieldDecl *D); |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 799 | |
| 800 | /// GetPropertyName - Return a unique constant for the given |
| 801 | /// name. The return value has type char *. |
| 802 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
| 803 | |
| 804 | // FIXME: This can be dropped once string functions are unified. |
| 805 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 806 | const Decl *Container); |
| 807 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 808 | /// GetClassName - Return a unique constant for the given selector's |
| 809 | /// name. The return value has type char *. |
| 810 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
| 811 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 812 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 813 | /// implementation for the __strong or __weak case. |
| 814 | /// |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 815 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
| 816 | bool ForStrongLayout); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 817 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 818 | void BuildAggrIvarLayout(const ObjCInterfaceDecl *OI, |
| 819 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 820 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 821 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 822 | unsigned int BytePos, bool ForStrongLayout, |
| 823 | int &Index, int &SkIndex, bool &HasUnion); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 824 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 825 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 826 | /// ivar layout bitmap. |
| 827 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 828 | const ObjCCommonTypesHelper &ObjCTypes); |
| 829 | |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 830 | /// EmitPropertyList - Emit the given property list. The return |
| 831 | /// value has type PropertyListPtrTy. |
| 832 | llvm::Constant *EmitPropertyList(const std::string &Name, |
| 833 | const Decl *Container, |
| 834 | const ObjCContainerDecl *OCD, |
| 835 | const ObjCCommonTypesHelper &ObjCTypes); |
| 836 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 837 | /// GetProtocolRef - Return a reference to the internal protocol |
| 838 | /// description, creating an empty one if it has not been |
| 839 | /// defined. The return value has type ProtocolPtrTy. |
| 840 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 841 | |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 842 | /// GetFieldBaseOffset - return's field byte offset. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 843 | uint64_t GetFieldBaseOffset(const ObjCInterfaceDecl *OI, |
| 844 | const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 845 | const FieldDecl *Field); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 846 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 847 | /// CreateMetadataVar - Create a global variable with internal |
| 848 | /// linkage for use by the Objective-C runtime. |
| 849 | /// |
| 850 | /// This is a convenience wrapper which not only creates the |
| 851 | /// variable, but also sets the section and alignment and adds the |
| 852 | /// global to the UsedGlobals list. |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 853 | /// |
| 854 | /// \param Name - The variable name. |
| 855 | /// \param Init - The variable initializer; this is also used to |
| 856 | /// define the type of the variable. |
| 857 | /// \param Section - The section the variable should go into, or 0. |
| 858 | /// \param Align - The alignment for the variable, or 0. |
| 859 | /// \param AddToUsed - Whether the variable should be added to |
Daniel Dunbar | c158306 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 860 | /// "llvm.used". |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 861 | llvm::GlobalVariable *CreateMetadataVar(const std::string &Name, |
| 862 | llvm::Constant *Init, |
| 863 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 864 | unsigned Align, |
| 865 | bool AddToUsed); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 866 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 867 | /// GetNamedIvarList - Return the list of ivars in the interface |
| 868 | /// itself (not including super classes and not including unnamed |
| 869 | /// bitfields). |
| 870 | /// |
| 871 | /// For the non-fragile ABI, this also includes synthesized property |
| 872 | /// ivars. |
| 873 | void GetNamedIvarList(const ObjCInterfaceDecl *OID, |
| 874 | llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const; |
| 875 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 876 | public: |
| 877 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm) |
| 878 | { } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 879 | |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 880 | virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 881 | |
| 882 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 883 | const ObjCContainerDecl *CD=0); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 884 | |
| 885 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
| 886 | |
| 887 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 888 | /// declaration, emitting it if necessary. The return value has type |
| 889 | /// ProtocolPtrTy. |
| 890 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
| 891 | |
| 892 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 893 | /// object for the given declaration, emitting it if needed. These |
| 894 | /// forward references will be filled in with empty bodies if no |
| 895 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 896 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 897 | }; |
| 898 | |
| 899 | class CGObjCMac : public CGObjCCommonMac { |
| 900 | private: |
| 901 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 902 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 903 | /// level information. |
| 904 | void EmitImageInfo(); |
| 905 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 906 | /// EmitModuleInfo - Another marker encoding module level |
| 907 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 908 | void EmitModuleInfo(); |
| 909 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 910 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 911 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 912 | llvm::Constant *EmitModuleSymbols(); |
| 913 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 914 | /// FinishModule - Write out global data structures at the end of |
| 915 | /// processing a translation unit. |
| 916 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 917 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 918 | /// EmitClassExtension - Generate the class extension structure used |
| 919 | /// to store the weak ivar layout and properties. The return value |
| 920 | /// has type ClassExtensionPtrTy. |
| 921 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 922 | |
| 923 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 924 | /// for the given class. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 925 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 926 | const ObjCInterfaceDecl *ID); |
| 927 | |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 928 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 929 | QualType ResultType, |
| 930 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 931 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 932 | QualType Arg0Ty, |
| 933 | bool IsSuper, |
| 934 | const CallArgList &CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 935 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 936 | /// EmitIvarList - Emit the ivar list for the given |
| 937 | /// implementation. If ForClass is true the list of class ivars |
| 938 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 939 | /// interface ivars will be emitted. The return value has type |
| 940 | /// IvarListPtrTy. |
| 941 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 942 | bool ForClass); |
| 943 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 944 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 945 | /// for the metaclass of the given interface. The return value has |
| 946 | /// type ClassPtrTy. |
| 947 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 948 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 949 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 950 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 951 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 952 | llvm::Constant *Protocols, |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 953 | const llvm::Type *InterfaceTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 954 | const ConstantVector &Methods); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 955 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 956 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 957 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 958 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 959 | |
| 960 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 961 | /// implementation. The return value has type MethodListPtrTy. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 962 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 963 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 964 | const ConstantVector &Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 965 | |
| 966 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 967 | /// method declarations. |
| 968 | /// - TypeName: The name for the type containing the methods. |
| 969 | /// - IsProtocol: True iff these methods are for a protocol. |
| 970 | /// - ClassMethds: True iff these are class methods. |
| 971 | /// - Required: When true, only "required" methods are |
| 972 | /// listed. Similarly, when false only "optional" methods are |
| 973 | /// listed. For classes this should always be true. |
| 974 | /// - begin, end: The method list to output. |
| 975 | /// |
| 976 | /// The return value has type MethodDescriptionListPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 977 | llvm::Constant *EmitMethodDescList(const std::string &Name, |
| 978 | const char *Section, |
| 979 | const ConstantVector &Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 980 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 981 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 982 | /// declaration, emitting it if necessary. The return value has type |
| 983 | /// ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 984 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 985 | |
| 986 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 987 | /// object for the given declaration, emitting it if needed. These |
| 988 | /// forward references will be filled in with empty bodies if no |
| 989 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 990 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 991 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 992 | /// EmitProtocolExtension - Generate the protocol extension |
| 993 | /// structure used to store optional instance and class methods, and |
| 994 | /// protocol properties. The return value has type |
| 995 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 996 | llvm::Constant * |
| 997 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 998 | const ConstantVector &OptInstanceMethods, |
| 999 | const ConstantVector &OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1000 | |
| 1001 | /// EmitProtocolList - Generate the list of referenced |
| 1002 | /// protocols. The return value has type ProtocolListPtrTy. |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1003 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 1004 | ObjCProtocolDecl::protocol_iterator begin, |
| 1005 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1006 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1007 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1008 | /// for the given selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1009 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1010 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1011 | public: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1012 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1013 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1014 | virtual llvm::Function *ModuleInitFunction(); |
| 1015 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1016 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1017 | QualType ResultType, |
| 1018 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1019 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1020 | bool IsClassMessage, |
| 1021 | const CallArgList &CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1022 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1023 | virtual CodeGen::RValue |
| 1024 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1025 | QualType ResultType, |
| 1026 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1027 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1028 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1029 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1030 | bool IsClassMessage, |
| 1031 | const CallArgList &CallArgs); |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1032 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1033 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1034 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1035 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1036 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1037 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1038 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1039 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1040 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1041 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1042 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1043 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1044 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1045 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 1046 | virtual llvm::Constant *GetPropertySetFunction(); |
| 1047 | virtual llvm::Constant *EnumerationMutationFunction(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1048 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 1049 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1050 | const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1051 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 1052 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1053 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 1054 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1055 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 1056 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1057 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 1058 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 1059 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 1060 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1061 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 1062 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 1063 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1064 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1065 | QualType ObjectTy, |
| 1066 | llvm::Value *BaseValue, |
| 1067 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1068 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1069 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1070 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1071 | const ObjCIvarDecl *Ivar); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1072 | }; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1073 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1074 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1075 | private: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1076 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1077 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 1078 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1079 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1080 | /// SuperClassReferences - uniqued super class references. |
| 1081 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; |
| 1082 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1083 | /// MetaClassReferences - uniqued meta class references. |
| 1084 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1085 | |
| 1086 | /// EHTypeReferences - uniqued class ehtype references. |
| 1087 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1088 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1089 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 1090 | /// processing a translation unit. |
| 1091 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1092 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 1093 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 1094 | unsigned InstanceStart, |
| 1095 | unsigned InstanceSize, |
| 1096 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1097 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
| 1098 | llvm::Constant *IsAGV, |
| 1099 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 1100 | llvm::Constant *ClassRoGV, |
| 1101 | bool HiddenVisibility); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1102 | |
| 1103 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
| 1104 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 1105 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
| 1106 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1107 | /// EmitMethodList - Emit the method list for the given |
| 1108 | /// implementation. The return value has type MethodListnfABITy. |
| 1109 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 1110 | const char *Section, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 1111 | const ConstantVector &Methods); |
| 1112 | /// EmitIvarList - Emit the ivar list for the given |
| 1113 | /// implementation. If ForClass is true the list of class ivars |
| 1114 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1115 | /// interface ivars will be emitted. The return value has type |
| 1116 | /// IvarListnfABIPtrTy. |
| 1117 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 1118 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1119 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 1120 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 1121 | unsigned long int offset); |
| 1122 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1123 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1124 | /// declaration, emitting it if necessary. The return value has type |
| 1125 | /// ProtocolPtrTy. |
| 1126 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
| 1127 | |
| 1128 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1129 | /// object for the given declaration, emitting it if needed. These |
| 1130 | /// forward references will be filled in with empty bodies if no |
| 1131 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1132 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
| 1133 | |
| 1134 | /// EmitProtocolList - Generate the list of referenced |
| 1135 | /// protocols. The return value has type ProtocolListPtrTy. |
| 1136 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 1137 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1138 | ObjCProtocolDecl::protocol_iterator end); |
| 1139 | |
| 1140 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1141 | QualType ResultType, |
| 1142 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 1143 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1144 | QualType Arg0Ty, |
| 1145 | bool IsSuper, |
| 1146 | const CallArgList &CallArgs); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 1147 | |
| 1148 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1149 | /// class of the given name. |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 1150 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name); |
| 1151 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1152 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1153 | /// for the given class reference. |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1154 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1155 | const ObjCInterfaceDecl *ID); |
| 1156 | |
| 1157 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1158 | /// for the given super class reference. |
| 1159 | llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder, |
| 1160 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1161 | |
| 1162 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 1163 | /// meta-data |
| 1164 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
| 1165 | const ObjCInterfaceDecl *ID); |
| 1166 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1167 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 1168 | /// the given ivar. |
| 1169 | /// |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 1170 | llvm::GlobalVariable * ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1171 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1172 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1173 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1174 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1175 | /// for the given selector. |
| 1176 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1177 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1178 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1179 | /// interface. The return value has type EHTypePtrTy. |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1180 | llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 1181 | bool ForDefinition); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1182 | |
| 1183 | const char *getMetaclassSymbolPrefix() const { |
| 1184 | return "OBJC_METACLASS_$_"; |
| 1185 | } |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1186 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1187 | const char *getClassSymbolPrefix() const { |
| 1188 | return "OBJC_CLASS_$_"; |
| 1189 | } |
| 1190 | |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1191 | void GetClassSizeInfo(const ObjCInterfaceDecl *OID, |
| 1192 | uint32_t &InstanceStart, |
| 1193 | uint32_t &InstanceSize); |
| 1194 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1195 | public: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1196 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1197 | // FIXME. All stubs for now! |
| 1198 | virtual llvm::Function *ModuleInitFunction(); |
| 1199 | |
| 1200 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1201 | QualType ResultType, |
| 1202 | Selector Sel, |
| 1203 | llvm::Value *Receiver, |
| 1204 | bool IsClassMessage, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1205 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1206 | |
| 1207 | virtual CodeGen::RValue |
| 1208 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 1209 | QualType ResultType, |
| 1210 | Selector Sel, |
| 1211 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1212 | bool isCategoryImpl, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1213 | llvm::Value *Receiver, |
| 1214 | bool IsClassMessage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1215 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1216 | |
| 1217 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1218 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1219 | |
| 1220 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel) |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1221 | { return EmitSelector(Builder, Sel); } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1222 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 1223 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1224 | |
| 1225 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1226 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 1227 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1228 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1229 | virtual llvm::Constant *GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1230 | return ObjCTypes.getGetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1231 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1232 | virtual llvm::Constant *GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1233 | return ObjCTypes.getSetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1234 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1235 | virtual llvm::Constant *EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1236 | return ObjCTypes.getEnumerationMutationFn(); |
Daniel Dunbar | 28ed084 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 1237 | } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1238 | |
| 1239 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 1240 | const Stmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1241 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 1242 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1243 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1244 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1245 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1246 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1247 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1248 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1249 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1250 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1251 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1252 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1253 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1254 | QualType ObjectTy, |
| 1255 | llvm::Value *BaseValue, |
| 1256 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1257 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1258 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1259 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1260 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1261 | }; |
| 1262 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1263 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1264 | |
| 1265 | /* *** Helper Functions *** */ |
| 1266 | |
| 1267 | /// getConstantGEP() - Help routine to construct simple GEPs. |
| 1268 | static llvm::Constant *getConstantGEP(llvm::Constant *C, |
| 1269 | unsigned idx0, |
| 1270 | unsigned idx1) { |
| 1271 | llvm::Value *Idxs[] = { |
| 1272 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0), |
| 1273 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1) |
| 1274 | }; |
| 1275 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2); |
| 1276 | } |
| 1277 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1278 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 1279 | /// class has the __objc_exception__ attribute. |
| 1280 | static bool hasObjCExceptionAttribute(const ObjCInterfaceDecl *OID) { |
Daniel Dunbar | b11fa0d | 2009-04-13 21:08:27 +0000 | [diff] [blame] | 1281 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1282 | return true; |
| 1283 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
| 1284 | return hasObjCExceptionAttribute(Super); |
| 1285 | return false; |
| 1286 | } |
| 1287 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1288 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1289 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1290 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
| 1291 | ObjCTypes(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1292 | { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1293 | ObjCABI = 1; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1294 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1297 | /// GetClass - Return a reference to the class for the given interface |
| 1298 | /// decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1299 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1300 | const ObjCInterfaceDecl *ID) { |
| 1301 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1305 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 1306 | return EmitSelector(Builder, Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1309 | /// Generate a constant CFString object. |
| 1310 | /* |
| 1311 | struct __builtin_CFString { |
| 1312 | const int *isa; // point to __CFConstantStringClassReference |
| 1313 | int flags; |
| 1314 | const char *str; |
| 1315 | long length; |
| 1316 | }; |
| 1317 | */ |
| 1318 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1319 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 1320 | const ObjCStringLiteral *SL) { |
Steve Naroff | 8d4141f | 2009-04-01 13:55:36 +0000 | [diff] [blame] | 1321 | return CGM.GetAddrOfConstantCFString(SL->getString()); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | /// Generates a message send where the super is the receiver. This is |
| 1325 | /// a message send to self with special delivery semantics indicating |
| 1326 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1327 | CodeGen::RValue |
| 1328 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1329 | QualType ResultType, |
| 1330 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1331 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1332 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1333 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1334 | bool IsClassMessage, |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1335 | const CodeGen::CallArgList &CallArgs) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1336 | // Create and init a super structure; this is a (receiver, class) |
| 1337 | // pair we will pass to objc_msgSendSuper. |
| 1338 | llvm::Value *ObjCSuper = |
| 1339 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 1340 | llvm::Value *ReceiverAsObject = |
| 1341 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 1342 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 1343 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1344 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1345 | // If this is a class message the metaclass is passed as the target. |
| 1346 | llvm::Value *Target; |
| 1347 | if (IsClassMessage) { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1348 | if (isCategoryImpl) { |
| 1349 | // Message sent to 'super' in a class method defined in a category |
| 1350 | // implementation requires an odd treatment. |
| 1351 | // If we are in a class method, we must retrieve the |
| 1352 | // _metaclass_ for the current class, pointed at by |
| 1353 | // the class's "isa" pointer. The following assumes that |
| 1354 | // isa" is the first ivar in a class (which it must be). |
| 1355 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1356 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 1357 | Target = CGF.Builder.CreateLoad(Target); |
| 1358 | } |
| 1359 | else { |
| 1360 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 1361 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 1362 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 1363 | Target = Super; |
| 1364 | } |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1365 | } else { |
| 1366 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1367 | } |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1368 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 1369 | // and ObjCTypes types. |
| 1370 | const llvm::Type *ClassTy = |
| 1371 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1372 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1373 | CGF.Builder.CreateStore(Target, |
| 1374 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 1375 | |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1376 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1377 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 1378 | true, CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1379 | } |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1380 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1381 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1382 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1383 | QualType ResultType, |
| 1384 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1385 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1386 | bool IsClassMessage, |
| 1387 | const CallArgList &CallArgs) { |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1388 | llvm::Value *Arg0 = |
| 1389 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1390 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1391 | Arg0, CGF.getContext().getObjCIdType(), |
| 1392 | false, CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1396 | QualType ResultType, |
| 1397 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1398 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1399 | QualType Arg0Ty, |
| 1400 | bool IsSuper, |
| 1401 | const CallArgList &CallArgs) { |
| 1402 | CallArgList ActualArgs; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1403 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 1404 | ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder, |
| 1405 | Sel)), |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1406 | CGF.getContext().getObjCSelType())); |
| 1407 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1408 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1409 | CodeGenTypes &Types = CGM.getTypes(); |
| 1410 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); |
| 1411 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1412 | |
| 1413 | llvm::Constant *Fn; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1414 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1415 | Fn = ObjCTypes.getSendStretFn(IsSuper); |
| 1416 | } else if (ResultType->isFloatingType()) { |
| 1417 | // FIXME: Sadly, this is wrong. This actually depends on the |
| 1418 | // architecture. This happens to be right for x86-32 though. |
| 1419 | Fn = ObjCTypes.getSendFpretFn(IsSuper); |
| 1420 | } else { |
| 1421 | Fn = ObjCTypes.getSendFn(IsSuper); |
| 1422 | } |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 1423 | Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy)); |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1424 | return CGF.EmitCall(FnInfo, Fn, ActualArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1427 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1428 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1429 | // FIXME: I don't understand why gcc generates this, or where it is |
| 1430 | // resolved. Investigate. Its also wasteful to look this up over and |
| 1431 | // over. |
| 1432 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1433 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1434 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
| 1435 | ObjCTypes.ExternalProtocolPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1438 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1439 | // FIXME: We shouldn't need this, the protocol decl should contain |
| 1440 | // enough information to tell us whether this was a declaration or a |
| 1441 | // definition. |
| 1442 | DefinedProtocols.insert(PD->getIdentifier()); |
| 1443 | |
| 1444 | // If we have generated a forward reference to this protocol, emit |
| 1445 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 1446 | // emitted. |
| 1447 | if (Protocols.count(PD->getIdentifier())) |
| 1448 | GetOrEmitProtocol(PD); |
| 1449 | } |
| 1450 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1451 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1452 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 1453 | return GetOrEmitProtocol(PD); |
| 1454 | return GetOrEmitProtocolRef(PD); |
| 1455 | } |
| 1456 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1457 | /* |
| 1458 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 1459 | struct _objc_protocol { |
| 1460 | struct _objc_protocol_extension *isa; |
| 1461 | char *protocol_name; |
| 1462 | struct _objc_protocol_list *protocol_list; |
| 1463 | struct _objc__method_prototype_list *instance_methods; |
| 1464 | struct _objc__method_prototype_list *class_methods |
| 1465 | }; |
| 1466 | |
| 1467 | See EmitProtocolExtension(). |
| 1468 | */ |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1469 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
| 1470 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1471 | |
| 1472 | // Early exit if a defining object has already been generated. |
| 1473 | if (Entry && Entry->hasInitializer()) |
| 1474 | return Entry; |
| 1475 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1476 | // FIXME: I don't understand why gcc generates this, or where it is |
| 1477 | // resolved. Investigate. Its also wasteful to look this up over and |
| 1478 | // over. |
| 1479 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1480 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1481 | const char *ProtocolName = PD->getNameAsCString(); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1482 | |
| 1483 | // Construct method lists. |
| 1484 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1485 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1486 | for (ObjCProtocolDecl::instmeth_iterator |
| 1487 | i = PD->instmeth_begin(CGM.getContext()), |
| 1488 | e = PD->instmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1489 | ObjCMethodDecl *MD = *i; |
| 1490 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1491 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1492 | OptInstanceMethods.push_back(C); |
| 1493 | } else { |
| 1494 | InstanceMethods.push_back(C); |
| 1495 | } |
| 1496 | } |
| 1497 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1498 | for (ObjCProtocolDecl::classmeth_iterator |
| 1499 | i = PD->classmeth_begin(CGM.getContext()), |
| 1500 | e = PD->classmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1501 | ObjCMethodDecl *MD = *i; |
| 1502 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1503 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1504 | OptClassMethods.push_back(C); |
| 1505 | } else { |
| 1506 | ClassMethods.push_back(C); |
| 1507 | } |
| 1508 | } |
| 1509 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1510 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1511 | Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1512 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1513 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1514 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1515 | PD->protocol_begin(), |
| 1516 | PD->protocol_end()); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1517 | Values[3] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1518 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" |
| 1519 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1520 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1521 | InstanceMethods); |
| 1522 | Values[4] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1523 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" |
| 1524 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1525 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1526 | ClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1527 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 1528 | Values); |
| 1529 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1530 | if (Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1531 | // Already created, fix the linkage and update the initializer. |
| 1532 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1533 | Entry->setInitializer(Init); |
| 1534 | } else { |
| 1535 | Entry = |
| 1536 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
| 1537 | llvm::GlobalValue::InternalLinkage, |
| 1538 | Init, |
| 1539 | std::string("\01L_OBJC_PROTOCOL_")+ProtocolName, |
| 1540 | &CGM.getModule()); |
| 1541 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1542 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1543 | UsedGlobals.push_back(Entry); |
| 1544 | // FIXME: Is this necessary? Why only for protocol? |
| 1545 | Entry->setAlignment(4); |
| 1546 | } |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1547 | |
| 1548 | return Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1551 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1552 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1553 | |
| 1554 | if (!Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1555 | // We use the initializer as a marker of whether this is a forward |
| 1556 | // reference or not. At module finalization we add the empty |
| 1557 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1558 | Entry = |
| 1559 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1560 | llvm::GlobalValue::ExternalLinkage, |
| 1561 | 0, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1562 | "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1563 | &CGM.getModule()); |
| 1564 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1565 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1566 | UsedGlobals.push_back(Entry); |
| 1567 | // FIXME: Is this necessary? Why only for protocol? |
| 1568 | Entry->setAlignment(4); |
| 1569 | } |
| 1570 | |
| 1571 | return Entry; |
| 1572 | } |
| 1573 | |
| 1574 | /* |
| 1575 | struct _objc_protocol_extension { |
| 1576 | uint32_t size; |
| 1577 | struct objc_method_description_list *optional_instance_methods; |
| 1578 | struct objc_method_description_list *optional_class_methods; |
| 1579 | struct objc_property_list *instance_properties; |
| 1580 | }; |
| 1581 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1582 | llvm::Constant * |
| 1583 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1584 | const ConstantVector &OptInstanceMethods, |
| 1585 | const ConstantVector &OptClassMethods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1586 | uint64_t Size = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1587 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1588 | std::vector<llvm::Constant*> Values(4); |
| 1589 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1590 | Values[1] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1591 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
| 1592 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1593 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1594 | OptInstanceMethods); |
| 1595 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1596 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" |
| 1597 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1598 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1599 | OptClassMethods); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1600 | Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + |
| 1601 | PD->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1602 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1603 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1604 | // Return null if no extension bits are used. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1605 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
| 1606 | Values[3]->isNullValue()) |
| 1607 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 1608 | |
| 1609 | llvm::Constant *Init = |
| 1610 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1611 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1612 | // No special section, but goes in llvm.used |
| 1613 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(), |
| 1614 | Init, |
| 1615 | 0, 0, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | /* |
| 1619 | struct objc_protocol_list { |
| 1620 | struct objc_protocol_list *next; |
| 1621 | long count; |
| 1622 | Protocol *list[]; |
| 1623 | }; |
| 1624 | */ |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1625 | llvm::Constant * |
| 1626 | CGObjCMac::EmitProtocolList(const std::string &Name, |
| 1627 | ObjCProtocolDecl::protocol_iterator begin, |
| 1628 | ObjCProtocolDecl::protocol_iterator end) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1629 | std::vector<llvm::Constant*> ProtocolRefs; |
| 1630 | |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1631 | for (; begin != end; ++begin) |
| 1632 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1633 | |
| 1634 | // Just return null for empty protocol lists |
| 1635 | if (ProtocolRefs.empty()) |
| 1636 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1637 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1638 | // This list is null terminated. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1639 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
| 1640 | |
| 1641 | std::vector<llvm::Constant*> Values(3); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1642 | // This field is only used by the runtime. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1643 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1644 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 1645 | Values[2] = |
| 1646 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 1647 | ProtocolRefs.size()), |
| 1648 | ProtocolRefs); |
| 1649 | |
| 1650 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1651 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1652 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1653 | 4, false); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1654 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
| 1655 | } |
| 1656 | |
| 1657 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1658 | struct _objc_property { |
| 1659 | const char * const name; |
| 1660 | const char * const attributes; |
| 1661 | }; |
| 1662 | |
| 1663 | struct _objc_property_list { |
| 1664 | uint32_t entsize; // sizeof (struct _objc_property) |
| 1665 | uint32_t prop_count; |
| 1666 | struct _objc_property[prop_count]; |
| 1667 | }; |
| 1668 | */ |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1669 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name, |
| 1670 | const Decl *Container, |
| 1671 | const ObjCContainerDecl *OCD, |
| 1672 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1673 | std::vector<llvm::Constant*> Properties, Prop(2); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1674 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(CGM.getContext()), |
| 1675 | E = OCD->prop_end(CGM.getContext()); I != E; ++I) { |
Steve Naroff | 93983f8 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 1676 | const ObjCPropertyDecl *PD = *I; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1677 | Prop[0] = GetPropertyName(PD->getIdentifier()); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1678 | Prop[1] = GetPropertyTypeString(PD, Container); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1679 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
| 1680 | Prop)); |
| 1681 | } |
| 1682 | |
| 1683 | // Return null for empty list. |
| 1684 | if (Properties.empty()) |
| 1685 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1686 | |
| 1687 | unsigned PropertySize = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1688 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1689 | std::vector<llvm::Constant*> Values(3); |
| 1690 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 1691 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
| 1692 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
| 1693 | Properties.size()); |
| 1694 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
| 1695 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1696 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1697 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1698 | CreateMetadataVar(Name, Init, |
| 1699 | (ObjCABI == 2) ? "__DATA, __objc_const" : |
| 1700 | "__OBJC,__property,regular,no_dead_strip", |
| 1701 | (ObjCABI == 2) ? 8 : 4, |
| 1702 | true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1703 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1707 | struct objc_method_description_list { |
| 1708 | int count; |
| 1709 | struct objc_method_description list[]; |
| 1710 | }; |
| 1711 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1712 | llvm::Constant * |
| 1713 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 1714 | std::vector<llvm::Constant*> Desc(2); |
| 1715 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1716 | ObjCTypes.SelectorPtrTy); |
| 1717 | Desc[1] = GetMethodVarType(MD); |
| 1718 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
| 1719 | Desc); |
| 1720 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1721 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1722 | llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name, |
| 1723 | const char *Section, |
| 1724 | const ConstantVector &Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1725 | // Return null for empty list. |
| 1726 | if (Methods.empty()) |
| 1727 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 1728 | |
| 1729 | std::vector<llvm::Constant*> Values(2); |
| 1730 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 1731 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
| 1732 | Methods.size()); |
| 1733 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
| 1734 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1735 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1736 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1737 | return llvm::ConstantExpr::getBitCast(GV, |
| 1738 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1741 | /* |
| 1742 | struct _objc_category { |
| 1743 | char *category_name; |
| 1744 | char *class_name; |
| 1745 | struct _objc_method_list *instance_methods; |
| 1746 | struct _objc_method_list *class_methods; |
| 1747 | struct _objc_protocol_list *protocols; |
| 1748 | uint32_t size; // <rdar://4585769> |
| 1749 | struct _objc_property_list *instance_properties; |
| 1750 | }; |
| 1751 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1752 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1753 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1754 | |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1755 | // FIXME: This is poor design, the OCD should have a pointer to the |
| 1756 | // category decl. Additionally, note that Category can be null for |
| 1757 | // the @implementation w/o an @interface case. Sema should just |
| 1758 | // create one for us as it does for @implementation so everyone else |
| 1759 | // can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1760 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1761 | const ObjCCategoryDecl *Category = |
| 1762 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1763 | std::string ExtName(Interface->getNameAsString() + "_" + |
| 1764 | OCD->getNameAsString()); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1765 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1766 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1767 | for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(), |
| 1768 | e = OCD->instmeth_end(); i != e; ++i) { |
| 1769 | // Instance methods should always be defined. |
| 1770 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1771 | } |
| 1772 | for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(), |
| 1773 | e = OCD->classmeth_end(); i != e; ++i) { |
| 1774 | // Class methods should always be defined. |
| 1775 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1776 | } |
| 1777 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1778 | std::vector<llvm::Constant*> Values(7); |
| 1779 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 1780 | Values[1] = GetClassName(Interface->getIdentifier()); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1781 | Values[2] = |
| 1782 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") + |
| 1783 | ExtName, |
| 1784 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1785 | InstanceMethods); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1786 | Values[3] = |
| 1787 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName, |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1788 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1789 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1790 | if (Category) { |
| 1791 | Values[4] = |
| 1792 | EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName, |
| 1793 | Category->protocol_begin(), |
| 1794 | Category->protocol_end()); |
| 1795 | } else { |
| 1796 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1797 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1798 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1799 | |
| 1800 | // If there is no category @interface then there can be no properties. |
| 1801 | if (Category) { |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1802 | Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1803 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1804 | } else { |
| 1805 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1806 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1807 | |
| 1808 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
| 1809 | Values); |
| 1810 | |
| 1811 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1812 | CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init, |
| 1813 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1814 | 4, true); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1815 | DefinedCategories.push_back(GV); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1818 | // FIXME: Get from somewhere? |
| 1819 | enum ClassFlags { |
| 1820 | eClassFlags_Factory = 0x00001, |
| 1821 | eClassFlags_Meta = 0x00002, |
| 1822 | // <rdr://5142207> |
| 1823 | eClassFlags_HasCXXStructors = 0x02000, |
| 1824 | eClassFlags_Hidden = 0x20000, |
| 1825 | eClassFlags_ABI2_Hidden = 0x00010, |
| 1826 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 1827 | }; |
| 1828 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1829 | /* |
| 1830 | struct _objc_class { |
| 1831 | Class isa; |
| 1832 | Class super_class; |
| 1833 | const char *name; |
| 1834 | long version; |
| 1835 | long info; |
| 1836 | long instance_size; |
| 1837 | struct _objc_ivar_list *ivars; |
| 1838 | struct _objc_method_list *methods; |
| 1839 | struct _objc_cache *cache; |
| 1840 | struct _objc_protocol_list *protocols; |
| 1841 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 1842 | const char *ivar_layout; |
| 1843 | struct _objc_class_ext *ext; |
| 1844 | }; |
| 1845 | |
| 1846 | See EmitClassExtension(); |
| 1847 | */ |
| 1848 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1849 | DefinedSymbols.insert(ID->getIdentifier()); |
| 1850 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1851 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1852 | // FIXME: Gross |
| 1853 | ObjCInterfaceDecl *Interface = |
| 1854 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1855 | llvm::Constant *Protocols = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1856 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1857 | Interface->protocol_begin(), |
| 1858 | Interface->protocol_end()); |
Daniel Dunbar | 84ad77a | 2009-04-22 09:39:34 +0000 | [diff] [blame] | 1859 | const llvm::Type *InterfaceTy = GetConcreteClassStruct(CGM, Interface); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1860 | unsigned Flags = eClassFlags_Factory; |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1861 | unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1862 | |
| 1863 | // FIXME: Set CXX-structors flag. |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1864 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1865 | Flags |= eClassFlags_Hidden; |
| 1866 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1867 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1868 | for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(), |
| 1869 | e = ID->instmeth_end(); i != e; ++i) { |
| 1870 | // Instance methods should always be defined. |
| 1871 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1872 | } |
| 1873 | for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(), |
| 1874 | e = ID->classmeth_end(); i != e; ++i) { |
| 1875 | // Class methods should always be defined. |
| 1876 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1877 | } |
| 1878 | |
| 1879 | for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(), |
| 1880 | e = ID->propimpl_end(); i != e; ++i) { |
| 1881 | ObjCPropertyImplDecl *PID = *i; |
| 1882 | |
| 1883 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 1884 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 1885 | |
| 1886 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 1887 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1888 | InstanceMethods.push_back(C); |
| 1889 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 1890 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1891 | InstanceMethods.push_back(C); |
| 1892 | } |
| 1893 | } |
| 1894 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1895 | std::vector<llvm::Constant*> Values(12); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1896 | Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1897 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1898 | // Record a reference to the super class. |
| 1899 | LazySymbols.insert(Super->getIdentifier()); |
| 1900 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1901 | Values[ 1] = |
| 1902 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1903 | ObjCTypes.ClassPtrTy); |
| 1904 | } else { |
| 1905 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1906 | } |
| 1907 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1908 | // Version is always 0. |
| 1909 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1910 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1911 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1912 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1913 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1914 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1915 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1916 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1917 | // cache is always NULL. |
| 1918 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1919 | Values[ 9] = Protocols; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1920 | // FIXME: Set ivar_layout |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame^] | 1921 | Values[10] = BuildIvarLayout(ID, true); |
| 1922 | // Values[10] = GetIvarLayoutName(0, ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1923 | Values[11] = EmitClassExtension(ID); |
| 1924 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1925 | Values); |
| 1926 | |
| 1927 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1928 | CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init, |
| 1929 | "__OBJC,__class,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1930 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1931 | DefinedClasses.push_back(GV); |
| 1932 | } |
| 1933 | |
| 1934 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1935 | llvm::Constant *Protocols, |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1936 | const llvm::Type *InterfaceTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1937 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1938 | unsigned Flags = eClassFlags_Meta; |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1939 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1940 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1941 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1942 | Flags |= eClassFlags_Hidden; |
| 1943 | |
| 1944 | std::vector<llvm::Constant*> Values(12); |
| 1945 | // The isa for the metaclass is the root of the hierarchy. |
| 1946 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 1947 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 1948 | Root = Super; |
| 1949 | Values[ 0] = |
| 1950 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
| 1951 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1952 | // The super class for the metaclass is emitted as the name of the |
| 1953 | // super class. The runtime fixes this up to point to the |
| 1954 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1955 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
| 1956 | Values[ 1] = |
| 1957 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1958 | ObjCTypes.ClassPtrTy); |
| 1959 | } else { |
| 1960 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1961 | } |
| 1962 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1963 | // Version is always 0. |
| 1964 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1965 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1966 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1967 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1968 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1969 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1970 | "__OBJC,__cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1971 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1972 | // cache is always NULL. |
| 1973 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1974 | Values[ 9] = Protocols; |
| 1975 | // ivar_layout for metaclass is always NULL. |
| 1976 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 1977 | // The class extension is always unused for metaclasses. |
| 1978 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 1979 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1980 | Values); |
| 1981 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1982 | std::string Name("\01L_OBJC_METACLASS_"); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1983 | Name += ID->getNameAsCString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1984 | |
| 1985 | // Check for a forward reference. |
| 1986 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 1987 | if (GV) { |
| 1988 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 1989 | "Forward metaclass reference has incorrect type."); |
| 1990 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 1991 | GV->setInitializer(Init); |
| 1992 | } else { |
| 1993 | GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1994 | llvm::GlobalValue::InternalLinkage, |
| 1995 | Init, Name, |
| 1996 | &CGM.getModule()); |
| 1997 | } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1998 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1999 | GV->setAlignment(4); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2000 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2001 | |
| 2002 | return GV; |
| 2003 | } |
| 2004 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2005 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2006 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2007 | |
| 2008 | // FIXME: Should we look these up somewhere other than the |
| 2009 | // module. Its a bit silly since we only generate these while |
| 2010 | // processing an implementation, so exactly one pointer would work |
| 2011 | // if know when we entered/exitted an implementation block. |
| 2012 | |
| 2013 | // Check for an existing forward reference. |
Fariborz Jahanian | b0d2794 | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 2014 | // Previously, metaclass with internal linkage may have been defined. |
| 2015 | // pass 'true' as 2nd argument so it is returned. |
| 2016 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) { |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2017 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2018 | "Forward metaclass reference has incorrect type."); |
| 2019 | return GV; |
| 2020 | } else { |
| 2021 | // Generate as an external reference to keep a consistent |
| 2022 | // module. This will be patched up when we emit the metaclass. |
| 2023 | return new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 2024 | llvm::GlobalValue::ExternalLinkage, |
| 2025 | 0, |
| 2026 | Name, |
| 2027 | &CGM.getModule()); |
| 2028 | } |
| 2029 | } |
| 2030 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2031 | /* |
| 2032 | struct objc_class_ext { |
| 2033 | uint32_t size; |
| 2034 | const char *weak_ivar_layout; |
| 2035 | struct _objc_property_list *properties; |
| 2036 | }; |
| 2037 | */ |
| 2038 | llvm::Constant * |
| 2039 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
| 2040 | uint64_t Size = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 2041 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2042 | |
| 2043 | std::vector<llvm::Constant*> Values(3); |
| 2044 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2045 | // FIXME: Output weak_ivar_layout string. |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame^] | 2046 | Values[1] = BuildIvarLayout(ID, false); |
| 2047 | // Values[1] = GetIvarLayoutName(0, ObjCTypes); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2048 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2049 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2050 | |
| 2051 | // Return null if no extension bits are used. |
| 2052 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
| 2053 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 2054 | |
| 2055 | llvm::Constant *Init = |
| 2056 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2057 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2058 | Init, "__OBJC,__class_ext,regular,no_dead_strip", |
| 2059 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2060 | } |
| 2061 | |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 2062 | /// getInterfaceDeclForIvar - Get the interface declaration node where |
| 2063 | /// this ivar is declared in. |
| 2064 | /// FIXME. Ideally, this info should be in the ivar node. But currently |
| 2065 | /// it is not and prevailing wisdom is that ASTs should not have more |
| 2066 | /// info than is absolutely needed, even though this info reflects the |
| 2067 | /// source language. |
| 2068 | /// |
| 2069 | static const ObjCInterfaceDecl *getInterfaceDeclForIvar( |
| 2070 | const ObjCInterfaceDecl *OI, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2071 | const ObjCIvarDecl *IVD, |
| 2072 | ASTContext &Context) { |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 2073 | if (!OI) |
| 2074 | return 0; |
| 2075 | assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface"); |
| 2076 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 2077 | E = OI->ivar_end(); I != E; ++I) |
| 2078 | if ((*I)->getIdentifier() == IVD->getIdentifier()) |
| 2079 | return OI; |
Fariborz Jahanian | 5a4b453 | 2009-03-31 17:00:52 +0000 | [diff] [blame] | 2080 | // look into properties. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2081 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context), |
| 2082 | E = OI->prop_end(Context); I != E; ++I) { |
Fariborz Jahanian | 5a4b453 | 2009-03-31 17:00:52 +0000 | [diff] [blame] | 2083 | ObjCPropertyDecl *PDecl = (*I); |
| 2084 | if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl()) |
| 2085 | if (IV->getIdentifier() == IVD->getIdentifier()) |
| 2086 | return OI; |
| 2087 | } |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2088 | return getInterfaceDeclForIvar(OI->getSuperClass(), IVD, Context); |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2091 | /* |
| 2092 | struct objc_ivar { |
| 2093 | char *ivar_name; |
| 2094 | char *ivar_type; |
| 2095 | int ivar_offset; |
| 2096 | }; |
| 2097 | |
| 2098 | struct objc_ivar_list { |
| 2099 | int ivar_count; |
| 2100 | struct objc_ivar list[count]; |
| 2101 | }; |
| 2102 | */ |
| 2103 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2104 | bool ForClass) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2105 | std::vector<llvm::Constant*> Ivars, Ivar(3); |
| 2106 | |
| 2107 | // When emitting the root class GCC emits ivar entries for the |
| 2108 | // actual class structure. It is not clear if we need to follow this |
| 2109 | // behavior; for now lets try and get away with not doing it. If so, |
| 2110 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 2111 | // for the class. |
| 2112 | if (ForClass) |
| 2113 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2114 | |
| 2115 | ObjCInterfaceDecl *OID = |
| 2116 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2117 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 2118 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
| 2119 | GetNamedIvarList(OID, OIvars); |
| 2120 | |
| 2121 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 2122 | ObjCIvarDecl *IVD = OIvars[i]; |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 2123 | Ivar[0] = GetMethodVarName(IVD->getIdentifier()); |
| 2124 | Ivar[1] = GetMethodVarType(IVD); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 2125 | Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2126 | ComputeIvarBaseOffset(CGM, OID, IVD)); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2127 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2128 | } |
| 2129 | |
| 2130 | // Return null for empty list. |
| 2131 | if (Ivars.empty()) |
| 2132 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
| 2133 | |
| 2134 | std::vector<llvm::Constant*> Values(2); |
| 2135 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 2136 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
| 2137 | Ivars.size()); |
| 2138 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
| 2139 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2140 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2141 | llvm::GlobalVariable *GV; |
| 2142 | if (ForClass) |
| 2143 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(), |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2144 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
| 2145 | 4, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2146 | else |
| 2147 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" |
| 2148 | + ID->getNameAsString(), |
| 2149 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2150 | 4, true); |
| 2151 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
| 2154 | /* |
| 2155 | struct objc_method { |
| 2156 | SEL method_name; |
| 2157 | char *method_types; |
| 2158 | void *method; |
| 2159 | }; |
| 2160 | |
| 2161 | struct objc_method_list { |
| 2162 | struct objc_method_list *obsolete; |
| 2163 | int count; |
| 2164 | struct objc_method methods_list[count]; |
| 2165 | }; |
| 2166 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2167 | |
| 2168 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 2169 | /// given method if it has been defined. The result is null if the |
| 2170 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2171 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2172 | // FIXME: Use DenseMap::lookup |
| 2173 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 2174 | if (!Fn) |
| 2175 | return 0; |
| 2176 | |
| 2177 | std::vector<llvm::Constant*> Method(3); |
| 2178 | Method[0] = |
| 2179 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 2180 | ObjCTypes.SelectorPtrTy); |
| 2181 | Method[1] = GetMethodVarType(MD); |
| 2182 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 2183 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 2184 | } |
| 2185 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2186 | llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name, |
| 2187 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2188 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2189 | // Return null for empty list. |
| 2190 | if (Methods.empty()) |
| 2191 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
| 2192 | |
| 2193 | std::vector<llvm::Constant*> Values(3); |
| 2194 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 2195 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 2196 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 2197 | Methods.size()); |
| 2198 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 2199 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2200 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2201 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2202 | return llvm::ConstantExpr::getBitCast(GV, |
| 2203 | ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2204 | } |
| 2205 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 2206 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 2207 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2208 | std::string Name; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 2209 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2210 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2211 | CodeGenTypes &Types = CGM.getTypes(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2212 | const llvm::FunctionType *MethodTy = |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2213 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2214 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2215 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2216 | llvm::GlobalValue::InternalLinkage, |
| 2217 | Name, |
| 2218 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2219 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2220 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2221 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2222 | } |
| 2223 | |
Daniel Dunbar | 48fa064 | 2009-04-19 02:03:42 +0000 | [diff] [blame] | 2224 | /// GetFieldBaseOffset - return the field's byte offset. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2225 | uint64_t CGObjCCommonMac::GetFieldBaseOffset(const ObjCInterfaceDecl *OI, |
| 2226 | const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 2227 | const FieldDecl *Field) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2228 | // Is this a C struct? |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 2229 | if (!OI) |
| 2230 | return Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2231 | return ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2232 | } |
| 2233 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2234 | llvm::GlobalVariable * |
| 2235 | CGObjCCommonMac::CreateMetadataVar(const std::string &Name, |
| 2236 | llvm::Constant *Init, |
| 2237 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2238 | unsigned Align, |
| 2239 | bool AddToUsed) { |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2240 | const llvm::Type *Ty = Init->getType(); |
| 2241 | llvm::GlobalVariable *GV = |
| 2242 | new llvm::GlobalVariable(Ty, false, |
| 2243 | llvm::GlobalValue::InternalLinkage, |
| 2244 | Init, |
| 2245 | Name, |
| 2246 | &CGM.getModule()); |
| 2247 | if (Section) |
| 2248 | GV->setSection(Section); |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2249 | if (Align) |
| 2250 | GV->setAlignment(Align); |
| 2251 | if (AddToUsed) |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2252 | UsedGlobals.push_back(GV); |
| 2253 | return GV; |
| 2254 | } |
| 2255 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2256 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2257 | // Abuse this interface function as a place to finalize. |
| 2258 | FinishModule(); |
| 2259 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2260 | return NULL; |
| 2261 | } |
| 2262 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2263 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2264 | return ObjCTypes.getGetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2267 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2268 | return ObjCTypes.getSetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2269 | } |
| 2270 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2271 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2272 | return ObjCTypes.getEnumerationMutationFn(); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2275 | /* |
| 2276 | |
| 2277 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 2278 | -- |
| 2279 | |
| 2280 | The basic framework for a @try-catch-finally is as follows: |
| 2281 | { |
| 2282 | objc_exception_data d; |
| 2283 | id _rethrow = null; |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2284 | bool _call_try_exit = true; |
| 2285 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2286 | objc_exception_try_enter(&d); |
| 2287 | if (!setjmp(d.jmp_buf)) { |
| 2288 | ... try body ... |
| 2289 | } else { |
| 2290 | // exception path |
| 2291 | id _caught = objc_exception_extract(&d); |
| 2292 | |
| 2293 | // enter new try scope for handlers |
| 2294 | if (!setjmp(d.jmp_buf)) { |
| 2295 | ... match exception and execute catch blocks ... |
| 2296 | |
| 2297 | // fell off end, rethrow. |
| 2298 | _rethrow = _caught; |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2299 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2300 | } else { |
| 2301 | // exception in catch block |
| 2302 | _rethrow = objc_exception_extract(&d); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2303 | _call_try_exit = false; |
| 2304 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2305 | } |
| 2306 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2307 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2308 | |
| 2309 | finally: |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2310 | if (_call_try_exit) |
| 2311 | objc_exception_try_exit(&d); |
| 2312 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2313 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2314 | ... dispatch to finally destination ... |
| 2315 | |
| 2316 | finally_rethrow: |
| 2317 | objc_exception_throw(_rethrow); |
| 2318 | |
| 2319 | finally_end: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2320 | } |
| 2321 | |
| 2322 | This framework differs slightly from the one gcc uses, in that gcc |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2323 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 2324 | and if the object should be rethrown. This breaks in the face of |
| 2325 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2326 | |
| 2327 | We specialize this framework for a few particular circumstances: |
| 2328 | |
| 2329 | - If there are no catch blocks, then we avoid emitting the second |
| 2330 | exception handling context. |
| 2331 | |
| 2332 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 2333 | e)) we avoid emitting the code to rethrow an uncaught exception. |
| 2334 | |
| 2335 | - FIXME: If there is no @finally block we can do a few more |
| 2336 | simplifications. |
| 2337 | |
| 2338 | Rethrows and Jumps-Through-Finally |
| 2339 | -- |
| 2340 | |
| 2341 | Support for implicit rethrows and jumping through the finally block is |
| 2342 | handled by storing the current exception-handling context in |
| 2343 | ObjCEHStack. |
| 2344 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2345 | In order to implement proper @finally semantics, we support one basic |
| 2346 | mechanism for jumping through the finally block to an arbitrary |
| 2347 | destination. Constructs which generate exits from a @try or @catch |
| 2348 | block use this mechanism to implement the proper semantics by chaining |
| 2349 | jumps, as necessary. |
| 2350 | |
| 2351 | This mechanism works like the one used for indirect goto: we |
| 2352 | arbitrarily assign an ID to each destination and store the ID for the |
| 2353 | destination in a variable prior to entering the finally block. At the |
| 2354 | end of the finally block we simply create a switch to the proper |
| 2355 | destination. |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2356 | |
| 2357 | Code gen for @synchronized(expr) stmt; |
| 2358 | Effectively generating code for: |
| 2359 | objc_sync_enter(expr); |
| 2360 | @try stmt @finally { objc_sync_exit(expr); } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2361 | */ |
| 2362 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2363 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 2364 | const Stmt &S) { |
| 2365 | bool isTry = isa<ObjCAtTryStmt>(S); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2366 | // Create various blocks we refer to for handling @finally. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2367 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2368 | llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit"); |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2369 | llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit"); |
| 2370 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
| 2371 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2372 | |
| 2373 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 2374 | // evaluation of the expression must occur before we enter the |
| 2375 | // @synchronized. We can safely avoid a temp here because jumps into |
| 2376 | // @synchronized are illegal & this will dominate uses. |
| 2377 | llvm::Value *SyncArg = 0; |
| 2378 | if (!isTry) { |
| 2379 | SyncArg = |
| 2380 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 2381 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 2382 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2383 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2384 | |
| 2385 | // Push an EH context entry, used for handling rethrows and jumps |
| 2386 | // through finally. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2387 | CGF.PushCleanupBlock(FinallyBlock); |
| 2388 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2389 | CGF.ObjCEHValueStack.push_back(0); |
| 2390 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2391 | // Allocate memory for the exception data and rethrow pointer. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2392 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 2393 | "exceptiondata.ptr"); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2394 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy, |
| 2395 | "_rethrow"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2396 | llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty, |
| 2397 | "_call_try_exit"); |
| 2398 | CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr); |
| 2399 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2400 | // Enter a new try block and call setjmp. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2401 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2402 | llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0, |
| 2403 | "jmpbufarray"); |
| 2404 | JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2405 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2406 | JmpBufPtr, "result"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2407 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2408 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 2409 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2410 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2411 | TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2412 | |
| 2413 | // Emit the @try block. |
| 2414 | CGF.EmitBlock(TryBlock); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2415 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 2416 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2417 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2418 | |
| 2419 | // Emit the "exception in @try" block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2420 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2421 | |
| 2422 | // Retrieve the exception object. We may emit multiple blocks but |
| 2423 | // nothing can cross this so the value is already in SSA form. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2424 | llvm::Value *Caught = |
| 2425 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2426 | ExceptionData, "caught"); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2427 | CGF.ObjCEHValueStack.back() = Caught; |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2428 | if (!isTry) |
| 2429 | { |
| 2430 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2431 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2432 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2433 | } |
| 2434 | else if (const ObjCAtCatchStmt* CatchStmt = |
| 2435 | cast<ObjCAtTryStmt>(S).getCatchStmts()) |
| 2436 | { |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2437 | // Enter a new exception try block (in case a @catch block throws |
| 2438 | // an exception). |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2439 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2440 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2441 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2442 | JmpBufPtr, "result"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2443 | llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2444 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2445 | llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch"); |
| 2446 | llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler"); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2447 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2448 | |
| 2449 | CGF.EmitBlock(CatchBlock); |
| 2450 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2451 | // Handle catch list. As a special case we check if everything is |
| 2452 | // matched and avoid generating code for falling off the end if |
| 2453 | // so. |
| 2454 | bool AllMatched = false; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2455 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2456 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2457 | |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2458 | const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2459 | const PointerType *PT = 0; |
| 2460 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2461 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2462 | if (!CatchParam) { |
| 2463 | AllMatched = true; |
| 2464 | } else { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2465 | PT = CatchParam->getType()->getAsPointerType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2466 | |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 2467 | // catch(id e) always matches. |
| 2468 | // FIXME: For the time being we also match id<X>; this should |
| 2469 | // be rejected by Sema instead. |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2470 | if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) || |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2471 | CatchParam->getType()->isObjCQualifiedIdType()) |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2472 | AllMatched = true; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2475 | if (AllMatched) { |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2476 | if (CatchParam) { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2477 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2478 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2479 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2480 | } |
Anders Carlsson | 1452f55 | 2008-09-11 08:21:54 +0000 | [diff] [blame] | 2481 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2482 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2483 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2484 | break; |
| 2485 | } |
| 2486 | |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2487 | assert(PT && "Unexpected non-pointer type in @catch"); |
| 2488 | QualType T = PT->getPointeeType(); |
Anders Carlsson | 4b7ff6e | 2008-09-11 06:35:14 +0000 | [diff] [blame] | 2489 | const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2490 | assert(ObjCType && "Catch parameter must have Objective-C type!"); |
| 2491 | |
| 2492 | // Check if the @catch block matches the exception object. |
| 2493 | llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl()); |
| 2494 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2495 | llvm::Value *Match = |
| 2496 | CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(), |
| 2497 | Class, Caught, "match"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2498 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2499 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2500 | |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2501 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2502 | MatchedBlock, NextCatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2503 | |
| 2504 | // Emit the @catch block. |
| 2505 | CGF.EmitBlock(MatchedBlock); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2506 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2507 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2508 | |
| 2509 | llvm::Value *Tmp = |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2510 | CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()), |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2511 | "tmp"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2512 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2513 | |
| 2514 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2515 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2516 | |
| 2517 | CGF.EmitBlock(NextCatchBlock); |
| 2518 | } |
| 2519 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2520 | if (!AllMatched) { |
| 2521 | // None of the handlers caught the exception, so store it to be |
| 2522 | // rethrown at the end of the @finally block. |
| 2523 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2524 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2525 | } |
| 2526 | |
| 2527 | // Emit the exception handler for the @catch blocks. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2528 | CGF.EmitBlock(CatchHandler); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2529 | CGF.Builder.CreateStore( |
| 2530 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2531 | ExceptionData), |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2532 | RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2533 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2534 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2535 | } else { |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2536 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2537 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2538 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2541 | // Pop the exception-handling stack entry. It is important to do |
| 2542 | // this now, because the code in the @finally block is not in this |
| 2543 | // context. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2544 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 2545 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2546 | CGF.ObjCEHValueStack.pop_back(); |
| 2547 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2548 | // Emit the @finally block. |
| 2549 | CGF.EmitBlock(FinallyBlock); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2550 | llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp"); |
| 2551 | |
| 2552 | CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit); |
| 2553 | |
| 2554 | CGF.EmitBlock(FinallyExit); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2555 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2556 | |
| 2557 | CGF.EmitBlock(FinallyNoExit); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2558 | if (isTry) { |
| 2559 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 2560 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 2561 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2562 | } else { |
| 2563 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 2564 | // @synchronized. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2565 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg); |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 2566 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2567 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2568 | // Emit the switch block |
| 2569 | if (Info.SwitchBlock) |
| 2570 | CGF.EmitBlock(Info.SwitchBlock); |
| 2571 | if (Info.EndBlock) |
| 2572 | CGF.EmitBlock(Info.EndBlock); |
| 2573 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2574 | CGF.EmitBlock(FinallyRethrow); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2575 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2576 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2577 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2578 | |
| 2579 | CGF.EmitBlock(FinallyEnd); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2580 | } |
| 2581 | |
| 2582 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2583 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2584 | llvm::Value *ExceptionAsObject; |
| 2585 | |
| 2586 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 2587 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
| 2588 | ExceptionAsObject = |
| 2589 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 2590 | } else { |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2591 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2592 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2593 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2594 | } |
| 2595 | |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2596 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2597 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2598 | |
| 2599 | // Clear the insertion point to indicate we are in unreachable code. |
| 2600 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2601 | } |
| 2602 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2603 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2604 | /// object: objc_read_weak (id *src) |
| 2605 | /// |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2606 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2607 | llvm::Value *AddrWeakObj) |
| 2608 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2609 | const llvm::Type* DestTy = |
| 2610 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2611 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2612 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2613 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2614 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2615 | return read_weak; |
| 2616 | } |
| 2617 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2618 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 2619 | /// objc_assign_weak (id src, id *dst) |
| 2620 | /// |
| 2621 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 2622 | llvm::Value *src, llvm::Value *dst) |
| 2623 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2624 | const llvm::Type * SrcTy = src->getType(); |
| 2625 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2626 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2627 | assert(Size <= 8 && "does not support size > 8"); |
| 2628 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2629 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2630 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2631 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2632 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2633 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 2634 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2635 | src, dst, "weakassign"); |
| 2636 | return; |
| 2637 | } |
| 2638 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2639 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 2640 | /// objc_assign_global (id src, id *dst) |
| 2641 | /// |
| 2642 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 2643 | llvm::Value *src, llvm::Value *dst) |
| 2644 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2645 | const llvm::Type * SrcTy = src->getType(); |
| 2646 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2647 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2648 | assert(Size <= 8 && "does not support size > 8"); |
| 2649 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2650 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2651 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2652 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2653 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2654 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2655 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2656 | src, dst, "globalassign"); |
| 2657 | return; |
| 2658 | } |
| 2659 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2660 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 2661 | /// objc_assign_ivar (id src, id *dst) |
| 2662 | /// |
| 2663 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 2664 | llvm::Value *src, llvm::Value *dst) |
| 2665 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2666 | const llvm::Type * SrcTy = src->getType(); |
| 2667 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2668 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2669 | assert(Size <= 8 && "does not support size > 8"); |
| 2670 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2671 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2672 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2673 | } |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2674 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2675 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2676 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(), |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2677 | src, dst, "assignivar"); |
| 2678 | return; |
| 2679 | } |
| 2680 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2681 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 2682 | /// objc_assign_strongCast (id src, id *dst) |
| 2683 | /// |
| 2684 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 2685 | llvm::Value *src, llvm::Value *dst) |
| 2686 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2687 | const llvm::Type * SrcTy = src->getType(); |
| 2688 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2689 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2690 | assert(Size <= 8 && "does not support size > 8"); |
| 2691 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2692 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2693 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2694 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2695 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2696 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2697 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2698 | src, dst, "weakassign"); |
| 2699 | return; |
| 2700 | } |
| 2701 | |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2702 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 2703 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2704 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 2705 | QualType ObjectTy, |
| 2706 | llvm::Value *BaseValue, |
| 2707 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2708 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 2709 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2710 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 2711 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2712 | } |
| 2713 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2714 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 2715 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2716 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2717 | uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2718 | return llvm::ConstantInt::get( |
| 2719 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 2720 | Offset); |
| 2721 | } |
| 2722 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2723 | /* *** Private Interface *** */ |
| 2724 | |
| 2725 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 2726 | /// level information. |
| 2727 | /// |
| 2728 | /// See: <rdr://4810609&4810587&4810587> |
| 2729 | /// struct IMAGE_INFO { |
| 2730 | /// unsigned version; |
| 2731 | /// unsigned flags; |
| 2732 | /// }; |
| 2733 | enum ImageInfoFlags { |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2734 | eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what |
| 2735 | // this implies. |
| 2736 | eImageInfo_GarbageCollected = (1 << 1), |
| 2737 | eImageInfo_GCOnly = (1 << 2), |
| 2738 | eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set. |
| 2739 | |
| 2740 | // A flag indicating that the module has no instances of an |
| 2741 | // @synthesize of a superclass variable. <rdar://problem/6803242> |
| 2742 | eImageInfo_CorrectedSynthesize = (1 << 4) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2743 | }; |
| 2744 | |
| 2745 | void CGObjCMac::EmitImageInfo() { |
| 2746 | unsigned version = 0; // Version is unused? |
| 2747 | unsigned flags = 0; |
| 2748 | |
| 2749 | // FIXME: Fix and continue? |
| 2750 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 2751 | flags |= eImageInfo_GarbageCollected; |
| 2752 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 2753 | flags |= eImageInfo_GCOnly; |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2754 | |
| 2755 | // We never allow @synthesize of a superclass property. |
| 2756 | flags |= eImageInfo_CorrectedSynthesize; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2757 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2758 | // Emitted as int[2]; |
| 2759 | llvm::Constant *values[2] = { |
| 2760 | llvm::ConstantInt::get(llvm::Type::Int32Ty, version), |
| 2761 | llvm::ConstantInt::get(llvm::Type::Int32Ty, flags) |
| 2762 | }; |
| 2763 | llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2764 | |
| 2765 | const char *Section; |
| 2766 | if (ObjCABI == 1) |
| 2767 | Section = "__OBJC, __image_info,regular"; |
| 2768 | else |
| 2769 | Section = "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2770 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2771 | CreateMetadataVar("\01L_OBJC_IMAGE_INFO", |
| 2772 | llvm::ConstantArray::get(AT, values, 2), |
| 2773 | Section, |
| 2774 | 0, |
| 2775 | true); |
| 2776 | GV->setConstant(true); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2777 | } |
| 2778 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2779 | |
| 2780 | // struct objc_module { |
| 2781 | // unsigned long version; |
| 2782 | // unsigned long size; |
| 2783 | // const char *name; |
| 2784 | // Symtab symtab; |
| 2785 | // }; |
| 2786 | |
| 2787 | // FIXME: Get from somewhere |
| 2788 | static const int ModuleVersion = 7; |
| 2789 | |
| 2790 | void CGObjCMac::EmitModuleInfo() { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 2791 | uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2792 | |
| 2793 | std::vector<llvm::Constant*> Values(4); |
| 2794 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion); |
| 2795 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2796 | // This used to be the filename, now it is unused. <rdr://4327263> |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2797 | Values[2] = GetClassName(&CGM.getContext().Idents.get("")); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2798 | Values[3] = EmitModuleSymbols(); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2799 | CreateMetadataVar("\01L_OBJC_MODULES", |
| 2800 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
| 2801 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2802 | 4, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2803 | } |
| 2804 | |
| 2805 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2806 | unsigned NumClasses = DefinedClasses.size(); |
| 2807 | unsigned NumCategories = DefinedCategories.size(); |
| 2808 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2809 | // Return null if no symbols were defined. |
| 2810 | if (!NumClasses && !NumCategories) |
| 2811 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
| 2812 | |
| 2813 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2814 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2815 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
| 2816 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 2817 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
| 2818 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2819 | // The runtime expects exactly the list of defined classes followed |
| 2820 | // by the list of defined categories, in a single array. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2821 | std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2822 | for (unsigned i=0; i<NumClasses; i++) |
| 2823 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 2824 | ObjCTypes.Int8PtrTy); |
| 2825 | for (unsigned i=0; i<NumCategories; i++) |
| 2826 | Symbols[NumClasses + i] = |
| 2827 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 2828 | ObjCTypes.Int8PtrTy); |
| 2829 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2830 | Values[4] = |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2831 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2832 | NumClasses + NumCategories), |
| 2833 | Symbols); |
| 2834 | |
| 2835 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2836 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2837 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2838 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 2839 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2840 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2841 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
| 2842 | } |
| 2843 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2844 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2845 | const ObjCInterfaceDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2846 | LazySymbols.insert(ID->getIdentifier()); |
| 2847 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2848 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 2849 | |
| 2850 | if (!Entry) { |
| 2851 | llvm::Constant *Casted = |
| 2852 | llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()), |
| 2853 | ObjCTypes.ClassPtrTy); |
| 2854 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2855 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 2856 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2857 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
| 2860 | return Builder.CreateLoad(Entry, false, "tmp"); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2863 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2864 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 2865 | |
| 2866 | if (!Entry) { |
| 2867 | llvm::Constant *Casted = |
| 2868 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 2869 | ObjCTypes.SelectorPtrTy); |
| 2870 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2871 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 2872 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2873 | 4, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2874 | } |
| 2875 | |
| 2876 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 2877 | } |
| 2878 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 2879 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2880 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2881 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2882 | if (!Entry) |
| 2883 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 2884 | llvm::ConstantArray::get(Ident->getName()), |
| 2885 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 2886 | 1, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2887 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2888 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2889 | } |
| 2890 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 2891 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 2892 | /// ivar layout bitmap. |
| 2893 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
| 2894 | const ObjCCommonTypesHelper &ObjCTypes) { |
| 2895 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 2896 | } |
| 2897 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2898 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCInterfaceDecl *OI, |
| 2899 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2900 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2901 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2902 | unsigned int BytePos, bool ForStrongLayout, |
| 2903 | int &Index, int &SkIndex, bool &HasUnion) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2904 | bool IsUnion = (RD && RD->isUnion()); |
| 2905 | uint64_t MaxUnionIvarSize = 0; |
| 2906 | uint64_t MaxSkippedUnionIvarSize = 0; |
| 2907 | FieldDecl *MaxField = 0; |
| 2908 | FieldDecl *MaxSkippedField = 0; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2909 | FieldDecl *LastFieldBitfield = 0; |
| 2910 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2911 | unsigned base = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2912 | if (RecFields.empty()) |
| 2913 | return; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2914 | if (IsUnion) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2915 | base = BytePos + GetFieldBaseOffset(OI, Layout, RecFields[0]); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2916 | unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0); |
| 2917 | unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth(); |
| 2918 | |
| 2919 | llvm::SmallVector<FieldDecl*, 16> TmpRecFields; |
| 2920 | |
| 2921 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2922 | FieldDecl *Field = RecFields[i]; |
| 2923 | // Skip over unnamed or bitfields |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2924 | if (!Field->getIdentifier() || Field->isBitField()) { |
| 2925 | LastFieldBitfield = Field; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2926 | continue; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2927 | } |
| 2928 | LastFieldBitfield = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2929 | QualType FQT = Field->getType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2930 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2931 | if (FQT->isUnionType()) |
| 2932 | HasUnion = true; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2933 | else |
| 2934 | assert(FQT->isRecordType() && |
| 2935 | "only union/record is supported for ivar layout bitmap"); |
| 2936 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2937 | const RecordType *RT = FQT->getAsRecordType(); |
| 2938 | const RecordDecl *RD = RT->getDecl(); |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 2939 | // FIXME - Find a more efficient way of passing records down. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2940 | TmpRecFields.append(RD->field_begin(CGM.getContext()), |
| 2941 | RD->field_end(CGM.getContext())); |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 2942 | const llvm::Type *Ty = CGM.getTypes().ConvertType(FQT); |
| 2943 | const llvm::StructLayout *RecLayout = |
| 2944 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); |
| 2945 | |
| 2946 | BuildAggrIvarLayout(0, RecLayout, RD, TmpRecFields, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2947 | BytePos + GetFieldBaseOffset(OI, Layout, Field), |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2948 | ForStrongLayout, Index, SkIndex, |
| 2949 | HasUnion); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2950 | TmpRecFields.clear(); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2951 | continue; |
| 2952 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2953 | |
| 2954 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2955 | const ConstantArrayType *CArray = |
| 2956 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2957 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2958 | assert(CArray && "only array with know element size is supported"); |
| 2959 | FQT = CArray->getElementType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2960 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 2961 | const ConstantArrayType *CArray = |
| 2962 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2963 | ElCount *= CArray->getSize().getZExtValue(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2964 | FQT = CArray->getElementType(); |
| 2965 | } |
| 2966 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2967 | assert(!FQT->isUnionType() && |
| 2968 | "layout for array of unions not supported"); |
| 2969 | if (FQT->isRecordType()) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2970 | int OldIndex = Index; |
| 2971 | int OldSkIndex = SkIndex; |
| 2972 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2973 | // FIXME - Use a common routine with the above! |
| 2974 | const RecordType *RT = FQT->getAsRecordType(); |
| 2975 | const RecordDecl *RD = RT->getDecl(); |
| 2976 | // FIXME - Find a more efficiant way of passing records down. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2977 | TmpRecFields.append(RD->field_begin(CGM.getContext()), |
| 2978 | RD->field_end(CGM.getContext())); |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 2979 | const llvm::Type *Ty = CGM.getTypes().ConvertType(FQT); |
| 2980 | const llvm::StructLayout *RecLayout = |
| 2981 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2982 | |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 2983 | BuildAggrIvarLayout(0, RecLayout, RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2984 | TmpRecFields, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2985 | BytePos + GetFieldBaseOffset(OI, Layout, Field), |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2986 | ForStrongLayout, Index, SkIndex, |
| 2987 | HasUnion); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2988 | TmpRecFields.clear(); |
| 2989 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2990 | // Replicate layout information for each array element. Note that |
| 2991 | // one element is already done. |
| 2992 | uint64_t ElIx = 1; |
| 2993 | for (int FirstIndex = Index, FirstSkIndex = SkIndex; |
| 2994 | ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2995 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2996 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 2997 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2998 | GC_IVAR gcivar; |
| 2999 | gcivar.ivar_bytepos = IvarsInfo[i].ivar_bytepos + Size*ElIx; |
| 3000 | gcivar.ivar_size = IvarsInfo[i].ivar_size; |
| 3001 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3002 | } |
| 3003 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3004 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3005 | GC_IVAR skivar; |
| 3006 | skivar.ivar_bytepos = SkipIvars[i].ivar_bytepos + Size*ElIx; |
| 3007 | skivar.ivar_size = SkipIvars[i].ivar_size; |
| 3008 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3009 | } |
| 3010 | } |
| 3011 | continue; |
| 3012 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3013 | } |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3014 | // At this point, we are done with Record/Union and array there of. |
| 3015 | // For other arrays we are down to its element type. |
| 3016 | QualType::GCAttrTypes GCAttr = QualType::GCNone; |
| 3017 | do { |
| 3018 | if (FQT.isObjCGCStrong() || FQT.isObjCGCWeak()) { |
| 3019 | GCAttr = FQT.isObjCGCStrong() ? QualType::Strong : QualType::Weak; |
| 3020 | break; |
| 3021 | } |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3022 | else if (CGM.getContext().isObjCObjectPointerType(FQT)) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3023 | GCAttr = QualType::Strong; |
| 3024 | break; |
| 3025 | } |
| 3026 | else if (const PointerType *PT = FQT->getAsPointerType()) { |
| 3027 | FQT = PT->getPointeeType(); |
| 3028 | } |
| 3029 | else { |
| 3030 | break; |
| 3031 | } |
| 3032 | } while (true); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3033 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3034 | if ((ForStrongLayout && GCAttr == QualType::Strong) |
| 3035 | || (!ForStrongLayout && GCAttr == QualType::Weak)) { |
| 3036 | if (IsUnion) |
| 3037 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3038 | uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType()) |
| 3039 | / WordSizeInBits; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3040 | if (UnionIvarSize > MaxUnionIvarSize) |
| 3041 | { |
| 3042 | MaxUnionIvarSize = UnionIvarSize; |
| 3043 | MaxField = Field; |
| 3044 | } |
| 3045 | } |
| 3046 | else |
| 3047 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3048 | GC_IVAR gcivar; |
| 3049 | gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field); |
| 3050 | gcivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) / |
| 3051 | WordSizeInBits; |
| 3052 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3053 | } |
| 3054 | } |
| 3055 | else if ((ForStrongLayout && |
| 3056 | (GCAttr == QualType::GCNone || GCAttr == QualType::Weak)) |
| 3057 | || (!ForStrongLayout && GCAttr != QualType::Weak)) { |
| 3058 | if (IsUnion) |
| 3059 | { |
| 3060 | uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType()); |
| 3061 | if (UnionIvarSize > MaxSkippedUnionIvarSize) |
| 3062 | { |
| 3063 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 3064 | MaxSkippedField = Field; |
| 3065 | } |
| 3066 | } |
| 3067 | else |
| 3068 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3069 | GC_IVAR skivar; |
| 3070 | skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field); |
| 3071 | skivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) / |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3072 | ByteSizeInBits; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3073 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3074 | } |
| 3075 | } |
| 3076 | } |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3077 | if (LastFieldBitfield) { |
| 3078 | // Last field was a bitfield. Must update skip info. |
| 3079 | GC_IVAR skivar; |
| 3080 | skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, |
| 3081 | LastFieldBitfield); |
| 3082 | Expr *BitWidth = LastFieldBitfield->getBitWidth(); |
| 3083 | uint64_t BitFieldSize = |
| 3084 | BitWidth->getIntegerConstantExprValue(CGM.getContext()).getZExtValue(); |
| 3085 | skivar.ivar_size = (BitFieldSize / ByteSizeInBits) |
| 3086 | + ((BitFieldSize % ByteSizeInBits) != 0); |
| 3087 | SkipIvars.push_back(skivar); ++SkIndex; |
| 3088 | } |
| 3089 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3090 | if (MaxField) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3091 | GC_IVAR gcivar; |
| 3092 | gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, MaxField); |
| 3093 | gcivar.ivar_size = MaxUnionIvarSize; |
| 3094 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3095 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3096 | |
| 3097 | if (MaxSkippedField) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3098 | GC_IVAR skivar; |
| 3099 | skivar.ivar_bytepos = BytePos + |
| 3100 | GetFieldBaseOffset(OI, Layout, MaxSkippedField); |
| 3101 | skivar.ivar_size = MaxSkippedUnionIvarSize; |
| 3102 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3103 | } |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3104 | } |
| 3105 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3106 | static int |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3107 | IvarBytePosCompare(const void *a, const void *b) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3108 | { |
| 3109 | unsigned int sa = ((CGObjCCommonMac::GC_IVAR *)a)->ivar_bytepos; |
| 3110 | unsigned int sb = ((CGObjCCommonMac::GC_IVAR *)b)->ivar_bytepos; |
| 3111 | |
| 3112 | if (sa < sb) |
| 3113 | return -1; |
| 3114 | if (sa > sb) |
| 3115 | return 1; |
| 3116 | return 0; |
| 3117 | } |
| 3118 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3119 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 3120 | /// implementation for the __strong or __weak case. |
| 3121 | /// The layout map displays which words in ivar list must be skipped |
| 3122 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 3123 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
| 3124 | /// of words to skip and right nibble is count of words to scan. So, each |
| 3125 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
| 3126 | /// represented by a 0x00 byte which also ends the string. |
| 3127 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 3128 | /// - id, Class |
| 3129 | /// - object * |
| 3130 | /// - __strong anything |
| 3131 | /// |
| 3132 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 3133 | /// - __weak anything |
| 3134 | /// |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3135 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3136 | const ObjCImplementationDecl *OMD, |
| 3137 | bool ForStrongLayout) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3138 | int Index = -1; |
| 3139 | int SkIndex = -1; |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3140 | bool hasUnion = false; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3141 | int SkipScan; |
| 3142 | unsigned int WordsToScan, WordsToSkip; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3143 | const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 3144 | if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) |
| 3145 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3146 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3147 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3148 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3149 | CGM.getContext().CollectObjCIvars(OI, RecFields); |
| 3150 | if (RecFields.empty()) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3151 | return llvm::Constant::getNullValue(PtrTy); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3152 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3153 | SkipIvars.clear(); |
| 3154 | IvarsInfo.clear(); |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 3155 | |
Daniel Dunbar | 84ad77a | 2009-04-22 09:39:34 +0000 | [diff] [blame] | 3156 | const llvm::StructLayout *Layout = |
| 3157 | CGM.getTargetData().getStructLayout(GetConcreteClassStruct(CGM, OI)); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3158 | BuildAggrIvarLayout(OI, Layout, 0, RecFields, 0, ForStrongLayout, |
| 3159 | Index, SkIndex, hasUnion); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3160 | if (Index == -1) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3161 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3162 | |
| 3163 | // Sort on byte position in case we encounterred a union nested in |
| 3164 | // the ivar list. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3165 | if (hasUnion && !IvarsInfo.empty()) |
| 3166 | qsort(&IvarsInfo[0], Index+1, sizeof(GC_IVAR), IvarBytePosCompare); |
| 3167 | if (hasUnion && !SkipIvars.empty()) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3168 | qsort(&SkipIvars[0], Index+1, sizeof(GC_IVAR), IvarBytePosCompare); |
| 3169 | |
| 3170 | // Build the string of skip/scan nibbles |
| 3171 | SkipScan = -1; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3172 | SkipScanIvars.clear(); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3173 | unsigned int WordSize = |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3174 | CGM.getTypes().getTargetData().getTypePaddedSize(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3175 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 3176 | WordsToSkip = 0; |
| 3177 | WordsToScan = IvarsInfo[0].ivar_size; |
| 3178 | } |
| 3179 | else { |
| 3180 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 3181 | WordsToScan = IvarsInfo[0].ivar_size; |
| 3182 | } |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3183 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3184 | { |
| 3185 | unsigned int TailPrevGCObjC = |
| 3186 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
| 3187 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) |
| 3188 | { |
| 3189 | // consecutive 'scanned' object pointers. |
| 3190 | WordsToScan += IvarsInfo[i].ivar_size; |
| 3191 | } |
| 3192 | else |
| 3193 | { |
| 3194 | // Skip over 'gc'able object pointer which lay over each other. |
| 3195 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 3196 | continue; |
| 3197 | // Must skip over 1 or more words. We save current skip/scan values |
| 3198 | // and start a new pair. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3199 | SKIP_SCAN SkScan; |
| 3200 | SkScan.skip = WordsToSkip; |
| 3201 | SkScan.scan = WordsToScan; |
| 3202 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
| 3203 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3204 | // Skip the hole. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3205 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 3206 | SkScan.scan = 0; |
| 3207 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3208 | WordsToSkip = 0; |
| 3209 | WordsToScan = IvarsInfo[i].ivar_size; |
| 3210 | } |
| 3211 | } |
| 3212 | if (WordsToScan > 0) |
| 3213 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3214 | SKIP_SCAN SkScan; |
| 3215 | SkScan.skip = WordsToSkip; |
| 3216 | SkScan.scan = WordsToScan; |
| 3217 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3218 | } |
| 3219 | |
| 3220 | bool BytesSkipped = false; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3221 | if (!SkipIvars.empty()) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3222 | { |
| 3223 | int LastByteSkipped = |
| 3224 | SkipIvars[SkIndex].ivar_bytepos + SkipIvars[SkIndex].ivar_size; |
| 3225 | int LastByteScanned = |
| 3226 | IvarsInfo[Index].ivar_bytepos + IvarsInfo[Index].ivar_size * WordSize; |
| 3227 | BytesSkipped = (LastByteSkipped > LastByteScanned); |
| 3228 | // Compute number of bytes to skip at the tail end of the last ivar scanned. |
| 3229 | if (BytesSkipped) |
| 3230 | { |
| 3231 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3232 | SKIP_SCAN SkScan; |
| 3233 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
| 3234 | SkScan.scan = 0; |
| 3235 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3236 | } |
| 3237 | } |
| 3238 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 3239 | // as 0xMN. |
| 3240 | for (int i = 0; i <= SkipScan; i++) |
| 3241 | { |
| 3242 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 3243 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 3244 | // 0xM0 followed by 0x0N detected. |
| 3245 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 3246 | for (int j = i+1; j < SkipScan; j++) |
| 3247 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 3248 | --SkipScan; |
| 3249 | } |
| 3250 | } |
| 3251 | |
| 3252 | // Generate the string. |
| 3253 | std::string BitMap; |
| 3254 | for (int i = 0; i <= SkipScan; i++) |
| 3255 | { |
| 3256 | unsigned char byte; |
| 3257 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 3258 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 3259 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 3260 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
| 3261 | |
| 3262 | if (skip_small > 0 || skip_big > 0) |
| 3263 | BytesSkipped = true; |
| 3264 | // first skip big. |
| 3265 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 3266 | BitMap += (unsigned char)(0xf0); |
| 3267 | |
| 3268 | // next (skip small, scan) |
| 3269 | if (skip_small) |
| 3270 | { |
| 3271 | byte = skip_small << 4; |
| 3272 | if (scan_big > 0) |
| 3273 | { |
| 3274 | byte |= 0xf; |
| 3275 | --scan_big; |
| 3276 | } |
| 3277 | else if (scan_small) |
| 3278 | { |
| 3279 | byte |= scan_small; |
| 3280 | scan_small = 0; |
| 3281 | } |
| 3282 | BitMap += byte; |
| 3283 | } |
| 3284 | // next scan big |
| 3285 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 3286 | BitMap += (unsigned char)(0x0f); |
| 3287 | // last scan small |
| 3288 | if (scan_small) |
| 3289 | { |
| 3290 | byte = scan_small; |
| 3291 | BitMap += byte; |
| 3292 | } |
| 3293 | } |
| 3294 | // null terminate string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3295 | unsigned char zero = 0; |
| 3296 | BitMap += zero; |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3297 | |
| 3298 | if (CGM.getLangOptions().ObjCGCBitmapPrint) { |
| 3299 | printf("\n%s ivar layout for class '%s': ", |
| 3300 | ForStrongLayout ? "strong" : "weak", |
| 3301 | OMD->getClassInterface()->getNameAsCString()); |
| 3302 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
| 3303 | for (unsigned i = 0; i < BitMap.size(); i++) |
| 3304 | if (!(s[i] & 0xf0)) |
| 3305 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3306 | else |
| 3307 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3308 | printf("\n"); |
| 3309 | } |
| 3310 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3311 | // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as |
| 3312 | // final layout. |
| 3313 | if (ForStrongLayout && !BytesSkipped) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3314 | return llvm::Constant::getNullValue(PtrTy); |
| 3315 | llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 3316 | llvm::ConstantArray::get(BitMap.c_str()), |
| 3317 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3318 | 1, true); |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3319 | return getConstantGEP(Entry, 0, 0); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3320 | } |
| 3321 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3322 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3323 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 3324 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3325 | // FIXME: Avoid std::string copying. |
| 3326 | if (!Entry) |
| 3327 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
| 3328 | llvm::ConstantArray::get(Sel.getAsString()), |
| 3329 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3330 | 1, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3331 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3332 | return getConstantGEP(Entry, 0, 0); |
| 3333 | } |
| 3334 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3335 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3336 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3337 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 3338 | } |
| 3339 | |
| 3340 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3341 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3342 | return GetMethodVarName(&CGM.getContext().Idents.get(Name)); |
| 3343 | } |
| 3344 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 3345 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3346 | std::string TypeStr; |
| 3347 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 3348 | |
| 3349 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3350 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3351 | if (!Entry) |
| 3352 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3353 | llvm::ConstantArray::get(TypeStr), |
| 3354 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3355 | 1, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3356 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3357 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3358 | } |
| 3359 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3360 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3361 | std::string TypeStr; |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3362 | CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D), |
| 3363 | TypeStr); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3364 | |
| 3365 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 3366 | |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3367 | if (!Entry) |
| 3368 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3369 | llvm::ConstantArray::get(TypeStr), |
| 3370 | "__TEXT,__cstring,cstring_literals", |
| 3371 | 1, true); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3372 | |
| 3373 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3374 | } |
| 3375 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3376 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3377 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3378 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
| 3379 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3380 | if (!Entry) |
| 3381 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
| 3382 | llvm::ConstantArray::get(Ident->getName()), |
| 3383 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3384 | 1, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3385 | |
| 3386 | return getConstantGEP(Entry, 0, 0); |
| 3387 | } |
| 3388 | |
| 3389 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3390 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3391 | llvm::Constant * |
| 3392 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 3393 | const Decl *Container) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3394 | std::string TypeStr; |
| 3395 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3396 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 3397 | } |
| 3398 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3399 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
| 3400 | const ObjCContainerDecl *CD, |
| 3401 | std::string &NameOut) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 3402 | NameOut = '\01'; |
| 3403 | NameOut += (D->isInstanceMethod() ? '-' : '+'); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3404 | NameOut += '['; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 3405 | assert (CD && "Missing container decl in GetNameForMethod"); |
| 3406 | NameOut += CD->getNameAsString(); |
Fariborz Jahanian | 1e9aef3 | 2009-04-16 18:34:20 +0000 | [diff] [blame] | 3407 | if (const ObjCCategoryImplDecl *CID = |
| 3408 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) { |
| 3409 | NameOut += '('; |
| 3410 | NameOut += CID->getNameAsString(); |
| 3411 | NameOut+= ')'; |
| 3412 | } |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3413 | NameOut += ' '; |
| 3414 | NameOut += D->getSelector().getAsString(); |
| 3415 | NameOut += ']'; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3416 | } |
| 3417 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3418 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3419 | EmitModuleInfo(); |
| 3420 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3421 | // Emit the dummy bodies for any protocols which were referenced but |
| 3422 | // never defined. |
| 3423 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
| 3424 | i = Protocols.begin(), e = Protocols.end(); i != e; ++i) { |
| 3425 | if (i->second->hasInitializer()) |
| 3426 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3427 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3428 | std::vector<llvm::Constant*> Values(5); |
| 3429 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 3430 | Values[1] = GetClassName(i->first); |
| 3431 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 3432 | Values[3] = Values[4] = |
| 3433 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 3434 | i->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 3435 | i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 3436 | Values)); |
| 3437 | } |
| 3438 | |
| 3439 | std::vector<llvm::Constant*> Used; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3440 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3441 | e = UsedGlobals.end(); i != e; ++i) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3442 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3443 | } |
| 3444 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3445 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3446 | llvm::GlobalValue *GV = |
| 3447 | new llvm::GlobalVariable(AT, false, |
| 3448 | llvm::GlobalValue::AppendingLinkage, |
| 3449 | llvm::ConstantArray::get(AT, Used), |
| 3450 | "llvm.used", |
| 3451 | &CGM.getModule()); |
| 3452 | |
| 3453 | GV->setSection("llvm.metadata"); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3454 | |
| 3455 | // Add assembler directives to add lazy undefined symbol references |
| 3456 | // for classes which are referenced but not defined. This is |
| 3457 | // important for correct linker interaction. |
| 3458 | |
| 3459 | // FIXME: Uh, this isn't particularly portable. |
| 3460 | std::stringstream s; |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3461 | |
| 3462 | if (!CGM.getModule().getModuleInlineAsm().empty()) |
| 3463 | s << "\n"; |
| 3464 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3465 | for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(), |
| 3466 | e = LazySymbols.end(); i != e; ++i) { |
| 3467 | s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n"; |
| 3468 | } |
| 3469 | for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(), |
| 3470 | e = DefinedSymbols.end(); i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3471 | s << "\t.objc_class_name_" << (*i)->getName() << "=0\n" |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3472 | << "\t.globl .objc_class_name_" << (*i)->getName() << "\n"; |
| 3473 | } |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3474 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3475 | CGM.getModule().appendModuleInlineAsm(s.str()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3476 | } |
| 3477 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3478 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3479 | : CGObjCCommonMac(cgm), |
| 3480 | ObjCTypes(cgm) |
| 3481 | { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3482 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3483 | ObjCABI = 2; |
| 3484 | } |
| 3485 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3486 | /* *** */ |
| 3487 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3488 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3489 | : CGM(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3490 | { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3491 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3492 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3493 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3494 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3495 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3496 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3497 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3498 | Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 3499 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3500 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 3501 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3502 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3503 | |
| 3504 | // FIXME: It would be nice to unify this with the opaque type, so |
| 3505 | // that the IR comes out a bit cleaner. |
| 3506 | const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 3507 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3508 | |
| 3509 | // I'm not sure I like this. The implicit coordination is a bit |
| 3510 | // gross. We should solve this in a reasonable fashion because this |
| 3511 | // is a pretty common task (match some runtime data structure with |
| 3512 | // an LLVM data structure). |
| 3513 | |
| 3514 | // FIXME: This is leaked. |
| 3515 | // FIXME: Merge with rewriter code? |
| 3516 | |
| 3517 | // struct _objc_super { |
| 3518 | // id self; |
| 3519 | // Class cls; |
| 3520 | // } |
| 3521 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3522 | SourceLocation(), |
| 3523 | &Ctx.Idents.get("_objc_super")); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3524 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3525 | Ctx.getObjCIdType(), 0, false)); |
| 3526 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3527 | Ctx.getObjCClassType(), 0, false)); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3528 | RD->completeDefinition(Ctx); |
| 3529 | |
| 3530 | SuperCTy = Ctx.getTagDeclType(RD); |
| 3531 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
| 3532 | |
| 3533 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3534 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 3535 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3536 | // struct _prop_t { |
| 3537 | // char *name; |
| 3538 | // char *attributes; |
| 3539 | // } |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 3540 | PropertyTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, NULL); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3541 | CGM.getModule().addTypeName("struct._prop_t", |
| 3542 | PropertyTy); |
| 3543 | |
| 3544 | // struct _prop_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3545 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3546 | // uint32_t count_of_properties; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3547 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3548 | // } |
| 3549 | PropertyListTy = llvm::StructType::get(IntTy, |
| 3550 | IntTy, |
| 3551 | llvm::ArrayType::get(PropertyTy, 0), |
| 3552 | NULL); |
| 3553 | CGM.getModule().addTypeName("struct._prop_list_t", |
| 3554 | PropertyListTy); |
| 3555 | // struct _prop_list_t * |
| 3556 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
| 3557 | |
| 3558 | // struct _objc_method { |
| 3559 | // SEL _cmd; |
| 3560 | // char *method_type; |
| 3561 | // char *_imp; |
| 3562 | // } |
| 3563 | MethodTy = llvm::StructType::get(SelectorPtrTy, |
| 3564 | Int8PtrTy, |
| 3565 | Int8PtrTy, |
| 3566 | NULL); |
| 3567 | CGM.getModule().addTypeName("struct._objc_method", MethodTy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3568 | |
| 3569 | // struct _objc_cache * |
| 3570 | CacheTy = llvm::OpaqueType::get(); |
| 3571 | CGM.getModule().addTypeName("struct._objc_cache", CacheTy); |
| 3572 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3573 | } |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3574 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3575 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3576 | : ObjCCommonTypesHelper(cgm) |
| 3577 | { |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3578 | // struct _objc_method_description { |
| 3579 | // SEL name; |
| 3580 | // char *types; |
| 3581 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3582 | MethodDescriptionTy = |
| 3583 | llvm::StructType::get(SelectorPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3584 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3585 | NULL); |
| 3586 | CGM.getModule().addTypeName("struct._objc_method_description", |
| 3587 | MethodDescriptionTy); |
| 3588 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3589 | // struct _objc_method_description_list { |
| 3590 | // int count; |
| 3591 | // struct _objc_method_description[1]; |
| 3592 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3593 | MethodDescriptionListTy = |
| 3594 | llvm::StructType::get(IntTy, |
| 3595 | llvm::ArrayType::get(MethodDescriptionTy, 0), |
| 3596 | NULL); |
| 3597 | CGM.getModule().addTypeName("struct._objc_method_description_list", |
| 3598 | MethodDescriptionListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3599 | |
| 3600 | // struct _objc_method_description_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3601 | MethodDescriptionListPtrTy = |
| 3602 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
| 3603 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3604 | // Protocol description structures |
| 3605 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3606 | // struct _objc_protocol_extension { |
| 3607 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 3608 | // struct _objc_method_description_list *optional_instance_methods; |
| 3609 | // struct _objc_method_description_list *optional_class_methods; |
| 3610 | // struct _objc_property_list *instance_properties; |
| 3611 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3612 | ProtocolExtensionTy = |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3613 | llvm::StructType::get(IntTy, |
| 3614 | MethodDescriptionListPtrTy, |
| 3615 | MethodDescriptionListPtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3616 | PropertyListPtrTy, |
| 3617 | NULL); |
| 3618 | CGM.getModule().addTypeName("struct._objc_protocol_extension", |
| 3619 | ProtocolExtensionTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3620 | |
| 3621 | // struct _objc_protocol_extension * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3622 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
| 3623 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3624 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3625 | |
| 3626 | llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(); |
| 3627 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3628 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3629 | const llvm::Type *T = |
| 3630 | llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3631 | LongTy, |
| 3632 | llvm::ArrayType::get(ProtocolTyHolder, 0), |
| 3633 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3634 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T); |
| 3635 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3636 | // struct _objc_protocol { |
| 3637 | // struct _objc_protocol_extension *isa; |
| 3638 | // char *protocol_name; |
| 3639 | // struct _objc_protocol **_objc_protocol_list; |
| 3640 | // struct _objc_method_description_list *instance_methods; |
| 3641 | // struct _objc_method_description_list *class_methods; |
| 3642 | // } |
| 3643 | T = llvm::StructType::get(ProtocolExtensionPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3644 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3645 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3646 | MethodDescriptionListPtrTy, |
| 3647 | MethodDescriptionListPtrTy, |
| 3648 | NULL); |
| 3649 | cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T); |
| 3650 | |
| 3651 | ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get()); |
| 3652 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3653 | ProtocolListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3654 | // struct _objc_protocol_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3655 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
| 3656 | |
| 3657 | ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get()); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3658 | CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3659 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3660 | |
| 3661 | // Class description structures |
| 3662 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3663 | // struct _objc_ivar { |
| 3664 | // char *ivar_name; |
| 3665 | // char *ivar_type; |
| 3666 | // int ivar_offset; |
| 3667 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3668 | IvarTy = llvm::StructType::get(Int8PtrTy, |
| 3669 | Int8PtrTy, |
| 3670 | IntTy, |
| 3671 | NULL); |
| 3672 | CGM.getModule().addTypeName("struct._objc_ivar", IvarTy); |
| 3673 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3674 | // struct _objc_ivar_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3675 | IvarListTy = llvm::OpaqueType::get(); |
| 3676 | CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy); |
| 3677 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
| 3678 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3679 | // struct _objc_method_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3680 | MethodListTy = llvm::OpaqueType::get(); |
| 3681 | CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy); |
| 3682 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
| 3683 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3684 | // struct _objc_class_extension * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3685 | ClassExtensionTy = |
| 3686 | llvm::StructType::get(IntTy, |
| 3687 | Int8PtrTy, |
| 3688 | PropertyListPtrTy, |
| 3689 | NULL); |
| 3690 | CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy); |
| 3691 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
| 3692 | |
| 3693 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3694 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3695 | // struct _objc_class { |
| 3696 | // Class isa; |
| 3697 | // Class super_class; |
| 3698 | // char *name; |
| 3699 | // long version; |
| 3700 | // long info; |
| 3701 | // long instance_size; |
| 3702 | // struct _objc_ivar_list *ivars; |
| 3703 | // struct _objc_method_list *methods; |
| 3704 | // struct _objc_cache *cache; |
| 3705 | // struct _objc_protocol_list *protocols; |
| 3706 | // char *ivar_layout; |
| 3707 | // struct _objc_class_ext *ext; |
| 3708 | // }; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3709 | T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3710 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3711 | Int8PtrTy, |
| 3712 | LongTy, |
| 3713 | LongTy, |
| 3714 | LongTy, |
| 3715 | IvarListPtrTy, |
| 3716 | MethodListPtrTy, |
| 3717 | CachePtrTy, |
| 3718 | ProtocolListPtrTy, |
| 3719 | Int8PtrTy, |
| 3720 | ClassExtensionPtrTy, |
| 3721 | NULL); |
| 3722 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T); |
| 3723 | |
| 3724 | ClassTy = cast<llvm::StructType>(ClassTyHolder.get()); |
| 3725 | CGM.getModule().addTypeName("struct._objc_class", ClassTy); |
| 3726 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
| 3727 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3728 | // struct _objc_category { |
| 3729 | // char *category_name; |
| 3730 | // char *class_name; |
| 3731 | // struct _objc_method_list *instance_method; |
| 3732 | // struct _objc_method_list *class_method; |
| 3733 | // uint32_t size; // sizeof(struct _objc_category) |
| 3734 | // struct _objc_property_list *instance_properties;// category's @property |
| 3735 | // } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3736 | CategoryTy = llvm::StructType::get(Int8PtrTy, |
| 3737 | Int8PtrTy, |
| 3738 | MethodListPtrTy, |
| 3739 | MethodListPtrTy, |
| 3740 | ProtocolListPtrTy, |
| 3741 | IntTy, |
| 3742 | PropertyListPtrTy, |
| 3743 | NULL); |
| 3744 | CGM.getModule().addTypeName("struct._objc_category", CategoryTy); |
| 3745 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3746 | // Global metadata structures |
| 3747 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3748 | // struct _objc_symtab { |
| 3749 | // long sel_ref_cnt; |
| 3750 | // SEL *refs; |
| 3751 | // short cls_def_cnt; |
| 3752 | // short cat_def_cnt; |
| 3753 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 3754 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3755 | SymtabTy = llvm::StructType::get(LongTy, |
| 3756 | SelectorPtrTy, |
| 3757 | ShortTy, |
| 3758 | ShortTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3759 | llvm::ArrayType::get(Int8PtrTy, 0), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3760 | NULL); |
| 3761 | CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy); |
| 3762 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
| 3763 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3764 | // struct _objc_module { |
| 3765 | // long version; |
| 3766 | // long size; // sizeof(struct _objc_module) |
| 3767 | // char *name; |
| 3768 | // struct _objc_symtab* symtab; |
| 3769 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3770 | ModuleTy = |
| 3771 | llvm::StructType::get(LongTy, |
| 3772 | LongTy, |
| 3773 | Int8PtrTy, |
| 3774 | SymtabPtrTy, |
| 3775 | NULL); |
| 3776 | CGM.getModule().addTypeName("struct._objc_module", ModuleTy); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3777 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 3778 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3779 | // FIXME: This is the size of the setjmp buffer and should be |
| 3780 | // target specific. 18 is what's used on 32-bit X86. |
| 3781 | uint64_t SetJmpBufferSize = 18; |
| 3782 | |
| 3783 | // Exceptions |
| 3784 | const llvm::Type *StackPtrTy = |
Daniel Dunbar | 1000491 | 2008-09-27 06:32:25 +0000 | [diff] [blame] | 3785 | llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3786 | |
| 3787 | ExceptionDataTy = |
| 3788 | llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty, |
| 3789 | SetJmpBufferSize), |
| 3790 | StackPtrTy, NULL); |
| 3791 | CGM.getModule().addTypeName("struct._objc_exception_data", |
| 3792 | ExceptionDataTy); |
| 3793 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3794 | } |
| 3795 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3796 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3797 | : ObjCCommonTypesHelper(cgm) |
| 3798 | { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3799 | // struct _method_list_t { |
| 3800 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 3801 | // uint32_t method_count; |
| 3802 | // struct _objc_method method_list[method_count]; |
| 3803 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3804 | MethodListnfABITy = llvm::StructType::get(IntTy, |
| 3805 | IntTy, |
| 3806 | llvm::ArrayType::get(MethodTy, 0), |
| 3807 | NULL); |
| 3808 | CGM.getModule().addTypeName("struct.__method_list_t", |
| 3809 | MethodListnfABITy); |
| 3810 | // struct method_list_t * |
| 3811 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3812 | |
| 3813 | // struct _protocol_t { |
| 3814 | // id isa; // NULL |
| 3815 | // const char * const protocol_name; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3816 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3817 | // const struct method_list_t * const instance_methods; |
| 3818 | // const struct method_list_t * const class_methods; |
| 3819 | // const struct method_list_t *optionalInstanceMethods; |
| 3820 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3821 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3822 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 3823 | // const uint32_t flags; // = 0 |
| 3824 | // } |
| 3825 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3826 | // Holder for struct _protocol_list_t * |
| 3827 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3828 | |
| 3829 | ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy, |
| 3830 | Int8PtrTy, |
| 3831 | llvm::PointerType::getUnqual( |
| 3832 | ProtocolListTyHolder), |
| 3833 | MethodListnfABIPtrTy, |
| 3834 | MethodListnfABIPtrTy, |
| 3835 | MethodListnfABIPtrTy, |
| 3836 | MethodListnfABIPtrTy, |
| 3837 | PropertyListPtrTy, |
| 3838 | IntTy, |
| 3839 | IntTy, |
| 3840 | NULL); |
| 3841 | CGM.getModule().addTypeName("struct._protocol_t", |
| 3842 | ProtocolnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3843 | |
| 3844 | // struct _protocol_t* |
| 3845 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3846 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3847 | // struct _protocol_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3848 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3849 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3850 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3851 | ProtocolListnfABITy = llvm::StructType::get(LongTy, |
| 3852 | llvm::ArrayType::get( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3853 | ProtocolnfABIPtrTy, 0), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3854 | NULL); |
| 3855 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3856 | ProtocolListnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3857 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo( |
| 3858 | ProtocolListnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3859 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3860 | // struct _objc_protocol_list* |
| 3861 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3862 | |
| 3863 | // struct _ivar_t { |
| 3864 | // unsigned long int *offset; // pointer to ivar offset location |
| 3865 | // char *name; |
| 3866 | // char *type; |
| 3867 | // uint32_t alignment; |
| 3868 | // uint32_t size; |
| 3869 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3870 | IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy), |
| 3871 | Int8PtrTy, |
| 3872 | Int8PtrTy, |
| 3873 | IntTy, |
| 3874 | IntTy, |
| 3875 | NULL); |
| 3876 | CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy); |
| 3877 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3878 | // struct _ivar_list_t { |
| 3879 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 3880 | // uint32 count; |
| 3881 | // struct _iver_t list[count]; |
| 3882 | // } |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3883 | IvarListnfABITy = llvm::StructType::get(IntTy, |
| 3884 | IntTy, |
| 3885 | llvm::ArrayType::get( |
| 3886 | IvarnfABITy, 0), |
| 3887 | NULL); |
| 3888 | CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy); |
| 3889 | |
| 3890 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3891 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3892 | // struct _class_ro_t { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3893 | // uint32_t const flags; |
| 3894 | // uint32_t const instanceStart; |
| 3895 | // uint32_t const instanceSize; |
| 3896 | // uint32_t const reserved; // only when building for 64bit targets |
| 3897 | // const uint8_t * const ivarLayout; |
| 3898 | // const char *const name; |
| 3899 | // const struct _method_list_t * const baseMethods; |
| 3900 | // const struct _objc_protocol_list *const baseProtocols; |
| 3901 | // const struct _ivar_list_t *const ivars; |
| 3902 | // const uint8_t * const weakIvarLayout; |
| 3903 | // const struct _prop_list_t * const properties; |
| 3904 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3905 | |
| 3906 | // FIXME. Add 'reserved' field in 64bit abi mode! |
| 3907 | ClassRonfABITy = llvm::StructType::get(IntTy, |
| 3908 | IntTy, |
| 3909 | IntTy, |
| 3910 | Int8PtrTy, |
| 3911 | Int8PtrTy, |
| 3912 | MethodListnfABIPtrTy, |
| 3913 | ProtocolListnfABIPtrTy, |
| 3914 | IvarListnfABIPtrTy, |
| 3915 | Int8PtrTy, |
| 3916 | PropertyListPtrTy, |
| 3917 | NULL); |
| 3918 | CGM.getModule().addTypeName("struct._class_ro_t", |
| 3919 | ClassRonfABITy); |
| 3920 | |
| 3921 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 3922 | std::vector<const llvm::Type*> Params; |
| 3923 | Params.push_back(ObjectPtrTy); |
| 3924 | Params.push_back(SelectorPtrTy); |
| 3925 | ImpnfABITy = llvm::PointerType::getUnqual( |
| 3926 | llvm::FunctionType::get(ObjectPtrTy, Params, false)); |
| 3927 | |
| 3928 | // struct _class_t { |
| 3929 | // struct _class_t *isa; |
| 3930 | // struct _class_t * const superclass; |
| 3931 | // void *cache; |
| 3932 | // IMP *vtable; |
| 3933 | // struct class_ro_t *ro; |
| 3934 | // } |
| 3935 | |
| 3936 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3937 | ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3938 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3939 | CachePtrTy, |
| 3940 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 3941 | llvm::PointerType::getUnqual( |
| 3942 | ClassRonfABITy), |
| 3943 | NULL); |
| 3944 | CGM.getModule().addTypeName("struct._class_t", ClassnfABITy); |
| 3945 | |
| 3946 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo( |
| 3947 | ClassnfABITy); |
| 3948 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3949 | // LLVM for struct _class_t * |
| 3950 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
| 3951 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3952 | // struct _category_t { |
| 3953 | // const char * const name; |
| 3954 | // struct _class_t *const cls; |
| 3955 | // const struct _method_list_t * const instance_methods; |
| 3956 | // const struct _method_list_t * const class_methods; |
| 3957 | // const struct _protocol_list_t * const protocols; |
| 3958 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3959 | // } |
| 3960 | CategorynfABITy = llvm::StructType::get(Int8PtrTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3961 | ClassnfABIPtrTy, |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3962 | MethodListnfABIPtrTy, |
| 3963 | MethodListnfABIPtrTy, |
| 3964 | ProtocolListnfABIPtrTy, |
| 3965 | PropertyListPtrTy, |
| 3966 | NULL); |
| 3967 | CGM.getModule().addTypeName("struct._category_t", CategorynfABITy); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3968 | |
| 3969 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3970 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3971 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3972 | |
| 3973 | // MessageRefTy - LLVM for: |
| 3974 | // struct _message_ref_t { |
| 3975 | // IMP messenger; |
| 3976 | // SEL name; |
| 3977 | // }; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3978 | |
| 3979 | // First the clang type for struct _message_ref_t |
| 3980 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3981 | SourceLocation(), |
| 3982 | &Ctx.Idents.get("_message_ref_t")); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3983 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3984 | Ctx.VoidPtrTy, 0, false)); |
| 3985 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3986 | Ctx.getObjCSelType(), 0, false)); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3987 | RD->completeDefinition(Ctx); |
| 3988 | |
| 3989 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 3990 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 3991 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3992 | |
| 3993 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 3994 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
| 3995 | |
| 3996 | // SuperMessageRefTy - LLVM for: |
| 3997 | // struct _super_message_ref_t { |
| 3998 | // SUPER_IMP messenger; |
| 3999 | // SEL name; |
| 4000 | // }; |
| 4001 | SuperMessageRefTy = llvm::StructType::get(ImpnfABITy, |
| 4002 | SelectorPtrTy, |
| 4003 | NULL); |
| 4004 | CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy); |
| 4005 | |
| 4006 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 4007 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
| 4008 | |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 4009 | |
| 4010 | // struct objc_typeinfo { |
| 4011 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 4012 | // const char* name; // c++ typeinfo string |
| 4013 | // Class cls; |
| 4014 | // }; |
| 4015 | EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy), |
| 4016 | Int8PtrTy, |
| 4017 | ClassnfABIPtrTy, |
| 4018 | NULL); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 4019 | CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 4020 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4021 | } |
| 4022 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4023 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
| 4024 | FinishNonFragileABIModule(); |
| 4025 | |
| 4026 | return NULL; |
| 4027 | } |
| 4028 | |
| 4029 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 4030 | // nonfragile abi has no module definition. |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4031 | |
| 4032 | // Build list of all implemented classe addresses in array |
| 4033 | // L_OBJC_LABEL_CLASS_$. |
| 4034 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$ |
| 4035 | // list of 'nonlazy' implementations (defined as those with a +load{} |
| 4036 | // method!!). |
| 4037 | unsigned NumClasses = DefinedClasses.size(); |
| 4038 | if (NumClasses) { |
| 4039 | std::vector<llvm::Constant*> Symbols(NumClasses); |
| 4040 | for (unsigned i=0; i<NumClasses; i++) |
| 4041 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 4042 | ObjCTypes.Int8PtrTy); |
| 4043 | llvm::Constant* Init = |
| 4044 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 4045 | NumClasses), |
| 4046 | Symbols); |
| 4047 | |
| 4048 | llvm::GlobalVariable *GV = |
| 4049 | new llvm::GlobalVariable(Init->getType(), false, |
| 4050 | llvm::GlobalValue::InternalLinkage, |
| 4051 | Init, |
| 4052 | "\01L_OBJC_LABEL_CLASS_$", |
| 4053 | &CGM.getModule()); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 4054 | GV->setAlignment(8); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4055 | GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip"); |
| 4056 | UsedGlobals.push_back(GV); |
| 4057 | } |
| 4058 | |
| 4059 | // Build list of all implemented category addresses in array |
| 4060 | // L_OBJC_LABEL_CATEGORY_$. |
| 4061 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$ |
| 4062 | // list of 'nonlazy' category implementations (defined as those with a +load{} |
| 4063 | // method!!). |
| 4064 | unsigned NumCategory = DefinedCategories.size(); |
| 4065 | if (NumCategory) { |
| 4066 | std::vector<llvm::Constant*> Symbols(NumCategory); |
| 4067 | for (unsigned i=0; i<NumCategory; i++) |
| 4068 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 4069 | ObjCTypes.Int8PtrTy); |
| 4070 | llvm::Constant* Init = |
| 4071 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 4072 | NumCategory), |
| 4073 | Symbols); |
| 4074 | |
| 4075 | llvm::GlobalVariable *GV = |
| 4076 | new llvm::GlobalVariable(Init->getType(), false, |
| 4077 | llvm::GlobalValue::InternalLinkage, |
| 4078 | Init, |
| 4079 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 4080 | &CGM.getModule()); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 4081 | GV->setAlignment(8); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4082 | GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip"); |
| 4083 | UsedGlobals.push_back(GV); |
| 4084 | } |
| 4085 | |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4086 | // static int L_OBJC_IMAGE_INFO[2] = { 0, flags }; |
| 4087 | // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0 |
| 4088 | std::vector<llvm::Constant*> Values(2); |
| 4089 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0); |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4090 | unsigned int flags = 0; |
Fariborz Jahanian | 66a5c2c | 2009-02-24 23:34:44 +0000 | [diff] [blame] | 4091 | // FIXME: Fix and continue? |
| 4092 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 4093 | flags |= eImageInfo_GarbageCollected; |
| 4094 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 4095 | flags |= eImageInfo_GCOnly; |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4096 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4097 | llvm::Constant* Init = llvm::ConstantArray::get( |
| 4098 | llvm::ArrayType::get(ObjCTypes.IntTy, 2), |
| 4099 | Values); |
| 4100 | llvm::GlobalVariable *IMGV = |
| 4101 | new llvm::GlobalVariable(Init->getType(), false, |
| 4102 | llvm::GlobalValue::InternalLinkage, |
| 4103 | Init, |
| 4104 | "\01L_OBJC_IMAGE_INFO", |
| 4105 | &CGM.getModule()); |
| 4106 | IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); |
| 4107 | UsedGlobals.push_back(IMGV); |
| 4108 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4109 | std::vector<llvm::Constant*> Used; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4110 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4111 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
| 4112 | e = UsedGlobals.end(); i != e; ++i) { |
| 4113 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
| 4114 | } |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4115 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4116 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
| 4117 | llvm::GlobalValue *GV = |
| 4118 | new llvm::GlobalVariable(AT, false, |
| 4119 | llvm::GlobalValue::AppendingLinkage, |
| 4120 | llvm::ConstantArray::get(AT, Used), |
| 4121 | "llvm.used", |
| 4122 | &CGM.getModule()); |
| 4123 | |
| 4124 | GV->setSection("llvm.metadata"); |
| 4125 | |
| 4126 | } |
| 4127 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4128 | // Metadata flags |
| 4129 | enum MetaDataDlags { |
| 4130 | CLS = 0x0, |
| 4131 | CLS_META = 0x1, |
| 4132 | CLS_ROOT = 0x2, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4133 | OBJC2_CLS_HIDDEN = 0x10, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4134 | CLS_EXCEPTION = 0x20 |
| 4135 | }; |
| 4136 | /// BuildClassRoTInitializer - generate meta-data for: |
| 4137 | /// struct _class_ro_t { |
| 4138 | /// uint32_t const flags; |
| 4139 | /// uint32_t const instanceStart; |
| 4140 | /// uint32_t const instanceSize; |
| 4141 | /// uint32_t const reserved; // only when building for 64bit targets |
| 4142 | /// const uint8_t * const ivarLayout; |
| 4143 | /// const char *const name; |
| 4144 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4145 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4146 | /// const struct _ivar_list_t *const ivars; |
| 4147 | /// const uint8_t * const weakIvarLayout; |
| 4148 | /// const struct _prop_list_t * const properties; |
| 4149 | /// } |
| 4150 | /// |
| 4151 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
| 4152 | unsigned flags, |
| 4153 | unsigned InstanceStart, |
| 4154 | unsigned InstanceSize, |
| 4155 | const ObjCImplementationDecl *ID) { |
| 4156 | std::string ClassName = ID->getNameAsString(); |
| 4157 | std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets! |
| 4158 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 4159 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 4160 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
| 4161 | // FIXME. For 64bit targets add 0 here. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4162 | // FIXME. ivarLayout is currently null! |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame^] | 4163 | Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4164 | : BuildIvarLayout(ID, true); |
| 4165 | // Values[ 3] = GetIvarLayoutName(0, ObjCTypes); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4166 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4167 | // const struct _method_list_t * const baseMethods; |
| 4168 | std::vector<llvm::Constant*> Methods; |
| 4169 | std::string MethodListName("\01l_OBJC_$_"); |
| 4170 | if (flags & CLS_META) { |
| 4171 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
| 4172 | for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(), |
| 4173 | e = ID->classmeth_end(); i != e; ++i) { |
| 4174 | // Class methods should always be defined. |
| 4175 | Methods.push_back(GetMethodConstant(*i)); |
| 4176 | } |
| 4177 | } else { |
| 4178 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
| 4179 | for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(), |
| 4180 | e = ID->instmeth_end(); i != e; ++i) { |
| 4181 | // Instance methods should always be defined. |
| 4182 | Methods.push_back(GetMethodConstant(*i)); |
| 4183 | } |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4184 | for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(), |
| 4185 | e = ID->propimpl_end(); i != e; ++i) { |
| 4186 | ObjCPropertyImplDecl *PID = *i; |
| 4187 | |
| 4188 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 4189 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 4190 | |
| 4191 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 4192 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4193 | Methods.push_back(C); |
| 4194 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 4195 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4196 | Methods.push_back(C); |
| 4197 | } |
| 4198 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4199 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4200 | Values[ 5] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4201 | "__DATA, __objc_const", Methods); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4202 | |
| 4203 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4204 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
| 4205 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
| 4206 | + OID->getNameAsString(), |
| 4207 | OID->protocol_begin(), |
| 4208 | OID->protocol_end()); |
| 4209 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4210 | if (flags & CLS_META) |
| 4211 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4212 | else |
| 4213 | Values[ 7] = EmitIvarList(ID); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4214 | // FIXME. weakIvarLayout is currently null. |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame^] | 4215 | Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4216 | : BuildIvarLayout(ID, false); |
| 4217 | // Values[ 8] = GetIvarLayoutName(0, ObjCTypes); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4218 | if (flags & CLS_META) |
| 4219 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4220 | else |
| 4221 | Values[ 9] = |
| 4222 | EmitPropertyList( |
| 4223 | "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
| 4224 | ID, ID->getClassInterface(), ObjCTypes); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4225 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
| 4226 | Values); |
| 4227 | llvm::GlobalVariable *CLASS_RO_GV = |
| 4228 | new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false, |
| 4229 | llvm::GlobalValue::InternalLinkage, |
| 4230 | Init, |
| 4231 | (flags & CLS_META) ? |
| 4232 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 4233 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName, |
| 4234 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4235 | CLASS_RO_GV->setAlignment( |
| 4236 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4237 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4238 | return CLASS_RO_GV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4239 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4240 | } |
| 4241 | |
| 4242 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 4243 | /// for the given ClassName for: |
| 4244 | /// struct _class_t { |
| 4245 | /// struct _class_t *isa; |
| 4246 | /// struct _class_t * const superclass; |
| 4247 | /// void *cache; |
| 4248 | /// IMP *vtable; |
| 4249 | /// struct class_ro_t *ro; |
| 4250 | /// } |
| 4251 | /// |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4252 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
| 4253 | std::string &ClassName, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4254 | llvm::Constant *IsAGV, |
| 4255 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4256 | llvm::Constant *ClassRoGV, |
| 4257 | bool HiddenVisibility) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4258 | std::vector<llvm::Constant*> Values(5); |
| 4259 | Values[0] = IsAGV; |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4260 | Values[1] = SuperClassGV |
| 4261 | ? SuperClassGV |
| 4262 | : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4263 | Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar |
| 4264 | Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar |
| 4265 | Values[4] = ClassRoGV; // &CLASS_RO_GV |
| 4266 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
| 4267 | Values); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4268 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 4269 | GV->setInitializer(Init); |
Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 4270 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4271 | GV->setAlignment( |
| 4272 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4273 | if (HiddenVisibility) |
| 4274 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4275 | return GV; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4276 | } |
| 4277 | |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4278 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCInterfaceDecl *OID, |
| 4279 | uint32_t &InstanceStart, |
| 4280 | uint32_t &InstanceSize) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4281 | // Find first and last (non-padding) ivars in this interface. |
| 4282 | |
| 4283 | // FIXME: Use iterator. |
| 4284 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
| 4285 | GetNamedIvarList(OID, OIvars); |
| 4286 | |
| 4287 | if (OIvars.empty()) { |
| 4288 | InstanceStart = InstanceSize = 0; |
| 4289 | return; |
Daniel Dunbar | d4ae6c0 | 2009-04-22 04:39:47 +0000 | [diff] [blame] | 4290 | } |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4291 | |
| 4292 | const ObjCIvarDecl *First = OIvars.front(); |
| 4293 | const ObjCIvarDecl *Last = OIvars.back(); |
| 4294 | |
| 4295 | InstanceStart = ComputeIvarBaseOffset(CGM, OID, First); |
| 4296 | const llvm::Type *FieldTy = |
| 4297 | CGM.getTypes().ConvertTypeForMem(Last->getType()); |
| 4298 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame^] | 4299 | // FIXME. This breaks compatibility with llvm-gcc-4.2 (but makes it compatible |
| 4300 | // with gcc-4.2). We postpone this for now. |
| 4301 | #if 0 |
| 4302 | if (Last->isBitField()) { |
| 4303 | Expr *BitWidth = Last->getBitWidth(); |
| 4304 | uint64_t BitFieldSize = |
| 4305 | BitWidth->getIntegerConstantExprValue(CGM.getContext()).getZExtValue(); |
| 4306 | Size = (BitFieldSize / 8) + ((BitFieldSize % 8) != 0); |
| 4307 | } |
| 4308 | #endif |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4309 | InstanceSize = ComputeIvarBaseOffset(CGM, OID, Last) + Size; |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4310 | } |
| 4311 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4312 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 4313 | std::string ClassName = ID->getNameAsString(); |
| 4314 | if (!ObjCEmptyCacheVar) { |
| 4315 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4316 | ObjCTypes.CacheTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4317 | false, |
| 4318 | llvm::GlobalValue::ExternalLinkage, |
| 4319 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4320 | "_objc_empty_cache", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4321 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4322 | |
| 4323 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4324 | ObjCTypes.ImpnfABITy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4325 | false, |
| 4326 | llvm::GlobalValue::ExternalLinkage, |
| 4327 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4328 | "_objc_empty_vtable", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4329 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4330 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4331 | assert(ID->getClassInterface() && |
| 4332 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 4333 | // FIXME: Is this correct (that meta class size is never computed)? |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4334 | uint32_t InstanceStart = |
| 4335 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy); |
| 4336 | uint32_t InstanceSize = InstanceStart; |
| 4337 | uint32_t flags = CLS_META; |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4338 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 4339 | std::string ObjCClassName(getClassSymbolPrefix()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4340 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4341 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4342 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4343 | bool classIsHidden = |
| 4344 | CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4345 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4346 | flags |= OBJC2_CLS_HIDDEN; |
| 4347 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4348 | // class is root |
| 4349 | flags |= CLS_ROOT; |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4350 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4351 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4352 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4353 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4354 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 4355 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 4356 | Root = Super; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4357 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4358 | // work on super class metadata symbol. |
| 4359 | std::string SuperClassName = |
| 4360 | ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4361 | SuperClassGV = GetClassGlobal(SuperClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4362 | } |
| 4363 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 4364 | InstanceStart, |
| 4365 | InstanceSize,ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4366 | std::string TClassName = ObjCMetaClassName + ClassName; |
| 4367 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4368 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 4369 | classIsHidden); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4370 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4371 | // Metadata for the class |
| 4372 | flags = CLS; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4373 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4374 | flags |= OBJC2_CLS_HIDDEN; |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4375 | |
| 4376 | if (hasObjCExceptionAttribute(ID->getClassInterface())) |
| 4377 | flags |= CLS_EXCEPTION; |
| 4378 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4379 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4380 | flags |= CLS_ROOT; |
| 4381 | SuperClassGV = 0; |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 4382 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4383 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4384 | std::string RootClassName = |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4385 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4386 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4387 | } |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4388 | GetClassSizeInfo(ID->getClassInterface(), InstanceStart, InstanceSize); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4389 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4390 | InstanceStart, |
| 4391 | InstanceSize, |
| 4392 | ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4393 | |
| 4394 | TClassName = ObjCClassName + ClassName; |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4395 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4396 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 4397 | classIsHidden); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4398 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4399 | |
| 4400 | // Force the definition of the EHType if necessary. |
| 4401 | if (flags & CLS_EXCEPTION) |
| 4402 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4403 | } |
| 4404 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4405 | /// GenerateProtocolRef - This routine is called to generate code for |
| 4406 | /// a protocol reference expression; as in: |
| 4407 | /// @code |
| 4408 | /// @protocol(Proto1); |
| 4409 | /// @endcode |
| 4410 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 4411 | /// which will hold address of the protocol meta-data. |
| 4412 | /// |
| 4413 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
| 4414 | const ObjCProtocolDecl *PD) { |
| 4415 | |
Fariborz Jahanian | 960cd06 | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 4416 | // This routine is called for @protocol only. So, we must build definition |
| 4417 | // of protocol's meta-data (not a reference to it!) |
| 4418 | // |
| 4419 | llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4420 | ObjCTypes.ExternalProtocolPtrTy); |
| 4421 | |
| 4422 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
| 4423 | ProtocolName += PD->getNameAsCString(); |
| 4424 | |
| 4425 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 4426 | if (PTGV) |
| 4427 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4428 | PTGV = new llvm::GlobalVariable( |
| 4429 | Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4430 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4431 | Init, |
| 4432 | ProtocolName, |
| 4433 | &CGM.getModule()); |
| 4434 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 4435 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4436 | UsedGlobals.push_back(PTGV); |
| 4437 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4438 | } |
| 4439 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4440 | /// GenerateCategory - Build metadata for a category implementation. |
| 4441 | /// struct _category_t { |
| 4442 | /// const char * const name; |
| 4443 | /// struct _class_t *const cls; |
| 4444 | /// const struct _method_list_t * const instance_methods; |
| 4445 | /// const struct _method_list_t * const class_methods; |
| 4446 | /// const struct _protocol_list_t * const protocols; |
| 4447 | /// const struct _prop_list_t * const properties; |
| 4448 | /// } |
| 4449 | /// |
| 4450 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) |
| 4451 | { |
| 4452 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4453 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
| 4454 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4455 | "_$_" + OCD->getNameAsString()); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4456 | std::string ExtClassName(getClassSymbolPrefix() + |
| 4457 | Interface->getNameAsString()); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4458 | |
| 4459 | std::vector<llvm::Constant*> Values(6); |
| 4460 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 4461 | // meta-class entry symbol |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4462 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4463 | Values[1] = ClassGV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4464 | std::vector<llvm::Constant*> Methods; |
| 4465 | std::string MethodListName(Prefix); |
| 4466 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
| 4467 | "_$_" + OCD->getNameAsString(); |
| 4468 | |
| 4469 | for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(), |
| 4470 | e = OCD->instmeth_end(); i != e; ++i) { |
| 4471 | // Instance methods should always be defined. |
| 4472 | Methods.push_back(GetMethodConstant(*i)); |
| 4473 | } |
| 4474 | |
| 4475 | Values[2] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4476 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4477 | Methods); |
| 4478 | |
| 4479 | MethodListName = Prefix; |
| 4480 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 4481 | OCD->getNameAsString(); |
| 4482 | Methods.clear(); |
| 4483 | for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(), |
| 4484 | e = OCD->classmeth_end(); i != e; ++i) { |
| 4485 | // Class methods should always be defined. |
| 4486 | Methods.push_back(GetMethodConstant(*i)); |
| 4487 | } |
| 4488 | |
| 4489 | Values[3] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4490 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4491 | Methods); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4492 | const ObjCCategoryDecl *Category = |
| 4493 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4494 | if (Category) { |
| 4495 | std::string ExtName(Interface->getNameAsString() + "_$_" + |
| 4496 | OCD->getNameAsString()); |
| 4497 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
| 4498 | + Interface->getNameAsString() + "_$_" |
| 4499 | + Category->getNameAsString(), |
| 4500 | Category->protocol_begin(), |
| 4501 | Category->protocol_end()); |
| 4502 | Values[5] = |
| 4503 | EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
| 4504 | OCD, Category, ObjCTypes); |
| 4505 | } |
| 4506 | else { |
| 4507 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4508 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4509 | } |
| 4510 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4511 | llvm::Constant *Init = |
| 4512 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
| 4513 | Values); |
| 4514 | llvm::GlobalVariable *GCATV |
| 4515 | = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy, |
| 4516 | false, |
| 4517 | llvm::GlobalValue::InternalLinkage, |
| 4518 | Init, |
| 4519 | ExtCatName, |
| 4520 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4521 | GCATV->setAlignment( |
| 4522 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4523 | GCATV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4524 | UsedGlobals.push_back(GCATV); |
| 4525 | DefinedCategories.push_back(GCATV); |
| 4526 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4527 | |
| 4528 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 4529 | /// given method if it has been defined. The result is null if the |
| 4530 | /// method has not been defined. The return value has type MethodPtrTy. |
| 4531 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
| 4532 | const ObjCMethodDecl *MD) { |
| 4533 | // FIXME: Use DenseMap::lookup |
| 4534 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 4535 | if (!Fn) |
| 4536 | return 0; |
| 4537 | |
| 4538 | std::vector<llvm::Constant*> Method(3); |
| 4539 | Method[0] = |
| 4540 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4541 | ObjCTypes.SelectorPtrTy); |
| 4542 | Method[1] = GetMethodVarType(MD); |
| 4543 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 4544 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 4545 | } |
| 4546 | |
| 4547 | /// EmitMethodList - Build meta-data for method declarations |
| 4548 | /// struct _method_list_t { |
| 4549 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 4550 | /// uint32_t method_count; |
| 4551 | /// struct _objc_method method_list[method_count]; |
| 4552 | /// } |
| 4553 | /// |
| 4554 | llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList( |
| 4555 | const std::string &Name, |
| 4556 | const char *Section, |
| 4557 | const ConstantVector &Methods) { |
| 4558 | // Return null for empty list. |
| 4559 | if (Methods.empty()) |
| 4560 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
| 4561 | |
| 4562 | std::vector<llvm::Constant*> Values(3); |
| 4563 | // sizeof(struct _objc_method) |
| 4564 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy); |
| 4565 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4566 | // method_count |
| 4567 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 4568 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 4569 | Methods.size()); |
| 4570 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 4571 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4572 | |
| 4573 | llvm::GlobalVariable *GV = |
| 4574 | new llvm::GlobalVariable(Init->getType(), false, |
| 4575 | llvm::GlobalValue::InternalLinkage, |
| 4576 | Init, |
| 4577 | Name, |
| 4578 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4579 | GV->setAlignment( |
| 4580 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4581 | GV->setSection(Section); |
| 4582 | UsedGlobals.push_back(GV); |
| 4583 | return llvm::ConstantExpr::getBitCast(GV, |
| 4584 | ObjCTypes.MethodListnfABIPtrTy); |
| 4585 | } |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4586 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4587 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 4588 | /// the given ivar. |
| 4589 | /// |
| 4590 | llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 4591 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4592 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 4593 | std::string Name = "OBJC_IVAR_$_" + |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4594 | getInterfaceDeclForIvar(ID, Ivar, CGM.getContext())->getNameAsString() + |
| 4595 | '.' + Ivar->getNameAsString(); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4596 | llvm::GlobalVariable *IvarOffsetGV = |
| 4597 | CGM.getModule().getGlobalVariable(Name); |
| 4598 | if (!IvarOffsetGV) |
| 4599 | IvarOffsetGV = |
| 4600 | new llvm::GlobalVariable(ObjCTypes.LongTy, |
| 4601 | false, |
| 4602 | llvm::GlobalValue::ExternalLinkage, |
| 4603 | 0, |
| 4604 | Name, |
| 4605 | &CGM.getModule()); |
| 4606 | return IvarOffsetGV; |
| 4607 | } |
| 4608 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4609 | llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar( |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4610 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4611 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4612 | unsigned long int Offset) { |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4613 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
| 4614 | IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy, |
| 4615 | Offset)); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4616 | IvarOffsetGV->setAlignment( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4617 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy)); |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4618 | |
| 4619 | // FIXME: This matches gcc, but shouldn't the visibility be set on |
| 4620 | // the use as well (i.e., in ObjCIvarOffsetVariable). |
| 4621 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 4622 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
| 4623 | CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden) |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4624 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4625 | else |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4626 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4627 | IvarOffsetGV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4628 | return IvarOffsetGV; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4629 | } |
| 4630 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4631 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 4632 | /// implementation. The return value has type |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4633 | /// IvarListnfABIPtrTy. |
| 4634 | /// struct _ivar_t { |
| 4635 | /// unsigned long int *offset; // pointer to ivar offset location |
| 4636 | /// char *name; |
| 4637 | /// char *type; |
| 4638 | /// uint32_t alignment; |
| 4639 | /// uint32_t size; |
| 4640 | /// } |
| 4641 | /// struct _ivar_list_t { |
| 4642 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 4643 | /// uint32 count; |
| 4644 | /// struct _iver_t list[count]; |
| 4645 | /// } |
| 4646 | /// |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4647 | |
| 4648 | void CGObjCCommonMac::GetNamedIvarList(const ObjCInterfaceDecl *OID, |
| 4649 | llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const { |
| 4650 | for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), |
| 4651 | E = OID->ivar_end(); I != E; ++I) { |
| 4652 | // Ignore unnamed bit-fields. |
| 4653 | if (!(*I)->getDeclName()) |
| 4654 | continue; |
| 4655 | |
| 4656 | Res.push_back(*I); |
| 4657 | } |
| 4658 | |
| 4659 | for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(CGM.getContext()), |
| 4660 | E = OID->prop_end(CGM.getContext()); I != E; ++I) |
| 4661 | if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl()) |
| 4662 | Res.push_back(IV); |
| 4663 | } |
| 4664 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4665 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
| 4666 | const ObjCImplementationDecl *ID) { |
| 4667 | |
| 4668 | std::vector<llvm::Constant*> Ivars, Ivar(5); |
| 4669 | |
| 4670 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4671 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
| 4672 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4673 | // FIXME. Consolidate this with similar code in GenerateClass. |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 4674 | |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4675 | // Collect declared and synthesized ivars in a small vector. |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4676 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4677 | GetNamedIvarList(OID, OIvars); |
Fariborz Jahanian | 99eee36 | 2009-04-01 19:37:34 +0000 | [diff] [blame] | 4678 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4679 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 4680 | ObjCIvarDecl *IVD = OIvars[i]; |
Daniel Dunbar | 3eec8aa | 2009-04-20 05:53:40 +0000 | [diff] [blame] | 4681 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD, |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4682 | ComputeIvarBaseOffset(CGM, OID, IVD)); |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4683 | Ivar[1] = GetMethodVarName(IVD->getIdentifier()); |
| 4684 | Ivar[2] = GetMethodVarType(IVD); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4685 | const llvm::Type *FieldTy = |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4686 | CGM.getTypes().ConvertTypeForMem(IVD->getType()); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4687 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
| 4688 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4689 | IVD->getType().getTypePtr()) >> 3; |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4690 | Align = llvm::Log2_32(Align); |
| 4691 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4692 | // NOTE. Size of a bitfield does not match gcc's, because of the |
| 4693 | // way bitfields are treated special in each. But I am told that |
| 4694 | // 'size' for bitfield ivars is ignored by the runtime so it does |
| 4695 | // not matter. If it matters, there is enough info to get the |
| 4696 | // bitfield right! |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4697 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4698 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
| 4699 | } |
| 4700 | // Return null for empty list. |
| 4701 | if (Ivars.empty()) |
| 4702 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4703 | std::vector<llvm::Constant*> Values(3); |
| 4704 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy); |
| 4705 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4706 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 4707 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
| 4708 | Ivars.size()); |
| 4709 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
| 4710 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4711 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 4712 | llvm::GlobalVariable *GV = |
| 4713 | new llvm::GlobalVariable(Init->getType(), false, |
| 4714 | llvm::GlobalValue::InternalLinkage, |
| 4715 | Init, |
| 4716 | Prefix + OID->getNameAsString(), |
| 4717 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4718 | GV->setAlignment( |
| 4719 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4720 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4721 | |
| 4722 | UsedGlobals.push_back(GV); |
| 4723 | return llvm::ConstantExpr::getBitCast(GV, |
| 4724 | ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4725 | } |
| 4726 | |
| 4727 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
| 4728 | const ObjCProtocolDecl *PD) { |
| 4729 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4730 | |
| 4731 | if (!Entry) { |
| 4732 | // We use the initializer as a marker of whether this is a forward |
| 4733 | // reference or not. At module finalization we add the empty |
| 4734 | // contents for protocols which were referenced but never defined. |
| 4735 | Entry = |
| 4736 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
| 4737 | llvm::GlobalValue::ExternalLinkage, |
| 4738 | 0, |
| 4739 | "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(), |
| 4740 | &CGM.getModule()); |
| 4741 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 4742 | UsedGlobals.push_back(Entry); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4743 | } |
| 4744 | |
| 4745 | return Entry; |
| 4746 | } |
| 4747 | |
| 4748 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 4749 | /// @code |
| 4750 | /// struct _protocol_t { |
| 4751 | /// id isa; // NULL |
| 4752 | /// const char * const protocol_name; |
| 4753 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 4754 | /// const struct method_list_t * const instance_methods; |
| 4755 | /// const struct method_list_t * const class_methods; |
| 4756 | /// const struct method_list_t *optionalInstanceMethods; |
| 4757 | /// const struct method_list_t *optionalClassMethods; |
| 4758 | /// const struct _prop_list_t * properties; |
| 4759 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 4760 | /// const uint32_t flags; // = 0 |
| 4761 | /// } |
| 4762 | /// @endcode |
| 4763 | /// |
| 4764 | |
| 4765 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
| 4766 | const ObjCProtocolDecl *PD) { |
| 4767 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4768 | |
| 4769 | // Early exit if a defining object has already been generated. |
| 4770 | if (Entry && Entry->hasInitializer()) |
| 4771 | return Entry; |
| 4772 | |
| 4773 | const char *ProtocolName = PD->getNameAsCString(); |
| 4774 | |
| 4775 | // Construct method lists. |
| 4776 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 4777 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4778 | for (ObjCProtocolDecl::instmeth_iterator |
| 4779 | i = PD->instmeth_begin(CGM.getContext()), |
| 4780 | e = PD->instmeth_end(CGM.getContext()); |
| 4781 | i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4782 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4783 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4784 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4785 | OptInstanceMethods.push_back(C); |
| 4786 | } else { |
| 4787 | InstanceMethods.push_back(C); |
| 4788 | } |
| 4789 | } |
| 4790 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4791 | for (ObjCProtocolDecl::classmeth_iterator |
| 4792 | i = PD->classmeth_begin(CGM.getContext()), |
| 4793 | e = PD->classmeth_end(CGM.getContext()); |
| 4794 | i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4795 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4796 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4797 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4798 | OptClassMethods.push_back(C); |
| 4799 | } else { |
| 4800 | ClassMethods.push_back(C); |
| 4801 | } |
| 4802 | } |
| 4803 | |
| 4804 | std::vector<llvm::Constant*> Values(10); |
| 4805 | // isa is NULL |
| 4806 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
| 4807 | Values[1] = GetClassName(PD->getIdentifier()); |
| 4808 | Values[2] = EmitProtocolList( |
| 4809 | "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(), |
| 4810 | PD->protocol_begin(), |
| 4811 | PD->protocol_end()); |
| 4812 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4813 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4814 | + PD->getNameAsString(), |
| 4815 | "__DATA, __objc_const", |
| 4816 | InstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4817 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4818 | + PD->getNameAsString(), |
| 4819 | "__DATA, __objc_const", |
| 4820 | ClassMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4821 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4822 | + PD->getNameAsString(), |
| 4823 | "__DATA, __objc_const", |
| 4824 | OptInstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4825 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4826 | + PD->getNameAsString(), |
| 4827 | "__DATA, __objc_const", |
| 4828 | OptClassMethods); |
| 4829 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(), |
| 4830 | 0, PD, ObjCTypes); |
| 4831 | uint32_t Size = |
| 4832 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy); |
| 4833 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4834 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
| 4835 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
| 4836 | Values); |
| 4837 | |
| 4838 | if (Entry) { |
| 4839 | // Already created, fix the linkage and update the initializer. |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4840 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4841 | Entry->setInitializer(Init); |
| 4842 | } else { |
| 4843 | Entry = |
| 4844 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4845 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4846 | Init, |
| 4847 | std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName, |
| 4848 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4849 | Entry->setAlignment( |
| 4850 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4851 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4852 | } |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4853 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4854 | |
| 4855 | // Use this protocol meta-data to build protocol list table in section |
| 4856 | // __DATA, __objc_protolist |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4857 | llvm::GlobalVariable *PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4858 | ObjCTypes.ProtocolnfABIPtrTy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4859 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4860 | Entry, |
| 4861 | std::string("\01l_OBJC_LABEL_PROTOCOL_$_") |
| 4862 | +ProtocolName, |
| 4863 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4864 | PTGV->setAlignment( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4865 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 4866 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4867 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4868 | UsedGlobals.push_back(PTGV); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4869 | return Entry; |
| 4870 | } |
| 4871 | |
| 4872 | /// EmitProtocolList - Generate protocol list meta-data: |
| 4873 | /// @code |
| 4874 | /// struct _protocol_list_t { |
| 4875 | /// long protocol_count; // Note, this is 32/64 bit |
| 4876 | /// struct _protocol_t[protocol_count]; |
| 4877 | /// } |
| 4878 | /// @endcode |
| 4879 | /// |
| 4880 | llvm::Constant * |
| 4881 | CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name, |
| 4882 | ObjCProtocolDecl::protocol_iterator begin, |
| 4883 | ObjCProtocolDecl::protocol_iterator end) { |
| 4884 | std::vector<llvm::Constant*> ProtocolRefs; |
| 4885 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4886 | // Just return null for empty protocol lists |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4887 | if (begin == end) |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4888 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4889 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4890 | // FIXME: We shouldn't need to do this lookup here, should we? |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4891 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
| 4892 | if (GV) |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4893 | return llvm::ConstantExpr::getBitCast(GV, |
| 4894 | ObjCTypes.ProtocolListnfABIPtrTy); |
| 4895 | |
| 4896 | for (; begin != end; ++begin) |
| 4897 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 4898 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4899 | // This list is null terminated. |
| 4900 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4901 | ObjCTypes.ProtocolnfABIPtrTy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4902 | |
| 4903 | std::vector<llvm::Constant*> Values(2); |
| 4904 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 4905 | Values[1] = |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4906 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4907 | ProtocolRefs.size()), |
| 4908 | ProtocolRefs); |
| 4909 | |
| 4910 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4911 | GV = new llvm::GlobalVariable(Init->getType(), false, |
| 4912 | llvm::GlobalValue::InternalLinkage, |
| 4913 | Init, |
| 4914 | Name, |
| 4915 | &CGM.getModule()); |
| 4916 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4917 | GV->setAlignment( |
| 4918 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4919 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4920 | return llvm::ConstantExpr::getBitCast(GV, |
| 4921 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4922 | } |
| 4923 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4924 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 4925 | /// struct _objc_method { |
| 4926 | /// SEL _cmd; |
| 4927 | /// char *method_type; |
| 4928 | /// char *_imp; |
| 4929 | /// } |
| 4930 | |
| 4931 | llvm::Constant * |
| 4932 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 4933 | std::vector<llvm::Constant*> Desc(3); |
| 4934 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4935 | ObjCTypes.SelectorPtrTy); |
| 4936 | Desc[1] = GetMethodVarType(MD); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4937 | // Protocol methods have no implementation. So, this entry is always NULL. |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4938 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 4939 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
| 4940 | } |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4941 | |
| 4942 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 4943 | /// This code gen. amounts to generating code for: |
| 4944 | /// @code |
| 4945 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 4946 | /// @encode |
| 4947 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4948 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4949 | CodeGen::CodeGenFunction &CGF, |
| 4950 | QualType ObjectTy, |
| 4951 | llvm::Value *BaseValue, |
| 4952 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4953 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 4954 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4955 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 4956 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4957 | } |
| 4958 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4959 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
| 4960 | CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 4961 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4962 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 4963 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar), |
| 4964 | false, "ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4965 | } |
| 4966 | |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4967 | CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend( |
| 4968 | CodeGen::CodeGenFunction &CGF, |
| 4969 | QualType ResultType, |
| 4970 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4971 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4972 | QualType Arg0Ty, |
| 4973 | bool IsSuper, |
| 4974 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4975 | // FIXME. Even though IsSuper is passes. This function doese not |
| 4976 | // handle calls to 'super' receivers. |
| 4977 | CodeGenTypes &Types = CGM.getTypes(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4978 | llvm::Value *Arg0 = Receiver; |
| 4979 | if (!IsSuper) |
| 4980 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4981 | |
| 4982 | // Find the message function name. |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 4983 | // FIXME. This is too much work to get the ABI-specific result type |
| 4984 | // needed to find the message name. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4985 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, |
| 4986 | llvm::SmallVector<QualType, 16>()); |
| 4987 | llvm::Constant *Fn; |
| 4988 | std::string Name("\01l_"); |
| 4989 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4990 | #if 0 |
| 4991 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4992 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4993 | Fn = ObjCTypes.getMessageSendIdStretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4994 | // FIXME. Is there a better way of getting these names. |
| 4995 | // They are available in RuntimeFunctions vector pair. |
| 4996 | Name += "objc_msgSendId_stret_fixup"; |
| 4997 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4998 | else |
| 4999 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5000 | if (IsSuper) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5001 | Fn = ObjCTypes.getMessageSendSuper2StretFixupFn(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5002 | Name += "objc_msgSendSuper2_stret_fixup"; |
| 5003 | } |
| 5004 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5005 | { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5006 | Fn = ObjCTypes.getMessageSendStretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5007 | Name += "objc_msgSend_stret_fixup"; |
| 5008 | } |
| 5009 | } |
Fariborz Jahanian | 1a6b368 | 2009-02-05 19:35:43 +0000 | [diff] [blame] | 5010 | else if (ResultType->isFloatingType() && |
| 5011 | // Selection of frret API only happens in 32bit nonfragile ABI. |
| 5012 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5013 | Fn = ObjCTypes.getMessageSendFpretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5014 | Name += "objc_msgSend_fpret_fixup"; |
| 5015 | } |
| 5016 | else { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5017 | #if 0 |
| 5018 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5019 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5020 | Fn = ObjCTypes.getMessageSendIdFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5021 | Name += "objc_msgSendId_fixup"; |
| 5022 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5023 | else |
| 5024 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5025 | if (IsSuper) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5026 | Fn = ObjCTypes.getMessageSendSuper2FixupFn(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5027 | Name += "objc_msgSendSuper2_fixup"; |
| 5028 | } |
| 5029 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5030 | { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5031 | Fn = ObjCTypes.getMessageSendFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5032 | Name += "objc_msgSend_fixup"; |
| 5033 | } |
| 5034 | } |
| 5035 | Name += '_'; |
| 5036 | std::string SelName(Sel.getAsString()); |
| 5037 | // Replace all ':' in selector name with '_' ouch! |
| 5038 | for(unsigned i = 0; i < SelName.size(); i++) |
| 5039 | if (SelName[i] == ':') |
| 5040 | SelName[i] = '_'; |
| 5041 | Name += SelName; |
| 5042 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5043 | if (!GV) { |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5044 | // Build message ref table entry. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5045 | std::vector<llvm::Constant*> Values(2); |
| 5046 | Values[0] = Fn; |
| 5047 | Values[1] = GetMethodVarName(Sel); |
| 5048 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 5049 | GV = new llvm::GlobalVariable(Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 5050 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5051 | Init, |
| 5052 | Name, |
| 5053 | &CGM.getModule()); |
| 5054 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | f59c1a6 | 2009-04-15 19:04:46 +0000 | [diff] [blame] | 5055 | GV->setAlignment(16); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5056 | GV->setSection("__DATA, __objc_msgrefs, coalesced"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5057 | } |
| 5058 | llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5059 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5060 | CallArgList ActualArgs; |
| 5061 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 5062 | ActualArgs.push_back(std::make_pair(RValue::get(Arg1), |
| 5063 | ObjCTypes.MessageRefCPtrTy)); |
| 5064 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5065 | const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs); |
| 5066 | llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0); |
| 5067 | Callee = CGF.Builder.CreateLoad(Callee); |
Fariborz Jahanian | 3ab75bd | 2009-02-14 21:25:36 +0000 | [diff] [blame] | 5068 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5069 | Callee = CGF.Builder.CreateBitCast(Callee, |
| 5070 | llvm::PointerType::getUnqual(FTy)); |
| 5071 | return CGF.EmitCall(FnInfo1, Callee, ActualArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5072 | } |
| 5073 | |
| 5074 | /// Generate code for a message send expression in the nonfragile abi. |
| 5075 | CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend( |
| 5076 | CodeGen::CodeGenFunction &CGF, |
| 5077 | QualType ResultType, |
| 5078 | Selector Sel, |
| 5079 | llvm::Value *Receiver, |
| 5080 | bool IsClassMessage, |
| 5081 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5082 | return EmitMessageSend(CGF, ResultType, Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5083 | Receiver, CGF.getContext().getObjCIdType(), |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5084 | false, CallArgs); |
| 5085 | } |
| 5086 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5087 | llvm::GlobalVariable * |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5088 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5089 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5090 | |
Daniel Dunbar | dfff230 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 5091 | if (!GV) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5092 | GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 5093 | llvm::GlobalValue::ExternalLinkage, |
| 5094 | 0, Name, &CGM.getModule()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5095 | } |
| 5096 | |
| 5097 | return GV; |
| 5098 | } |
| 5099 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5100 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5101 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5102 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 5103 | |
| 5104 | if (!Entry) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5105 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5106 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5107 | Entry = |
| 5108 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5109 | llvm::GlobalValue::InternalLinkage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5110 | ClassGV, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5111 | "\01L_OBJC_CLASSLIST_REFERENCES_$_", |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5112 | &CGM.getModule()); |
| 5113 | Entry->setAlignment( |
| 5114 | CGM.getTargetData().getPrefTypeAlignment( |
| 5115 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5116 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
| 5117 | UsedGlobals.push_back(Entry); |
| 5118 | } |
| 5119 | |
| 5120 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5121 | } |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5122 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5123 | llvm::Value * |
| 5124 | CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder, |
| 5125 | const ObjCInterfaceDecl *ID) { |
| 5126 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
| 5127 | |
| 5128 | if (!Entry) { |
| 5129 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5130 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
| 5131 | Entry = |
| 5132 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5133 | llvm::GlobalValue::InternalLinkage, |
| 5134 | ClassGV, |
| 5135 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 5136 | &CGM.getModule()); |
| 5137 | Entry->setAlignment( |
| 5138 | CGM.getTargetData().getPrefTypeAlignment( |
| 5139 | ObjCTypes.ClassnfABIPtrTy)); |
| 5140 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5141 | UsedGlobals.push_back(Entry); |
| 5142 | } |
| 5143 | |
| 5144 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5145 | } |
| 5146 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5147 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 5148 | /// meta-data |
| 5149 | /// |
| 5150 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 5151 | const ObjCInterfaceDecl *ID) { |
| 5152 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 5153 | if (Entry) |
| 5154 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5155 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5156 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5157 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5158 | Entry = |
| 5159 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5160 | llvm::GlobalValue::InternalLinkage, |
| 5161 | MetaClassGV, |
| 5162 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 5163 | &CGM.getModule()); |
| 5164 | Entry->setAlignment( |
| 5165 | CGM.getTargetData().getPrefTypeAlignment( |
| 5166 | ObjCTypes.ClassnfABIPtrTy)); |
| 5167 | |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5168 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5169 | UsedGlobals.push_back(Entry); |
| 5170 | |
| 5171 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5172 | } |
| 5173 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5174 | /// GetClass - Return a reference to the class for the given interface |
| 5175 | /// decl. |
| 5176 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 5177 | const ObjCInterfaceDecl *ID) { |
| 5178 | return EmitClassRef(Builder, ID); |
| 5179 | } |
| 5180 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5181 | /// Generates a message send where the super is the receiver. This is |
| 5182 | /// a message send to self with special delivery semantics indicating |
| 5183 | /// which class's method should be called. |
| 5184 | CodeGen::RValue |
| 5185 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 5186 | QualType ResultType, |
| 5187 | Selector Sel, |
| 5188 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5189 | bool isCategoryImpl, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5190 | llvm::Value *Receiver, |
| 5191 | bool IsClassMessage, |
| 5192 | const CodeGen::CallArgList &CallArgs) { |
| 5193 | // ... |
| 5194 | // Create and init a super structure; this is a (receiver, class) |
| 5195 | // pair we will pass to objc_msgSendSuper. |
| 5196 | llvm::Value *ObjCSuper = |
| 5197 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 5198 | |
| 5199 | llvm::Value *ReceiverAsObject = |
| 5200 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 5201 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 5202 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
| 5203 | |
| 5204 | // 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] | 5205 | llvm::Value *Target; |
| 5206 | if (IsClassMessage) { |
| 5207 | if (isCategoryImpl) { |
| 5208 | // Message sent to "super' in a class method defined in |
| 5209 | // a category implementation. |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5210 | Target = EmitClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5211 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 5212 | Target = CGF.Builder.CreateLoad(Target); |
| 5213 | } |
| 5214 | else |
| 5215 | Target = EmitMetaClassRef(CGF.Builder, Class); |
| 5216 | } |
| 5217 | else |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5218 | Target = EmitSuperClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5219 | |
| 5220 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 5221 | // and ObjCTypes types. |
| 5222 | const llvm::Type *ClassTy = |
| 5223 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 5224 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 5225 | CGF.Builder.CreateStore(Target, |
| 5226 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 5227 | |
| 5228 | return EmitMessageSend(CGF, ResultType, Sel, |
| 5229 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 5230 | true, CallArgs); |
| 5231 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5232 | |
| 5233 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
| 5234 | Selector Sel) { |
| 5235 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 5236 | |
| 5237 | if (!Entry) { |
| 5238 | llvm::Constant *Casted = |
| 5239 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 5240 | ObjCTypes.SelectorPtrTy); |
| 5241 | Entry = |
| 5242 | new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false, |
| 5243 | llvm::GlobalValue::InternalLinkage, |
| 5244 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_", |
| 5245 | &CGM.getModule()); |
| 5246 | Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip"); |
| 5247 | UsedGlobals.push_back(Entry); |
| 5248 | } |
| 5249 | |
| 5250 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5251 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5252 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 5253 | /// objc_assign_ivar (id src, id *dst) |
| 5254 | /// |
| 5255 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 5256 | llvm::Value *src, llvm::Value *dst) |
| 5257 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5258 | const llvm::Type * SrcTy = src->getType(); |
| 5259 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5260 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5261 | assert(Size <= 8 && "does not support size > 8"); |
| 5262 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5263 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5264 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5265 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5266 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5267 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5268 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5269 | src, dst, "assignivar"); |
| 5270 | return; |
| 5271 | } |
| 5272 | |
| 5273 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 5274 | /// objc_assign_strongCast (id src, id *dst) |
| 5275 | /// |
| 5276 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
| 5277 | CodeGen::CodeGenFunction &CGF, |
| 5278 | llvm::Value *src, llvm::Value *dst) |
| 5279 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5280 | const llvm::Type * SrcTy = src->getType(); |
| 5281 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5282 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5283 | assert(Size <= 8 && "does not support size > 8"); |
| 5284 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5285 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5286 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5287 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5288 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5289 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5290 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5291 | src, dst, "weakassign"); |
| 5292 | return; |
| 5293 | } |
| 5294 | |
| 5295 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 5296 | /// object: objc_read_weak (id *src) |
| 5297 | /// |
| 5298 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
| 5299 | CodeGen::CodeGenFunction &CGF, |
| 5300 | llvm::Value *AddrWeakObj) |
| 5301 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5302 | const llvm::Type* DestTy = |
| 5303 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5304 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 5305 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5306 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5307 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5308 | return read_weak; |
| 5309 | } |
| 5310 | |
| 5311 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 5312 | /// objc_assign_weak (id src, id *dst) |
| 5313 | /// |
| 5314 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 5315 | llvm::Value *src, llvm::Value *dst) |
| 5316 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5317 | const llvm::Type * SrcTy = src->getType(); |
| 5318 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5319 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5320 | assert(Size <= 8 && "does not support size > 8"); |
| 5321 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5322 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5323 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5324 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5325 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5326 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 5327 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5328 | src, dst, "weakassign"); |
| 5329 | return; |
| 5330 | } |
| 5331 | |
| 5332 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 5333 | /// objc_assign_global (id src, id *dst) |
| 5334 | /// |
| 5335 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 5336 | llvm::Value *src, llvm::Value *dst) |
| 5337 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5338 | const llvm::Type * SrcTy = src->getType(); |
| 5339 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5340 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5341 | assert(Size <= 8 && "does not support size > 8"); |
| 5342 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5343 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5344 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5345 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5346 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5347 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5348 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5349 | src, dst, "globalassign"); |
| 5350 | return; |
| 5351 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5352 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5353 | void |
| 5354 | CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 5355 | const Stmt &S) { |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5356 | bool isTry = isa<ObjCAtTryStmt>(S); |
| 5357 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 5358 | llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5359 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5360 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5361 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5362 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
| 5363 | |
| 5364 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 5365 | // evaluation of the expression must occur before we enter the |
| 5366 | // @synchronized. We can safely avoid a temp here because jumps into |
| 5367 | // @synchronized are illegal & this will dominate uses. |
| 5368 | llvm::Value *SyncArg = 0; |
| 5369 | if (!isTry) { |
| 5370 | SyncArg = |
| 5371 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 5372 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5373 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5374 | } |
| 5375 | |
| 5376 | // Push an EH context entry, used for handling rethrows and jumps |
| 5377 | // through finally. |
| 5378 | CGF.PushCleanupBlock(FinallyBlock); |
| 5379 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5380 | CGF.setInvokeDest(TryHandler); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5381 | |
| 5382 | CGF.EmitBlock(TryBlock); |
| 5383 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 5384 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
| 5385 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5386 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5387 | // Emit the exception handler. |
| 5388 | |
| 5389 | CGF.EmitBlock(TryHandler); |
| 5390 | |
| 5391 | llvm::Value *llvm_eh_exception = |
| 5392 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
| 5393 | llvm::Value *llvm_eh_selector_i64 = |
| 5394 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64); |
| 5395 | llvm::Value *llvm_eh_typeid_for_i64 = |
| 5396 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64); |
| 5397 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5398 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow"); |
| 5399 | |
| 5400 | llvm::SmallVector<llvm::Value*, 8> SelectorArgs; |
| 5401 | SelectorArgs.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5402 | SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5403 | |
| 5404 | // Construct the lists of (type, catch body) to handle. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5405 | llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5406 | bool HasCatchAll = false; |
| 5407 | if (isTry) { |
| 5408 | if (const ObjCAtCatchStmt* CatchStmt = |
| 5409 | cast<ObjCAtTryStmt>(S).getCatchStmts()) { |
| 5410 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5411 | const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5412 | Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody())); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5413 | |
| 5414 | // catch(...) always matches. |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5415 | if (!CatchDecl) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5416 | // Use i8* null here to signal this is a catch all, not a cleanup. |
| 5417 | llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 5418 | SelectorArgs.push_back(Null); |
| 5419 | HasCatchAll = true; |
| 5420 | break; |
| 5421 | } |
| 5422 | |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5423 | if (CGF.getContext().isObjCIdType(CatchDecl->getType()) || |
| 5424 | CatchDecl->getType()->isObjCQualifiedIdType()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5425 | llvm::Value *IDEHType = |
| 5426 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 5427 | if (!IDEHType) |
| 5428 | IDEHType = |
| 5429 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5430 | llvm::GlobalValue::ExternalLinkage, |
| 5431 | 0, "OBJC_EHTYPE_id", &CGM.getModule()); |
| 5432 | SelectorArgs.push_back(IDEHType); |
| 5433 | HasCatchAll = true; |
| 5434 | break; |
| 5435 | } |
| 5436 | |
| 5437 | // All other types should be Objective-C interface pointer types. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5438 | const PointerType *PT = CatchDecl->getType()->getAsPointerType(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5439 | assert(PT && "Invalid @catch type."); |
| 5440 | const ObjCInterfaceType *IT = |
| 5441 | PT->getPointeeType()->getAsObjCInterfaceType(); |
| 5442 | assert(IT && "Invalid @catch type."); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5443 | llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5444 | SelectorArgs.push_back(EHType); |
| 5445 | } |
| 5446 | } |
| 5447 | } |
| 5448 | |
| 5449 | // We use a cleanup unless there was already a catch all. |
| 5450 | if (!HasCatchAll) { |
| 5451 | SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5452 | Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5453 | } |
| 5454 | |
| 5455 | llvm::Value *Selector = |
| 5456 | CGF.Builder.CreateCall(llvm_eh_selector_i64, |
| 5457 | SelectorArgs.begin(), SelectorArgs.end(), |
| 5458 | "selector"); |
| 5459 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5460 | const ParmVarDecl *CatchParam = Handlers[i].first; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5461 | const Stmt *CatchBody = Handlers[i].second; |
| 5462 | |
| 5463 | llvm::BasicBlock *Next = 0; |
| 5464 | |
| 5465 | // The last handler always matches. |
| 5466 | if (i + 1 != e) { |
| 5467 | assert(CatchParam && "Only last handler can be a catch all."); |
| 5468 | |
| 5469 | llvm::BasicBlock *Match = CGF.createBasicBlock("match"); |
| 5470 | Next = CGF.createBasicBlock("catch.next"); |
| 5471 | llvm::Value *Id = |
| 5472 | CGF.Builder.CreateCall(llvm_eh_typeid_for_i64, |
| 5473 | CGF.Builder.CreateBitCast(SelectorArgs[i+2], |
| 5474 | ObjCTypes.Int8PtrTy)); |
| 5475 | CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id), |
| 5476 | Match, Next); |
| 5477 | |
| 5478 | CGF.EmitBlock(Match); |
| 5479 | } |
| 5480 | |
| 5481 | if (CatchBody) { |
| 5482 | llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end"); |
| 5483 | llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler"); |
| 5484 | |
| 5485 | // Cleanups must call objc_end_catch. |
| 5486 | // |
| 5487 | // FIXME: It seems incorrect for objc_begin_catch to be inside |
| 5488 | // this context, but this matches gcc. |
| 5489 | CGF.PushCleanupBlock(MatchEnd); |
| 5490 | CGF.setInvokeDest(MatchHandler); |
| 5491 | |
| 5492 | llvm::Value *ExcObject = |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5493 | CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5494 | |
| 5495 | // Bind the catch parameter if it exists. |
| 5496 | if (CatchParam) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5497 | ExcObject = |
| 5498 | CGF.Builder.CreateBitCast(ExcObject, |
| 5499 | CGF.ConvertType(CatchParam->getType())); |
| 5500 | // CatchParam is a ParmVarDecl because of the grammar |
| 5501 | // construction used to handle this, but for codegen purposes |
| 5502 | // we treat this as a local decl. |
| 5503 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
| 5504 | CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5505 | } |
| 5506 | |
| 5507 | CGF.ObjCEHValueStack.push_back(ExcObject); |
| 5508 | CGF.EmitStmt(CatchBody); |
| 5509 | CGF.ObjCEHValueStack.pop_back(); |
| 5510 | |
| 5511 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5512 | |
| 5513 | CGF.EmitBlock(MatchHandler); |
| 5514 | |
| 5515 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5516 | // We are required to emit this call to satisfy LLVM, even |
| 5517 | // though we don't use the result. |
| 5518 | llvm::SmallVector<llvm::Value*, 8> Args; |
| 5519 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5520 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5521 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5522 | 0)); |
| 5523 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5524 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5525 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5526 | |
| 5527 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5528 | |
| 5529 | CGF.EmitBlock(MatchEnd); |
| 5530 | |
| 5531 | // Unfortunately, we also have to generate another EH frame here |
| 5532 | // in case this throws. |
| 5533 | llvm::BasicBlock *MatchEndHandler = |
| 5534 | CGF.createBasicBlock("match.end.handler"); |
| 5535 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5536 | CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5537 | Cont, MatchEndHandler, |
| 5538 | Args.begin(), Args.begin()); |
| 5539 | |
| 5540 | CGF.EmitBlock(Cont); |
| 5541 | if (Info.SwitchBlock) |
| 5542 | CGF.EmitBlock(Info.SwitchBlock); |
| 5543 | if (Info.EndBlock) |
| 5544 | CGF.EmitBlock(Info.EndBlock); |
| 5545 | |
| 5546 | CGF.EmitBlock(MatchEndHandler); |
| 5547 | Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5548 | // We are required to emit this call to satisfy LLVM, even |
| 5549 | // though we don't use the result. |
| 5550 | Args.clear(); |
| 5551 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5552 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5553 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5554 | 0)); |
| 5555 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5556 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5557 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5558 | |
| 5559 | if (Next) |
| 5560 | CGF.EmitBlock(Next); |
| 5561 | } else { |
| 5562 | assert(!Next && "catchup should be last handler."); |
| 5563 | |
| 5564 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5565 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5566 | } |
| 5567 | } |
| 5568 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5569 | // Pop the cleanup entry, the @finally is outside this cleanup |
| 5570 | // scope. |
| 5571 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5572 | CGF.setInvokeDest(PrevLandingPad); |
| 5573 | |
| 5574 | CGF.EmitBlock(FinallyBlock); |
| 5575 | |
| 5576 | if (isTry) { |
| 5577 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 5578 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 5579 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 5580 | } else { |
| 5581 | // Emit 'objc_sync_exit(expr)' as finally's sole statement for |
| 5582 | // @synchronized. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5583 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5584 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5585 | |
| 5586 | if (Info.SwitchBlock) |
| 5587 | CGF.EmitBlock(Info.SwitchBlock); |
| 5588 | if (Info.EndBlock) |
| 5589 | CGF.EmitBlock(Info.EndBlock); |
| 5590 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5591 | // Branch around the rethrow code. |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5592 | CGF.EmitBranch(FinallyEnd); |
| 5593 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5594 | CGF.EmitBlock(FinallyRethrow); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5595 | CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5596 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5597 | CGF.Builder.CreateUnreachable(); |
| 5598 | |
| 5599 | CGF.EmitBlock(FinallyEnd); |
| 5600 | } |
| 5601 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5602 | /// EmitThrowStmt - Generate code for a throw statement. |
| 5603 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 5604 | const ObjCAtThrowStmt &S) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5605 | llvm::Value *Exception; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5606 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5607 | Exception = CGF.EmitScalarExpr(ThrowExpr); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5608 | } else { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5609 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
| 5610 | "Unexpected rethrow outside @catch block."); |
| 5611 | Exception = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5612 | } |
| 5613 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5614 | llvm::Value *ExceptionAsObject = |
| 5615 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 5616 | llvm::BasicBlock *InvokeDest = CGF.getInvokeDest(); |
| 5617 | if (InvokeDest) { |
| 5618 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5619 | CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5620 | Cont, InvokeDest, |
| 5621 | &ExceptionAsObject, &ExceptionAsObject + 1); |
| 5622 | CGF.EmitBlock(Cont); |
| 5623 | } else |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5624 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5625 | CGF.Builder.CreateUnreachable(); |
| 5626 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5627 | // Clear the insertion point to indicate we are in unreachable code. |
| 5628 | CGF.Builder.ClearInsertionPoint(); |
| 5629 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5630 | |
| 5631 | llvm::Value * |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5632 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 5633 | bool ForDefinition) { |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5634 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5635 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5636 | // If we don't need a definition, return the entry if found or check |
| 5637 | // if we use an external reference. |
| 5638 | if (!ForDefinition) { |
| 5639 | if (Entry) |
| 5640 | return Entry; |
Daniel Dunbar | 7e075cb | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 5641 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5642 | // If this type (or a super class) has the __objc_exception__ |
| 5643 | // attribute, emit an external reference. |
| 5644 | if (hasObjCExceptionAttribute(ID)) |
| 5645 | return Entry = |
| 5646 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5647 | llvm::GlobalValue::ExternalLinkage, |
| 5648 | 0, |
| 5649 | (std::string("OBJC_EHTYPE_$_") + |
| 5650 | ID->getIdentifier()->getName()), |
| 5651 | &CGM.getModule()); |
| 5652 | } |
| 5653 | |
| 5654 | // Otherwise we need to either make a new entry or fill in the |
| 5655 | // initializer. |
| 5656 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5657 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5658 | std::string VTableName = "objc_ehtype_vtable"; |
| 5659 | llvm::GlobalVariable *VTableGV = |
| 5660 | CGM.getModule().getGlobalVariable(VTableName); |
| 5661 | if (!VTableGV) |
| 5662 | VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false, |
| 5663 | llvm::GlobalValue::ExternalLinkage, |
| 5664 | 0, VTableName, &CGM.getModule()); |
| 5665 | |
| 5666 | llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2); |
| 5667 | |
| 5668 | std::vector<llvm::Constant*> Values(3); |
| 5669 | Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1); |
| 5670 | Values[1] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5671 | Values[2] = GetClassGlobal(ClassName); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5672 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
| 5673 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5674 | if (Entry) { |
| 5675 | Entry->setInitializer(Init); |
| 5676 | } else { |
| 5677 | Entry = new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5678 | llvm::GlobalValue::WeakAnyLinkage, |
| 5679 | Init, |
| 5680 | (std::string("OBJC_EHTYPE_$_") + |
| 5681 | ID->getIdentifier()->getName()), |
| 5682 | &CGM.getModule()); |
| 5683 | } |
| 5684 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 5685 | if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden) |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5686 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5687 | Entry->setAlignment(8); |
| 5688 | |
| 5689 | if (ForDefinition) { |
| 5690 | Entry->setSection("__DATA,__objc_const"); |
| 5691 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 5692 | } else { |
| 5693 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 5694 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5695 | |
| 5696 | return Entry; |
| 5697 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5698 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5699 | /* *** */ |
| 5700 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5701 | CodeGen::CGObjCRuntime * |
| 5702 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 5703 | return new CGObjCMac(CGM); |
| 5704 | } |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5705 | |
| 5706 | CodeGen::CGObjCRuntime * |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5707 | CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5708 | return new CGObjCNonFragileABIMac(CGM); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5709 | } |