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) {} |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 710 | |
| 711 | // Allow sorting based on byte pos. |
| 712 | bool operator<(const GC_IVAR &b) const { |
| 713 | return ivar_bytepos < b.ivar_bytepos; |
| 714 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 715 | }; |
| 716 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 717 | class SKIP_SCAN { |
| 718 | public: |
| 719 | unsigned int skip; |
| 720 | unsigned int scan; |
| 721 | SKIP_SCAN() : skip(0), scan(0) {} |
| 722 | }; |
| 723 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 724 | protected: |
| 725 | CodeGen::CodeGenModule &CGM; |
| 726 | // FIXME! May not be needing this after all. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 727 | unsigned ObjCABI; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 728 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 729 | // gc ivar layout bitmap calculation helper caches. |
| 730 | llvm::SmallVector<GC_IVAR, 16> SkipIvars; |
| 731 | llvm::SmallVector<GC_IVAR, 16> IvarsInfo; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 732 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 733 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 734 | /// DefinedSymbols and FinishModule(). |
| 735 | std::set<IdentifierInfo*> LazySymbols; |
| 736 | |
| 737 | /// DefinedSymbols - External symbols which are defined by this |
| 738 | /// module. The symbols in this list and LazySymbols are used to add |
| 739 | /// special linker symbols which ensure that Objective-C modules are |
| 740 | /// linked properly. |
| 741 | std::set<IdentifierInfo*> DefinedSymbols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 742 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 743 | /// ClassNames - uniqued class names. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 744 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 745 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 746 | /// MethodVarNames - uniqued method variable names. |
| 747 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 748 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 749 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 750 | /// a StringMap here because have no other unique reference. |
| 751 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 752 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 753 | /// MethodDefinitions - map of methods which have been defined in |
| 754 | /// this translation unit. |
| 755 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 756 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 757 | /// PropertyNames - uniqued method variable names. |
| 758 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 759 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 760 | /// ClassReferences - uniqued class references. |
| 761 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 762 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 763 | /// SelectorReferences - uniqued selector references. |
| 764 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 765 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 766 | /// Protocols - Protocols for which an objc_protocol structure has |
| 767 | /// been emitted. Forward declarations are handled by creating an |
| 768 | /// empty structure whose initializer is filled in when/if defined. |
| 769 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 770 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 771 | /// DefinedProtocols - Protocols which have actually been |
| 772 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 773 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 774 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 775 | /// DefinedClasses - List of defined classes. |
| 776 | std::vector<llvm::GlobalValue*> DefinedClasses; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 777 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 778 | /// DefinedCategories - List of defined categories. |
| 779 | std::vector<llvm::GlobalValue*> DefinedCategories; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 780 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 781 | /// UsedGlobals - List of globals to pack into the llvm.used metadata |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 782 | /// to prevent them from being clobbered. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 783 | std::vector<llvm::GlobalVariable*> UsedGlobals; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 784 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 785 | /// GetNameForMethod - Return a name for the given method. |
| 786 | /// \param[out] NameOut - The return value. |
| 787 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 788 | const ObjCContainerDecl *CD, |
| 789 | std::string &NameOut); |
| 790 | |
| 791 | /// GetMethodVarName - Return a unique constant for the given |
| 792 | /// selector's name. The return value has type char *. |
| 793 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 794 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
| 795 | llvm::Constant *GetMethodVarName(const std::string &Name); |
| 796 | |
| 797 | /// GetMethodVarType - Return a unique constant for the given |
| 798 | /// selector's name. The return value has type char *. |
| 799 | |
| 800 | // FIXME: This is a horrible name. |
| 801 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 802 | llvm::Constant *GetMethodVarType(const FieldDecl *D); |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 803 | |
| 804 | /// GetPropertyName - Return a unique constant for the given |
| 805 | /// name. The return value has type char *. |
| 806 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
| 807 | |
| 808 | // FIXME: This can be dropped once string functions are unified. |
| 809 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 810 | const Decl *Container); |
| 811 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 812 | /// GetClassName - Return a unique constant for the given selector's |
| 813 | /// name. The return value has type char *. |
| 814 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
| 815 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 816 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 817 | /// implementation for the __strong or __weak case. |
| 818 | /// |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 819 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
| 820 | bool ForStrongLayout); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 821 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 822 | void BuildAggrIvarLayout(const ObjCInterfaceDecl *OI, |
| 823 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 824 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 825 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 826 | unsigned int BytePos, bool ForStrongLayout, |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 827 | bool &HasUnion); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 828 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 829 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 830 | /// ivar layout bitmap. |
| 831 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 832 | const ObjCCommonTypesHelper &ObjCTypes); |
| 833 | |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 834 | /// EmitPropertyList - Emit the given property list. The return |
| 835 | /// value has type PropertyListPtrTy. |
| 836 | llvm::Constant *EmitPropertyList(const std::string &Name, |
| 837 | const Decl *Container, |
| 838 | const ObjCContainerDecl *OCD, |
| 839 | const ObjCCommonTypesHelper &ObjCTypes); |
| 840 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 841 | /// GetProtocolRef - Return a reference to the internal protocol |
| 842 | /// description, creating an empty one if it has not been |
| 843 | /// defined. The return value has type ProtocolPtrTy. |
| 844 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 845 | |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 846 | /// GetFieldBaseOffset - return's field byte offset. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 847 | uint64_t GetFieldBaseOffset(const ObjCInterfaceDecl *OI, |
| 848 | const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 849 | const FieldDecl *Field); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 850 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 851 | /// CreateMetadataVar - Create a global variable with internal |
| 852 | /// linkage for use by the Objective-C runtime. |
| 853 | /// |
| 854 | /// This is a convenience wrapper which not only creates the |
| 855 | /// variable, but also sets the section and alignment and adds the |
| 856 | /// global to the UsedGlobals list. |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 857 | /// |
| 858 | /// \param Name - The variable name. |
| 859 | /// \param Init - The variable initializer; this is also used to |
| 860 | /// define the type of the variable. |
| 861 | /// \param Section - The section the variable should go into, or 0. |
| 862 | /// \param Align - The alignment for the variable, or 0. |
| 863 | /// \param AddToUsed - Whether the variable should be added to |
Daniel Dunbar | c158306 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 864 | /// "llvm.used". |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 865 | llvm::GlobalVariable *CreateMetadataVar(const std::string &Name, |
| 866 | llvm::Constant *Init, |
| 867 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 868 | unsigned Align, |
| 869 | bool AddToUsed); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 870 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 871 | /// GetNamedIvarList - Return the list of ivars in the interface |
| 872 | /// itself (not including super classes and not including unnamed |
| 873 | /// bitfields). |
| 874 | /// |
| 875 | /// For the non-fragile ABI, this also includes synthesized property |
| 876 | /// ivars. |
| 877 | void GetNamedIvarList(const ObjCInterfaceDecl *OID, |
| 878 | llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const; |
| 879 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 880 | public: |
| 881 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm) |
| 882 | { } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 883 | |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 884 | virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 885 | |
| 886 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 887 | const ObjCContainerDecl *CD=0); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 888 | |
| 889 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
| 890 | |
| 891 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 892 | /// declaration, emitting it if necessary. The return value has type |
| 893 | /// ProtocolPtrTy. |
| 894 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
| 895 | |
| 896 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 897 | /// object for the given declaration, emitting it if needed. These |
| 898 | /// forward references will be filled in with empty bodies if no |
| 899 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 900 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 901 | }; |
| 902 | |
| 903 | class CGObjCMac : public CGObjCCommonMac { |
| 904 | private: |
| 905 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 906 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 907 | /// level information. |
| 908 | void EmitImageInfo(); |
| 909 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 910 | /// EmitModuleInfo - Another marker encoding module level |
| 911 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 912 | void EmitModuleInfo(); |
| 913 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 914 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 915 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 916 | llvm::Constant *EmitModuleSymbols(); |
| 917 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 918 | /// FinishModule - Write out global data structures at the end of |
| 919 | /// processing a translation unit. |
| 920 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 921 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 922 | /// EmitClassExtension - Generate the class extension structure used |
| 923 | /// to store the weak ivar layout and properties. The return value |
| 924 | /// has type ClassExtensionPtrTy. |
| 925 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 926 | |
| 927 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 928 | /// for the given class. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 929 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 930 | const ObjCInterfaceDecl *ID); |
| 931 | |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 932 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 933 | QualType ResultType, |
| 934 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 935 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 936 | QualType Arg0Ty, |
| 937 | bool IsSuper, |
| 938 | const CallArgList &CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 939 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 940 | /// EmitIvarList - Emit the ivar list for the given |
| 941 | /// implementation. If ForClass is true the list of class ivars |
| 942 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 943 | /// interface ivars will be emitted. The return value has type |
| 944 | /// IvarListPtrTy. |
| 945 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 946 | bool ForClass); |
| 947 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 948 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 949 | /// for the metaclass of the given interface. The return value has |
| 950 | /// type ClassPtrTy. |
| 951 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 952 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 953 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 954 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 955 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 956 | llvm::Constant *Protocols, |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 957 | const llvm::Type *InterfaceTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 958 | const ConstantVector &Methods); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 959 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 960 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 961 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 962 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 963 | |
| 964 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 965 | /// implementation. The return value has type MethodListPtrTy. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 966 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 967 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 968 | const ConstantVector &Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 969 | |
| 970 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 971 | /// method declarations. |
| 972 | /// - TypeName: The name for the type containing the methods. |
| 973 | /// - IsProtocol: True iff these methods are for a protocol. |
| 974 | /// - ClassMethds: True iff these are class methods. |
| 975 | /// - Required: When true, only "required" methods are |
| 976 | /// listed. Similarly, when false only "optional" methods are |
| 977 | /// listed. For classes this should always be true. |
| 978 | /// - begin, end: The method list to output. |
| 979 | /// |
| 980 | /// The return value has type MethodDescriptionListPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 981 | llvm::Constant *EmitMethodDescList(const std::string &Name, |
| 982 | const char *Section, |
| 983 | const ConstantVector &Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 984 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 985 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 986 | /// declaration, emitting it if necessary. The return value has type |
| 987 | /// ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 988 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 989 | |
| 990 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 991 | /// object for the given declaration, emitting it if needed. These |
| 992 | /// forward references will be filled in with empty bodies if no |
| 993 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 994 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 995 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 996 | /// EmitProtocolExtension - Generate the protocol extension |
| 997 | /// structure used to store optional instance and class methods, and |
| 998 | /// protocol properties. The return value has type |
| 999 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1000 | llvm::Constant * |
| 1001 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1002 | const ConstantVector &OptInstanceMethods, |
| 1003 | const ConstantVector &OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1004 | |
| 1005 | /// EmitProtocolList - Generate the list of referenced |
| 1006 | /// protocols. The return value has type ProtocolListPtrTy. |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1007 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 1008 | ObjCProtocolDecl::protocol_iterator begin, |
| 1009 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1010 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1011 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1012 | /// for the given selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1013 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1014 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1015 | public: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1016 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1017 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1018 | virtual llvm::Function *ModuleInitFunction(); |
| 1019 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1020 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1021 | QualType ResultType, |
| 1022 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1023 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1024 | bool IsClassMessage, |
| 1025 | const CallArgList &CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1026 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1027 | virtual CodeGen::RValue |
| 1028 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1029 | QualType ResultType, |
| 1030 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1031 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1032 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1033 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1034 | bool IsClassMessage, |
| 1035 | const CallArgList &CallArgs); |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1036 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1037 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1038 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1039 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1040 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1041 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1042 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1043 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1044 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1045 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1046 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1047 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1048 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1049 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 1050 | virtual llvm::Constant *GetPropertySetFunction(); |
| 1051 | virtual llvm::Constant *EnumerationMutationFunction(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1052 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 1053 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1054 | const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1055 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 1056 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1057 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 1058 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1059 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 1060 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1061 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 1062 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 1063 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 1064 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1065 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 1066 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 1067 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1068 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1069 | QualType ObjectTy, |
| 1070 | llvm::Value *BaseValue, |
| 1071 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1072 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1073 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1074 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1075 | const ObjCIvarDecl *Ivar); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1076 | }; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1077 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1078 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1079 | private: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1080 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1081 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 1082 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1083 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1084 | /// SuperClassReferences - uniqued super class references. |
| 1085 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; |
| 1086 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1087 | /// MetaClassReferences - uniqued meta class references. |
| 1088 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1089 | |
| 1090 | /// EHTypeReferences - uniqued class ehtype references. |
| 1091 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1092 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1093 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 1094 | /// processing a translation unit. |
| 1095 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1096 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 1097 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 1098 | unsigned InstanceStart, |
| 1099 | unsigned InstanceSize, |
| 1100 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1101 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
| 1102 | llvm::Constant *IsAGV, |
| 1103 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 1104 | llvm::Constant *ClassRoGV, |
| 1105 | bool HiddenVisibility); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1106 | |
| 1107 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
| 1108 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 1109 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
| 1110 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1111 | /// EmitMethodList - Emit the method list for the given |
| 1112 | /// implementation. The return value has type MethodListnfABITy. |
| 1113 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 1114 | const char *Section, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 1115 | const ConstantVector &Methods); |
| 1116 | /// EmitIvarList - Emit the ivar list for the given |
| 1117 | /// implementation. If ForClass is true the list of class ivars |
| 1118 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1119 | /// interface ivars will be emitted. The return value has type |
| 1120 | /// IvarListnfABIPtrTy. |
| 1121 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 1122 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1123 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 1124 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 1125 | unsigned long int offset); |
| 1126 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1127 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1128 | /// declaration, emitting it if necessary. The return value has type |
| 1129 | /// ProtocolPtrTy. |
| 1130 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
| 1131 | |
| 1132 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1133 | /// object for the given declaration, emitting it if needed. These |
| 1134 | /// forward references will be filled in with empty bodies if no |
| 1135 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1136 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
| 1137 | |
| 1138 | /// EmitProtocolList - Generate the list of referenced |
| 1139 | /// protocols. The return value has type ProtocolListPtrTy. |
| 1140 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 1141 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1142 | ObjCProtocolDecl::protocol_iterator end); |
| 1143 | |
| 1144 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1145 | QualType ResultType, |
| 1146 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 1147 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1148 | QualType Arg0Ty, |
| 1149 | bool IsSuper, |
| 1150 | const CallArgList &CallArgs); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 1151 | |
| 1152 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1153 | /// class of the given name. |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 1154 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name); |
| 1155 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1156 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1157 | /// for the given class reference. |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1158 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1159 | const ObjCInterfaceDecl *ID); |
| 1160 | |
| 1161 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1162 | /// for the given super class reference. |
| 1163 | llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder, |
| 1164 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1165 | |
| 1166 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 1167 | /// meta-data |
| 1168 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
| 1169 | const ObjCInterfaceDecl *ID); |
| 1170 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1171 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 1172 | /// the given ivar. |
| 1173 | /// |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 1174 | llvm::GlobalVariable * ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1175 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1176 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1177 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1178 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1179 | /// for the given selector. |
| 1180 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1181 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1182 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1183 | /// interface. The return value has type EHTypePtrTy. |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1184 | llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 1185 | bool ForDefinition); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1186 | |
| 1187 | const char *getMetaclassSymbolPrefix() const { |
| 1188 | return "OBJC_METACLASS_$_"; |
| 1189 | } |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1190 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1191 | const char *getClassSymbolPrefix() const { |
| 1192 | return "OBJC_CLASS_$_"; |
| 1193 | } |
| 1194 | |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1195 | void GetClassSizeInfo(const ObjCInterfaceDecl *OID, |
| 1196 | uint32_t &InstanceStart, |
| 1197 | uint32_t &InstanceSize); |
| 1198 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1199 | public: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1200 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1201 | // FIXME. All stubs for now! |
| 1202 | virtual llvm::Function *ModuleInitFunction(); |
| 1203 | |
| 1204 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1205 | QualType ResultType, |
| 1206 | Selector Sel, |
| 1207 | llvm::Value *Receiver, |
| 1208 | bool IsClassMessage, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1209 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1210 | |
| 1211 | virtual CodeGen::RValue |
| 1212 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 1213 | QualType ResultType, |
| 1214 | Selector Sel, |
| 1215 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1216 | bool isCategoryImpl, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1217 | llvm::Value *Receiver, |
| 1218 | bool IsClassMessage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1219 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1220 | |
| 1221 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1222 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1223 | |
| 1224 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel) |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1225 | { return EmitSelector(Builder, Sel); } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1226 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 1227 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1228 | |
| 1229 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1230 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 1231 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1232 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1233 | virtual llvm::Constant *GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1234 | return ObjCTypes.getGetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1235 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1236 | virtual llvm::Constant *GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1237 | return ObjCTypes.getSetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1238 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1239 | virtual llvm::Constant *EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1240 | return ObjCTypes.getEnumerationMutationFn(); |
Daniel Dunbar | 28ed084 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 1241 | } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1242 | |
| 1243 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 1244 | const Stmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1245 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 1246 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1247 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1248 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1249 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1250 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1251 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1252 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1253 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1254 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1255 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1256 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1257 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1258 | QualType ObjectTy, |
| 1259 | llvm::Value *BaseValue, |
| 1260 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1261 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1262 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1263 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1264 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1265 | }; |
| 1266 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1267 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1268 | |
| 1269 | /* *** Helper Functions *** */ |
| 1270 | |
| 1271 | /// getConstantGEP() - Help routine to construct simple GEPs. |
| 1272 | static llvm::Constant *getConstantGEP(llvm::Constant *C, |
| 1273 | unsigned idx0, |
| 1274 | unsigned idx1) { |
| 1275 | llvm::Value *Idxs[] = { |
| 1276 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0), |
| 1277 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1) |
| 1278 | }; |
| 1279 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2); |
| 1280 | } |
| 1281 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1282 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 1283 | /// class has the __objc_exception__ attribute. |
| 1284 | static bool hasObjCExceptionAttribute(const ObjCInterfaceDecl *OID) { |
Daniel Dunbar | b11fa0d | 2009-04-13 21:08:27 +0000 | [diff] [blame] | 1285 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1286 | return true; |
| 1287 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
| 1288 | return hasObjCExceptionAttribute(Super); |
| 1289 | return false; |
| 1290 | } |
| 1291 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1292 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1293 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1294 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
| 1295 | ObjCTypes(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1296 | { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1297 | ObjCABI = 1; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1298 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1301 | /// GetClass - Return a reference to the class for the given interface |
| 1302 | /// decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1303 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1304 | const ObjCInterfaceDecl *ID) { |
| 1305 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1309 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 1310 | return EmitSelector(Builder, Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1313 | /// Generate a constant CFString object. |
| 1314 | /* |
| 1315 | struct __builtin_CFString { |
| 1316 | const int *isa; // point to __CFConstantStringClassReference |
| 1317 | int flags; |
| 1318 | const char *str; |
| 1319 | long length; |
| 1320 | }; |
| 1321 | */ |
| 1322 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1323 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 1324 | const ObjCStringLiteral *SL) { |
Steve Naroff | 8d4141f | 2009-04-01 13:55:36 +0000 | [diff] [blame] | 1325 | return CGM.GetAddrOfConstantCFString(SL->getString()); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | /// Generates a message send where the super is the receiver. This is |
| 1329 | /// a message send to self with special delivery semantics indicating |
| 1330 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1331 | CodeGen::RValue |
| 1332 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1333 | QualType ResultType, |
| 1334 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1335 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1336 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1337 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1338 | bool IsClassMessage, |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1339 | const CodeGen::CallArgList &CallArgs) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1340 | // Create and init a super structure; this is a (receiver, class) |
| 1341 | // pair we will pass to objc_msgSendSuper. |
| 1342 | llvm::Value *ObjCSuper = |
| 1343 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 1344 | llvm::Value *ReceiverAsObject = |
| 1345 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 1346 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 1347 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1348 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1349 | // If this is a class message the metaclass is passed as the target. |
| 1350 | llvm::Value *Target; |
| 1351 | if (IsClassMessage) { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1352 | if (isCategoryImpl) { |
| 1353 | // Message sent to 'super' in a class method defined in a category |
| 1354 | // implementation requires an odd treatment. |
| 1355 | // If we are in a class method, we must retrieve the |
| 1356 | // _metaclass_ for the current class, pointed at by |
| 1357 | // the class's "isa" pointer. The following assumes that |
| 1358 | // isa" is the first ivar in a class (which it must be). |
| 1359 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1360 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 1361 | Target = CGF.Builder.CreateLoad(Target); |
| 1362 | } |
| 1363 | else { |
| 1364 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 1365 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 1366 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 1367 | Target = Super; |
| 1368 | } |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1369 | } else { |
| 1370 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1371 | } |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1372 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 1373 | // and ObjCTypes types. |
| 1374 | const llvm::Type *ClassTy = |
| 1375 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1376 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1377 | CGF.Builder.CreateStore(Target, |
| 1378 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 1379 | |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1380 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1381 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 1382 | true, CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1383 | } |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1384 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1385 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1386 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1387 | QualType ResultType, |
| 1388 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1389 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1390 | bool IsClassMessage, |
| 1391 | const CallArgList &CallArgs) { |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1392 | llvm::Value *Arg0 = |
| 1393 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1394 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1395 | Arg0, CGF.getContext().getObjCIdType(), |
| 1396 | false, CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1400 | QualType ResultType, |
| 1401 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1402 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1403 | QualType Arg0Ty, |
| 1404 | bool IsSuper, |
| 1405 | const CallArgList &CallArgs) { |
| 1406 | CallArgList ActualArgs; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1407 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 1408 | ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder, |
| 1409 | Sel)), |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1410 | CGF.getContext().getObjCSelType())); |
| 1411 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1412 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1413 | CodeGenTypes &Types = CGM.getTypes(); |
| 1414 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); |
| 1415 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1416 | |
| 1417 | llvm::Constant *Fn; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1418 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1419 | Fn = ObjCTypes.getSendStretFn(IsSuper); |
| 1420 | } else if (ResultType->isFloatingType()) { |
| 1421 | // FIXME: Sadly, this is wrong. This actually depends on the |
| 1422 | // architecture. This happens to be right for x86-32 though. |
| 1423 | Fn = ObjCTypes.getSendFpretFn(IsSuper); |
| 1424 | } else { |
| 1425 | Fn = ObjCTypes.getSendFn(IsSuper); |
| 1426 | } |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 1427 | Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy)); |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1428 | return CGF.EmitCall(FnInfo, Fn, ActualArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1431 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1432 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1433 | // FIXME: I don't understand why gcc generates this, or where it is |
| 1434 | // resolved. Investigate. Its also wasteful to look this up over and |
| 1435 | // over. |
| 1436 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1437 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1438 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
| 1439 | ObjCTypes.ExternalProtocolPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1442 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1443 | // FIXME: We shouldn't need this, the protocol decl should contain |
| 1444 | // enough information to tell us whether this was a declaration or a |
| 1445 | // definition. |
| 1446 | DefinedProtocols.insert(PD->getIdentifier()); |
| 1447 | |
| 1448 | // If we have generated a forward reference to this protocol, emit |
| 1449 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 1450 | // emitted. |
| 1451 | if (Protocols.count(PD->getIdentifier())) |
| 1452 | GetOrEmitProtocol(PD); |
| 1453 | } |
| 1454 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1455 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1456 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 1457 | return GetOrEmitProtocol(PD); |
| 1458 | return GetOrEmitProtocolRef(PD); |
| 1459 | } |
| 1460 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1461 | /* |
| 1462 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 1463 | struct _objc_protocol { |
| 1464 | struct _objc_protocol_extension *isa; |
| 1465 | char *protocol_name; |
| 1466 | struct _objc_protocol_list *protocol_list; |
| 1467 | struct _objc__method_prototype_list *instance_methods; |
| 1468 | struct _objc__method_prototype_list *class_methods |
| 1469 | }; |
| 1470 | |
| 1471 | See EmitProtocolExtension(). |
| 1472 | */ |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1473 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
| 1474 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1475 | |
| 1476 | // Early exit if a defining object has already been generated. |
| 1477 | if (Entry && Entry->hasInitializer()) |
| 1478 | return Entry; |
| 1479 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1480 | // FIXME: I don't understand why gcc generates this, or where it is |
| 1481 | // resolved. Investigate. Its also wasteful to look this up over and |
| 1482 | // over. |
| 1483 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1484 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1485 | const char *ProtocolName = PD->getNameAsCString(); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1486 | |
| 1487 | // Construct method lists. |
| 1488 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1489 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1490 | for (ObjCProtocolDecl::instmeth_iterator |
| 1491 | i = PD->instmeth_begin(CGM.getContext()), |
| 1492 | e = PD->instmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1493 | ObjCMethodDecl *MD = *i; |
| 1494 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1495 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1496 | OptInstanceMethods.push_back(C); |
| 1497 | } else { |
| 1498 | InstanceMethods.push_back(C); |
| 1499 | } |
| 1500 | } |
| 1501 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1502 | for (ObjCProtocolDecl::classmeth_iterator |
| 1503 | i = PD->classmeth_begin(CGM.getContext()), |
| 1504 | e = PD->classmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1505 | ObjCMethodDecl *MD = *i; |
| 1506 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1507 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1508 | OptClassMethods.push_back(C); |
| 1509 | } else { |
| 1510 | ClassMethods.push_back(C); |
| 1511 | } |
| 1512 | } |
| 1513 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1514 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1515 | Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1516 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1517 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1518 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1519 | PD->protocol_begin(), |
| 1520 | PD->protocol_end()); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1521 | Values[3] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1522 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" |
| 1523 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1524 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1525 | InstanceMethods); |
| 1526 | Values[4] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1527 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" |
| 1528 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1529 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1530 | ClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1531 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 1532 | Values); |
| 1533 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1534 | if (Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1535 | // Already created, fix the linkage and update the initializer. |
| 1536 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1537 | Entry->setInitializer(Init); |
| 1538 | } else { |
| 1539 | Entry = |
| 1540 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
| 1541 | llvm::GlobalValue::InternalLinkage, |
| 1542 | Init, |
| 1543 | std::string("\01L_OBJC_PROTOCOL_")+ProtocolName, |
| 1544 | &CGM.getModule()); |
| 1545 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1546 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1547 | UsedGlobals.push_back(Entry); |
| 1548 | // FIXME: Is this necessary? Why only for protocol? |
| 1549 | Entry->setAlignment(4); |
| 1550 | } |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1551 | |
| 1552 | return Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1555 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1556 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1557 | |
| 1558 | if (!Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1559 | // We use the initializer as a marker of whether this is a forward |
| 1560 | // reference or not. At module finalization we add the empty |
| 1561 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1562 | Entry = |
| 1563 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1564 | llvm::GlobalValue::ExternalLinkage, |
| 1565 | 0, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1566 | "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1567 | &CGM.getModule()); |
| 1568 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1569 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1570 | UsedGlobals.push_back(Entry); |
| 1571 | // FIXME: Is this necessary? Why only for protocol? |
| 1572 | Entry->setAlignment(4); |
| 1573 | } |
| 1574 | |
| 1575 | return Entry; |
| 1576 | } |
| 1577 | |
| 1578 | /* |
| 1579 | struct _objc_protocol_extension { |
| 1580 | uint32_t size; |
| 1581 | struct objc_method_description_list *optional_instance_methods; |
| 1582 | struct objc_method_description_list *optional_class_methods; |
| 1583 | struct objc_property_list *instance_properties; |
| 1584 | }; |
| 1585 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1586 | llvm::Constant * |
| 1587 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1588 | const ConstantVector &OptInstanceMethods, |
| 1589 | const ConstantVector &OptClassMethods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1590 | uint64_t Size = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1591 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1592 | std::vector<llvm::Constant*> Values(4); |
| 1593 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1594 | Values[1] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1595 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
| 1596 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1597 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1598 | OptInstanceMethods); |
| 1599 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1600 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" |
| 1601 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1602 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1603 | OptClassMethods); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1604 | Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + |
| 1605 | PD->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1606 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1607 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1608 | // Return null if no extension bits are used. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1609 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
| 1610 | Values[3]->isNullValue()) |
| 1611 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 1612 | |
| 1613 | llvm::Constant *Init = |
| 1614 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1615 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1616 | // No special section, but goes in llvm.used |
| 1617 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(), |
| 1618 | Init, |
| 1619 | 0, 0, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | /* |
| 1623 | struct objc_protocol_list { |
| 1624 | struct objc_protocol_list *next; |
| 1625 | long count; |
| 1626 | Protocol *list[]; |
| 1627 | }; |
| 1628 | */ |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1629 | llvm::Constant * |
| 1630 | CGObjCMac::EmitProtocolList(const std::string &Name, |
| 1631 | ObjCProtocolDecl::protocol_iterator begin, |
| 1632 | ObjCProtocolDecl::protocol_iterator end) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1633 | std::vector<llvm::Constant*> ProtocolRefs; |
| 1634 | |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1635 | for (; begin != end; ++begin) |
| 1636 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1637 | |
| 1638 | // Just return null for empty protocol lists |
| 1639 | if (ProtocolRefs.empty()) |
| 1640 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1641 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1642 | // This list is null terminated. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1643 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
| 1644 | |
| 1645 | std::vector<llvm::Constant*> Values(3); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1646 | // This field is only used by the runtime. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1647 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1648 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 1649 | Values[2] = |
| 1650 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 1651 | ProtocolRefs.size()), |
| 1652 | ProtocolRefs); |
| 1653 | |
| 1654 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1655 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1656 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1657 | 4, false); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1658 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
| 1659 | } |
| 1660 | |
| 1661 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1662 | struct _objc_property { |
| 1663 | const char * const name; |
| 1664 | const char * const attributes; |
| 1665 | }; |
| 1666 | |
| 1667 | struct _objc_property_list { |
| 1668 | uint32_t entsize; // sizeof (struct _objc_property) |
| 1669 | uint32_t prop_count; |
| 1670 | struct _objc_property[prop_count]; |
| 1671 | }; |
| 1672 | */ |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1673 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name, |
| 1674 | const Decl *Container, |
| 1675 | const ObjCContainerDecl *OCD, |
| 1676 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1677 | std::vector<llvm::Constant*> Properties, Prop(2); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1678 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(CGM.getContext()), |
| 1679 | E = OCD->prop_end(CGM.getContext()); I != E; ++I) { |
Steve Naroff | 93983f8 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 1680 | const ObjCPropertyDecl *PD = *I; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1681 | Prop[0] = GetPropertyName(PD->getIdentifier()); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1682 | Prop[1] = GetPropertyTypeString(PD, Container); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1683 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
| 1684 | Prop)); |
| 1685 | } |
| 1686 | |
| 1687 | // Return null for empty list. |
| 1688 | if (Properties.empty()) |
| 1689 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1690 | |
| 1691 | unsigned PropertySize = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1692 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1693 | std::vector<llvm::Constant*> Values(3); |
| 1694 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 1695 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
| 1696 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
| 1697 | Properties.size()); |
| 1698 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
| 1699 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1700 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1701 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1702 | CreateMetadataVar(Name, Init, |
| 1703 | (ObjCABI == 2) ? "__DATA, __objc_const" : |
| 1704 | "__OBJC,__property,regular,no_dead_strip", |
| 1705 | (ObjCABI == 2) ? 8 : 4, |
| 1706 | true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1707 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1711 | struct objc_method_description_list { |
| 1712 | int count; |
| 1713 | struct objc_method_description list[]; |
| 1714 | }; |
| 1715 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1716 | llvm::Constant * |
| 1717 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 1718 | std::vector<llvm::Constant*> Desc(2); |
| 1719 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1720 | ObjCTypes.SelectorPtrTy); |
| 1721 | Desc[1] = GetMethodVarType(MD); |
| 1722 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
| 1723 | Desc); |
| 1724 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1725 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1726 | llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name, |
| 1727 | const char *Section, |
| 1728 | const ConstantVector &Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1729 | // Return null for empty list. |
| 1730 | if (Methods.empty()) |
| 1731 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 1732 | |
| 1733 | std::vector<llvm::Constant*> Values(2); |
| 1734 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 1735 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
| 1736 | Methods.size()); |
| 1737 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
| 1738 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1739 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1740 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1741 | return llvm::ConstantExpr::getBitCast(GV, |
| 1742 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1745 | /* |
| 1746 | struct _objc_category { |
| 1747 | char *category_name; |
| 1748 | char *class_name; |
| 1749 | struct _objc_method_list *instance_methods; |
| 1750 | struct _objc_method_list *class_methods; |
| 1751 | struct _objc_protocol_list *protocols; |
| 1752 | uint32_t size; // <rdar://4585769> |
| 1753 | struct _objc_property_list *instance_properties; |
| 1754 | }; |
| 1755 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1756 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1757 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1758 | |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1759 | // FIXME: This is poor design, the OCD should have a pointer to the |
| 1760 | // category decl. Additionally, note that Category can be null for |
| 1761 | // the @implementation w/o an @interface case. Sema should just |
| 1762 | // create one for us as it does for @implementation so everyone else |
| 1763 | // can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1764 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1765 | const ObjCCategoryDecl *Category = |
| 1766 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1767 | std::string ExtName(Interface->getNameAsString() + "_" + |
| 1768 | OCD->getNameAsString()); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1769 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1770 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1771 | for (ObjCCategoryImplDecl::instmeth_iterator |
| 1772 | i = OCD->instmeth_begin(CGM.getContext()), |
| 1773 | e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1774 | // Instance methods should always be defined. |
| 1775 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1776 | } |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1777 | for (ObjCCategoryImplDecl::classmeth_iterator |
| 1778 | i = OCD->classmeth_begin(CGM.getContext()), |
| 1779 | e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1780 | // Class methods should always be defined. |
| 1781 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1782 | } |
| 1783 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1784 | std::vector<llvm::Constant*> Values(7); |
| 1785 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 1786 | Values[1] = GetClassName(Interface->getIdentifier()); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1787 | Values[2] = |
| 1788 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") + |
| 1789 | ExtName, |
| 1790 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1791 | InstanceMethods); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1792 | Values[3] = |
| 1793 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName, |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1794 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1795 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1796 | if (Category) { |
| 1797 | Values[4] = |
| 1798 | EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName, |
| 1799 | Category->protocol_begin(), |
| 1800 | Category->protocol_end()); |
| 1801 | } else { |
| 1802 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1803 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1804 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1805 | |
| 1806 | // If there is no category @interface then there can be no properties. |
| 1807 | if (Category) { |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1808 | Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1809 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1810 | } else { |
| 1811 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1812 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1813 | |
| 1814 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
| 1815 | Values); |
| 1816 | |
| 1817 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1818 | CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init, |
| 1819 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1820 | 4, true); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1821 | DefinedCategories.push_back(GV); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1822 | } |
| 1823 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1824 | // FIXME: Get from somewhere? |
| 1825 | enum ClassFlags { |
| 1826 | eClassFlags_Factory = 0x00001, |
| 1827 | eClassFlags_Meta = 0x00002, |
| 1828 | // <rdr://5142207> |
| 1829 | eClassFlags_HasCXXStructors = 0x02000, |
| 1830 | eClassFlags_Hidden = 0x20000, |
| 1831 | eClassFlags_ABI2_Hidden = 0x00010, |
| 1832 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 1833 | }; |
| 1834 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1835 | /* |
| 1836 | struct _objc_class { |
| 1837 | Class isa; |
| 1838 | Class super_class; |
| 1839 | const char *name; |
| 1840 | long version; |
| 1841 | long info; |
| 1842 | long instance_size; |
| 1843 | struct _objc_ivar_list *ivars; |
| 1844 | struct _objc_method_list *methods; |
| 1845 | struct _objc_cache *cache; |
| 1846 | struct _objc_protocol_list *protocols; |
| 1847 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 1848 | const char *ivar_layout; |
| 1849 | struct _objc_class_ext *ext; |
| 1850 | }; |
| 1851 | |
| 1852 | See EmitClassExtension(); |
| 1853 | */ |
| 1854 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1855 | DefinedSymbols.insert(ID->getIdentifier()); |
| 1856 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1857 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1858 | // FIXME: Gross |
| 1859 | ObjCInterfaceDecl *Interface = |
| 1860 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1861 | llvm::Constant *Protocols = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1862 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1863 | Interface->protocol_begin(), |
| 1864 | Interface->protocol_end()); |
Daniel Dunbar | 84ad77a | 2009-04-22 09:39:34 +0000 | [diff] [blame] | 1865 | const llvm::Type *InterfaceTy = GetConcreteClassStruct(CGM, Interface); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1866 | unsigned Flags = eClassFlags_Factory; |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1867 | unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1868 | |
| 1869 | // FIXME: Set CXX-structors flag. |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1870 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1871 | Flags |= eClassFlags_Hidden; |
| 1872 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1873 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1874 | for (ObjCImplementationDecl::instmeth_iterator |
| 1875 | i = ID->instmeth_begin(CGM.getContext()), |
| 1876 | e = ID->instmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1877 | // Instance methods should always be defined. |
| 1878 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1879 | } |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1880 | for (ObjCImplementationDecl::classmeth_iterator |
| 1881 | i = ID->classmeth_begin(CGM.getContext()), |
| 1882 | e = ID->classmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1883 | // Class methods should always be defined. |
| 1884 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1885 | } |
| 1886 | |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1887 | for (ObjCImplementationDecl::propimpl_iterator |
| 1888 | i = ID->propimpl_begin(CGM.getContext()), |
| 1889 | e = ID->propimpl_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1890 | ObjCPropertyImplDecl *PID = *i; |
| 1891 | |
| 1892 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 1893 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 1894 | |
| 1895 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 1896 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1897 | InstanceMethods.push_back(C); |
| 1898 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 1899 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1900 | InstanceMethods.push_back(C); |
| 1901 | } |
| 1902 | } |
| 1903 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1904 | std::vector<llvm::Constant*> Values(12); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1905 | Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1906 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1907 | // Record a reference to the super class. |
| 1908 | LazySymbols.insert(Super->getIdentifier()); |
| 1909 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1910 | Values[ 1] = |
| 1911 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1912 | ObjCTypes.ClassPtrTy); |
| 1913 | } else { |
| 1914 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1915 | } |
| 1916 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1917 | // Version is always 0. |
| 1918 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1919 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1920 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1921 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1922 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1923 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1924 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1925 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1926 | // cache is always NULL. |
| 1927 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1928 | Values[ 9] = Protocols; |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 1929 | Values[10] = BuildIvarLayout(ID, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1930 | Values[11] = EmitClassExtension(ID); |
| 1931 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1932 | Values); |
| 1933 | |
| 1934 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1935 | CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init, |
| 1936 | "__OBJC,__class,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1937 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1938 | DefinedClasses.push_back(GV); |
| 1939 | } |
| 1940 | |
| 1941 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1942 | llvm::Constant *Protocols, |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1943 | const llvm::Type *InterfaceTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1944 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1945 | unsigned Flags = eClassFlags_Meta; |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1946 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1947 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1948 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1949 | Flags |= eClassFlags_Hidden; |
| 1950 | |
| 1951 | std::vector<llvm::Constant*> Values(12); |
| 1952 | // The isa for the metaclass is the root of the hierarchy. |
| 1953 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 1954 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 1955 | Root = Super; |
| 1956 | Values[ 0] = |
| 1957 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
| 1958 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1959 | // The super class for the metaclass is emitted as the name of the |
| 1960 | // super class. The runtime fixes this up to point to the |
| 1961 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1962 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
| 1963 | Values[ 1] = |
| 1964 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1965 | ObjCTypes.ClassPtrTy); |
| 1966 | } else { |
| 1967 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1968 | } |
| 1969 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1970 | // Version is always 0. |
| 1971 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1972 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1973 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1974 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1975 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1976 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1977 | "__OBJC,__cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1978 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1979 | // cache is always NULL. |
| 1980 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1981 | Values[ 9] = Protocols; |
| 1982 | // ivar_layout for metaclass is always NULL. |
| 1983 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 1984 | // The class extension is always unused for metaclasses. |
| 1985 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 1986 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1987 | Values); |
| 1988 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1989 | std::string Name("\01L_OBJC_METACLASS_"); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1990 | Name += ID->getNameAsCString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1991 | |
| 1992 | // Check for a forward reference. |
| 1993 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 1994 | if (GV) { |
| 1995 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 1996 | "Forward metaclass reference has incorrect type."); |
| 1997 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 1998 | GV->setInitializer(Init); |
| 1999 | } else { |
| 2000 | GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 2001 | llvm::GlobalValue::InternalLinkage, |
| 2002 | Init, Name, |
| 2003 | &CGM.getModule()); |
| 2004 | } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2005 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2006 | GV->setAlignment(4); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2007 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2008 | |
| 2009 | return GV; |
| 2010 | } |
| 2011 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2012 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2013 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2014 | |
| 2015 | // FIXME: Should we look these up somewhere other than the |
| 2016 | // module. Its a bit silly since we only generate these while |
| 2017 | // processing an implementation, so exactly one pointer would work |
| 2018 | // if know when we entered/exitted an implementation block. |
| 2019 | |
| 2020 | // Check for an existing forward reference. |
Fariborz Jahanian | b0d2794 | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 2021 | // Previously, metaclass with internal linkage may have been defined. |
| 2022 | // pass 'true' as 2nd argument so it is returned. |
| 2023 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) { |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2024 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2025 | "Forward metaclass reference has incorrect type."); |
| 2026 | return GV; |
| 2027 | } else { |
| 2028 | // Generate as an external reference to keep a consistent |
| 2029 | // module. This will be patched up when we emit the metaclass. |
| 2030 | return new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 2031 | llvm::GlobalValue::ExternalLinkage, |
| 2032 | 0, |
| 2033 | Name, |
| 2034 | &CGM.getModule()); |
| 2035 | } |
| 2036 | } |
| 2037 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2038 | /* |
| 2039 | struct objc_class_ext { |
| 2040 | uint32_t size; |
| 2041 | const char *weak_ivar_layout; |
| 2042 | struct _objc_property_list *properties; |
| 2043 | }; |
| 2044 | */ |
| 2045 | llvm::Constant * |
| 2046 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
| 2047 | uint64_t Size = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 2048 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2049 | |
| 2050 | std::vector<llvm::Constant*> Values(3); |
| 2051 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 2052 | Values[1] = BuildIvarLayout(ID, false); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2053 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2054 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2055 | |
| 2056 | // Return null if no extension bits are used. |
| 2057 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
| 2058 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 2059 | |
| 2060 | llvm::Constant *Init = |
| 2061 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2062 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2063 | Init, "__OBJC,__class_ext,regular,no_dead_strip", |
| 2064 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 2067 | /// getInterfaceDeclForIvar - Get the interface declaration node where |
| 2068 | /// this ivar is declared in. |
| 2069 | /// FIXME. Ideally, this info should be in the ivar node. But currently |
| 2070 | /// it is not and prevailing wisdom is that ASTs should not have more |
| 2071 | /// info than is absolutely needed, even though this info reflects the |
| 2072 | /// source language. |
| 2073 | /// |
| 2074 | static const ObjCInterfaceDecl *getInterfaceDeclForIvar( |
| 2075 | const ObjCInterfaceDecl *OI, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2076 | const ObjCIvarDecl *IVD, |
| 2077 | ASTContext &Context) { |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 2078 | if (!OI) |
| 2079 | return 0; |
| 2080 | assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface"); |
| 2081 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 2082 | E = OI->ivar_end(); I != E; ++I) |
| 2083 | if ((*I)->getIdentifier() == IVD->getIdentifier()) |
| 2084 | return OI; |
Fariborz Jahanian | 5a4b453 | 2009-03-31 17:00:52 +0000 | [diff] [blame] | 2085 | // look into properties. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2086 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context), |
| 2087 | E = OI->prop_end(Context); I != E; ++I) { |
Fariborz Jahanian | 5a4b453 | 2009-03-31 17:00:52 +0000 | [diff] [blame] | 2088 | ObjCPropertyDecl *PDecl = (*I); |
| 2089 | if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl()) |
| 2090 | if (IV->getIdentifier() == IVD->getIdentifier()) |
| 2091 | return OI; |
| 2092 | } |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2093 | return getInterfaceDeclForIvar(OI->getSuperClass(), IVD, Context); |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2096 | /* |
| 2097 | struct objc_ivar { |
| 2098 | char *ivar_name; |
| 2099 | char *ivar_type; |
| 2100 | int ivar_offset; |
| 2101 | }; |
| 2102 | |
| 2103 | struct objc_ivar_list { |
| 2104 | int ivar_count; |
| 2105 | struct objc_ivar list[count]; |
| 2106 | }; |
| 2107 | */ |
| 2108 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2109 | bool ForClass) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2110 | std::vector<llvm::Constant*> Ivars, Ivar(3); |
| 2111 | |
| 2112 | // When emitting the root class GCC emits ivar entries for the |
| 2113 | // actual class structure. It is not clear if we need to follow this |
| 2114 | // behavior; for now lets try and get away with not doing it. If so, |
| 2115 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 2116 | // for the class. |
| 2117 | if (ForClass) |
| 2118 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2119 | |
| 2120 | ObjCInterfaceDecl *OID = |
| 2121 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2122 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 2123 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
| 2124 | GetNamedIvarList(OID, OIvars); |
| 2125 | |
| 2126 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 2127 | ObjCIvarDecl *IVD = OIvars[i]; |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 2128 | Ivar[0] = GetMethodVarName(IVD->getIdentifier()); |
| 2129 | Ivar[1] = GetMethodVarType(IVD); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 2130 | Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2131 | ComputeIvarBaseOffset(CGM, OID, IVD)); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2132 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | // Return null for empty list. |
| 2136 | if (Ivars.empty()) |
| 2137 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
| 2138 | |
| 2139 | std::vector<llvm::Constant*> Values(2); |
| 2140 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 2141 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
| 2142 | Ivars.size()); |
| 2143 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
| 2144 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2145 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2146 | llvm::GlobalVariable *GV; |
| 2147 | if (ForClass) |
| 2148 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(), |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2149 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
| 2150 | 4, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2151 | else |
| 2152 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" |
| 2153 | + ID->getNameAsString(), |
| 2154 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2155 | 4, true); |
| 2156 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
| 2159 | /* |
| 2160 | struct objc_method { |
| 2161 | SEL method_name; |
| 2162 | char *method_types; |
| 2163 | void *method; |
| 2164 | }; |
| 2165 | |
| 2166 | struct objc_method_list { |
| 2167 | struct objc_method_list *obsolete; |
| 2168 | int count; |
| 2169 | struct objc_method methods_list[count]; |
| 2170 | }; |
| 2171 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2172 | |
| 2173 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 2174 | /// given method if it has been defined. The result is null if the |
| 2175 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2176 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2177 | // FIXME: Use DenseMap::lookup |
| 2178 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 2179 | if (!Fn) |
| 2180 | return 0; |
| 2181 | |
| 2182 | std::vector<llvm::Constant*> Method(3); |
| 2183 | Method[0] = |
| 2184 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 2185 | ObjCTypes.SelectorPtrTy); |
| 2186 | Method[1] = GetMethodVarType(MD); |
| 2187 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 2188 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 2189 | } |
| 2190 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2191 | llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name, |
| 2192 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2193 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2194 | // Return null for empty list. |
| 2195 | if (Methods.empty()) |
| 2196 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
| 2197 | |
| 2198 | std::vector<llvm::Constant*> Values(3); |
| 2199 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 2200 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 2201 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 2202 | Methods.size()); |
| 2203 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 2204 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2205 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2206 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2207 | return llvm::ConstantExpr::getBitCast(GV, |
| 2208 | ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 2211 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 2212 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2213 | std::string Name; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 2214 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2215 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2216 | CodeGenTypes &Types = CGM.getTypes(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2217 | const llvm::FunctionType *MethodTy = |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2218 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2219 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2220 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2221 | llvm::GlobalValue::InternalLinkage, |
| 2222 | Name, |
| 2223 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2224 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2225 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2226 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2227 | } |
| 2228 | |
Daniel Dunbar | 48fa064 | 2009-04-19 02:03:42 +0000 | [diff] [blame] | 2229 | /// GetFieldBaseOffset - return the field's byte offset. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2230 | uint64_t CGObjCCommonMac::GetFieldBaseOffset(const ObjCInterfaceDecl *OI, |
| 2231 | const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 2232 | const FieldDecl *Field) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2233 | // Is this a C struct? |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 2234 | if (!OI) |
| 2235 | return Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2236 | return ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2239 | llvm::GlobalVariable * |
| 2240 | CGObjCCommonMac::CreateMetadataVar(const std::string &Name, |
| 2241 | llvm::Constant *Init, |
| 2242 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2243 | unsigned Align, |
| 2244 | bool AddToUsed) { |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2245 | const llvm::Type *Ty = Init->getType(); |
| 2246 | llvm::GlobalVariable *GV = |
| 2247 | new llvm::GlobalVariable(Ty, false, |
| 2248 | llvm::GlobalValue::InternalLinkage, |
| 2249 | Init, |
| 2250 | Name, |
| 2251 | &CGM.getModule()); |
| 2252 | if (Section) |
| 2253 | GV->setSection(Section); |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2254 | if (Align) |
| 2255 | GV->setAlignment(Align); |
| 2256 | if (AddToUsed) |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2257 | UsedGlobals.push_back(GV); |
| 2258 | return GV; |
| 2259 | } |
| 2260 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2261 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2262 | // Abuse this interface function as a place to finalize. |
| 2263 | FinishModule(); |
| 2264 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2265 | return NULL; |
| 2266 | } |
| 2267 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2268 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2269 | return ObjCTypes.getGetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2272 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2273 | return ObjCTypes.getSetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2274 | } |
| 2275 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2276 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2277 | return ObjCTypes.getEnumerationMutationFn(); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2278 | } |
| 2279 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2280 | /* |
| 2281 | |
| 2282 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 2283 | -- |
| 2284 | |
| 2285 | The basic framework for a @try-catch-finally is as follows: |
| 2286 | { |
| 2287 | objc_exception_data d; |
| 2288 | id _rethrow = null; |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2289 | bool _call_try_exit = true; |
| 2290 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2291 | objc_exception_try_enter(&d); |
| 2292 | if (!setjmp(d.jmp_buf)) { |
| 2293 | ... try body ... |
| 2294 | } else { |
| 2295 | // exception path |
| 2296 | id _caught = objc_exception_extract(&d); |
| 2297 | |
| 2298 | // enter new try scope for handlers |
| 2299 | if (!setjmp(d.jmp_buf)) { |
| 2300 | ... match exception and execute catch blocks ... |
| 2301 | |
| 2302 | // fell off end, rethrow. |
| 2303 | _rethrow = _caught; |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2304 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2305 | } else { |
| 2306 | // exception in catch block |
| 2307 | _rethrow = objc_exception_extract(&d); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2308 | _call_try_exit = false; |
| 2309 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2310 | } |
| 2311 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2312 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2313 | |
| 2314 | finally: |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2315 | if (_call_try_exit) |
| 2316 | objc_exception_try_exit(&d); |
| 2317 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2318 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2319 | ... dispatch to finally destination ... |
| 2320 | |
| 2321 | finally_rethrow: |
| 2322 | objc_exception_throw(_rethrow); |
| 2323 | |
| 2324 | finally_end: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2325 | } |
| 2326 | |
| 2327 | This framework differs slightly from the one gcc uses, in that gcc |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2328 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 2329 | and if the object should be rethrown. This breaks in the face of |
| 2330 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2331 | |
| 2332 | We specialize this framework for a few particular circumstances: |
| 2333 | |
| 2334 | - If there are no catch blocks, then we avoid emitting the second |
| 2335 | exception handling context. |
| 2336 | |
| 2337 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 2338 | e)) we avoid emitting the code to rethrow an uncaught exception. |
| 2339 | |
| 2340 | - FIXME: If there is no @finally block we can do a few more |
| 2341 | simplifications. |
| 2342 | |
| 2343 | Rethrows and Jumps-Through-Finally |
| 2344 | -- |
| 2345 | |
| 2346 | Support for implicit rethrows and jumping through the finally block is |
| 2347 | handled by storing the current exception-handling context in |
| 2348 | ObjCEHStack. |
| 2349 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2350 | In order to implement proper @finally semantics, we support one basic |
| 2351 | mechanism for jumping through the finally block to an arbitrary |
| 2352 | destination. Constructs which generate exits from a @try or @catch |
| 2353 | block use this mechanism to implement the proper semantics by chaining |
| 2354 | jumps, as necessary. |
| 2355 | |
| 2356 | This mechanism works like the one used for indirect goto: we |
| 2357 | arbitrarily assign an ID to each destination and store the ID for the |
| 2358 | destination in a variable prior to entering the finally block. At the |
| 2359 | end of the finally block we simply create a switch to the proper |
| 2360 | destination. |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2361 | |
| 2362 | Code gen for @synchronized(expr) stmt; |
| 2363 | Effectively generating code for: |
| 2364 | objc_sync_enter(expr); |
| 2365 | @try stmt @finally { objc_sync_exit(expr); } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2366 | */ |
| 2367 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2368 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 2369 | const Stmt &S) { |
| 2370 | bool isTry = isa<ObjCAtTryStmt>(S); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2371 | // Create various blocks we refer to for handling @finally. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2372 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2373 | llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit"); |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2374 | llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit"); |
| 2375 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
| 2376 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2377 | |
| 2378 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 2379 | // evaluation of the expression must occur before we enter the |
| 2380 | // @synchronized. We can safely avoid a temp here because jumps into |
| 2381 | // @synchronized are illegal & this will dominate uses. |
| 2382 | llvm::Value *SyncArg = 0; |
| 2383 | if (!isTry) { |
| 2384 | SyncArg = |
| 2385 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 2386 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 2387 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2388 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2389 | |
| 2390 | // Push an EH context entry, used for handling rethrows and jumps |
| 2391 | // through finally. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2392 | CGF.PushCleanupBlock(FinallyBlock); |
| 2393 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2394 | CGF.ObjCEHValueStack.push_back(0); |
| 2395 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2396 | // Allocate memory for the exception data and rethrow pointer. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2397 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 2398 | "exceptiondata.ptr"); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2399 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy, |
| 2400 | "_rethrow"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2401 | llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty, |
| 2402 | "_call_try_exit"); |
| 2403 | CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr); |
| 2404 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2405 | // Enter a new try block and call setjmp. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2406 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2407 | llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0, |
| 2408 | "jmpbufarray"); |
| 2409 | JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2410 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2411 | JmpBufPtr, "result"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2412 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2413 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 2414 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2415 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2416 | TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2417 | |
| 2418 | // Emit the @try block. |
| 2419 | CGF.EmitBlock(TryBlock); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2420 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 2421 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2422 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2423 | |
| 2424 | // Emit the "exception in @try" block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2425 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2426 | |
| 2427 | // Retrieve the exception object. We may emit multiple blocks but |
| 2428 | // nothing can cross this so the value is already in SSA form. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2429 | llvm::Value *Caught = |
| 2430 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2431 | ExceptionData, "caught"); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2432 | CGF.ObjCEHValueStack.back() = Caught; |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2433 | if (!isTry) |
| 2434 | { |
| 2435 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2436 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2437 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2438 | } |
| 2439 | else if (const ObjCAtCatchStmt* CatchStmt = |
| 2440 | cast<ObjCAtTryStmt>(S).getCatchStmts()) |
| 2441 | { |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2442 | // Enter a new exception try block (in case a @catch block throws |
| 2443 | // an exception). |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2444 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2445 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2446 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2447 | JmpBufPtr, "result"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2448 | llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2449 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2450 | llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch"); |
| 2451 | llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler"); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2452 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2453 | |
| 2454 | CGF.EmitBlock(CatchBlock); |
| 2455 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2456 | // Handle catch list. As a special case we check if everything is |
| 2457 | // matched and avoid generating code for falling off the end if |
| 2458 | // so. |
| 2459 | bool AllMatched = false; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2460 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2461 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2462 | |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2463 | const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2464 | const PointerType *PT = 0; |
| 2465 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2466 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2467 | if (!CatchParam) { |
| 2468 | AllMatched = true; |
| 2469 | } else { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2470 | PT = CatchParam->getType()->getAsPointerType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2471 | |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 2472 | // catch(id e) always matches. |
| 2473 | // FIXME: For the time being we also match id<X>; this should |
| 2474 | // be rejected by Sema instead. |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2475 | if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) || |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2476 | CatchParam->getType()->isObjCQualifiedIdType()) |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2477 | AllMatched = true; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2478 | } |
| 2479 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2480 | if (AllMatched) { |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2481 | if (CatchParam) { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2482 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2483 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2484 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2485 | } |
Anders Carlsson | 1452f55 | 2008-09-11 08:21:54 +0000 | [diff] [blame] | 2486 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2487 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2488 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2489 | break; |
| 2490 | } |
| 2491 | |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2492 | assert(PT && "Unexpected non-pointer type in @catch"); |
| 2493 | QualType T = PT->getPointeeType(); |
Anders Carlsson | 4b7ff6e | 2008-09-11 06:35:14 +0000 | [diff] [blame] | 2494 | const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2495 | assert(ObjCType && "Catch parameter must have Objective-C type!"); |
| 2496 | |
| 2497 | // Check if the @catch block matches the exception object. |
| 2498 | llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl()); |
| 2499 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2500 | llvm::Value *Match = |
| 2501 | CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(), |
| 2502 | Class, Caught, "match"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2503 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2504 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2505 | |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2506 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2507 | MatchedBlock, NextCatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2508 | |
| 2509 | // Emit the @catch block. |
| 2510 | CGF.EmitBlock(MatchedBlock); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2511 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2512 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2513 | |
| 2514 | llvm::Value *Tmp = |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2515 | CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()), |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2516 | "tmp"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2517 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2518 | |
| 2519 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2520 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2521 | |
| 2522 | CGF.EmitBlock(NextCatchBlock); |
| 2523 | } |
| 2524 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2525 | if (!AllMatched) { |
| 2526 | // None of the handlers caught the exception, so store it to be |
| 2527 | // rethrown at the end of the @finally block. |
| 2528 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2529 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2530 | } |
| 2531 | |
| 2532 | // Emit the exception handler for the @catch blocks. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2533 | CGF.EmitBlock(CatchHandler); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2534 | CGF.Builder.CreateStore( |
| 2535 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2536 | ExceptionData), |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2537 | RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2538 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2539 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2540 | } else { |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2541 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2542 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2543 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2544 | } |
| 2545 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2546 | // Pop the exception-handling stack entry. It is important to do |
| 2547 | // this now, because the code in the @finally block is not in this |
| 2548 | // context. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2549 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 2550 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2551 | CGF.ObjCEHValueStack.pop_back(); |
| 2552 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2553 | // Emit the @finally block. |
| 2554 | CGF.EmitBlock(FinallyBlock); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2555 | llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp"); |
| 2556 | |
| 2557 | CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit); |
| 2558 | |
| 2559 | CGF.EmitBlock(FinallyExit); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2560 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2561 | |
| 2562 | CGF.EmitBlock(FinallyNoExit); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2563 | if (isTry) { |
| 2564 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 2565 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 2566 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2567 | } else { |
| 2568 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 2569 | // @synchronized. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2570 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg); |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 2571 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2572 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2573 | // Emit the switch block |
| 2574 | if (Info.SwitchBlock) |
| 2575 | CGF.EmitBlock(Info.SwitchBlock); |
| 2576 | if (Info.EndBlock) |
| 2577 | CGF.EmitBlock(Info.EndBlock); |
| 2578 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2579 | CGF.EmitBlock(FinallyRethrow); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2580 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2581 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2582 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2583 | |
| 2584 | CGF.EmitBlock(FinallyEnd); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2588 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2589 | llvm::Value *ExceptionAsObject; |
| 2590 | |
| 2591 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 2592 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
| 2593 | ExceptionAsObject = |
| 2594 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 2595 | } else { |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2596 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2597 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2598 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2599 | } |
| 2600 | |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2601 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2602 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2603 | |
| 2604 | // Clear the insertion point to indicate we are in unreachable code. |
| 2605 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2606 | } |
| 2607 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2608 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2609 | /// object: objc_read_weak (id *src) |
| 2610 | /// |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2611 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2612 | llvm::Value *AddrWeakObj) |
| 2613 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2614 | const llvm::Type* DestTy = |
| 2615 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2616 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2617 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2618 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2619 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2620 | return read_weak; |
| 2621 | } |
| 2622 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2623 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 2624 | /// objc_assign_weak (id src, id *dst) |
| 2625 | /// |
| 2626 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 2627 | llvm::Value *src, llvm::Value *dst) |
| 2628 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2629 | const llvm::Type * SrcTy = src->getType(); |
| 2630 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2631 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2632 | assert(Size <= 8 && "does not support size > 8"); |
| 2633 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2634 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2635 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2636 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2637 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2638 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 2639 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2640 | src, dst, "weakassign"); |
| 2641 | return; |
| 2642 | } |
| 2643 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2644 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 2645 | /// objc_assign_global (id src, id *dst) |
| 2646 | /// |
| 2647 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 2648 | llvm::Value *src, llvm::Value *dst) |
| 2649 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2650 | const llvm::Type * SrcTy = src->getType(); |
| 2651 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2652 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2653 | assert(Size <= 8 && "does not support size > 8"); |
| 2654 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2655 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2656 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2657 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2658 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2659 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2660 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2661 | src, dst, "globalassign"); |
| 2662 | return; |
| 2663 | } |
| 2664 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2665 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 2666 | /// objc_assign_ivar (id src, id *dst) |
| 2667 | /// |
| 2668 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 2669 | llvm::Value *src, llvm::Value *dst) |
| 2670 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2671 | const llvm::Type * SrcTy = src->getType(); |
| 2672 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2673 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2674 | assert(Size <= 8 && "does not support size > 8"); |
| 2675 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2676 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2677 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2678 | } |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2679 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2680 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2681 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(), |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2682 | src, dst, "assignivar"); |
| 2683 | return; |
| 2684 | } |
| 2685 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2686 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 2687 | /// objc_assign_strongCast (id src, id *dst) |
| 2688 | /// |
| 2689 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 2690 | llvm::Value *src, llvm::Value *dst) |
| 2691 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2692 | const llvm::Type * SrcTy = src->getType(); |
| 2693 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2694 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2695 | assert(Size <= 8 && "does not support size > 8"); |
| 2696 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2697 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2698 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2699 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2700 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2701 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2702 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2703 | src, dst, "weakassign"); |
| 2704 | return; |
| 2705 | } |
| 2706 | |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2707 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 2708 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2709 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 2710 | QualType ObjectTy, |
| 2711 | llvm::Value *BaseValue, |
| 2712 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2713 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 2714 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2715 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 2716 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2717 | } |
| 2718 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2719 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 2720 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2721 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2722 | uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2723 | return llvm::ConstantInt::get( |
| 2724 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 2725 | Offset); |
| 2726 | } |
| 2727 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2728 | /* *** Private Interface *** */ |
| 2729 | |
| 2730 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 2731 | /// level information. |
| 2732 | /// |
| 2733 | /// See: <rdr://4810609&4810587&4810587> |
| 2734 | /// struct IMAGE_INFO { |
| 2735 | /// unsigned version; |
| 2736 | /// unsigned flags; |
| 2737 | /// }; |
| 2738 | enum ImageInfoFlags { |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2739 | eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what |
| 2740 | // this implies. |
| 2741 | eImageInfo_GarbageCollected = (1 << 1), |
| 2742 | eImageInfo_GCOnly = (1 << 2), |
| 2743 | eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set. |
| 2744 | |
| 2745 | // A flag indicating that the module has no instances of an |
| 2746 | // @synthesize of a superclass variable. <rdar://problem/6803242> |
| 2747 | eImageInfo_CorrectedSynthesize = (1 << 4) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2748 | }; |
| 2749 | |
| 2750 | void CGObjCMac::EmitImageInfo() { |
| 2751 | unsigned version = 0; // Version is unused? |
| 2752 | unsigned flags = 0; |
| 2753 | |
| 2754 | // FIXME: Fix and continue? |
| 2755 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 2756 | flags |= eImageInfo_GarbageCollected; |
| 2757 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 2758 | flags |= eImageInfo_GCOnly; |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2759 | |
| 2760 | // We never allow @synthesize of a superclass property. |
| 2761 | flags |= eImageInfo_CorrectedSynthesize; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2762 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2763 | // Emitted as int[2]; |
| 2764 | llvm::Constant *values[2] = { |
| 2765 | llvm::ConstantInt::get(llvm::Type::Int32Ty, version), |
| 2766 | llvm::ConstantInt::get(llvm::Type::Int32Ty, flags) |
| 2767 | }; |
| 2768 | llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2769 | |
| 2770 | const char *Section; |
| 2771 | if (ObjCABI == 1) |
| 2772 | Section = "__OBJC, __image_info,regular"; |
| 2773 | else |
| 2774 | Section = "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2775 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2776 | CreateMetadataVar("\01L_OBJC_IMAGE_INFO", |
| 2777 | llvm::ConstantArray::get(AT, values, 2), |
| 2778 | Section, |
| 2779 | 0, |
| 2780 | true); |
| 2781 | GV->setConstant(true); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2784 | |
| 2785 | // struct objc_module { |
| 2786 | // unsigned long version; |
| 2787 | // unsigned long size; |
| 2788 | // const char *name; |
| 2789 | // Symtab symtab; |
| 2790 | // }; |
| 2791 | |
| 2792 | // FIXME: Get from somewhere |
| 2793 | static const int ModuleVersion = 7; |
| 2794 | |
| 2795 | void CGObjCMac::EmitModuleInfo() { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 2796 | uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2797 | |
| 2798 | std::vector<llvm::Constant*> Values(4); |
| 2799 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion); |
| 2800 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2801 | // This used to be the filename, now it is unused. <rdr://4327263> |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2802 | Values[2] = GetClassName(&CGM.getContext().Idents.get("")); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2803 | Values[3] = EmitModuleSymbols(); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2804 | CreateMetadataVar("\01L_OBJC_MODULES", |
| 2805 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
| 2806 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2807 | 4, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2808 | } |
| 2809 | |
| 2810 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2811 | unsigned NumClasses = DefinedClasses.size(); |
| 2812 | unsigned NumCategories = DefinedCategories.size(); |
| 2813 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2814 | // Return null if no symbols were defined. |
| 2815 | if (!NumClasses && !NumCategories) |
| 2816 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
| 2817 | |
| 2818 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2819 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2820 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
| 2821 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 2822 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
| 2823 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2824 | // The runtime expects exactly the list of defined classes followed |
| 2825 | // by the list of defined categories, in a single array. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2826 | std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2827 | for (unsigned i=0; i<NumClasses; i++) |
| 2828 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 2829 | ObjCTypes.Int8PtrTy); |
| 2830 | for (unsigned i=0; i<NumCategories; i++) |
| 2831 | Symbols[NumClasses + i] = |
| 2832 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 2833 | ObjCTypes.Int8PtrTy); |
| 2834 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2835 | Values[4] = |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2836 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2837 | NumClasses + NumCategories), |
| 2838 | Symbols); |
| 2839 | |
| 2840 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2841 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2842 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2843 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 2844 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2845 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2846 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
| 2847 | } |
| 2848 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2849 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2850 | const ObjCInterfaceDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2851 | LazySymbols.insert(ID->getIdentifier()); |
| 2852 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2853 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 2854 | |
| 2855 | if (!Entry) { |
| 2856 | llvm::Constant *Casted = |
| 2857 | llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()), |
| 2858 | ObjCTypes.ClassPtrTy); |
| 2859 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2860 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 2861 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2862 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2863 | } |
| 2864 | |
| 2865 | return Builder.CreateLoad(Entry, false, "tmp"); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2866 | } |
| 2867 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2868 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2869 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 2870 | |
| 2871 | if (!Entry) { |
| 2872 | llvm::Constant *Casted = |
| 2873 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 2874 | ObjCTypes.SelectorPtrTy); |
| 2875 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2876 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 2877 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2878 | 4, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2879 | } |
| 2880 | |
| 2881 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 2882 | } |
| 2883 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 2884 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2885 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2886 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2887 | if (!Entry) |
| 2888 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 2889 | llvm::ConstantArray::get(Ident->getName()), |
| 2890 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 2891 | 1, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2892 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2893 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2894 | } |
| 2895 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 2896 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 2897 | /// ivar layout bitmap. |
| 2898 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
| 2899 | const ObjCCommonTypesHelper &ObjCTypes) { |
| 2900 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 2901 | } |
| 2902 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2903 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCInterfaceDecl *OI, |
| 2904 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2905 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2906 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2907 | unsigned int BytePos, bool ForStrongLayout, |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 2908 | bool &HasUnion) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2909 | bool IsUnion = (RD && RD->isUnion()); |
| 2910 | uint64_t MaxUnionIvarSize = 0; |
| 2911 | uint64_t MaxSkippedUnionIvarSize = 0; |
| 2912 | FieldDecl *MaxField = 0; |
| 2913 | FieldDecl *MaxSkippedField = 0; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2914 | FieldDecl *LastFieldBitfield = 0; |
| 2915 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2916 | unsigned base = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2917 | if (RecFields.empty()) |
| 2918 | return; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2919 | if (IsUnion) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2920 | base = BytePos + GetFieldBaseOffset(OI, Layout, RecFields[0]); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2921 | unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0); |
| 2922 | unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth(); |
| 2923 | |
| 2924 | llvm::SmallVector<FieldDecl*, 16> TmpRecFields; |
| 2925 | |
| 2926 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2927 | FieldDecl *Field = RecFields[i]; |
| 2928 | // Skip over unnamed or bitfields |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2929 | if (!Field->getIdentifier() || Field->isBitField()) { |
| 2930 | LastFieldBitfield = Field; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2931 | continue; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2932 | } |
| 2933 | LastFieldBitfield = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2934 | QualType FQT = Field->getType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2935 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2936 | if (FQT->isUnionType()) |
| 2937 | HasUnion = true; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2938 | else |
| 2939 | assert(FQT->isRecordType() && |
| 2940 | "only union/record is supported for ivar layout bitmap"); |
| 2941 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2942 | const RecordType *RT = FQT->getAsRecordType(); |
| 2943 | const RecordDecl *RD = RT->getDecl(); |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 2944 | // FIXME - Find a more efficient way of passing records down. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2945 | TmpRecFields.append(RD->field_begin(CGM.getContext()), |
| 2946 | RD->field_end(CGM.getContext())); |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 2947 | const llvm::Type *Ty = CGM.getTypes().ConvertType(FQT); |
| 2948 | const llvm::StructLayout *RecLayout = |
| 2949 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); |
| 2950 | |
| 2951 | BuildAggrIvarLayout(0, RecLayout, RD, TmpRecFields, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2952 | BytePos + GetFieldBaseOffset(OI, Layout, Field), |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 2953 | ForStrongLayout, HasUnion); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2954 | TmpRecFields.clear(); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2955 | continue; |
| 2956 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2957 | |
| 2958 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2959 | const ConstantArrayType *CArray = |
| 2960 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2961 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2962 | assert(CArray && "only array with know element size is supported"); |
| 2963 | FQT = CArray->getElementType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2964 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 2965 | const ConstantArrayType *CArray = |
| 2966 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2967 | ElCount *= CArray->getSize().getZExtValue(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2968 | FQT = CArray->getElementType(); |
| 2969 | } |
| 2970 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2971 | assert(!FQT->isUnionType() && |
| 2972 | "layout for array of unions not supported"); |
| 2973 | if (FQT->isRecordType()) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 2974 | int OldIndex = IvarsInfo.size() - 1; |
| 2975 | int OldSkIndex = SkipIvars.size() -1; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2976 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2977 | // FIXME - Use a common routine with the above! |
| 2978 | const RecordType *RT = FQT->getAsRecordType(); |
| 2979 | const RecordDecl *RD = RT->getDecl(); |
| 2980 | // FIXME - Find a more efficiant way of passing records down. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2981 | TmpRecFields.append(RD->field_begin(CGM.getContext()), |
| 2982 | RD->field_end(CGM.getContext())); |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 2983 | const llvm::Type *Ty = CGM.getTypes().ConvertType(FQT); |
| 2984 | const llvm::StructLayout *RecLayout = |
| 2985 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2986 | |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 2987 | BuildAggrIvarLayout(0, RecLayout, RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2988 | TmpRecFields, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2989 | BytePos + GetFieldBaseOffset(OI, Layout, Field), |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 2990 | ForStrongLayout, HasUnion); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2991 | TmpRecFields.clear(); |
| 2992 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2993 | // Replicate layout information for each array element. Note that |
| 2994 | // one element is already done. |
| 2995 | uint64_t ElIx = 1; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 2996 | for (int FirstIndex = IvarsInfo.size() - 1, |
| 2997 | FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2998 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2999 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 3000 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3001 | GC_IVAR gcivar; |
| 3002 | gcivar.ivar_bytepos = IvarsInfo[i].ivar_bytepos + Size*ElIx; |
| 3003 | gcivar.ivar_size = IvarsInfo[i].ivar_size; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3004 | IvarsInfo.push_back(gcivar); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3005 | } |
| 3006 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3007 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3008 | GC_IVAR skivar; |
| 3009 | skivar.ivar_bytepos = SkipIvars[i].ivar_bytepos + Size*ElIx; |
| 3010 | skivar.ivar_size = SkipIvars[i].ivar_size; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3011 | SkipIvars.push_back(skivar); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3012 | } |
| 3013 | } |
| 3014 | continue; |
| 3015 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3016 | } |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3017 | // At this point, we are done with Record/Union and array there of. |
| 3018 | // For other arrays we are down to its element type. |
| 3019 | QualType::GCAttrTypes GCAttr = QualType::GCNone; |
| 3020 | do { |
| 3021 | if (FQT.isObjCGCStrong() || FQT.isObjCGCWeak()) { |
| 3022 | GCAttr = FQT.isObjCGCStrong() ? QualType::Strong : QualType::Weak; |
| 3023 | break; |
| 3024 | } |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3025 | else if (CGM.getContext().isObjCObjectPointerType(FQT)) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3026 | GCAttr = QualType::Strong; |
| 3027 | break; |
| 3028 | } |
| 3029 | else if (const PointerType *PT = FQT->getAsPointerType()) { |
| 3030 | FQT = PT->getPointeeType(); |
| 3031 | } |
| 3032 | else { |
| 3033 | break; |
| 3034 | } |
| 3035 | } while (true); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3036 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3037 | if ((ForStrongLayout && GCAttr == QualType::Strong) |
| 3038 | || (!ForStrongLayout && GCAttr == QualType::Weak)) { |
| 3039 | if (IsUnion) |
| 3040 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3041 | uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType()) |
| 3042 | / WordSizeInBits; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3043 | if (UnionIvarSize > MaxUnionIvarSize) |
| 3044 | { |
| 3045 | MaxUnionIvarSize = UnionIvarSize; |
| 3046 | MaxField = Field; |
| 3047 | } |
| 3048 | } |
| 3049 | else |
| 3050 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3051 | GC_IVAR gcivar; |
| 3052 | gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field); |
| 3053 | gcivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) / |
| 3054 | WordSizeInBits; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3055 | IvarsInfo.push_back(gcivar); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3056 | } |
| 3057 | } |
| 3058 | else if ((ForStrongLayout && |
| 3059 | (GCAttr == QualType::GCNone || GCAttr == QualType::Weak)) |
| 3060 | || (!ForStrongLayout && GCAttr != QualType::Weak)) { |
| 3061 | if (IsUnion) |
| 3062 | { |
| 3063 | uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType()); |
| 3064 | if (UnionIvarSize > MaxSkippedUnionIvarSize) |
| 3065 | { |
| 3066 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 3067 | MaxSkippedField = Field; |
| 3068 | } |
| 3069 | } |
| 3070 | else |
| 3071 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3072 | GC_IVAR skivar; |
| 3073 | skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field); |
| 3074 | skivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) / |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3075 | ByteSizeInBits; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3076 | SkipIvars.push_back(skivar); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3077 | } |
| 3078 | } |
| 3079 | } |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3080 | if (LastFieldBitfield) { |
| 3081 | // Last field was a bitfield. Must update skip info. |
| 3082 | GC_IVAR skivar; |
| 3083 | skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, |
| 3084 | LastFieldBitfield); |
| 3085 | Expr *BitWidth = LastFieldBitfield->getBitWidth(); |
| 3086 | uint64_t BitFieldSize = |
| 3087 | BitWidth->getIntegerConstantExprValue(CGM.getContext()).getZExtValue(); |
| 3088 | skivar.ivar_size = (BitFieldSize / ByteSizeInBits) |
| 3089 | + ((BitFieldSize % ByteSizeInBits) != 0); |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3090 | SkipIvars.push_back(skivar); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3091 | } |
| 3092 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3093 | if (MaxField) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3094 | GC_IVAR gcivar; |
| 3095 | gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, MaxField); |
| 3096 | gcivar.ivar_size = MaxUnionIvarSize; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3097 | IvarsInfo.push_back(gcivar); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3098 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3099 | |
| 3100 | if (MaxSkippedField) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3101 | GC_IVAR skivar; |
| 3102 | skivar.ivar_bytepos = BytePos + |
| 3103 | GetFieldBaseOffset(OI, Layout, MaxSkippedField); |
| 3104 | skivar.ivar_size = MaxSkippedUnionIvarSize; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3105 | SkipIvars.push_back(skivar); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3106 | } |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3107 | } |
| 3108 | |
| 3109 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 3110 | /// implementation for the __strong or __weak case. |
| 3111 | /// The layout map displays which words in ivar list must be skipped |
| 3112 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 3113 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
| 3114 | /// of words to skip and right nibble is count of words to scan. So, each |
| 3115 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
| 3116 | /// represented by a 0x00 byte which also ends the string. |
| 3117 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 3118 | /// - id, Class |
| 3119 | /// - object * |
| 3120 | /// - __strong anything |
| 3121 | /// |
| 3122 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 3123 | /// - __weak anything |
| 3124 | /// |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3125 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3126 | const ObjCImplementationDecl *OMD, |
| 3127 | bool ForStrongLayout) { |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3128 | bool hasUnion = false; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3129 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3130 | unsigned int WordsToScan, WordsToSkip; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3131 | const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 3132 | if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) |
| 3133 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3134 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3135 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3136 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3137 | CGM.getContext().CollectObjCIvars(OI, RecFields); |
| 3138 | if (RecFields.empty()) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3139 | return llvm::Constant::getNullValue(PtrTy); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3140 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3141 | SkipIvars.clear(); |
| 3142 | IvarsInfo.clear(); |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 3143 | |
Daniel Dunbar | 84ad77a | 2009-04-22 09:39:34 +0000 | [diff] [blame] | 3144 | const llvm::StructLayout *Layout = |
| 3145 | CGM.getTargetData().getStructLayout(GetConcreteClassStruct(CGM, OI)); |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3146 | BuildAggrIvarLayout(OI, Layout, 0, RecFields, 0, ForStrongLayout, hasUnion); |
| 3147 | if (IvarsInfo.empty()) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3148 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3149 | |
| 3150 | // Sort on byte position in case we encounterred a union nested in |
| 3151 | // the ivar list. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3152 | if (hasUnion && !IvarsInfo.empty()) |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 3153 | std::sort(IvarsInfo.begin(), IvarsInfo.end()); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3154 | if (hasUnion && !SkipIvars.empty()) |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 3155 | std::sort(SkipIvars.begin(), SkipIvars.end()); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3156 | |
| 3157 | // Build the string of skip/scan nibbles |
Fariborz Jahanian | 8c2f2d1 | 2009-04-24 17:15:27 +0000 | [diff] [blame^] | 3158 | llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3159 | unsigned int WordSize = |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3160 | CGM.getTypes().getTargetData().getTypePaddedSize(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3161 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 3162 | WordsToSkip = 0; |
| 3163 | WordsToScan = IvarsInfo[0].ivar_size; |
| 3164 | } |
| 3165 | else { |
| 3166 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 3167 | WordsToScan = IvarsInfo[0].ivar_size; |
| 3168 | } |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3169 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3170 | { |
| 3171 | unsigned int TailPrevGCObjC = |
| 3172 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
| 3173 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) |
| 3174 | { |
| 3175 | // consecutive 'scanned' object pointers. |
| 3176 | WordsToScan += IvarsInfo[i].ivar_size; |
| 3177 | } |
| 3178 | else |
| 3179 | { |
| 3180 | // Skip over 'gc'able object pointer which lay over each other. |
| 3181 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 3182 | continue; |
| 3183 | // Must skip over 1 or more words. We save current skip/scan values |
| 3184 | // and start a new pair. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3185 | SKIP_SCAN SkScan; |
| 3186 | SkScan.skip = WordsToSkip; |
| 3187 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3188 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3189 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3190 | // Skip the hole. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3191 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 3192 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3193 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3194 | WordsToSkip = 0; |
| 3195 | WordsToScan = IvarsInfo[i].ivar_size; |
| 3196 | } |
| 3197 | } |
| 3198 | if (WordsToScan > 0) |
| 3199 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3200 | SKIP_SCAN SkScan; |
| 3201 | SkScan.skip = WordsToSkip; |
| 3202 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3203 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | bool BytesSkipped = false; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3207 | if (!SkipIvars.empty()) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3208 | { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3209 | unsigned int LastIndex = SkipIvars.size()-1; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3210 | int LastByteSkipped = |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3211 | SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size; |
| 3212 | LastIndex = IvarsInfo.size()-1; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3213 | int LastByteScanned = |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3214 | IvarsInfo[LastIndex].ivar_bytepos + |
| 3215 | IvarsInfo[LastIndex].ivar_size * WordSize; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3216 | BytesSkipped = (LastByteSkipped > LastByteScanned); |
| 3217 | // Compute number of bytes to skip at the tail end of the last ivar scanned. |
| 3218 | if (BytesSkipped) |
| 3219 | { |
| 3220 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3221 | SKIP_SCAN SkScan; |
| 3222 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
| 3223 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3224 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3225 | } |
| 3226 | } |
| 3227 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 3228 | // as 0xMN. |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3229 | int SkipScan = SkipScanIvars.size()-1; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3230 | for (int i = 0; i <= SkipScan; i++) |
| 3231 | { |
| 3232 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 3233 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 3234 | // 0xM0 followed by 0x0N detected. |
| 3235 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 3236 | for (int j = i+1; j < SkipScan; j++) |
| 3237 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 3238 | --SkipScan; |
| 3239 | } |
| 3240 | } |
| 3241 | |
| 3242 | // Generate the string. |
| 3243 | std::string BitMap; |
| 3244 | for (int i = 0; i <= SkipScan; i++) |
| 3245 | { |
| 3246 | unsigned char byte; |
| 3247 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 3248 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 3249 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 3250 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
| 3251 | |
| 3252 | if (skip_small > 0 || skip_big > 0) |
| 3253 | BytesSkipped = true; |
| 3254 | // first skip big. |
| 3255 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 3256 | BitMap += (unsigned char)(0xf0); |
| 3257 | |
| 3258 | // next (skip small, scan) |
| 3259 | if (skip_small) |
| 3260 | { |
| 3261 | byte = skip_small << 4; |
| 3262 | if (scan_big > 0) |
| 3263 | { |
| 3264 | byte |= 0xf; |
| 3265 | --scan_big; |
| 3266 | } |
| 3267 | else if (scan_small) |
| 3268 | { |
| 3269 | byte |= scan_small; |
| 3270 | scan_small = 0; |
| 3271 | } |
| 3272 | BitMap += byte; |
| 3273 | } |
| 3274 | // next scan big |
| 3275 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 3276 | BitMap += (unsigned char)(0x0f); |
| 3277 | // last scan small |
| 3278 | if (scan_small) |
| 3279 | { |
| 3280 | byte = scan_small; |
| 3281 | BitMap += byte; |
| 3282 | } |
| 3283 | } |
| 3284 | // null terminate string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3285 | unsigned char zero = 0; |
| 3286 | BitMap += zero; |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3287 | |
| 3288 | if (CGM.getLangOptions().ObjCGCBitmapPrint) { |
| 3289 | printf("\n%s ivar layout for class '%s': ", |
| 3290 | ForStrongLayout ? "strong" : "weak", |
| 3291 | OMD->getClassInterface()->getNameAsCString()); |
| 3292 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
| 3293 | for (unsigned i = 0; i < BitMap.size(); i++) |
| 3294 | if (!(s[i] & 0xf0)) |
| 3295 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3296 | else |
| 3297 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3298 | printf("\n"); |
| 3299 | } |
| 3300 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3301 | // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as |
| 3302 | // final layout. |
| 3303 | if (ForStrongLayout && !BytesSkipped) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3304 | return llvm::Constant::getNullValue(PtrTy); |
| 3305 | llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 3306 | llvm::ConstantArray::get(BitMap.c_str()), |
| 3307 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3308 | 1, true); |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3309 | return getConstantGEP(Entry, 0, 0); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3310 | } |
| 3311 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3312 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3313 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 3314 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3315 | // FIXME: Avoid std::string copying. |
| 3316 | if (!Entry) |
| 3317 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
| 3318 | llvm::ConstantArray::get(Sel.getAsString()), |
| 3319 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3320 | 1, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3321 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3322 | return getConstantGEP(Entry, 0, 0); |
| 3323 | } |
| 3324 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3325 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3326 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3327 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 3328 | } |
| 3329 | |
| 3330 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3331 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3332 | return GetMethodVarName(&CGM.getContext().Idents.get(Name)); |
| 3333 | } |
| 3334 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 3335 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3336 | std::string TypeStr; |
| 3337 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 3338 | |
| 3339 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3340 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3341 | if (!Entry) |
| 3342 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3343 | llvm::ConstantArray::get(TypeStr), |
| 3344 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3345 | 1, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3346 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3347 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3348 | } |
| 3349 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3350 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3351 | std::string TypeStr; |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3352 | CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D), |
| 3353 | TypeStr); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3354 | |
| 3355 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 3356 | |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3357 | if (!Entry) |
| 3358 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3359 | llvm::ConstantArray::get(TypeStr), |
| 3360 | "__TEXT,__cstring,cstring_literals", |
| 3361 | 1, true); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3362 | |
| 3363 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3364 | } |
| 3365 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3366 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3367 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3368 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
| 3369 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3370 | if (!Entry) |
| 3371 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
| 3372 | llvm::ConstantArray::get(Ident->getName()), |
| 3373 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3374 | 1, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3375 | |
| 3376 | return getConstantGEP(Entry, 0, 0); |
| 3377 | } |
| 3378 | |
| 3379 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3380 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3381 | llvm::Constant * |
| 3382 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 3383 | const Decl *Container) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3384 | std::string TypeStr; |
| 3385 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3386 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 3387 | } |
| 3388 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3389 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
| 3390 | const ObjCContainerDecl *CD, |
| 3391 | std::string &NameOut) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 3392 | NameOut = '\01'; |
| 3393 | NameOut += (D->isInstanceMethod() ? '-' : '+'); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3394 | NameOut += '['; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 3395 | assert (CD && "Missing container decl in GetNameForMethod"); |
| 3396 | NameOut += CD->getNameAsString(); |
Fariborz Jahanian | 1e9aef3 | 2009-04-16 18:34:20 +0000 | [diff] [blame] | 3397 | if (const ObjCCategoryImplDecl *CID = |
| 3398 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) { |
| 3399 | NameOut += '('; |
| 3400 | NameOut += CID->getNameAsString(); |
| 3401 | NameOut+= ')'; |
| 3402 | } |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3403 | NameOut += ' '; |
| 3404 | NameOut += D->getSelector().getAsString(); |
| 3405 | NameOut += ']'; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3406 | } |
| 3407 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3408 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3409 | EmitModuleInfo(); |
| 3410 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3411 | // Emit the dummy bodies for any protocols which were referenced but |
| 3412 | // never defined. |
| 3413 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
| 3414 | i = Protocols.begin(), e = Protocols.end(); i != e; ++i) { |
| 3415 | if (i->second->hasInitializer()) |
| 3416 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3417 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3418 | std::vector<llvm::Constant*> Values(5); |
| 3419 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 3420 | Values[1] = GetClassName(i->first); |
| 3421 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 3422 | Values[3] = Values[4] = |
| 3423 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 3424 | i->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 3425 | i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 3426 | Values)); |
| 3427 | } |
| 3428 | |
| 3429 | std::vector<llvm::Constant*> Used; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3430 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3431 | e = UsedGlobals.end(); i != e; ++i) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3432 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3433 | } |
| 3434 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3435 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3436 | llvm::GlobalValue *GV = |
| 3437 | new llvm::GlobalVariable(AT, false, |
| 3438 | llvm::GlobalValue::AppendingLinkage, |
| 3439 | llvm::ConstantArray::get(AT, Used), |
| 3440 | "llvm.used", |
| 3441 | &CGM.getModule()); |
| 3442 | |
| 3443 | GV->setSection("llvm.metadata"); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3444 | |
| 3445 | // Add assembler directives to add lazy undefined symbol references |
| 3446 | // for classes which are referenced but not defined. This is |
| 3447 | // important for correct linker interaction. |
| 3448 | |
| 3449 | // FIXME: Uh, this isn't particularly portable. |
| 3450 | std::stringstream s; |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3451 | |
| 3452 | if (!CGM.getModule().getModuleInlineAsm().empty()) |
| 3453 | s << "\n"; |
| 3454 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3455 | for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(), |
| 3456 | e = LazySymbols.end(); i != e; ++i) { |
| 3457 | s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n"; |
| 3458 | } |
| 3459 | for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(), |
| 3460 | e = DefinedSymbols.end(); i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3461 | s << "\t.objc_class_name_" << (*i)->getName() << "=0\n" |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3462 | << "\t.globl .objc_class_name_" << (*i)->getName() << "\n"; |
| 3463 | } |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3464 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3465 | CGM.getModule().appendModuleInlineAsm(s.str()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3466 | } |
| 3467 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3468 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3469 | : CGObjCCommonMac(cgm), |
| 3470 | ObjCTypes(cgm) |
| 3471 | { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3472 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3473 | ObjCABI = 2; |
| 3474 | } |
| 3475 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3476 | /* *** */ |
| 3477 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3478 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3479 | : CGM(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3480 | { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3481 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3482 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3483 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3484 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3485 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3486 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3487 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3488 | Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 3489 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3490 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 3491 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3492 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3493 | |
| 3494 | // FIXME: It would be nice to unify this with the opaque type, so |
| 3495 | // that the IR comes out a bit cleaner. |
| 3496 | const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 3497 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3498 | |
| 3499 | // I'm not sure I like this. The implicit coordination is a bit |
| 3500 | // gross. We should solve this in a reasonable fashion because this |
| 3501 | // is a pretty common task (match some runtime data structure with |
| 3502 | // an LLVM data structure). |
| 3503 | |
| 3504 | // FIXME: This is leaked. |
| 3505 | // FIXME: Merge with rewriter code? |
| 3506 | |
| 3507 | // struct _objc_super { |
| 3508 | // id self; |
| 3509 | // Class cls; |
| 3510 | // } |
| 3511 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3512 | SourceLocation(), |
| 3513 | &Ctx.Idents.get("_objc_super")); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3514 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3515 | Ctx.getObjCIdType(), 0, false)); |
| 3516 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3517 | Ctx.getObjCClassType(), 0, false)); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3518 | RD->completeDefinition(Ctx); |
| 3519 | |
| 3520 | SuperCTy = Ctx.getTagDeclType(RD); |
| 3521 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
| 3522 | |
| 3523 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3524 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 3525 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3526 | // struct _prop_t { |
| 3527 | // char *name; |
| 3528 | // char *attributes; |
| 3529 | // } |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 3530 | PropertyTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, NULL); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3531 | CGM.getModule().addTypeName("struct._prop_t", |
| 3532 | PropertyTy); |
| 3533 | |
| 3534 | // struct _prop_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3535 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3536 | // uint32_t count_of_properties; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3537 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3538 | // } |
| 3539 | PropertyListTy = llvm::StructType::get(IntTy, |
| 3540 | IntTy, |
| 3541 | llvm::ArrayType::get(PropertyTy, 0), |
| 3542 | NULL); |
| 3543 | CGM.getModule().addTypeName("struct._prop_list_t", |
| 3544 | PropertyListTy); |
| 3545 | // struct _prop_list_t * |
| 3546 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
| 3547 | |
| 3548 | // struct _objc_method { |
| 3549 | // SEL _cmd; |
| 3550 | // char *method_type; |
| 3551 | // char *_imp; |
| 3552 | // } |
| 3553 | MethodTy = llvm::StructType::get(SelectorPtrTy, |
| 3554 | Int8PtrTy, |
| 3555 | Int8PtrTy, |
| 3556 | NULL); |
| 3557 | CGM.getModule().addTypeName("struct._objc_method", MethodTy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3558 | |
| 3559 | // struct _objc_cache * |
| 3560 | CacheTy = llvm::OpaqueType::get(); |
| 3561 | CGM.getModule().addTypeName("struct._objc_cache", CacheTy); |
| 3562 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3563 | } |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3564 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3565 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3566 | : ObjCCommonTypesHelper(cgm) |
| 3567 | { |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3568 | // struct _objc_method_description { |
| 3569 | // SEL name; |
| 3570 | // char *types; |
| 3571 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3572 | MethodDescriptionTy = |
| 3573 | llvm::StructType::get(SelectorPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3574 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3575 | NULL); |
| 3576 | CGM.getModule().addTypeName("struct._objc_method_description", |
| 3577 | MethodDescriptionTy); |
| 3578 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3579 | // struct _objc_method_description_list { |
| 3580 | // int count; |
| 3581 | // struct _objc_method_description[1]; |
| 3582 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3583 | MethodDescriptionListTy = |
| 3584 | llvm::StructType::get(IntTy, |
| 3585 | llvm::ArrayType::get(MethodDescriptionTy, 0), |
| 3586 | NULL); |
| 3587 | CGM.getModule().addTypeName("struct._objc_method_description_list", |
| 3588 | MethodDescriptionListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3589 | |
| 3590 | // struct _objc_method_description_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3591 | MethodDescriptionListPtrTy = |
| 3592 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
| 3593 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3594 | // Protocol description structures |
| 3595 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3596 | // struct _objc_protocol_extension { |
| 3597 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 3598 | // struct _objc_method_description_list *optional_instance_methods; |
| 3599 | // struct _objc_method_description_list *optional_class_methods; |
| 3600 | // struct _objc_property_list *instance_properties; |
| 3601 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3602 | ProtocolExtensionTy = |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3603 | llvm::StructType::get(IntTy, |
| 3604 | MethodDescriptionListPtrTy, |
| 3605 | MethodDescriptionListPtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3606 | PropertyListPtrTy, |
| 3607 | NULL); |
| 3608 | CGM.getModule().addTypeName("struct._objc_protocol_extension", |
| 3609 | ProtocolExtensionTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3610 | |
| 3611 | // struct _objc_protocol_extension * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3612 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
| 3613 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3614 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3615 | |
| 3616 | llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(); |
| 3617 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3618 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3619 | const llvm::Type *T = |
| 3620 | llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3621 | LongTy, |
| 3622 | llvm::ArrayType::get(ProtocolTyHolder, 0), |
| 3623 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3624 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T); |
| 3625 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3626 | // struct _objc_protocol { |
| 3627 | // struct _objc_protocol_extension *isa; |
| 3628 | // char *protocol_name; |
| 3629 | // struct _objc_protocol **_objc_protocol_list; |
| 3630 | // struct _objc_method_description_list *instance_methods; |
| 3631 | // struct _objc_method_description_list *class_methods; |
| 3632 | // } |
| 3633 | T = llvm::StructType::get(ProtocolExtensionPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3634 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3635 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3636 | MethodDescriptionListPtrTy, |
| 3637 | MethodDescriptionListPtrTy, |
| 3638 | NULL); |
| 3639 | cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T); |
| 3640 | |
| 3641 | ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get()); |
| 3642 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3643 | ProtocolListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3644 | // struct _objc_protocol_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3645 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
| 3646 | |
| 3647 | ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get()); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3648 | CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3649 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3650 | |
| 3651 | // Class description structures |
| 3652 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3653 | // struct _objc_ivar { |
| 3654 | // char *ivar_name; |
| 3655 | // char *ivar_type; |
| 3656 | // int ivar_offset; |
| 3657 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3658 | IvarTy = llvm::StructType::get(Int8PtrTy, |
| 3659 | Int8PtrTy, |
| 3660 | IntTy, |
| 3661 | NULL); |
| 3662 | CGM.getModule().addTypeName("struct._objc_ivar", IvarTy); |
| 3663 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3664 | // struct _objc_ivar_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3665 | IvarListTy = llvm::OpaqueType::get(); |
| 3666 | CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy); |
| 3667 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
| 3668 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3669 | // struct _objc_method_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3670 | MethodListTy = llvm::OpaqueType::get(); |
| 3671 | CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy); |
| 3672 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
| 3673 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3674 | // struct _objc_class_extension * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3675 | ClassExtensionTy = |
| 3676 | llvm::StructType::get(IntTy, |
| 3677 | Int8PtrTy, |
| 3678 | PropertyListPtrTy, |
| 3679 | NULL); |
| 3680 | CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy); |
| 3681 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
| 3682 | |
| 3683 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3684 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3685 | // struct _objc_class { |
| 3686 | // Class isa; |
| 3687 | // Class super_class; |
| 3688 | // char *name; |
| 3689 | // long version; |
| 3690 | // long info; |
| 3691 | // long instance_size; |
| 3692 | // struct _objc_ivar_list *ivars; |
| 3693 | // struct _objc_method_list *methods; |
| 3694 | // struct _objc_cache *cache; |
| 3695 | // struct _objc_protocol_list *protocols; |
| 3696 | // char *ivar_layout; |
| 3697 | // struct _objc_class_ext *ext; |
| 3698 | // }; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3699 | T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3700 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3701 | Int8PtrTy, |
| 3702 | LongTy, |
| 3703 | LongTy, |
| 3704 | LongTy, |
| 3705 | IvarListPtrTy, |
| 3706 | MethodListPtrTy, |
| 3707 | CachePtrTy, |
| 3708 | ProtocolListPtrTy, |
| 3709 | Int8PtrTy, |
| 3710 | ClassExtensionPtrTy, |
| 3711 | NULL); |
| 3712 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T); |
| 3713 | |
| 3714 | ClassTy = cast<llvm::StructType>(ClassTyHolder.get()); |
| 3715 | CGM.getModule().addTypeName("struct._objc_class", ClassTy); |
| 3716 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
| 3717 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3718 | // struct _objc_category { |
| 3719 | // char *category_name; |
| 3720 | // char *class_name; |
| 3721 | // struct _objc_method_list *instance_method; |
| 3722 | // struct _objc_method_list *class_method; |
| 3723 | // uint32_t size; // sizeof(struct _objc_category) |
| 3724 | // struct _objc_property_list *instance_properties;// category's @property |
| 3725 | // } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3726 | CategoryTy = llvm::StructType::get(Int8PtrTy, |
| 3727 | Int8PtrTy, |
| 3728 | MethodListPtrTy, |
| 3729 | MethodListPtrTy, |
| 3730 | ProtocolListPtrTy, |
| 3731 | IntTy, |
| 3732 | PropertyListPtrTy, |
| 3733 | NULL); |
| 3734 | CGM.getModule().addTypeName("struct._objc_category", CategoryTy); |
| 3735 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3736 | // Global metadata structures |
| 3737 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3738 | // struct _objc_symtab { |
| 3739 | // long sel_ref_cnt; |
| 3740 | // SEL *refs; |
| 3741 | // short cls_def_cnt; |
| 3742 | // short cat_def_cnt; |
| 3743 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 3744 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3745 | SymtabTy = llvm::StructType::get(LongTy, |
| 3746 | SelectorPtrTy, |
| 3747 | ShortTy, |
| 3748 | ShortTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3749 | llvm::ArrayType::get(Int8PtrTy, 0), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3750 | NULL); |
| 3751 | CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy); |
| 3752 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
| 3753 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3754 | // struct _objc_module { |
| 3755 | // long version; |
| 3756 | // long size; // sizeof(struct _objc_module) |
| 3757 | // char *name; |
| 3758 | // struct _objc_symtab* symtab; |
| 3759 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3760 | ModuleTy = |
| 3761 | llvm::StructType::get(LongTy, |
| 3762 | LongTy, |
| 3763 | Int8PtrTy, |
| 3764 | SymtabPtrTy, |
| 3765 | NULL); |
| 3766 | CGM.getModule().addTypeName("struct._objc_module", ModuleTy); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3767 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 3768 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3769 | // FIXME: This is the size of the setjmp buffer and should be |
| 3770 | // target specific. 18 is what's used on 32-bit X86. |
| 3771 | uint64_t SetJmpBufferSize = 18; |
| 3772 | |
| 3773 | // Exceptions |
| 3774 | const llvm::Type *StackPtrTy = |
Daniel Dunbar | 1000491 | 2008-09-27 06:32:25 +0000 | [diff] [blame] | 3775 | llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3776 | |
| 3777 | ExceptionDataTy = |
| 3778 | llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty, |
| 3779 | SetJmpBufferSize), |
| 3780 | StackPtrTy, NULL); |
| 3781 | CGM.getModule().addTypeName("struct._objc_exception_data", |
| 3782 | ExceptionDataTy); |
| 3783 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3784 | } |
| 3785 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3786 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3787 | : ObjCCommonTypesHelper(cgm) |
| 3788 | { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3789 | // struct _method_list_t { |
| 3790 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 3791 | // uint32_t method_count; |
| 3792 | // struct _objc_method method_list[method_count]; |
| 3793 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3794 | MethodListnfABITy = llvm::StructType::get(IntTy, |
| 3795 | IntTy, |
| 3796 | llvm::ArrayType::get(MethodTy, 0), |
| 3797 | NULL); |
| 3798 | CGM.getModule().addTypeName("struct.__method_list_t", |
| 3799 | MethodListnfABITy); |
| 3800 | // struct method_list_t * |
| 3801 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3802 | |
| 3803 | // struct _protocol_t { |
| 3804 | // id isa; // NULL |
| 3805 | // const char * const protocol_name; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3806 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3807 | // const struct method_list_t * const instance_methods; |
| 3808 | // const struct method_list_t * const class_methods; |
| 3809 | // const struct method_list_t *optionalInstanceMethods; |
| 3810 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3811 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3812 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 3813 | // const uint32_t flags; // = 0 |
| 3814 | // } |
| 3815 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3816 | // Holder for struct _protocol_list_t * |
| 3817 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3818 | |
| 3819 | ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy, |
| 3820 | Int8PtrTy, |
| 3821 | llvm::PointerType::getUnqual( |
| 3822 | ProtocolListTyHolder), |
| 3823 | MethodListnfABIPtrTy, |
| 3824 | MethodListnfABIPtrTy, |
| 3825 | MethodListnfABIPtrTy, |
| 3826 | MethodListnfABIPtrTy, |
| 3827 | PropertyListPtrTy, |
| 3828 | IntTy, |
| 3829 | IntTy, |
| 3830 | NULL); |
| 3831 | CGM.getModule().addTypeName("struct._protocol_t", |
| 3832 | ProtocolnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3833 | |
| 3834 | // struct _protocol_t* |
| 3835 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3836 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3837 | // struct _protocol_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3838 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3839 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3840 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3841 | ProtocolListnfABITy = llvm::StructType::get(LongTy, |
| 3842 | llvm::ArrayType::get( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3843 | ProtocolnfABIPtrTy, 0), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3844 | NULL); |
| 3845 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3846 | ProtocolListnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3847 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo( |
| 3848 | ProtocolListnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3849 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3850 | // struct _objc_protocol_list* |
| 3851 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3852 | |
| 3853 | // struct _ivar_t { |
| 3854 | // unsigned long int *offset; // pointer to ivar offset location |
| 3855 | // char *name; |
| 3856 | // char *type; |
| 3857 | // uint32_t alignment; |
| 3858 | // uint32_t size; |
| 3859 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3860 | IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy), |
| 3861 | Int8PtrTy, |
| 3862 | Int8PtrTy, |
| 3863 | IntTy, |
| 3864 | IntTy, |
| 3865 | NULL); |
| 3866 | CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy); |
| 3867 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3868 | // struct _ivar_list_t { |
| 3869 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 3870 | // uint32 count; |
| 3871 | // struct _iver_t list[count]; |
| 3872 | // } |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3873 | IvarListnfABITy = llvm::StructType::get(IntTy, |
| 3874 | IntTy, |
| 3875 | llvm::ArrayType::get( |
| 3876 | IvarnfABITy, 0), |
| 3877 | NULL); |
| 3878 | CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy); |
| 3879 | |
| 3880 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3881 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3882 | // struct _class_ro_t { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3883 | // uint32_t const flags; |
| 3884 | // uint32_t const instanceStart; |
| 3885 | // uint32_t const instanceSize; |
| 3886 | // uint32_t const reserved; // only when building for 64bit targets |
| 3887 | // const uint8_t * const ivarLayout; |
| 3888 | // const char *const name; |
| 3889 | // const struct _method_list_t * const baseMethods; |
| 3890 | // const struct _objc_protocol_list *const baseProtocols; |
| 3891 | // const struct _ivar_list_t *const ivars; |
| 3892 | // const uint8_t * const weakIvarLayout; |
| 3893 | // const struct _prop_list_t * const properties; |
| 3894 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3895 | |
| 3896 | // FIXME. Add 'reserved' field in 64bit abi mode! |
| 3897 | ClassRonfABITy = llvm::StructType::get(IntTy, |
| 3898 | IntTy, |
| 3899 | IntTy, |
| 3900 | Int8PtrTy, |
| 3901 | Int8PtrTy, |
| 3902 | MethodListnfABIPtrTy, |
| 3903 | ProtocolListnfABIPtrTy, |
| 3904 | IvarListnfABIPtrTy, |
| 3905 | Int8PtrTy, |
| 3906 | PropertyListPtrTy, |
| 3907 | NULL); |
| 3908 | CGM.getModule().addTypeName("struct._class_ro_t", |
| 3909 | ClassRonfABITy); |
| 3910 | |
| 3911 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 3912 | std::vector<const llvm::Type*> Params; |
| 3913 | Params.push_back(ObjectPtrTy); |
| 3914 | Params.push_back(SelectorPtrTy); |
| 3915 | ImpnfABITy = llvm::PointerType::getUnqual( |
| 3916 | llvm::FunctionType::get(ObjectPtrTy, Params, false)); |
| 3917 | |
| 3918 | // struct _class_t { |
| 3919 | // struct _class_t *isa; |
| 3920 | // struct _class_t * const superclass; |
| 3921 | // void *cache; |
| 3922 | // IMP *vtable; |
| 3923 | // struct class_ro_t *ro; |
| 3924 | // } |
| 3925 | |
| 3926 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3927 | ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3928 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3929 | CachePtrTy, |
| 3930 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 3931 | llvm::PointerType::getUnqual( |
| 3932 | ClassRonfABITy), |
| 3933 | NULL); |
| 3934 | CGM.getModule().addTypeName("struct._class_t", ClassnfABITy); |
| 3935 | |
| 3936 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo( |
| 3937 | ClassnfABITy); |
| 3938 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3939 | // LLVM for struct _class_t * |
| 3940 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
| 3941 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3942 | // struct _category_t { |
| 3943 | // const char * const name; |
| 3944 | // struct _class_t *const cls; |
| 3945 | // const struct _method_list_t * const instance_methods; |
| 3946 | // const struct _method_list_t * const class_methods; |
| 3947 | // const struct _protocol_list_t * const protocols; |
| 3948 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3949 | // } |
| 3950 | CategorynfABITy = llvm::StructType::get(Int8PtrTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3951 | ClassnfABIPtrTy, |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3952 | MethodListnfABIPtrTy, |
| 3953 | MethodListnfABIPtrTy, |
| 3954 | ProtocolListnfABIPtrTy, |
| 3955 | PropertyListPtrTy, |
| 3956 | NULL); |
| 3957 | CGM.getModule().addTypeName("struct._category_t", CategorynfABITy); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3958 | |
| 3959 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3960 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3961 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3962 | |
| 3963 | // MessageRefTy - LLVM for: |
| 3964 | // struct _message_ref_t { |
| 3965 | // IMP messenger; |
| 3966 | // SEL name; |
| 3967 | // }; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3968 | |
| 3969 | // First the clang type for struct _message_ref_t |
| 3970 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3971 | SourceLocation(), |
| 3972 | &Ctx.Idents.get("_message_ref_t")); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3973 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3974 | Ctx.VoidPtrTy, 0, false)); |
| 3975 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3976 | Ctx.getObjCSelType(), 0, false)); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3977 | RD->completeDefinition(Ctx); |
| 3978 | |
| 3979 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 3980 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 3981 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3982 | |
| 3983 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 3984 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
| 3985 | |
| 3986 | // SuperMessageRefTy - LLVM for: |
| 3987 | // struct _super_message_ref_t { |
| 3988 | // SUPER_IMP messenger; |
| 3989 | // SEL name; |
| 3990 | // }; |
| 3991 | SuperMessageRefTy = llvm::StructType::get(ImpnfABITy, |
| 3992 | SelectorPtrTy, |
| 3993 | NULL); |
| 3994 | CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy); |
| 3995 | |
| 3996 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 3997 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
| 3998 | |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 3999 | |
| 4000 | // struct objc_typeinfo { |
| 4001 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 4002 | // const char* name; // c++ typeinfo string |
| 4003 | // Class cls; |
| 4004 | // }; |
| 4005 | EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy), |
| 4006 | Int8PtrTy, |
| 4007 | ClassnfABIPtrTy, |
| 4008 | NULL); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 4009 | CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 4010 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4011 | } |
| 4012 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4013 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
| 4014 | FinishNonFragileABIModule(); |
| 4015 | |
| 4016 | return NULL; |
| 4017 | } |
| 4018 | |
| 4019 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 4020 | // nonfragile abi has no module definition. |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4021 | |
| 4022 | // Build list of all implemented classe addresses in array |
| 4023 | // L_OBJC_LABEL_CLASS_$. |
| 4024 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$ |
| 4025 | // list of 'nonlazy' implementations (defined as those with a +load{} |
| 4026 | // method!!). |
| 4027 | unsigned NumClasses = DefinedClasses.size(); |
| 4028 | if (NumClasses) { |
| 4029 | std::vector<llvm::Constant*> Symbols(NumClasses); |
| 4030 | for (unsigned i=0; i<NumClasses; i++) |
| 4031 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 4032 | ObjCTypes.Int8PtrTy); |
| 4033 | llvm::Constant* Init = |
| 4034 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 4035 | NumClasses), |
| 4036 | Symbols); |
| 4037 | |
| 4038 | llvm::GlobalVariable *GV = |
| 4039 | new llvm::GlobalVariable(Init->getType(), false, |
| 4040 | llvm::GlobalValue::InternalLinkage, |
| 4041 | Init, |
| 4042 | "\01L_OBJC_LABEL_CLASS_$", |
| 4043 | &CGM.getModule()); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 4044 | GV->setAlignment(8); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4045 | GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip"); |
| 4046 | UsedGlobals.push_back(GV); |
| 4047 | } |
| 4048 | |
| 4049 | // Build list of all implemented category addresses in array |
| 4050 | // L_OBJC_LABEL_CATEGORY_$. |
| 4051 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$ |
| 4052 | // list of 'nonlazy' category implementations (defined as those with a +load{} |
| 4053 | // method!!). |
| 4054 | unsigned NumCategory = DefinedCategories.size(); |
| 4055 | if (NumCategory) { |
| 4056 | std::vector<llvm::Constant*> Symbols(NumCategory); |
| 4057 | for (unsigned i=0; i<NumCategory; i++) |
| 4058 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 4059 | ObjCTypes.Int8PtrTy); |
| 4060 | llvm::Constant* Init = |
| 4061 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 4062 | NumCategory), |
| 4063 | Symbols); |
| 4064 | |
| 4065 | llvm::GlobalVariable *GV = |
| 4066 | new llvm::GlobalVariable(Init->getType(), false, |
| 4067 | llvm::GlobalValue::InternalLinkage, |
| 4068 | Init, |
| 4069 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 4070 | &CGM.getModule()); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 4071 | GV->setAlignment(8); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4072 | GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip"); |
| 4073 | UsedGlobals.push_back(GV); |
| 4074 | } |
| 4075 | |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4076 | // static int L_OBJC_IMAGE_INFO[2] = { 0, flags }; |
| 4077 | // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0 |
| 4078 | std::vector<llvm::Constant*> Values(2); |
| 4079 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0); |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4080 | unsigned int flags = 0; |
Fariborz Jahanian | 66a5c2c | 2009-02-24 23:34:44 +0000 | [diff] [blame] | 4081 | // FIXME: Fix and continue? |
| 4082 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 4083 | flags |= eImageInfo_GarbageCollected; |
| 4084 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 4085 | flags |= eImageInfo_GCOnly; |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4086 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4087 | llvm::Constant* Init = llvm::ConstantArray::get( |
| 4088 | llvm::ArrayType::get(ObjCTypes.IntTy, 2), |
| 4089 | Values); |
| 4090 | llvm::GlobalVariable *IMGV = |
| 4091 | new llvm::GlobalVariable(Init->getType(), false, |
| 4092 | llvm::GlobalValue::InternalLinkage, |
| 4093 | Init, |
| 4094 | "\01L_OBJC_IMAGE_INFO", |
| 4095 | &CGM.getModule()); |
| 4096 | IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); |
Daniel Dunbar | 325f758 | 2009-04-23 08:03:21 +0000 | [diff] [blame] | 4097 | IMGV->setConstant(true); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4098 | UsedGlobals.push_back(IMGV); |
| 4099 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4100 | std::vector<llvm::Constant*> Used; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4101 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4102 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
| 4103 | e = UsedGlobals.end(); i != e; ++i) { |
| 4104 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
| 4105 | } |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4106 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4107 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
| 4108 | llvm::GlobalValue *GV = |
| 4109 | new llvm::GlobalVariable(AT, false, |
| 4110 | llvm::GlobalValue::AppendingLinkage, |
| 4111 | llvm::ConstantArray::get(AT, Used), |
| 4112 | "llvm.used", |
| 4113 | &CGM.getModule()); |
| 4114 | |
| 4115 | GV->setSection("llvm.metadata"); |
| 4116 | |
| 4117 | } |
| 4118 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4119 | // Metadata flags |
| 4120 | enum MetaDataDlags { |
| 4121 | CLS = 0x0, |
| 4122 | CLS_META = 0x1, |
| 4123 | CLS_ROOT = 0x2, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4124 | OBJC2_CLS_HIDDEN = 0x10, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4125 | CLS_EXCEPTION = 0x20 |
| 4126 | }; |
| 4127 | /// BuildClassRoTInitializer - generate meta-data for: |
| 4128 | /// struct _class_ro_t { |
| 4129 | /// uint32_t const flags; |
| 4130 | /// uint32_t const instanceStart; |
| 4131 | /// uint32_t const instanceSize; |
| 4132 | /// uint32_t const reserved; // only when building for 64bit targets |
| 4133 | /// const uint8_t * const ivarLayout; |
| 4134 | /// const char *const name; |
| 4135 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4136 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4137 | /// const struct _ivar_list_t *const ivars; |
| 4138 | /// const uint8_t * const weakIvarLayout; |
| 4139 | /// const struct _prop_list_t * const properties; |
| 4140 | /// } |
| 4141 | /// |
| 4142 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
| 4143 | unsigned flags, |
| 4144 | unsigned InstanceStart, |
| 4145 | unsigned InstanceSize, |
| 4146 | const ObjCImplementationDecl *ID) { |
| 4147 | std::string ClassName = ID->getNameAsString(); |
| 4148 | std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets! |
| 4149 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 4150 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 4151 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
| 4152 | // FIXME. For 64bit targets add 0 here. |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 4153 | Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4154 | : BuildIvarLayout(ID, true); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4155 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4156 | // const struct _method_list_t * const baseMethods; |
| 4157 | std::vector<llvm::Constant*> Methods; |
| 4158 | std::string MethodListName("\01l_OBJC_$_"); |
| 4159 | if (flags & CLS_META) { |
| 4160 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4161 | for (ObjCImplementationDecl::classmeth_iterator |
| 4162 | i = ID->classmeth_begin(CGM.getContext()), |
| 4163 | e = ID->classmeth_end(CGM.getContext()); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4164 | // Class methods should always be defined. |
| 4165 | Methods.push_back(GetMethodConstant(*i)); |
| 4166 | } |
| 4167 | } else { |
| 4168 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4169 | for (ObjCImplementationDecl::instmeth_iterator |
| 4170 | i = ID->instmeth_begin(CGM.getContext()), |
| 4171 | e = ID->instmeth_end(CGM.getContext()); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4172 | // Instance methods should always be defined. |
| 4173 | Methods.push_back(GetMethodConstant(*i)); |
| 4174 | } |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4175 | for (ObjCImplementationDecl::propimpl_iterator |
| 4176 | i = ID->propimpl_begin(CGM.getContext()), |
| 4177 | e = ID->propimpl_end(CGM.getContext()); i != e; ++i) { |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4178 | ObjCPropertyImplDecl *PID = *i; |
| 4179 | |
| 4180 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 4181 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 4182 | |
| 4183 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 4184 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4185 | Methods.push_back(C); |
| 4186 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 4187 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4188 | Methods.push_back(C); |
| 4189 | } |
| 4190 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4191 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4192 | Values[ 5] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4193 | "__DATA, __objc_const", Methods); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4194 | |
| 4195 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4196 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
| 4197 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
| 4198 | + OID->getNameAsString(), |
| 4199 | OID->protocol_begin(), |
| 4200 | OID->protocol_end()); |
| 4201 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4202 | if (flags & CLS_META) |
| 4203 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4204 | else |
| 4205 | Values[ 7] = EmitIvarList(ID); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 4206 | Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4207 | : BuildIvarLayout(ID, false); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4208 | if (flags & CLS_META) |
| 4209 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4210 | else |
| 4211 | Values[ 9] = |
| 4212 | EmitPropertyList( |
| 4213 | "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
| 4214 | ID, ID->getClassInterface(), ObjCTypes); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4215 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
| 4216 | Values); |
| 4217 | llvm::GlobalVariable *CLASS_RO_GV = |
| 4218 | new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false, |
| 4219 | llvm::GlobalValue::InternalLinkage, |
| 4220 | Init, |
| 4221 | (flags & CLS_META) ? |
| 4222 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 4223 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName, |
| 4224 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4225 | CLASS_RO_GV->setAlignment( |
| 4226 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4227 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4228 | return CLASS_RO_GV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4229 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4230 | } |
| 4231 | |
| 4232 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 4233 | /// for the given ClassName for: |
| 4234 | /// struct _class_t { |
| 4235 | /// struct _class_t *isa; |
| 4236 | /// struct _class_t * const superclass; |
| 4237 | /// void *cache; |
| 4238 | /// IMP *vtable; |
| 4239 | /// struct class_ro_t *ro; |
| 4240 | /// } |
| 4241 | /// |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4242 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
| 4243 | std::string &ClassName, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4244 | llvm::Constant *IsAGV, |
| 4245 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4246 | llvm::Constant *ClassRoGV, |
| 4247 | bool HiddenVisibility) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4248 | std::vector<llvm::Constant*> Values(5); |
| 4249 | Values[0] = IsAGV; |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4250 | Values[1] = SuperClassGV |
| 4251 | ? SuperClassGV |
| 4252 | : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4253 | Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar |
| 4254 | Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar |
| 4255 | Values[4] = ClassRoGV; // &CLASS_RO_GV |
| 4256 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
| 4257 | Values); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4258 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 4259 | GV->setInitializer(Init); |
Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 4260 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4261 | GV->setAlignment( |
| 4262 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4263 | if (HiddenVisibility) |
| 4264 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4265 | return GV; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4266 | } |
| 4267 | |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4268 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCInterfaceDecl *OID, |
| 4269 | uint32_t &InstanceStart, |
| 4270 | uint32_t &InstanceSize) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4271 | // Find first and last (non-padding) ivars in this interface. |
| 4272 | |
| 4273 | // FIXME: Use iterator. |
| 4274 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
| 4275 | GetNamedIvarList(OID, OIvars); |
| 4276 | |
| 4277 | if (OIvars.empty()) { |
| 4278 | InstanceStart = InstanceSize = 0; |
| 4279 | return; |
Daniel Dunbar | d4ae6c0 | 2009-04-22 04:39:47 +0000 | [diff] [blame] | 4280 | } |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4281 | |
| 4282 | const ObjCIvarDecl *First = OIvars.front(); |
| 4283 | const ObjCIvarDecl *Last = OIvars.back(); |
| 4284 | |
| 4285 | InstanceStart = ComputeIvarBaseOffset(CGM, OID, First); |
| 4286 | const llvm::Type *FieldTy = |
| 4287 | CGM.getTypes().ConvertTypeForMem(Last->getType()); |
| 4288 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 4289 | // FIXME. This breaks compatibility with llvm-gcc-4.2 (but makes it compatible |
| 4290 | // with gcc-4.2). We postpone this for now. |
| 4291 | #if 0 |
| 4292 | if (Last->isBitField()) { |
| 4293 | Expr *BitWidth = Last->getBitWidth(); |
| 4294 | uint64_t BitFieldSize = |
| 4295 | BitWidth->getIntegerConstantExprValue(CGM.getContext()).getZExtValue(); |
| 4296 | Size = (BitFieldSize / 8) + ((BitFieldSize % 8) != 0); |
| 4297 | } |
| 4298 | #endif |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4299 | InstanceSize = ComputeIvarBaseOffset(CGM, OID, Last) + Size; |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4300 | } |
| 4301 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4302 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 4303 | std::string ClassName = ID->getNameAsString(); |
| 4304 | if (!ObjCEmptyCacheVar) { |
| 4305 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4306 | ObjCTypes.CacheTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4307 | false, |
| 4308 | llvm::GlobalValue::ExternalLinkage, |
| 4309 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4310 | "_objc_empty_cache", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4311 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4312 | |
| 4313 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4314 | ObjCTypes.ImpnfABITy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4315 | false, |
| 4316 | llvm::GlobalValue::ExternalLinkage, |
| 4317 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4318 | "_objc_empty_vtable", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4319 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4320 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4321 | assert(ID->getClassInterface() && |
| 4322 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 4323 | // FIXME: Is this correct (that meta class size is never computed)? |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4324 | uint32_t InstanceStart = |
| 4325 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy); |
| 4326 | uint32_t InstanceSize = InstanceStart; |
| 4327 | uint32_t flags = CLS_META; |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4328 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 4329 | std::string ObjCClassName(getClassSymbolPrefix()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4330 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4331 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4332 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4333 | bool classIsHidden = |
| 4334 | CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4335 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4336 | flags |= OBJC2_CLS_HIDDEN; |
| 4337 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4338 | // class is root |
| 4339 | flags |= CLS_ROOT; |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4340 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4341 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4342 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4343 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4344 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 4345 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 4346 | Root = Super; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4347 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4348 | // work on super class metadata symbol. |
| 4349 | std::string SuperClassName = |
| 4350 | ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4351 | SuperClassGV = GetClassGlobal(SuperClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4352 | } |
| 4353 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 4354 | InstanceStart, |
| 4355 | InstanceSize,ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4356 | std::string TClassName = ObjCMetaClassName + ClassName; |
| 4357 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4358 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 4359 | classIsHidden); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4360 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4361 | // Metadata for the class |
| 4362 | flags = CLS; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4363 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4364 | flags |= OBJC2_CLS_HIDDEN; |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4365 | |
| 4366 | if (hasObjCExceptionAttribute(ID->getClassInterface())) |
| 4367 | flags |= CLS_EXCEPTION; |
| 4368 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4369 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4370 | flags |= CLS_ROOT; |
| 4371 | SuperClassGV = 0; |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 4372 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4373 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4374 | std::string RootClassName = |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4375 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4376 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4377 | } |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4378 | GetClassSizeInfo(ID->getClassInterface(), InstanceStart, InstanceSize); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4379 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4380 | InstanceStart, |
| 4381 | InstanceSize, |
| 4382 | ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4383 | |
| 4384 | TClassName = ObjCClassName + ClassName; |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4385 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4386 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 4387 | classIsHidden); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4388 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4389 | |
| 4390 | // Force the definition of the EHType if necessary. |
| 4391 | if (flags & CLS_EXCEPTION) |
| 4392 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4393 | } |
| 4394 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4395 | /// GenerateProtocolRef - This routine is called to generate code for |
| 4396 | /// a protocol reference expression; as in: |
| 4397 | /// @code |
| 4398 | /// @protocol(Proto1); |
| 4399 | /// @endcode |
| 4400 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 4401 | /// which will hold address of the protocol meta-data. |
| 4402 | /// |
| 4403 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
| 4404 | const ObjCProtocolDecl *PD) { |
| 4405 | |
Fariborz Jahanian | 960cd06 | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 4406 | // This routine is called for @protocol only. So, we must build definition |
| 4407 | // of protocol's meta-data (not a reference to it!) |
| 4408 | // |
| 4409 | llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4410 | ObjCTypes.ExternalProtocolPtrTy); |
| 4411 | |
| 4412 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
| 4413 | ProtocolName += PD->getNameAsCString(); |
| 4414 | |
| 4415 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 4416 | if (PTGV) |
| 4417 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4418 | PTGV = new llvm::GlobalVariable( |
| 4419 | Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4420 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4421 | Init, |
| 4422 | ProtocolName, |
| 4423 | &CGM.getModule()); |
| 4424 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 4425 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4426 | UsedGlobals.push_back(PTGV); |
| 4427 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4428 | } |
| 4429 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4430 | /// GenerateCategory - Build metadata for a category implementation. |
| 4431 | /// struct _category_t { |
| 4432 | /// const char * const name; |
| 4433 | /// struct _class_t *const cls; |
| 4434 | /// const struct _method_list_t * const instance_methods; |
| 4435 | /// const struct _method_list_t * const class_methods; |
| 4436 | /// const struct _protocol_list_t * const protocols; |
| 4437 | /// const struct _prop_list_t * const properties; |
| 4438 | /// } |
| 4439 | /// |
| 4440 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) |
| 4441 | { |
| 4442 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4443 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
| 4444 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4445 | "_$_" + OCD->getNameAsString()); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4446 | std::string ExtClassName(getClassSymbolPrefix() + |
| 4447 | Interface->getNameAsString()); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4448 | |
| 4449 | std::vector<llvm::Constant*> Values(6); |
| 4450 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 4451 | // meta-class entry symbol |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4452 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4453 | Values[1] = ClassGV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4454 | std::vector<llvm::Constant*> Methods; |
| 4455 | std::string MethodListName(Prefix); |
| 4456 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
| 4457 | "_$_" + OCD->getNameAsString(); |
| 4458 | |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4459 | for (ObjCCategoryImplDecl::instmeth_iterator |
| 4460 | i = OCD->instmeth_begin(CGM.getContext()), |
| 4461 | e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4462 | // Instance methods should always be defined. |
| 4463 | Methods.push_back(GetMethodConstant(*i)); |
| 4464 | } |
| 4465 | |
| 4466 | Values[2] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4467 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4468 | Methods); |
| 4469 | |
| 4470 | MethodListName = Prefix; |
| 4471 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 4472 | OCD->getNameAsString(); |
| 4473 | Methods.clear(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4474 | for (ObjCCategoryImplDecl::classmeth_iterator |
| 4475 | i = OCD->classmeth_begin(CGM.getContext()), |
| 4476 | e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4477 | // Class methods should always be defined. |
| 4478 | Methods.push_back(GetMethodConstant(*i)); |
| 4479 | } |
| 4480 | |
| 4481 | Values[3] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4482 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4483 | Methods); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4484 | const ObjCCategoryDecl *Category = |
| 4485 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4486 | if (Category) { |
| 4487 | std::string ExtName(Interface->getNameAsString() + "_$_" + |
| 4488 | OCD->getNameAsString()); |
| 4489 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
| 4490 | + Interface->getNameAsString() + "_$_" |
| 4491 | + Category->getNameAsString(), |
| 4492 | Category->protocol_begin(), |
| 4493 | Category->protocol_end()); |
| 4494 | Values[5] = |
| 4495 | EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
| 4496 | OCD, Category, ObjCTypes); |
| 4497 | } |
| 4498 | else { |
| 4499 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4500 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4501 | } |
| 4502 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4503 | llvm::Constant *Init = |
| 4504 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
| 4505 | Values); |
| 4506 | llvm::GlobalVariable *GCATV |
| 4507 | = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy, |
| 4508 | false, |
| 4509 | llvm::GlobalValue::InternalLinkage, |
| 4510 | Init, |
| 4511 | ExtCatName, |
| 4512 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4513 | GCATV->setAlignment( |
| 4514 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4515 | GCATV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4516 | UsedGlobals.push_back(GCATV); |
| 4517 | DefinedCategories.push_back(GCATV); |
| 4518 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4519 | |
| 4520 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 4521 | /// given method if it has been defined. The result is null if the |
| 4522 | /// method has not been defined. The return value has type MethodPtrTy. |
| 4523 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
| 4524 | const ObjCMethodDecl *MD) { |
| 4525 | // FIXME: Use DenseMap::lookup |
| 4526 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 4527 | if (!Fn) |
| 4528 | return 0; |
| 4529 | |
| 4530 | std::vector<llvm::Constant*> Method(3); |
| 4531 | Method[0] = |
| 4532 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4533 | ObjCTypes.SelectorPtrTy); |
| 4534 | Method[1] = GetMethodVarType(MD); |
| 4535 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 4536 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 4537 | } |
| 4538 | |
| 4539 | /// EmitMethodList - Build meta-data for method declarations |
| 4540 | /// struct _method_list_t { |
| 4541 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 4542 | /// uint32_t method_count; |
| 4543 | /// struct _objc_method method_list[method_count]; |
| 4544 | /// } |
| 4545 | /// |
| 4546 | llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList( |
| 4547 | const std::string &Name, |
| 4548 | const char *Section, |
| 4549 | const ConstantVector &Methods) { |
| 4550 | // Return null for empty list. |
| 4551 | if (Methods.empty()) |
| 4552 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
| 4553 | |
| 4554 | std::vector<llvm::Constant*> Values(3); |
| 4555 | // sizeof(struct _objc_method) |
| 4556 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy); |
| 4557 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4558 | // method_count |
| 4559 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 4560 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 4561 | Methods.size()); |
| 4562 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 4563 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4564 | |
| 4565 | llvm::GlobalVariable *GV = |
| 4566 | new llvm::GlobalVariable(Init->getType(), false, |
| 4567 | llvm::GlobalValue::InternalLinkage, |
| 4568 | Init, |
| 4569 | Name, |
| 4570 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4571 | GV->setAlignment( |
| 4572 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4573 | GV->setSection(Section); |
| 4574 | UsedGlobals.push_back(GV); |
| 4575 | return llvm::ConstantExpr::getBitCast(GV, |
| 4576 | ObjCTypes.MethodListnfABIPtrTy); |
| 4577 | } |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4578 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4579 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 4580 | /// the given ivar. |
| 4581 | /// |
| 4582 | llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 4583 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4584 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 4585 | std::string Name = "OBJC_IVAR_$_" + |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4586 | getInterfaceDeclForIvar(ID, Ivar, CGM.getContext())->getNameAsString() + |
| 4587 | '.' + Ivar->getNameAsString(); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4588 | llvm::GlobalVariable *IvarOffsetGV = |
| 4589 | CGM.getModule().getGlobalVariable(Name); |
| 4590 | if (!IvarOffsetGV) |
| 4591 | IvarOffsetGV = |
| 4592 | new llvm::GlobalVariable(ObjCTypes.LongTy, |
| 4593 | false, |
| 4594 | llvm::GlobalValue::ExternalLinkage, |
| 4595 | 0, |
| 4596 | Name, |
| 4597 | &CGM.getModule()); |
| 4598 | return IvarOffsetGV; |
| 4599 | } |
| 4600 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4601 | llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar( |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4602 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4603 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4604 | unsigned long int Offset) { |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4605 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
| 4606 | IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy, |
| 4607 | Offset)); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4608 | IvarOffsetGV->setAlignment( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4609 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy)); |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4610 | |
| 4611 | // FIXME: This matches gcc, but shouldn't the visibility be set on |
| 4612 | // the use as well (i.e., in ObjCIvarOffsetVariable). |
| 4613 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 4614 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
| 4615 | CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden) |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4616 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4617 | else |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4618 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4619 | IvarOffsetGV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4620 | return IvarOffsetGV; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4621 | } |
| 4622 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4623 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 4624 | /// implementation. The return value has type |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4625 | /// IvarListnfABIPtrTy. |
| 4626 | /// struct _ivar_t { |
| 4627 | /// unsigned long int *offset; // pointer to ivar offset location |
| 4628 | /// char *name; |
| 4629 | /// char *type; |
| 4630 | /// uint32_t alignment; |
| 4631 | /// uint32_t size; |
| 4632 | /// } |
| 4633 | /// struct _ivar_list_t { |
| 4634 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 4635 | /// uint32 count; |
| 4636 | /// struct _iver_t list[count]; |
| 4637 | /// } |
| 4638 | /// |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4639 | |
| 4640 | void CGObjCCommonMac::GetNamedIvarList(const ObjCInterfaceDecl *OID, |
| 4641 | llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const { |
| 4642 | for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), |
| 4643 | E = OID->ivar_end(); I != E; ++I) { |
| 4644 | // Ignore unnamed bit-fields. |
| 4645 | if (!(*I)->getDeclName()) |
| 4646 | continue; |
| 4647 | |
| 4648 | Res.push_back(*I); |
| 4649 | } |
| 4650 | |
| 4651 | for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(CGM.getContext()), |
| 4652 | E = OID->prop_end(CGM.getContext()); I != E; ++I) |
| 4653 | if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl()) |
| 4654 | Res.push_back(IV); |
| 4655 | } |
| 4656 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4657 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
| 4658 | const ObjCImplementationDecl *ID) { |
| 4659 | |
| 4660 | std::vector<llvm::Constant*> Ivars, Ivar(5); |
| 4661 | |
| 4662 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4663 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
| 4664 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4665 | // FIXME. Consolidate this with similar code in GenerateClass. |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 4666 | |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4667 | // Collect declared and synthesized ivars in a small vector. |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4668 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4669 | GetNamedIvarList(OID, OIvars); |
Fariborz Jahanian | 99eee36 | 2009-04-01 19:37:34 +0000 | [diff] [blame] | 4670 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4671 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 4672 | ObjCIvarDecl *IVD = OIvars[i]; |
Daniel Dunbar | 3eec8aa | 2009-04-20 05:53:40 +0000 | [diff] [blame] | 4673 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD, |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4674 | ComputeIvarBaseOffset(CGM, OID, IVD)); |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4675 | Ivar[1] = GetMethodVarName(IVD->getIdentifier()); |
| 4676 | Ivar[2] = GetMethodVarType(IVD); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4677 | const llvm::Type *FieldTy = |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4678 | CGM.getTypes().ConvertTypeForMem(IVD->getType()); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4679 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
| 4680 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4681 | IVD->getType().getTypePtr()) >> 3; |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4682 | Align = llvm::Log2_32(Align); |
| 4683 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4684 | // NOTE. Size of a bitfield does not match gcc's, because of the |
| 4685 | // way bitfields are treated special in each. But I am told that |
| 4686 | // 'size' for bitfield ivars is ignored by the runtime so it does |
| 4687 | // not matter. If it matters, there is enough info to get the |
| 4688 | // bitfield right! |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4689 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4690 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
| 4691 | } |
| 4692 | // Return null for empty list. |
| 4693 | if (Ivars.empty()) |
| 4694 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4695 | std::vector<llvm::Constant*> Values(3); |
| 4696 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy); |
| 4697 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4698 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 4699 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
| 4700 | Ivars.size()); |
| 4701 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
| 4702 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4703 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 4704 | llvm::GlobalVariable *GV = |
| 4705 | new llvm::GlobalVariable(Init->getType(), false, |
| 4706 | llvm::GlobalValue::InternalLinkage, |
| 4707 | Init, |
| 4708 | Prefix + OID->getNameAsString(), |
| 4709 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4710 | GV->setAlignment( |
| 4711 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4712 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4713 | |
| 4714 | UsedGlobals.push_back(GV); |
| 4715 | return llvm::ConstantExpr::getBitCast(GV, |
| 4716 | ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4717 | } |
| 4718 | |
| 4719 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
| 4720 | const ObjCProtocolDecl *PD) { |
| 4721 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4722 | |
| 4723 | if (!Entry) { |
| 4724 | // We use the initializer as a marker of whether this is a forward |
| 4725 | // reference or not. At module finalization we add the empty |
| 4726 | // contents for protocols which were referenced but never defined. |
| 4727 | Entry = |
| 4728 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
| 4729 | llvm::GlobalValue::ExternalLinkage, |
| 4730 | 0, |
| 4731 | "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(), |
| 4732 | &CGM.getModule()); |
| 4733 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 4734 | UsedGlobals.push_back(Entry); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4735 | } |
| 4736 | |
| 4737 | return Entry; |
| 4738 | } |
| 4739 | |
| 4740 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 4741 | /// @code |
| 4742 | /// struct _protocol_t { |
| 4743 | /// id isa; // NULL |
| 4744 | /// const char * const protocol_name; |
| 4745 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 4746 | /// const struct method_list_t * const instance_methods; |
| 4747 | /// const struct method_list_t * const class_methods; |
| 4748 | /// const struct method_list_t *optionalInstanceMethods; |
| 4749 | /// const struct method_list_t *optionalClassMethods; |
| 4750 | /// const struct _prop_list_t * properties; |
| 4751 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 4752 | /// const uint32_t flags; // = 0 |
| 4753 | /// } |
| 4754 | /// @endcode |
| 4755 | /// |
| 4756 | |
| 4757 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
| 4758 | const ObjCProtocolDecl *PD) { |
| 4759 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4760 | |
| 4761 | // Early exit if a defining object has already been generated. |
| 4762 | if (Entry && Entry->hasInitializer()) |
| 4763 | return Entry; |
| 4764 | |
| 4765 | const char *ProtocolName = PD->getNameAsCString(); |
| 4766 | |
| 4767 | // Construct method lists. |
| 4768 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 4769 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4770 | for (ObjCProtocolDecl::instmeth_iterator |
| 4771 | i = PD->instmeth_begin(CGM.getContext()), |
| 4772 | e = PD->instmeth_end(CGM.getContext()); |
| 4773 | i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4774 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4775 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4776 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4777 | OptInstanceMethods.push_back(C); |
| 4778 | } else { |
| 4779 | InstanceMethods.push_back(C); |
| 4780 | } |
| 4781 | } |
| 4782 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4783 | for (ObjCProtocolDecl::classmeth_iterator |
| 4784 | i = PD->classmeth_begin(CGM.getContext()), |
| 4785 | e = PD->classmeth_end(CGM.getContext()); |
| 4786 | i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4787 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4788 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4789 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4790 | OptClassMethods.push_back(C); |
| 4791 | } else { |
| 4792 | ClassMethods.push_back(C); |
| 4793 | } |
| 4794 | } |
| 4795 | |
| 4796 | std::vector<llvm::Constant*> Values(10); |
| 4797 | // isa is NULL |
| 4798 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
| 4799 | Values[1] = GetClassName(PD->getIdentifier()); |
| 4800 | Values[2] = EmitProtocolList( |
| 4801 | "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(), |
| 4802 | PD->protocol_begin(), |
| 4803 | PD->protocol_end()); |
| 4804 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4805 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4806 | + PD->getNameAsString(), |
| 4807 | "__DATA, __objc_const", |
| 4808 | InstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4809 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4810 | + PD->getNameAsString(), |
| 4811 | "__DATA, __objc_const", |
| 4812 | ClassMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4813 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4814 | + PD->getNameAsString(), |
| 4815 | "__DATA, __objc_const", |
| 4816 | OptInstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4817 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4818 | + PD->getNameAsString(), |
| 4819 | "__DATA, __objc_const", |
| 4820 | OptClassMethods); |
| 4821 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(), |
| 4822 | 0, PD, ObjCTypes); |
| 4823 | uint32_t Size = |
| 4824 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy); |
| 4825 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4826 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
| 4827 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
| 4828 | Values); |
| 4829 | |
| 4830 | if (Entry) { |
| 4831 | // Already created, fix the linkage and update the initializer. |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4832 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4833 | Entry->setInitializer(Init); |
| 4834 | } else { |
| 4835 | Entry = |
| 4836 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4837 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4838 | Init, |
| 4839 | std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName, |
| 4840 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4841 | Entry->setAlignment( |
| 4842 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4843 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4844 | } |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4845 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4846 | |
| 4847 | // Use this protocol meta-data to build protocol list table in section |
| 4848 | // __DATA, __objc_protolist |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4849 | llvm::GlobalVariable *PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4850 | ObjCTypes.ProtocolnfABIPtrTy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4851 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4852 | Entry, |
| 4853 | std::string("\01l_OBJC_LABEL_PROTOCOL_$_") |
| 4854 | +ProtocolName, |
| 4855 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4856 | PTGV->setAlignment( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4857 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 4858 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4859 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4860 | UsedGlobals.push_back(PTGV); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4861 | return Entry; |
| 4862 | } |
| 4863 | |
| 4864 | /// EmitProtocolList - Generate protocol list meta-data: |
| 4865 | /// @code |
| 4866 | /// struct _protocol_list_t { |
| 4867 | /// long protocol_count; // Note, this is 32/64 bit |
| 4868 | /// struct _protocol_t[protocol_count]; |
| 4869 | /// } |
| 4870 | /// @endcode |
| 4871 | /// |
| 4872 | llvm::Constant * |
| 4873 | CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name, |
| 4874 | ObjCProtocolDecl::protocol_iterator begin, |
| 4875 | ObjCProtocolDecl::protocol_iterator end) { |
| 4876 | std::vector<llvm::Constant*> ProtocolRefs; |
| 4877 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4878 | // Just return null for empty protocol lists |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4879 | if (begin == end) |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4880 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4881 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4882 | // FIXME: We shouldn't need to do this lookup here, should we? |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4883 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
| 4884 | if (GV) |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4885 | return llvm::ConstantExpr::getBitCast(GV, |
| 4886 | ObjCTypes.ProtocolListnfABIPtrTy); |
| 4887 | |
| 4888 | for (; begin != end; ++begin) |
| 4889 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 4890 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4891 | // This list is null terminated. |
| 4892 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4893 | ObjCTypes.ProtocolnfABIPtrTy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4894 | |
| 4895 | std::vector<llvm::Constant*> Values(2); |
| 4896 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 4897 | Values[1] = |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4898 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4899 | ProtocolRefs.size()), |
| 4900 | ProtocolRefs); |
| 4901 | |
| 4902 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4903 | GV = new llvm::GlobalVariable(Init->getType(), false, |
| 4904 | llvm::GlobalValue::InternalLinkage, |
| 4905 | Init, |
| 4906 | Name, |
| 4907 | &CGM.getModule()); |
| 4908 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4909 | GV->setAlignment( |
| 4910 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4911 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4912 | return llvm::ConstantExpr::getBitCast(GV, |
| 4913 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4914 | } |
| 4915 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4916 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 4917 | /// struct _objc_method { |
| 4918 | /// SEL _cmd; |
| 4919 | /// char *method_type; |
| 4920 | /// char *_imp; |
| 4921 | /// } |
| 4922 | |
| 4923 | llvm::Constant * |
| 4924 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 4925 | std::vector<llvm::Constant*> Desc(3); |
| 4926 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4927 | ObjCTypes.SelectorPtrTy); |
| 4928 | Desc[1] = GetMethodVarType(MD); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4929 | // Protocol methods have no implementation. So, this entry is always NULL. |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4930 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 4931 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
| 4932 | } |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4933 | |
| 4934 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 4935 | /// This code gen. amounts to generating code for: |
| 4936 | /// @code |
| 4937 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 4938 | /// @encode |
| 4939 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4940 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4941 | CodeGen::CodeGenFunction &CGF, |
| 4942 | QualType ObjectTy, |
| 4943 | llvm::Value *BaseValue, |
| 4944 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4945 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 4946 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4947 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 4948 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4949 | } |
| 4950 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4951 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
| 4952 | CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 4953 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4954 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 4955 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar), |
| 4956 | false, "ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4957 | } |
| 4958 | |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4959 | CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend( |
| 4960 | CodeGen::CodeGenFunction &CGF, |
| 4961 | QualType ResultType, |
| 4962 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4963 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4964 | QualType Arg0Ty, |
| 4965 | bool IsSuper, |
| 4966 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4967 | // FIXME. Even though IsSuper is passes. This function doese not |
| 4968 | // handle calls to 'super' receivers. |
| 4969 | CodeGenTypes &Types = CGM.getTypes(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4970 | llvm::Value *Arg0 = Receiver; |
| 4971 | if (!IsSuper) |
| 4972 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4973 | |
| 4974 | // Find the message function name. |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 4975 | // FIXME. This is too much work to get the ABI-specific result type |
| 4976 | // needed to find the message name. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4977 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, |
| 4978 | llvm::SmallVector<QualType, 16>()); |
| 4979 | llvm::Constant *Fn; |
| 4980 | std::string Name("\01l_"); |
| 4981 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4982 | #if 0 |
| 4983 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4984 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4985 | Fn = ObjCTypes.getMessageSendIdStretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4986 | // FIXME. Is there a better way of getting these names. |
| 4987 | // They are available in RuntimeFunctions vector pair. |
| 4988 | Name += "objc_msgSendId_stret_fixup"; |
| 4989 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4990 | else |
| 4991 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4992 | if (IsSuper) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4993 | Fn = ObjCTypes.getMessageSendSuper2StretFixupFn(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4994 | Name += "objc_msgSendSuper2_stret_fixup"; |
| 4995 | } |
| 4996 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4997 | { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4998 | Fn = ObjCTypes.getMessageSendStretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4999 | Name += "objc_msgSend_stret_fixup"; |
| 5000 | } |
| 5001 | } |
Fariborz Jahanian | 1a6b368 | 2009-02-05 19:35:43 +0000 | [diff] [blame] | 5002 | else if (ResultType->isFloatingType() && |
| 5003 | // Selection of frret API only happens in 32bit nonfragile ABI. |
| 5004 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5005 | Fn = ObjCTypes.getMessageSendFpretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5006 | Name += "objc_msgSend_fpret_fixup"; |
| 5007 | } |
| 5008 | else { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5009 | #if 0 |
| 5010 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5011 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5012 | Fn = ObjCTypes.getMessageSendIdFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5013 | Name += "objc_msgSendId_fixup"; |
| 5014 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5015 | else |
| 5016 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5017 | if (IsSuper) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5018 | Fn = ObjCTypes.getMessageSendSuper2FixupFn(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5019 | Name += "objc_msgSendSuper2_fixup"; |
| 5020 | } |
| 5021 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5022 | { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5023 | Fn = ObjCTypes.getMessageSendFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5024 | Name += "objc_msgSend_fixup"; |
| 5025 | } |
| 5026 | } |
| 5027 | Name += '_'; |
| 5028 | std::string SelName(Sel.getAsString()); |
| 5029 | // Replace all ':' in selector name with '_' ouch! |
| 5030 | for(unsigned i = 0; i < SelName.size(); i++) |
| 5031 | if (SelName[i] == ':') |
| 5032 | SelName[i] = '_'; |
| 5033 | Name += SelName; |
| 5034 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5035 | if (!GV) { |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5036 | // Build message ref table entry. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5037 | std::vector<llvm::Constant*> Values(2); |
| 5038 | Values[0] = Fn; |
| 5039 | Values[1] = GetMethodVarName(Sel); |
| 5040 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 5041 | GV = new llvm::GlobalVariable(Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 5042 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5043 | Init, |
| 5044 | Name, |
| 5045 | &CGM.getModule()); |
| 5046 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | f59c1a6 | 2009-04-15 19:04:46 +0000 | [diff] [blame] | 5047 | GV->setAlignment(16); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5048 | GV->setSection("__DATA, __objc_msgrefs, coalesced"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5049 | } |
| 5050 | llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5051 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5052 | CallArgList ActualArgs; |
| 5053 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 5054 | ActualArgs.push_back(std::make_pair(RValue::get(Arg1), |
| 5055 | ObjCTypes.MessageRefCPtrTy)); |
| 5056 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5057 | const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs); |
| 5058 | llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0); |
| 5059 | Callee = CGF.Builder.CreateLoad(Callee); |
Fariborz Jahanian | 3ab75bd | 2009-02-14 21:25:36 +0000 | [diff] [blame] | 5060 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5061 | Callee = CGF.Builder.CreateBitCast(Callee, |
| 5062 | llvm::PointerType::getUnqual(FTy)); |
| 5063 | return CGF.EmitCall(FnInfo1, Callee, ActualArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5064 | } |
| 5065 | |
| 5066 | /// Generate code for a message send expression in the nonfragile abi. |
| 5067 | CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend( |
| 5068 | CodeGen::CodeGenFunction &CGF, |
| 5069 | QualType ResultType, |
| 5070 | Selector Sel, |
| 5071 | llvm::Value *Receiver, |
| 5072 | bool IsClassMessage, |
| 5073 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5074 | return EmitMessageSend(CGF, ResultType, Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5075 | Receiver, CGF.getContext().getObjCIdType(), |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5076 | false, CallArgs); |
| 5077 | } |
| 5078 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5079 | llvm::GlobalVariable * |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5080 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5081 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5082 | |
Daniel Dunbar | dfff230 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 5083 | if (!GV) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5084 | GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 5085 | llvm::GlobalValue::ExternalLinkage, |
| 5086 | 0, Name, &CGM.getModule()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5087 | } |
| 5088 | |
| 5089 | return GV; |
| 5090 | } |
| 5091 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5092 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5093 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5094 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 5095 | |
| 5096 | if (!Entry) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5097 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5098 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5099 | Entry = |
| 5100 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5101 | llvm::GlobalValue::InternalLinkage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5102 | ClassGV, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5103 | "\01L_OBJC_CLASSLIST_REFERENCES_$_", |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5104 | &CGM.getModule()); |
| 5105 | Entry->setAlignment( |
| 5106 | CGM.getTargetData().getPrefTypeAlignment( |
| 5107 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5108 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
| 5109 | UsedGlobals.push_back(Entry); |
| 5110 | } |
| 5111 | |
| 5112 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5113 | } |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5114 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5115 | llvm::Value * |
| 5116 | CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder, |
| 5117 | const ObjCInterfaceDecl *ID) { |
| 5118 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
| 5119 | |
| 5120 | if (!Entry) { |
| 5121 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5122 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
| 5123 | Entry = |
| 5124 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5125 | llvm::GlobalValue::InternalLinkage, |
| 5126 | ClassGV, |
| 5127 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 5128 | &CGM.getModule()); |
| 5129 | Entry->setAlignment( |
| 5130 | CGM.getTargetData().getPrefTypeAlignment( |
| 5131 | ObjCTypes.ClassnfABIPtrTy)); |
| 5132 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5133 | UsedGlobals.push_back(Entry); |
| 5134 | } |
| 5135 | |
| 5136 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5137 | } |
| 5138 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5139 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 5140 | /// meta-data |
| 5141 | /// |
| 5142 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 5143 | const ObjCInterfaceDecl *ID) { |
| 5144 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 5145 | if (Entry) |
| 5146 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5147 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5148 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5149 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5150 | Entry = |
| 5151 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5152 | llvm::GlobalValue::InternalLinkage, |
| 5153 | MetaClassGV, |
| 5154 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 5155 | &CGM.getModule()); |
| 5156 | Entry->setAlignment( |
| 5157 | CGM.getTargetData().getPrefTypeAlignment( |
| 5158 | ObjCTypes.ClassnfABIPtrTy)); |
| 5159 | |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5160 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5161 | UsedGlobals.push_back(Entry); |
| 5162 | |
| 5163 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5164 | } |
| 5165 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5166 | /// GetClass - Return a reference to the class for the given interface |
| 5167 | /// decl. |
| 5168 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 5169 | const ObjCInterfaceDecl *ID) { |
| 5170 | return EmitClassRef(Builder, ID); |
| 5171 | } |
| 5172 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5173 | /// Generates a message send where the super is the receiver. This is |
| 5174 | /// a message send to self with special delivery semantics indicating |
| 5175 | /// which class's method should be called. |
| 5176 | CodeGen::RValue |
| 5177 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 5178 | QualType ResultType, |
| 5179 | Selector Sel, |
| 5180 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5181 | bool isCategoryImpl, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5182 | llvm::Value *Receiver, |
| 5183 | bool IsClassMessage, |
| 5184 | const CodeGen::CallArgList &CallArgs) { |
| 5185 | // ... |
| 5186 | // Create and init a super structure; this is a (receiver, class) |
| 5187 | // pair we will pass to objc_msgSendSuper. |
| 5188 | llvm::Value *ObjCSuper = |
| 5189 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 5190 | |
| 5191 | llvm::Value *ReceiverAsObject = |
| 5192 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 5193 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 5194 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
| 5195 | |
| 5196 | // 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] | 5197 | llvm::Value *Target; |
| 5198 | if (IsClassMessage) { |
| 5199 | if (isCategoryImpl) { |
| 5200 | // Message sent to "super' in a class method defined in |
| 5201 | // a category implementation. |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5202 | Target = EmitClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5203 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 5204 | Target = CGF.Builder.CreateLoad(Target); |
| 5205 | } |
| 5206 | else |
| 5207 | Target = EmitMetaClassRef(CGF.Builder, Class); |
| 5208 | } |
| 5209 | else |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5210 | Target = EmitSuperClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5211 | |
| 5212 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 5213 | // and ObjCTypes types. |
| 5214 | const llvm::Type *ClassTy = |
| 5215 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 5216 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 5217 | CGF.Builder.CreateStore(Target, |
| 5218 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 5219 | |
| 5220 | return EmitMessageSend(CGF, ResultType, Sel, |
| 5221 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 5222 | true, CallArgs); |
| 5223 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5224 | |
| 5225 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
| 5226 | Selector Sel) { |
| 5227 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 5228 | |
| 5229 | if (!Entry) { |
| 5230 | llvm::Constant *Casted = |
| 5231 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 5232 | ObjCTypes.SelectorPtrTy); |
| 5233 | Entry = |
| 5234 | new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false, |
| 5235 | llvm::GlobalValue::InternalLinkage, |
| 5236 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_", |
| 5237 | &CGM.getModule()); |
| 5238 | Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip"); |
| 5239 | UsedGlobals.push_back(Entry); |
| 5240 | } |
| 5241 | |
| 5242 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5243 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5244 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 5245 | /// objc_assign_ivar (id src, id *dst) |
| 5246 | /// |
| 5247 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 5248 | llvm::Value *src, llvm::Value *dst) |
| 5249 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5250 | const llvm::Type * SrcTy = src->getType(); |
| 5251 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5252 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5253 | assert(Size <= 8 && "does not support size > 8"); |
| 5254 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5255 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5256 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5257 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5258 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5259 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5260 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5261 | src, dst, "assignivar"); |
| 5262 | return; |
| 5263 | } |
| 5264 | |
| 5265 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 5266 | /// objc_assign_strongCast (id src, id *dst) |
| 5267 | /// |
| 5268 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
| 5269 | CodeGen::CodeGenFunction &CGF, |
| 5270 | llvm::Value *src, llvm::Value *dst) |
| 5271 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5272 | const llvm::Type * SrcTy = src->getType(); |
| 5273 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5274 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5275 | assert(Size <= 8 && "does not support size > 8"); |
| 5276 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5277 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5278 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5279 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5280 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5281 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5282 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5283 | src, dst, "weakassign"); |
| 5284 | return; |
| 5285 | } |
| 5286 | |
| 5287 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 5288 | /// object: objc_read_weak (id *src) |
| 5289 | /// |
| 5290 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
| 5291 | CodeGen::CodeGenFunction &CGF, |
| 5292 | llvm::Value *AddrWeakObj) |
| 5293 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5294 | const llvm::Type* DestTy = |
| 5295 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5296 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 5297 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5298 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5299 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5300 | return read_weak; |
| 5301 | } |
| 5302 | |
| 5303 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 5304 | /// objc_assign_weak (id src, id *dst) |
| 5305 | /// |
| 5306 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 5307 | llvm::Value *src, llvm::Value *dst) |
| 5308 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5309 | const llvm::Type * SrcTy = src->getType(); |
| 5310 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5311 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5312 | assert(Size <= 8 && "does not support size > 8"); |
| 5313 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5314 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5315 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5316 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5317 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5318 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 5319 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5320 | src, dst, "weakassign"); |
| 5321 | return; |
| 5322 | } |
| 5323 | |
| 5324 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 5325 | /// objc_assign_global (id src, id *dst) |
| 5326 | /// |
| 5327 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 5328 | llvm::Value *src, llvm::Value *dst) |
| 5329 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5330 | const llvm::Type * SrcTy = src->getType(); |
| 5331 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5332 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5333 | assert(Size <= 8 && "does not support size > 8"); |
| 5334 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5335 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5336 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5337 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5338 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5339 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5340 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5341 | src, dst, "globalassign"); |
| 5342 | return; |
| 5343 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5344 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5345 | void |
| 5346 | CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 5347 | const Stmt &S) { |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5348 | bool isTry = isa<ObjCAtTryStmt>(S); |
| 5349 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 5350 | llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5351 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5352 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5353 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5354 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
| 5355 | |
| 5356 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 5357 | // evaluation of the expression must occur before we enter the |
| 5358 | // @synchronized. We can safely avoid a temp here because jumps into |
| 5359 | // @synchronized are illegal & this will dominate uses. |
| 5360 | llvm::Value *SyncArg = 0; |
| 5361 | if (!isTry) { |
| 5362 | SyncArg = |
| 5363 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 5364 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5365 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5366 | } |
| 5367 | |
| 5368 | // Push an EH context entry, used for handling rethrows and jumps |
| 5369 | // through finally. |
| 5370 | CGF.PushCleanupBlock(FinallyBlock); |
| 5371 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5372 | CGF.setInvokeDest(TryHandler); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5373 | |
| 5374 | CGF.EmitBlock(TryBlock); |
| 5375 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 5376 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
| 5377 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5378 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5379 | // Emit the exception handler. |
| 5380 | |
| 5381 | CGF.EmitBlock(TryHandler); |
| 5382 | |
| 5383 | llvm::Value *llvm_eh_exception = |
| 5384 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
| 5385 | llvm::Value *llvm_eh_selector_i64 = |
| 5386 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64); |
| 5387 | llvm::Value *llvm_eh_typeid_for_i64 = |
| 5388 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64); |
| 5389 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5390 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow"); |
| 5391 | |
| 5392 | llvm::SmallVector<llvm::Value*, 8> SelectorArgs; |
| 5393 | SelectorArgs.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5394 | SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5395 | |
| 5396 | // Construct the lists of (type, catch body) to handle. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5397 | llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5398 | bool HasCatchAll = false; |
| 5399 | if (isTry) { |
| 5400 | if (const ObjCAtCatchStmt* CatchStmt = |
| 5401 | cast<ObjCAtTryStmt>(S).getCatchStmts()) { |
| 5402 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5403 | const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5404 | Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody())); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5405 | |
| 5406 | // catch(...) always matches. |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5407 | if (!CatchDecl) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5408 | // Use i8* null here to signal this is a catch all, not a cleanup. |
| 5409 | llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 5410 | SelectorArgs.push_back(Null); |
| 5411 | HasCatchAll = true; |
| 5412 | break; |
| 5413 | } |
| 5414 | |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5415 | if (CGF.getContext().isObjCIdType(CatchDecl->getType()) || |
| 5416 | CatchDecl->getType()->isObjCQualifiedIdType()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5417 | llvm::Value *IDEHType = |
| 5418 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 5419 | if (!IDEHType) |
| 5420 | IDEHType = |
| 5421 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5422 | llvm::GlobalValue::ExternalLinkage, |
| 5423 | 0, "OBJC_EHTYPE_id", &CGM.getModule()); |
| 5424 | SelectorArgs.push_back(IDEHType); |
| 5425 | HasCatchAll = true; |
| 5426 | break; |
| 5427 | } |
| 5428 | |
| 5429 | // All other types should be Objective-C interface pointer types. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5430 | const PointerType *PT = CatchDecl->getType()->getAsPointerType(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5431 | assert(PT && "Invalid @catch type."); |
| 5432 | const ObjCInterfaceType *IT = |
| 5433 | PT->getPointeeType()->getAsObjCInterfaceType(); |
| 5434 | assert(IT && "Invalid @catch type."); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5435 | llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5436 | SelectorArgs.push_back(EHType); |
| 5437 | } |
| 5438 | } |
| 5439 | } |
| 5440 | |
| 5441 | // We use a cleanup unless there was already a catch all. |
| 5442 | if (!HasCatchAll) { |
| 5443 | SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5444 | Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5445 | } |
| 5446 | |
| 5447 | llvm::Value *Selector = |
| 5448 | CGF.Builder.CreateCall(llvm_eh_selector_i64, |
| 5449 | SelectorArgs.begin(), SelectorArgs.end(), |
| 5450 | "selector"); |
| 5451 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5452 | const ParmVarDecl *CatchParam = Handlers[i].first; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5453 | const Stmt *CatchBody = Handlers[i].second; |
| 5454 | |
| 5455 | llvm::BasicBlock *Next = 0; |
| 5456 | |
| 5457 | // The last handler always matches. |
| 5458 | if (i + 1 != e) { |
| 5459 | assert(CatchParam && "Only last handler can be a catch all."); |
| 5460 | |
| 5461 | llvm::BasicBlock *Match = CGF.createBasicBlock("match"); |
| 5462 | Next = CGF.createBasicBlock("catch.next"); |
| 5463 | llvm::Value *Id = |
| 5464 | CGF.Builder.CreateCall(llvm_eh_typeid_for_i64, |
| 5465 | CGF.Builder.CreateBitCast(SelectorArgs[i+2], |
| 5466 | ObjCTypes.Int8PtrTy)); |
| 5467 | CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id), |
| 5468 | Match, Next); |
| 5469 | |
| 5470 | CGF.EmitBlock(Match); |
| 5471 | } |
| 5472 | |
| 5473 | if (CatchBody) { |
| 5474 | llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end"); |
| 5475 | llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler"); |
| 5476 | |
| 5477 | // Cleanups must call objc_end_catch. |
| 5478 | // |
| 5479 | // FIXME: It seems incorrect for objc_begin_catch to be inside |
| 5480 | // this context, but this matches gcc. |
| 5481 | CGF.PushCleanupBlock(MatchEnd); |
| 5482 | CGF.setInvokeDest(MatchHandler); |
| 5483 | |
| 5484 | llvm::Value *ExcObject = |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5485 | CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5486 | |
| 5487 | // Bind the catch parameter if it exists. |
| 5488 | if (CatchParam) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5489 | ExcObject = |
| 5490 | CGF.Builder.CreateBitCast(ExcObject, |
| 5491 | CGF.ConvertType(CatchParam->getType())); |
| 5492 | // CatchParam is a ParmVarDecl because of the grammar |
| 5493 | // construction used to handle this, but for codegen purposes |
| 5494 | // we treat this as a local decl. |
| 5495 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
| 5496 | CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5497 | } |
| 5498 | |
| 5499 | CGF.ObjCEHValueStack.push_back(ExcObject); |
| 5500 | CGF.EmitStmt(CatchBody); |
| 5501 | CGF.ObjCEHValueStack.pop_back(); |
| 5502 | |
| 5503 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5504 | |
| 5505 | CGF.EmitBlock(MatchHandler); |
| 5506 | |
| 5507 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5508 | // We are required to emit this call to satisfy LLVM, even |
| 5509 | // though we don't use the result. |
| 5510 | llvm::SmallVector<llvm::Value*, 8> Args; |
| 5511 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5512 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5513 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5514 | 0)); |
| 5515 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5516 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5517 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5518 | |
| 5519 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5520 | |
| 5521 | CGF.EmitBlock(MatchEnd); |
| 5522 | |
| 5523 | // Unfortunately, we also have to generate another EH frame here |
| 5524 | // in case this throws. |
| 5525 | llvm::BasicBlock *MatchEndHandler = |
| 5526 | CGF.createBasicBlock("match.end.handler"); |
| 5527 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5528 | CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5529 | Cont, MatchEndHandler, |
| 5530 | Args.begin(), Args.begin()); |
| 5531 | |
| 5532 | CGF.EmitBlock(Cont); |
| 5533 | if (Info.SwitchBlock) |
| 5534 | CGF.EmitBlock(Info.SwitchBlock); |
| 5535 | if (Info.EndBlock) |
| 5536 | CGF.EmitBlock(Info.EndBlock); |
| 5537 | |
| 5538 | CGF.EmitBlock(MatchEndHandler); |
| 5539 | Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5540 | // We are required to emit this call to satisfy LLVM, even |
| 5541 | // though we don't use the result. |
| 5542 | Args.clear(); |
| 5543 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5544 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5545 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5546 | 0)); |
| 5547 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5548 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5549 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5550 | |
| 5551 | if (Next) |
| 5552 | CGF.EmitBlock(Next); |
| 5553 | } else { |
| 5554 | assert(!Next && "catchup should be last handler."); |
| 5555 | |
| 5556 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5557 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5558 | } |
| 5559 | } |
| 5560 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5561 | // Pop the cleanup entry, the @finally is outside this cleanup |
| 5562 | // scope. |
| 5563 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5564 | CGF.setInvokeDest(PrevLandingPad); |
| 5565 | |
| 5566 | CGF.EmitBlock(FinallyBlock); |
| 5567 | |
| 5568 | if (isTry) { |
| 5569 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 5570 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 5571 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 5572 | } else { |
| 5573 | // Emit 'objc_sync_exit(expr)' as finally's sole statement for |
| 5574 | // @synchronized. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5575 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5576 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5577 | |
| 5578 | if (Info.SwitchBlock) |
| 5579 | CGF.EmitBlock(Info.SwitchBlock); |
| 5580 | if (Info.EndBlock) |
| 5581 | CGF.EmitBlock(Info.EndBlock); |
| 5582 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5583 | // Branch around the rethrow code. |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5584 | CGF.EmitBranch(FinallyEnd); |
| 5585 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5586 | CGF.EmitBlock(FinallyRethrow); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5587 | CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5588 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5589 | CGF.Builder.CreateUnreachable(); |
| 5590 | |
| 5591 | CGF.EmitBlock(FinallyEnd); |
| 5592 | } |
| 5593 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5594 | /// EmitThrowStmt - Generate code for a throw statement. |
| 5595 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 5596 | const ObjCAtThrowStmt &S) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5597 | llvm::Value *Exception; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5598 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5599 | Exception = CGF.EmitScalarExpr(ThrowExpr); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5600 | } else { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5601 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
| 5602 | "Unexpected rethrow outside @catch block."); |
| 5603 | Exception = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5604 | } |
| 5605 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5606 | llvm::Value *ExceptionAsObject = |
| 5607 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 5608 | llvm::BasicBlock *InvokeDest = CGF.getInvokeDest(); |
| 5609 | if (InvokeDest) { |
| 5610 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5611 | CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5612 | Cont, InvokeDest, |
| 5613 | &ExceptionAsObject, &ExceptionAsObject + 1); |
| 5614 | CGF.EmitBlock(Cont); |
| 5615 | } else |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5616 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5617 | CGF.Builder.CreateUnreachable(); |
| 5618 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5619 | // Clear the insertion point to indicate we are in unreachable code. |
| 5620 | CGF.Builder.ClearInsertionPoint(); |
| 5621 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5622 | |
| 5623 | llvm::Value * |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5624 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 5625 | bool ForDefinition) { |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5626 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5627 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5628 | // If we don't need a definition, return the entry if found or check |
| 5629 | // if we use an external reference. |
| 5630 | if (!ForDefinition) { |
| 5631 | if (Entry) |
| 5632 | return Entry; |
Daniel Dunbar | 7e075cb | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 5633 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5634 | // If this type (or a super class) has the __objc_exception__ |
| 5635 | // attribute, emit an external reference. |
| 5636 | if (hasObjCExceptionAttribute(ID)) |
| 5637 | return Entry = |
| 5638 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5639 | llvm::GlobalValue::ExternalLinkage, |
| 5640 | 0, |
| 5641 | (std::string("OBJC_EHTYPE_$_") + |
| 5642 | ID->getIdentifier()->getName()), |
| 5643 | &CGM.getModule()); |
| 5644 | } |
| 5645 | |
| 5646 | // Otherwise we need to either make a new entry or fill in the |
| 5647 | // initializer. |
| 5648 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5649 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5650 | std::string VTableName = "objc_ehtype_vtable"; |
| 5651 | llvm::GlobalVariable *VTableGV = |
| 5652 | CGM.getModule().getGlobalVariable(VTableName); |
| 5653 | if (!VTableGV) |
| 5654 | VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false, |
| 5655 | llvm::GlobalValue::ExternalLinkage, |
| 5656 | 0, VTableName, &CGM.getModule()); |
| 5657 | |
| 5658 | llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2); |
| 5659 | |
| 5660 | std::vector<llvm::Constant*> Values(3); |
| 5661 | Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1); |
| 5662 | Values[1] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5663 | Values[2] = GetClassGlobal(ClassName); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5664 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
| 5665 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5666 | if (Entry) { |
| 5667 | Entry->setInitializer(Init); |
| 5668 | } else { |
| 5669 | Entry = new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5670 | llvm::GlobalValue::WeakAnyLinkage, |
| 5671 | Init, |
| 5672 | (std::string("OBJC_EHTYPE_$_") + |
| 5673 | ID->getIdentifier()->getName()), |
| 5674 | &CGM.getModule()); |
| 5675 | } |
| 5676 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 5677 | if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden) |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5678 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5679 | Entry->setAlignment(8); |
| 5680 | |
| 5681 | if (ForDefinition) { |
| 5682 | Entry->setSection("__DATA,__objc_const"); |
| 5683 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 5684 | } else { |
| 5685 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 5686 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5687 | |
| 5688 | return Entry; |
| 5689 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5690 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5691 | /* *** */ |
| 5692 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5693 | CodeGen::CGObjCRuntime * |
| 5694 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 5695 | return new CGObjCMac(CGM); |
| 5696 | } |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5697 | |
| 5698 | CodeGen::CGObjCRuntime * |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5699 | CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5700 | return new CGObjCNonFragileABIMac(CGM); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5701 | } |