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 | |
| 36 | uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM, |
| 37 | const ObjCInterfaceDecl *OID, |
| 38 | const ObjCIvarDecl *Ivar) { |
| 39 | assert(!OID->isForwardDecl() && "Invalid interface decl!"); |
| 40 | QualType T = CGM.getContext().getObjCInterfaceType(OID); |
| 41 | const llvm::StructType *InterfaceTy = |
| 42 | cast<llvm::StructType>(CGM.getTypes().ConvertType(T)); |
| 43 | const llvm::StructLayout *Layout = |
| 44 | CGM.getTargetData().getStructLayout(InterfaceTy); |
| 45 | const FieldDecl *Field = |
| 46 | OID->lookupFieldDeclForIvar(CGM.getContext(), Ivar); |
| 47 | if (!Field->isBitField()) |
| 48 | return Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); |
| 49 | |
| 50 | // FIXME. Must be a better way of getting a bitfield base offset. |
| 51 | CodeGenTypes::BitFieldInfo BFI = CGM.getTypes().getBitFieldInfo(Field); |
| 52 | // FIXME: The "field no" for bitfields is something completely |
| 53 | // different; it is the offset in multiples of the base type size! |
| 54 | uint64_t Offset = CGM.getTypes().getLLVMFieldNo(Field); |
| 55 | const llvm::Type *Ty = |
| 56 | CGM.getTypes().ConvertTypeForMemRecursive(Field->getType()); |
| 57 | Offset *= CGM.getTypes().getTargetData().getTypePaddedSizeInBits(Ty); |
| 58 | return (Offset + BFI.Begin) / 8; |
| 59 | } |
| 60 | |
| 61 | LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF, |
| 62 | const ObjCInterfaceDecl *OID, |
| 63 | llvm::Value *BaseValue, |
| 64 | const ObjCIvarDecl *Ivar, |
| 65 | unsigned CVRQualifiers, |
| 66 | llvm::Value *Offset) { |
| 67 | // FIXME: For now, we use an implementation based on just computing |
| 68 | // the offset and calculating things directly. For optimization |
| 69 | // purposes, it would be cleaner to use a GEP on the proper type |
| 70 | // since the structure layout is fixed; however for that we need to |
| 71 | // be able to walk the class chain for an Ivar. |
| 72 | const FieldDecl *Field = |
| 73 | OID->lookupFieldDeclForIvar(CGF.CGM.getContext(), Ivar); |
| 74 | |
| 75 | // (char *) BaseValue |
| 76 | llvm::Type *I8Ptr = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 77 | llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr); |
| 78 | // (char*)BaseValue + Offset_symbol |
| 79 | V = CGF.Builder.CreateGEP(V, Offset, "add.ptr"); |
| 80 | // (type *)((char*)BaseValue + Offset_symbol) |
| 81 | const llvm::Type *IvarTy = |
| 82 | CGF.CGM.getTypes().ConvertTypeForMem(Ivar->getType()); |
| 83 | llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy); |
| 84 | V = CGF.Builder.CreateBitCast(V, ptrIvarTy); |
| 85 | |
| 86 | if (Ivar->isBitField()) { |
| 87 | QualType FieldTy = Field->getType(); |
| 88 | CodeGenTypes::BitFieldInfo bitFieldInfo = |
| 89 | CGF.CGM.getTypes().getBitFieldInfo(Field); |
| 90 | return LValue::MakeBitfield(V, bitFieldInfo.Begin % 8, bitFieldInfo.Size, |
| 91 | FieldTy->isSignedIntegerType(), |
| 92 | FieldTy.getCVRQualifiers()|CVRQualifiers); |
| 93 | } |
| 94 | |
| 95 | LValue LV = LValue::MakeAddr(V, |
| 96 | Ivar->getType().getCVRQualifiers()|CVRQualifiers, |
| 97 | CGF.CGM.getContext().getObjCGCAttrKind(Ivar->getType())); |
| 98 | LValue::SetObjCIvar(LV, true); |
| 99 | return LV; |
| 100 | } |
| 101 | |
| 102 | /// |
| 103 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 104 | namespace { |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 105 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 106 | typedef std::vector<llvm::Constant*> ConstantVector; |
| 107 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 108 | // FIXME: We should find a nicer way to make the labels for |
| 109 | // metadata, string concatenation is lame. |
| 110 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 111 | class ObjCCommonTypesHelper { |
| 112 | protected: |
| 113 | CodeGen::CodeGenModule &CGM; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 114 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 115 | public: |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 116 | const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 117 | const llvm::Type *Int8PtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 118 | |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 119 | /// ObjectPtrTy - LLVM type for object handles (typeof(id)) |
| 120 | const llvm::Type *ObjectPtrTy; |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 121 | |
| 122 | /// PtrObjectPtrTy - LLVM type for id * |
| 123 | const llvm::Type *PtrObjectPtrTy; |
| 124 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 125 | /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL)) |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 126 | const llvm::Type *SelectorPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 127 | /// ProtocolPtrTy - LLVM type for external protocol handles |
| 128 | /// (typeof(Protocol)) |
| 129 | const llvm::Type *ExternalProtocolPtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 130 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 131 | // SuperCTy - clang type for struct objc_super. |
| 132 | QualType SuperCTy; |
| 133 | // SuperPtrCTy - clang type for struct objc_super *. |
| 134 | QualType SuperPtrCTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 135 | |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 136 | /// SuperTy - LLVM type for struct objc_super. |
| 137 | const llvm::StructType *SuperTy; |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 138 | /// SuperPtrTy - LLVM type for struct objc_super *. |
| 139 | const llvm::Type *SuperPtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 140 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 141 | /// PropertyTy - LLVM type for struct objc_property (struct _prop_t |
| 142 | /// in GCC parlance). |
| 143 | const llvm::StructType *PropertyTy; |
| 144 | |
| 145 | /// PropertyListTy - LLVM type for struct objc_property_list |
| 146 | /// (_prop_list_t in GCC parlance). |
| 147 | const llvm::StructType *PropertyListTy; |
| 148 | /// PropertyListPtrTy - LLVM type for struct objc_property_list*. |
| 149 | const llvm::Type *PropertyListPtrTy; |
| 150 | |
| 151 | // MethodTy - LLVM type for struct objc_method. |
| 152 | const llvm::StructType *MethodTy; |
| 153 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 154 | /// CacheTy - LLVM type for struct objc_cache. |
| 155 | const llvm::Type *CacheTy; |
| 156 | /// CachePtrTy - LLVM type for struct objc_cache *. |
| 157 | const llvm::Type *CachePtrTy; |
| 158 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 159 | llvm::Constant *getGetPropertyFn() { |
| 160 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 161 | ASTContext &Ctx = CGM.getContext(); |
| 162 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
| 163 | llvm::SmallVector<QualType,16> Params; |
| 164 | QualType IdType = Ctx.getObjCIdType(); |
| 165 | QualType SelType = Ctx.getObjCSelType(); |
| 166 | Params.push_back(IdType); |
| 167 | Params.push_back(SelType); |
| 168 | Params.push_back(Ctx.LongTy); |
| 169 | Params.push_back(Ctx.BoolTy); |
| 170 | const llvm::FunctionType *FTy = |
| 171 | Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false); |
| 172 | return CGM.CreateRuntimeFunction(FTy, "objc_getProperty"); |
| 173 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 174 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 175 | llvm::Constant *getSetPropertyFn() { |
| 176 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 177 | ASTContext &Ctx = CGM.getContext(); |
| 178 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
| 179 | llvm::SmallVector<QualType,16> Params; |
| 180 | QualType IdType = Ctx.getObjCIdType(); |
| 181 | QualType SelType = Ctx.getObjCSelType(); |
| 182 | Params.push_back(IdType); |
| 183 | Params.push_back(SelType); |
| 184 | Params.push_back(Ctx.LongTy); |
| 185 | Params.push_back(IdType); |
| 186 | Params.push_back(Ctx.BoolTy); |
| 187 | Params.push_back(Ctx.BoolTy); |
| 188 | const llvm::FunctionType *FTy = |
| 189 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
| 190 | return CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); |
| 191 | } |
| 192 | |
| 193 | llvm::Constant *getEnumerationMutationFn() { |
| 194 | // void objc_enumerationMutation (id) |
| 195 | std::vector<const llvm::Type*> Args; |
| 196 | Args.push_back(ObjectPtrTy); |
| 197 | llvm::FunctionType *FTy = |
| 198 | llvm::FunctionType::get(llvm::Type::VoidTy, Args, false); |
| 199 | return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation"); |
| 200 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 201 | |
| 202 | /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 203 | llvm::Constant *getGcReadWeakFn() { |
| 204 | // id objc_read_weak (id *) |
| 205 | std::vector<const llvm::Type*> Args; |
| 206 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 207 | llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 208 | return CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
| 209 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 210 | |
| 211 | /// GcAssignWeakFn -- LLVM objc_assign_weak function. |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 212 | llvm::Constant *getGcAssignWeakFn() { |
| 213 | // id objc_assign_weak (id, id *) |
| 214 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 215 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 216 | llvm::FunctionType *FTy = |
| 217 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 218 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 219 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 220 | |
| 221 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 222 | llvm::Constant *getGcAssignGlobalFn() { |
| 223 | // id objc_assign_global(id, id *) |
| 224 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 225 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 226 | llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 227 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 228 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 229 | |
| 230 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 231 | llvm::Constant *getGcAssignIvarFn() { |
| 232 | // id objc_assign_ivar(id, id *) |
| 233 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 234 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 235 | llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 236 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 237 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 238 | |
| 239 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 240 | llvm::Constant *getGcAssignStrongCastFn() { |
| 241 | // id objc_assign_global(id, id *) |
| 242 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 243 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 244 | llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 245 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); |
| 246 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 247 | |
| 248 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 249 | llvm::Constant *getExceptionThrowFn() { |
| 250 | // void objc_exception_throw(id) |
| 251 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 252 | llvm::FunctionType *FTy = |
| 253 | llvm::FunctionType::get(llvm::Type::VoidTy, Args, false); |
| 254 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
| 255 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 256 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 257 | /// SyncEnterFn - LLVM object_sync_enter function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 258 | llvm::Constant *getSyncEnterFn() { |
| 259 | // void objc_sync_enter (id) |
| 260 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 261 | llvm::FunctionType *FTy = |
| 262 | llvm::FunctionType::get(llvm::Type::VoidTy, Args, false); |
| 263 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 264 | } |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 265 | |
| 266 | /// SyncExitFn - LLVM object_sync_exit function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 267 | llvm::Constant *getSyncExitFn() { |
| 268 | // void objc_sync_exit (id) |
| 269 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 270 | llvm::FunctionType *FTy = |
| 271 | llvm::FunctionType::get(llvm::Type::VoidTy, Args, false); |
| 272 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
| 273 | } |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 274 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 275 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
| 276 | ~ObjCCommonTypesHelper(){} |
| 277 | }; |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 278 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 279 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 280 | /// construction of varies types used during ObjC generation. |
| 281 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
| 282 | private: |
| 283 | |
Chris Lattner | 4176b0c | 2009-04-22 02:32:31 +0000 | [diff] [blame] | 284 | llvm::Constant *getMessageSendFn() { |
| 285 | // id objc_msgSend (id, SEL, ...) |
| 286 | std::vector<const llvm::Type*> Params; |
| 287 | Params.push_back(ObjectPtrTy); |
| 288 | Params.push_back(SelectorPtrTy); |
| 289 | return |
| 290 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 291 | Params, true), |
| 292 | "objc_msgSend"); |
| 293 | } |
| 294 | |
| 295 | llvm::Constant *getMessageSendStretFn() { |
| 296 | // id objc_msgSend_stret (id, SEL, ...) |
| 297 | std::vector<const llvm::Type*> Params; |
| 298 | Params.push_back(ObjectPtrTy); |
| 299 | Params.push_back(SelectorPtrTy); |
| 300 | return |
| 301 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 302 | Params, true), |
| 303 | "objc_msgSend_stret"); |
| 304 | |
| 305 | } |
| 306 | |
| 307 | llvm::Constant *getMessageSendFpretFn() { |
| 308 | // FIXME: This should be long double on x86_64? |
| 309 | // [double | long double] objc_msgSend_fpret(id self, SEL op, ...) |
| 310 | std::vector<const llvm::Type*> Params; |
| 311 | Params.push_back(ObjectPtrTy); |
| 312 | Params.push_back(SelectorPtrTy); |
| 313 | return |
| 314 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy, |
| 315 | Params, |
| 316 | true), |
| 317 | "objc_msgSend_fpret"); |
| 318 | |
| 319 | } |
| 320 | |
| 321 | llvm::Constant *getMessageSendSuperFn() { |
| 322 | // id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
| 323 | std::vector<const llvm::Type*> Params; |
| 324 | Params.push_back(SuperPtrTy); |
| 325 | Params.push_back(SelectorPtrTy); |
| 326 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 327 | Params, true), |
| 328 | "objc_msgSendSuper"); |
| 329 | } |
| 330 | llvm::Constant *getMessageSendSuperStretFn() { |
| 331 | // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super, |
| 332 | // SEL op, ...) |
| 333 | std::vector<const llvm::Type*> Params; |
| 334 | Params.push_back(Int8PtrTy); |
| 335 | Params.push_back(SuperPtrTy); |
| 336 | Params.push_back(SelectorPtrTy); |
| 337 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 338 | Params, true), |
| 339 | "objc_msgSendSuper_stret"); |
| 340 | } |
| 341 | |
| 342 | llvm::Constant *getMessageSendSuperFpretFn() { |
| 343 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 344 | return getMessageSendSuperFn(); |
| 345 | } |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 346 | |
| 347 | public: |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 348 | /// SymtabTy - LLVM type for struct objc_symtab. |
| 349 | const llvm::StructType *SymtabTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 350 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
| 351 | const llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 352 | /// ModuleTy - LLVM type for struct objc_module. |
| 353 | const llvm::StructType *ModuleTy; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 354 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 355 | /// ProtocolTy - LLVM type for struct objc_protocol. |
| 356 | const llvm::StructType *ProtocolTy; |
| 357 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
| 358 | const llvm::Type *ProtocolPtrTy; |
| 359 | /// ProtocolExtensionTy - LLVM type for struct |
| 360 | /// objc_protocol_extension. |
| 361 | const llvm::StructType *ProtocolExtensionTy; |
| 362 | /// ProtocolExtensionTy - LLVM type for struct |
| 363 | /// objc_protocol_extension *. |
| 364 | const llvm::Type *ProtocolExtensionPtrTy; |
| 365 | /// MethodDescriptionTy - LLVM type for struct |
| 366 | /// objc_method_description. |
| 367 | const llvm::StructType *MethodDescriptionTy; |
| 368 | /// MethodDescriptionListTy - LLVM type for struct |
| 369 | /// objc_method_description_list. |
| 370 | const llvm::StructType *MethodDescriptionListTy; |
| 371 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 372 | /// objc_method_description_list *. |
| 373 | const llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 374 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
| 375 | const llvm::Type *ProtocolListTy; |
| 376 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
| 377 | const llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 378 | /// CategoryTy - LLVM type for struct objc_category. |
| 379 | const llvm::StructType *CategoryTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 380 | /// ClassTy - LLVM type for struct objc_class. |
| 381 | const llvm::StructType *ClassTy; |
| 382 | /// ClassPtrTy - LLVM type for struct objc_class *. |
| 383 | const llvm::Type *ClassPtrTy; |
| 384 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
| 385 | const llvm::StructType *ClassExtensionTy; |
| 386 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
| 387 | const llvm::Type *ClassExtensionPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 388 | // IvarTy - LLVM type for struct objc_ivar. |
| 389 | const llvm::StructType *IvarTy; |
| 390 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
| 391 | const llvm::Type *IvarListTy; |
| 392 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
| 393 | const llvm::Type *IvarListPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 394 | /// MethodListTy - LLVM type for struct objc_method_list. |
| 395 | const llvm::Type *MethodListTy; |
| 396 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
| 397 | const llvm::Type *MethodListPtrTy; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 398 | |
| 399 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
| 400 | const llvm::Type *ExceptionDataTy; |
| 401 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 402 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 403 | llvm::Constant *getExceptionTryEnterFn() { |
| 404 | std::vector<const llvm::Type*> Params; |
| 405 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 406 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 407 | Params, false), |
| 408 | "objc_exception_try_enter"); |
| 409 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 410 | |
| 411 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 412 | llvm::Constant *getExceptionTryExitFn() { |
| 413 | std::vector<const llvm::Type*> Params; |
| 414 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 415 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 416 | Params, false), |
| 417 | "objc_exception_try_exit"); |
| 418 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 419 | |
| 420 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 421 | llvm::Constant *getExceptionExtractFn() { |
| 422 | std::vector<const llvm::Type*> Params; |
| 423 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 424 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 425 | Params, false), |
| 426 | "objc_exception_extract"); |
| 427 | |
| 428 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 429 | |
| 430 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 431 | llvm::Constant *getExceptionMatchFn() { |
| 432 | std::vector<const llvm::Type*> Params; |
| 433 | Params.push_back(ClassPtrTy); |
| 434 | Params.push_back(ObjectPtrTy); |
| 435 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 436 | Params, false), |
| 437 | "objc_exception_match"); |
| 438 | |
| 439 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 440 | |
| 441 | /// SetJmpFn - LLVM _setjmp function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 442 | llvm::Constant *getSetJmpFn() { |
| 443 | std::vector<const llvm::Type*> Params; |
| 444 | Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty)); |
| 445 | return |
| 446 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 447 | Params, false), |
| 448 | "_setjmp"); |
| 449 | |
| 450 | } |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 451 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 452 | public: |
| 453 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 454 | ~ObjCTypesHelper() {} |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 455 | |
| 456 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 457 | llvm::Constant *getSendFn(bool IsSuper) { |
Chris Lattner | 4176b0c | 2009-04-22 02:32:31 +0000 | [diff] [blame] | 458 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFn(); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 461 | llvm::Constant *getSendStretFn(bool IsSuper) { |
Chris Lattner | 4176b0c | 2009-04-22 02:32:31 +0000 | [diff] [blame] | 462 | return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn(); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 465 | llvm::Constant *getSendFpretFn(bool IsSuper) { |
Chris Lattner | 4176b0c | 2009-04-22 02:32:31 +0000 | [diff] [blame] | 466 | return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn(); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 467 | } |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 468 | }; |
| 469 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 470 | /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 471 | /// modern abi |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 472 | class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 473 | public: |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 474 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 475 | // MethodListnfABITy - LLVM for struct _method_list_t |
| 476 | const llvm::StructType *MethodListnfABITy; |
| 477 | |
| 478 | // MethodListnfABIPtrTy - LLVM for struct _method_list_t* |
| 479 | const llvm::Type *MethodListnfABIPtrTy; |
| 480 | |
| 481 | // ProtocolnfABITy = LLVM for struct _protocol_t |
| 482 | const llvm::StructType *ProtocolnfABITy; |
| 483 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 484 | // ProtocolnfABIPtrTy = LLVM for struct _protocol_t* |
| 485 | const llvm::Type *ProtocolnfABIPtrTy; |
| 486 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 487 | // ProtocolListnfABITy - LLVM for struct _objc_protocol_list |
| 488 | const llvm::StructType *ProtocolListnfABITy; |
| 489 | |
| 490 | // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list* |
| 491 | const llvm::Type *ProtocolListnfABIPtrTy; |
| 492 | |
| 493 | // ClassnfABITy - LLVM for struct _class_t |
| 494 | const llvm::StructType *ClassnfABITy; |
| 495 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 496 | // ClassnfABIPtrTy - LLVM for struct _class_t* |
| 497 | const llvm::Type *ClassnfABIPtrTy; |
| 498 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 499 | // IvarnfABITy - LLVM for struct _ivar_t |
| 500 | const llvm::StructType *IvarnfABITy; |
| 501 | |
| 502 | // IvarListnfABITy - LLVM for struct _ivar_list_t |
| 503 | const llvm::StructType *IvarListnfABITy; |
| 504 | |
| 505 | // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t* |
| 506 | const llvm::Type *IvarListnfABIPtrTy; |
| 507 | |
| 508 | // ClassRonfABITy - LLVM for struct _class_ro_t |
| 509 | const llvm::StructType *ClassRonfABITy; |
| 510 | |
| 511 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 512 | const llvm::Type *ImpnfABITy; |
| 513 | |
| 514 | // CategorynfABITy - LLVM for struct _category_t |
| 515 | const llvm::StructType *CategorynfABITy; |
| 516 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 517 | // New types for nonfragile abi messaging. |
| 518 | |
| 519 | // MessageRefTy - LLVM for: |
| 520 | // struct _message_ref_t { |
| 521 | // IMP messenger; |
| 522 | // SEL name; |
| 523 | // }; |
| 524 | const llvm::StructType *MessageRefTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 525 | // MessageRefCTy - clang type for struct _message_ref_t |
| 526 | QualType MessageRefCTy; |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 527 | |
| 528 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 529 | const llvm::Type *MessageRefPtrTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 530 | // MessageRefCPtrTy - clang type for struct _message_ref_t* |
| 531 | QualType MessageRefCPtrTy; |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 532 | |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 533 | // MessengerTy - Type of the messenger (shown as IMP above) |
| 534 | const llvm::FunctionType *MessengerTy; |
| 535 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 536 | // SuperMessageRefTy - LLVM for: |
| 537 | // struct _super_message_ref_t { |
| 538 | // SUPER_IMP messenger; |
| 539 | // SEL name; |
| 540 | // }; |
| 541 | const llvm::StructType *SuperMessageRefTy; |
| 542 | |
| 543 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 544 | const llvm::Type *SuperMessageRefPtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 545 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 546 | llvm::Constant *getMessageSendFixupFn() { |
| 547 | // id objc_msgSend_fixup(id, struct message_ref_t*, ...) |
| 548 | std::vector<const llvm::Type*> Params; |
| 549 | Params.push_back(ObjectPtrTy); |
| 550 | Params.push_back(MessageRefPtrTy); |
| 551 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 552 | Params, true), |
| 553 | "objc_msgSend_fixup"); |
| 554 | } |
| 555 | |
| 556 | llvm::Constant *getMessageSendFpretFixupFn() { |
| 557 | // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...) |
| 558 | std::vector<const llvm::Type*> Params; |
| 559 | Params.push_back(ObjectPtrTy); |
| 560 | Params.push_back(MessageRefPtrTy); |
| 561 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 562 | Params, true), |
| 563 | "objc_msgSend_fpret_fixup"); |
| 564 | } |
| 565 | |
| 566 | llvm::Constant *getMessageSendStretFixupFn() { |
| 567 | // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...) |
| 568 | std::vector<const llvm::Type*> Params; |
| 569 | Params.push_back(ObjectPtrTy); |
| 570 | Params.push_back(MessageRefPtrTy); |
| 571 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 572 | Params, true), |
| 573 | "objc_msgSend_stret_fixup"); |
| 574 | } |
| 575 | |
| 576 | llvm::Constant *getMessageSendIdFixupFn() { |
| 577 | // id objc_msgSendId_fixup(id, struct message_ref_t*, ...) |
| 578 | std::vector<const llvm::Type*> Params; |
| 579 | Params.push_back(ObjectPtrTy); |
| 580 | Params.push_back(MessageRefPtrTy); |
| 581 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 582 | Params, true), |
| 583 | "objc_msgSendId_fixup"); |
| 584 | } |
| 585 | |
| 586 | llvm::Constant *getMessageSendIdStretFixupFn() { |
| 587 | // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...) |
| 588 | std::vector<const llvm::Type*> Params; |
| 589 | Params.push_back(ObjectPtrTy); |
| 590 | Params.push_back(MessageRefPtrTy); |
| 591 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 592 | Params, true), |
| 593 | "objc_msgSendId_stret_fixup"); |
| 594 | } |
| 595 | llvm::Constant *getMessageSendSuper2FixupFn() { |
| 596 | // id objc_msgSendSuper2_fixup (struct objc_super *, |
| 597 | // struct _super_message_ref_t*, ...) |
| 598 | std::vector<const llvm::Type*> Params; |
| 599 | Params.push_back(SuperPtrTy); |
| 600 | Params.push_back(SuperMessageRefPtrTy); |
| 601 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 602 | Params, true), |
| 603 | "objc_msgSendSuper2_fixup"); |
| 604 | } |
| 605 | |
| 606 | llvm::Constant *getMessageSendSuper2StretFixupFn() { |
| 607 | // id objc_msgSendSuper2_stret_fixup(struct objc_super *, |
| 608 | // struct _super_message_ref_t*, ...) |
| 609 | std::vector<const llvm::Type*> Params; |
| 610 | Params.push_back(SuperPtrTy); |
| 611 | Params.push_back(SuperMessageRefPtrTy); |
| 612 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 613 | Params, true), |
| 614 | "objc_msgSendSuper2_stret_fixup"); |
| 615 | } |
| 616 | |
| 617 | |
| 618 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 619 | /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C |
| 620 | /// exception personality function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 621 | llvm::Value *getEHPersonalityPtr() { |
| 622 | llvm::Constant *Personality = |
| 623 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 624 | std::vector<const llvm::Type*>(), |
| 625 | true), |
| 626 | "__objc_personality_v0"); |
| 627 | return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy); |
| 628 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 629 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 630 | llvm::Constant *getUnwindResumeOrRethrowFn() { |
| 631 | std::vector<const llvm::Type*> Params; |
| 632 | Params.push_back(Int8PtrTy); |
| 633 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 634 | Params, false), |
| 635 | "_Unwind_Resume_or_Rethrow"); |
| 636 | } |
| 637 | |
| 638 | llvm::Constant *getObjCEndCatchFn() { |
| 639 | std::vector<const llvm::Type*> Params; |
| 640 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 641 | Params, false), |
| 642 | "objc_end_catch"); |
| 643 | |
| 644 | } |
| 645 | |
| 646 | llvm::Constant *getObjCBeginCatchFn() { |
| 647 | std::vector<const llvm::Type*> Params; |
| 648 | Params.push_back(Int8PtrTy); |
| 649 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy, |
| 650 | Params, false), |
| 651 | "objc_begin_catch"); |
| 652 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 653 | |
| 654 | const llvm::StructType *EHTypeTy; |
| 655 | const llvm::Type *EHTypePtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 656 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 657 | ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); |
| 658 | ~ObjCNonFragileABITypesHelper(){} |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 659 | }; |
| 660 | |
| 661 | class CGObjCCommonMac : public CodeGen::CGObjCRuntime { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 662 | public: |
| 663 | // FIXME - accessibility |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 664 | class GC_IVAR { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 665 | public: |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 666 | unsigned int ivar_bytepos; |
| 667 | unsigned int ivar_size; |
| 668 | GC_IVAR() : ivar_bytepos(0), ivar_size(0) {} |
| 669 | }; |
| 670 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 671 | class SKIP_SCAN { |
| 672 | public: |
| 673 | unsigned int skip; |
| 674 | unsigned int scan; |
| 675 | SKIP_SCAN() : skip(0), scan(0) {} |
| 676 | }; |
| 677 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 678 | protected: |
| 679 | CodeGen::CodeGenModule &CGM; |
| 680 | // FIXME! May not be needing this after all. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 681 | unsigned ObjCABI; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 682 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 683 | // gc ivar layout bitmap calculation helper caches. |
| 684 | llvm::SmallVector<GC_IVAR, 16> SkipIvars; |
| 685 | llvm::SmallVector<GC_IVAR, 16> IvarsInfo; |
| 686 | llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 687 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 688 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 689 | /// DefinedSymbols and FinishModule(). |
| 690 | std::set<IdentifierInfo*> LazySymbols; |
| 691 | |
| 692 | /// DefinedSymbols - External symbols which are defined by this |
| 693 | /// module. The symbols in this list and LazySymbols are used to add |
| 694 | /// special linker symbols which ensure that Objective-C modules are |
| 695 | /// linked properly. |
| 696 | std::set<IdentifierInfo*> DefinedSymbols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 697 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 698 | /// ClassNames - uniqued class names. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 699 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 700 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 701 | /// MethodVarNames - uniqued method variable names. |
| 702 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 703 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 704 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 705 | /// a StringMap here because have no other unique reference. |
| 706 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 707 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 708 | /// MethodDefinitions - map of methods which have been defined in |
| 709 | /// this translation unit. |
| 710 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 711 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 712 | /// PropertyNames - uniqued method variable names. |
| 713 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 714 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 715 | /// ClassReferences - uniqued class references. |
| 716 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 717 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 718 | /// SelectorReferences - uniqued selector references. |
| 719 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 720 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 721 | /// Protocols - Protocols for which an objc_protocol structure has |
| 722 | /// been emitted. Forward declarations are handled by creating an |
| 723 | /// empty structure whose initializer is filled in when/if defined. |
| 724 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 725 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 726 | /// DefinedProtocols - Protocols which have actually been |
| 727 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 728 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 729 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 730 | /// DefinedClasses - List of defined classes. |
| 731 | std::vector<llvm::GlobalValue*> DefinedClasses; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 732 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 733 | /// DefinedCategories - List of defined categories. |
| 734 | std::vector<llvm::GlobalValue*> DefinedCategories; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 735 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 736 | /// UsedGlobals - List of globals to pack into the llvm.used metadata |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 737 | /// to prevent them from being clobbered. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 738 | std::vector<llvm::GlobalVariable*> UsedGlobals; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 739 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 740 | /// GetNameForMethod - Return a name for the given method. |
| 741 | /// \param[out] NameOut - The return value. |
| 742 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 743 | const ObjCContainerDecl *CD, |
| 744 | std::string &NameOut); |
| 745 | |
| 746 | /// GetMethodVarName - Return a unique constant for the given |
| 747 | /// selector's name. The return value has type char *. |
| 748 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 749 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
| 750 | llvm::Constant *GetMethodVarName(const std::string &Name); |
| 751 | |
| 752 | /// GetMethodVarType - Return a unique constant for the given |
| 753 | /// selector's name. The return value has type char *. |
| 754 | |
| 755 | // FIXME: This is a horrible name. |
| 756 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 757 | llvm::Constant *GetMethodVarType(const FieldDecl *D); |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 758 | |
| 759 | /// GetPropertyName - Return a unique constant for the given |
| 760 | /// name. The return value has type char *. |
| 761 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
| 762 | |
| 763 | // FIXME: This can be dropped once string functions are unified. |
| 764 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 765 | const Decl *Container); |
| 766 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 767 | /// GetClassName - Return a unique constant for the given selector's |
| 768 | /// name. The return value has type char *. |
| 769 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
| 770 | |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 771 | /// GetInterfaceDeclStructLayout - Get layout for ivars of given |
| 772 | /// interface declaration. |
| 773 | const llvm::StructLayout *GetInterfaceDeclStructLayout( |
| 774 | const ObjCInterfaceDecl *ID) const; |
| 775 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 776 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 777 | /// implementation for the __strong or __weak case. |
| 778 | /// |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 779 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
| 780 | bool ForStrongLayout); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 781 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 782 | void BuildAggrIvarLayout(const ObjCInterfaceDecl *OI, |
| 783 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 784 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 785 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 786 | unsigned int BytePos, bool ForStrongLayout, |
| 787 | int &Index, int &SkIndex, bool &HasUnion); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 788 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 789 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 790 | /// ivar layout bitmap. |
| 791 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 792 | const ObjCCommonTypesHelper &ObjCTypes); |
| 793 | |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 794 | /// EmitPropertyList - Emit the given property list. The return |
| 795 | /// value has type PropertyListPtrTy. |
| 796 | llvm::Constant *EmitPropertyList(const std::string &Name, |
| 797 | const Decl *Container, |
| 798 | const ObjCContainerDecl *OCD, |
| 799 | const ObjCCommonTypesHelper &ObjCTypes); |
| 800 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 801 | /// GetProtocolRef - Return a reference to the internal protocol |
| 802 | /// description, creating an empty one if it has not been |
| 803 | /// defined. The return value has type ProtocolPtrTy. |
| 804 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 805 | |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 806 | /// GetFieldBaseOffset - return's field byte offset. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 807 | uint64_t GetFieldBaseOffset(const ObjCInterfaceDecl *OI, |
| 808 | const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 809 | const FieldDecl *Field); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 810 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 811 | /// CreateMetadataVar - Create a global variable with internal |
| 812 | /// linkage for use by the Objective-C runtime. |
| 813 | /// |
| 814 | /// This is a convenience wrapper which not only creates the |
| 815 | /// variable, but also sets the section and alignment and adds the |
| 816 | /// global to the UsedGlobals list. |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 817 | /// |
| 818 | /// \param Name - The variable name. |
| 819 | /// \param Init - The variable initializer; this is also used to |
| 820 | /// define the type of the variable. |
| 821 | /// \param Section - The section the variable should go into, or 0. |
| 822 | /// \param Align - The alignment for the variable, or 0. |
| 823 | /// \param AddToUsed - Whether the variable should be added to |
Daniel Dunbar | c158306 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 824 | /// "llvm.used". |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 825 | llvm::GlobalVariable *CreateMetadataVar(const std::string &Name, |
| 826 | llvm::Constant *Init, |
| 827 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 828 | unsigned Align, |
| 829 | bool AddToUsed); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 830 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 831 | /// GetNamedIvarList - Return the list of ivars in the interface |
| 832 | /// itself (not including super classes and not including unnamed |
| 833 | /// bitfields). |
| 834 | /// |
| 835 | /// For the non-fragile ABI, this also includes synthesized property |
| 836 | /// ivars. |
| 837 | void GetNamedIvarList(const ObjCInterfaceDecl *OID, |
| 838 | llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const; |
| 839 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 840 | public: |
| 841 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm) |
| 842 | { } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 843 | |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 844 | virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 845 | |
| 846 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 847 | const ObjCContainerDecl *CD=0); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 848 | |
| 849 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
| 850 | |
| 851 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 852 | /// declaration, emitting it if necessary. The return value has type |
| 853 | /// ProtocolPtrTy. |
| 854 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
| 855 | |
| 856 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 857 | /// object for the given declaration, emitting it if needed. These |
| 858 | /// forward references will be filled in with empty bodies if no |
| 859 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 860 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 861 | }; |
| 862 | |
| 863 | class CGObjCMac : public CGObjCCommonMac { |
| 864 | private: |
| 865 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 866 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 867 | /// level information. |
| 868 | void EmitImageInfo(); |
| 869 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 870 | /// EmitModuleInfo - Another marker encoding module level |
| 871 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 872 | void EmitModuleInfo(); |
| 873 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 874 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 875 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 876 | llvm::Constant *EmitModuleSymbols(); |
| 877 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 878 | /// FinishModule - Write out global data structures at the end of |
| 879 | /// processing a translation unit. |
| 880 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 881 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 882 | /// EmitClassExtension - Generate the class extension structure used |
| 883 | /// to store the weak ivar layout and properties. The return value |
| 884 | /// has type ClassExtensionPtrTy. |
| 885 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 886 | |
| 887 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 888 | /// for the given class. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 889 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 890 | const ObjCInterfaceDecl *ID); |
| 891 | |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 892 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 893 | QualType ResultType, |
| 894 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 895 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 896 | QualType Arg0Ty, |
| 897 | bool IsSuper, |
| 898 | const CallArgList &CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 899 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 900 | /// EmitIvarList - Emit the ivar list for the given |
| 901 | /// implementation. If ForClass is true the list of class ivars |
| 902 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 903 | /// interface ivars will be emitted. The return value has type |
| 904 | /// IvarListPtrTy. |
| 905 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 906 | bool ForClass); |
| 907 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 908 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 909 | /// for the metaclass of the given interface. The return value has |
| 910 | /// type ClassPtrTy. |
| 911 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 912 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 913 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 914 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 915 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 916 | llvm::Constant *Protocols, |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 917 | const llvm::Type *InterfaceTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 918 | const ConstantVector &Methods); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 919 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 920 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 921 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 922 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 923 | |
| 924 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 925 | /// implementation. The return value has type MethodListPtrTy. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 926 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 927 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 928 | const ConstantVector &Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 929 | |
| 930 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 931 | /// method declarations. |
| 932 | /// - TypeName: The name for the type containing the methods. |
| 933 | /// - IsProtocol: True iff these methods are for a protocol. |
| 934 | /// - ClassMethds: True iff these are class methods. |
| 935 | /// - Required: When true, only "required" methods are |
| 936 | /// listed. Similarly, when false only "optional" methods are |
| 937 | /// listed. For classes this should always be true. |
| 938 | /// - begin, end: The method list to output. |
| 939 | /// |
| 940 | /// The return value has type MethodDescriptionListPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 941 | llvm::Constant *EmitMethodDescList(const std::string &Name, |
| 942 | const char *Section, |
| 943 | const ConstantVector &Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 944 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 945 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 946 | /// declaration, emitting it if necessary. The return value has type |
| 947 | /// ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 948 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 949 | |
| 950 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 951 | /// object for the given declaration, emitting it if needed. These |
| 952 | /// forward references will be filled in with empty bodies if no |
| 953 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 954 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 955 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 956 | /// EmitProtocolExtension - Generate the protocol extension |
| 957 | /// structure used to store optional instance and class methods, and |
| 958 | /// protocol properties. The return value has type |
| 959 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 960 | llvm::Constant * |
| 961 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 962 | const ConstantVector &OptInstanceMethods, |
| 963 | const ConstantVector &OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 964 | |
| 965 | /// EmitProtocolList - Generate the list of referenced |
| 966 | /// protocols. The return value has type ProtocolListPtrTy. |
Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 967 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 968 | ObjCProtocolDecl::protocol_iterator begin, |
| 969 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 970 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 971 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 972 | /// for the given selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 973 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 974 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 975 | public: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 976 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 977 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 978 | virtual llvm::Function *ModuleInitFunction(); |
| 979 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 980 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 981 | QualType ResultType, |
| 982 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 983 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 984 | bool IsClassMessage, |
| 985 | const CallArgList &CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 986 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 987 | virtual CodeGen::RValue |
| 988 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 989 | QualType ResultType, |
| 990 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 991 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 992 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 993 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 994 | bool IsClassMessage, |
| 995 | const CallArgList &CallArgs); |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 996 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 997 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 998 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 999 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1000 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1001 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1002 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1003 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1004 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1005 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1006 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1007 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1008 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1009 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 1010 | virtual llvm::Constant *GetPropertySetFunction(); |
| 1011 | virtual llvm::Constant *EnumerationMutationFunction(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1012 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 1013 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1014 | const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1015 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 1016 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1017 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 1018 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1019 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 1020 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1021 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 1022 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 1023 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 1024 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1025 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 1026 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 1027 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1028 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1029 | QualType ObjectTy, |
| 1030 | llvm::Value *BaseValue, |
| 1031 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1032 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1033 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1034 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1035 | const ObjCIvarDecl *Ivar); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1036 | }; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1037 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1038 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1039 | private: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1040 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1041 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 1042 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1043 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1044 | /// SuperClassReferences - uniqued super class references. |
| 1045 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; |
| 1046 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1047 | /// MetaClassReferences - uniqued meta class references. |
| 1048 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1049 | |
| 1050 | /// EHTypeReferences - uniqued class ehtype references. |
| 1051 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1052 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1053 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 1054 | /// processing a translation unit. |
| 1055 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1056 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 1057 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 1058 | unsigned InstanceStart, |
| 1059 | unsigned InstanceSize, |
| 1060 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1061 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
| 1062 | llvm::Constant *IsAGV, |
| 1063 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 1064 | llvm::Constant *ClassRoGV, |
| 1065 | bool HiddenVisibility); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1066 | |
| 1067 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
| 1068 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 1069 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
| 1070 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1071 | /// EmitMethodList - Emit the method list for the given |
| 1072 | /// implementation. The return value has type MethodListnfABITy. |
| 1073 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 1074 | const char *Section, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 1075 | const ConstantVector &Methods); |
| 1076 | /// EmitIvarList - Emit the ivar list for the given |
| 1077 | /// implementation. If ForClass is true the list of class ivars |
| 1078 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1079 | /// interface ivars will be emitted. The return value has type |
| 1080 | /// IvarListnfABIPtrTy. |
| 1081 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 1082 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1083 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 1084 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 1085 | unsigned long int offset); |
| 1086 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1087 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1088 | /// declaration, emitting it if necessary. The return value has type |
| 1089 | /// ProtocolPtrTy. |
| 1090 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
| 1091 | |
| 1092 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1093 | /// object for the given declaration, emitting it if needed. These |
| 1094 | /// forward references will be filled in with empty bodies if no |
| 1095 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1096 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
| 1097 | |
| 1098 | /// EmitProtocolList - Generate the list of referenced |
| 1099 | /// protocols. The return value has type ProtocolListPtrTy. |
| 1100 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 1101 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1102 | ObjCProtocolDecl::protocol_iterator end); |
| 1103 | |
| 1104 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1105 | QualType ResultType, |
| 1106 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 1107 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1108 | QualType Arg0Ty, |
| 1109 | bool IsSuper, |
| 1110 | const CallArgList &CallArgs); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 1111 | |
| 1112 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1113 | /// class of the given name. |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 1114 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name); |
| 1115 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1116 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1117 | /// for the given class reference. |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1118 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1119 | const ObjCInterfaceDecl *ID); |
| 1120 | |
| 1121 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1122 | /// for the given super class reference. |
| 1123 | llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder, |
| 1124 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1125 | |
| 1126 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 1127 | /// meta-data |
| 1128 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
| 1129 | const ObjCInterfaceDecl *ID); |
| 1130 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1131 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 1132 | /// the given ivar. |
| 1133 | /// |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 1134 | llvm::GlobalVariable * ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1135 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1136 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1137 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1138 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1139 | /// for the given selector. |
| 1140 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1141 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1142 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1143 | /// interface. The return value has type EHTypePtrTy. |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1144 | llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 1145 | bool ForDefinition); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1146 | |
| 1147 | const char *getMetaclassSymbolPrefix() const { |
| 1148 | return "OBJC_METACLASS_$_"; |
| 1149 | } |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 1150 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1151 | const char *getClassSymbolPrefix() const { |
| 1152 | return "OBJC_CLASS_$_"; |
| 1153 | } |
| 1154 | |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1155 | void GetClassSizeInfo(const ObjCInterfaceDecl *OID, |
| 1156 | uint32_t &InstanceStart, |
| 1157 | uint32_t &InstanceSize); |
| 1158 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1159 | public: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1160 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1161 | // FIXME. All stubs for now! |
| 1162 | virtual llvm::Function *ModuleInitFunction(); |
| 1163 | |
| 1164 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1165 | QualType ResultType, |
| 1166 | Selector Sel, |
| 1167 | llvm::Value *Receiver, |
| 1168 | bool IsClassMessage, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1169 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1170 | |
| 1171 | virtual CodeGen::RValue |
| 1172 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 1173 | QualType ResultType, |
| 1174 | Selector Sel, |
| 1175 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1176 | bool isCategoryImpl, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1177 | llvm::Value *Receiver, |
| 1178 | bool IsClassMessage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1179 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1180 | |
| 1181 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1182 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1183 | |
| 1184 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel) |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1185 | { return EmitSelector(Builder, Sel); } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1186 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 1187 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1188 | |
| 1189 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1190 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 1191 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1192 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1193 | virtual llvm::Constant *GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1194 | return ObjCTypes.getGetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1195 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1196 | virtual llvm::Constant *GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1197 | return ObjCTypes.getSetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1198 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1199 | virtual llvm::Constant *EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1200 | return ObjCTypes.getEnumerationMutationFn(); |
Daniel Dunbar | 28ed084 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 1201 | } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1202 | |
| 1203 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 1204 | const Stmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1205 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 1206 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1207 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1208 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1209 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1210 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1211 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1212 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1213 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1214 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1215 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1216 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1217 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1218 | QualType ObjectTy, |
| 1219 | llvm::Value *BaseValue, |
| 1220 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1221 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1222 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1223 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1224 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1225 | }; |
| 1226 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1227 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1228 | |
| 1229 | /* *** Helper Functions *** */ |
| 1230 | |
| 1231 | /// getConstantGEP() - Help routine to construct simple GEPs. |
| 1232 | static llvm::Constant *getConstantGEP(llvm::Constant *C, |
| 1233 | unsigned idx0, |
| 1234 | unsigned idx1) { |
| 1235 | llvm::Value *Idxs[] = { |
| 1236 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0), |
| 1237 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1) |
| 1238 | }; |
| 1239 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2); |
| 1240 | } |
| 1241 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1242 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 1243 | /// class has the __objc_exception__ attribute. |
| 1244 | static bool hasObjCExceptionAttribute(const ObjCInterfaceDecl *OID) { |
Daniel Dunbar | b11fa0d | 2009-04-13 21:08:27 +0000 | [diff] [blame] | 1245 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1246 | return true; |
| 1247 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
| 1248 | return hasObjCExceptionAttribute(Super); |
| 1249 | return false; |
| 1250 | } |
| 1251 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1252 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1253 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1254 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
| 1255 | ObjCTypes(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1256 | { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1257 | ObjCABI = 1; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1258 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1261 | /// GetClass - Return a reference to the class for the given interface |
| 1262 | /// decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1263 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1264 | const ObjCInterfaceDecl *ID) { |
| 1265 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1269 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 1270 | return EmitSelector(Builder, Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1273 | /// Generate a constant CFString object. |
| 1274 | /* |
| 1275 | struct __builtin_CFString { |
| 1276 | const int *isa; // point to __CFConstantStringClassReference |
| 1277 | int flags; |
| 1278 | const char *str; |
| 1279 | long length; |
| 1280 | }; |
| 1281 | */ |
| 1282 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1283 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 1284 | const ObjCStringLiteral *SL) { |
Steve Naroff | 8d4141f | 2009-04-01 13:55:36 +0000 | [diff] [blame] | 1285 | return CGM.GetAddrOfConstantCFString(SL->getString()); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | /// Generates a message send where the super is the receiver. This is |
| 1289 | /// a message send to self with special delivery semantics indicating |
| 1290 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1291 | CodeGen::RValue |
| 1292 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1293 | QualType ResultType, |
| 1294 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1295 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1296 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1297 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1298 | bool IsClassMessage, |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1299 | const CodeGen::CallArgList &CallArgs) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1300 | // Create and init a super structure; this is a (receiver, class) |
| 1301 | // pair we will pass to objc_msgSendSuper. |
| 1302 | llvm::Value *ObjCSuper = |
| 1303 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 1304 | llvm::Value *ReceiverAsObject = |
| 1305 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 1306 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 1307 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1308 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1309 | // If this is a class message the metaclass is passed as the target. |
| 1310 | llvm::Value *Target; |
| 1311 | if (IsClassMessage) { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1312 | if (isCategoryImpl) { |
| 1313 | // Message sent to 'super' in a class method defined in a category |
| 1314 | // implementation requires an odd treatment. |
| 1315 | // If we are in a class method, we must retrieve the |
| 1316 | // _metaclass_ for the current class, pointed at by |
| 1317 | // the class's "isa" pointer. The following assumes that |
| 1318 | // isa" is the first ivar in a class (which it must be). |
| 1319 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1320 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 1321 | Target = CGF.Builder.CreateLoad(Target); |
| 1322 | } |
| 1323 | else { |
| 1324 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 1325 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 1326 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 1327 | Target = Super; |
| 1328 | } |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1329 | } else { |
| 1330 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1331 | } |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1332 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 1333 | // and ObjCTypes types. |
| 1334 | const llvm::Type *ClassTy = |
| 1335 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1336 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1337 | CGF.Builder.CreateStore(Target, |
| 1338 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 1339 | |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1340 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1341 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 1342 | true, CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1343 | } |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1344 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1345 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1346 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1347 | QualType ResultType, |
| 1348 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1349 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1350 | bool IsClassMessage, |
| 1351 | const CallArgList &CallArgs) { |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1352 | llvm::Value *Arg0 = |
| 1353 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1354 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1355 | Arg0, CGF.getContext().getObjCIdType(), |
| 1356 | false, CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1360 | QualType ResultType, |
| 1361 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1362 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1363 | QualType Arg0Ty, |
| 1364 | bool IsSuper, |
| 1365 | const CallArgList &CallArgs) { |
| 1366 | CallArgList ActualArgs; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1367 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 1368 | ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder, |
| 1369 | Sel)), |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1370 | CGF.getContext().getObjCSelType())); |
| 1371 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1372 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1373 | CodeGenTypes &Types = CGM.getTypes(); |
| 1374 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); |
| 1375 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1376 | |
| 1377 | llvm::Constant *Fn; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1378 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1379 | Fn = ObjCTypes.getSendStretFn(IsSuper); |
| 1380 | } else if (ResultType->isFloatingType()) { |
| 1381 | // FIXME: Sadly, this is wrong. This actually depends on the |
| 1382 | // architecture. This happens to be right for x86-32 though. |
| 1383 | Fn = ObjCTypes.getSendFpretFn(IsSuper); |
| 1384 | } else { |
| 1385 | Fn = ObjCTypes.getSendFn(IsSuper); |
| 1386 | } |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 1387 | Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy)); |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1388 | return CGF.EmitCall(FnInfo, Fn, ActualArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1391 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1392 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1393 | // FIXME: I don't understand why gcc generates this, or where it is |
| 1394 | // resolved. Investigate. Its also wasteful to look this up over and |
| 1395 | // over. |
| 1396 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1397 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1398 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
| 1399 | ObjCTypes.ExternalProtocolPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1402 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1403 | // FIXME: We shouldn't need this, the protocol decl should contain |
| 1404 | // enough information to tell us whether this was a declaration or a |
| 1405 | // definition. |
| 1406 | DefinedProtocols.insert(PD->getIdentifier()); |
| 1407 | |
| 1408 | // If we have generated a forward reference to this protocol, emit |
| 1409 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 1410 | // emitted. |
| 1411 | if (Protocols.count(PD->getIdentifier())) |
| 1412 | GetOrEmitProtocol(PD); |
| 1413 | } |
| 1414 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1415 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1416 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 1417 | return GetOrEmitProtocol(PD); |
| 1418 | return GetOrEmitProtocolRef(PD); |
| 1419 | } |
| 1420 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1421 | /* |
| 1422 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 1423 | struct _objc_protocol { |
| 1424 | struct _objc_protocol_extension *isa; |
| 1425 | char *protocol_name; |
| 1426 | struct _objc_protocol_list *protocol_list; |
| 1427 | struct _objc__method_prototype_list *instance_methods; |
| 1428 | struct _objc__method_prototype_list *class_methods |
| 1429 | }; |
| 1430 | |
| 1431 | See EmitProtocolExtension(). |
| 1432 | */ |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1433 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
| 1434 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1435 | |
| 1436 | // Early exit if a defining object has already been generated. |
| 1437 | if (Entry && Entry->hasInitializer()) |
| 1438 | return Entry; |
| 1439 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1440 | // FIXME: I don't understand why gcc generates this, or where it is |
| 1441 | // resolved. Investigate. Its also wasteful to look this up over and |
| 1442 | // over. |
| 1443 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1444 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1445 | const char *ProtocolName = PD->getNameAsCString(); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1446 | |
| 1447 | // Construct method lists. |
| 1448 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1449 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1450 | for (ObjCProtocolDecl::instmeth_iterator |
| 1451 | i = PD->instmeth_begin(CGM.getContext()), |
| 1452 | e = PD->instmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1453 | ObjCMethodDecl *MD = *i; |
| 1454 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1455 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1456 | OptInstanceMethods.push_back(C); |
| 1457 | } else { |
| 1458 | InstanceMethods.push_back(C); |
| 1459 | } |
| 1460 | } |
| 1461 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1462 | for (ObjCProtocolDecl::classmeth_iterator |
| 1463 | i = PD->classmeth_begin(CGM.getContext()), |
| 1464 | e = PD->classmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1465 | ObjCMethodDecl *MD = *i; |
| 1466 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1467 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1468 | OptClassMethods.push_back(C); |
| 1469 | } else { |
| 1470 | ClassMethods.push_back(C); |
| 1471 | } |
| 1472 | } |
| 1473 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1474 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1475 | Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1476 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1477 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1478 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(), |
Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1479 | PD->protocol_begin(), |
| 1480 | PD->protocol_end()); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1481 | Values[3] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1482 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" |
| 1483 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1484 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1485 | InstanceMethods); |
| 1486 | Values[4] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1487 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" |
| 1488 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1489 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1490 | ClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1491 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 1492 | Values); |
| 1493 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1494 | if (Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1495 | // Already created, fix the linkage and update the initializer. |
| 1496 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1497 | Entry->setInitializer(Init); |
| 1498 | } else { |
| 1499 | Entry = |
| 1500 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
| 1501 | llvm::GlobalValue::InternalLinkage, |
| 1502 | Init, |
| 1503 | std::string("\01L_OBJC_PROTOCOL_")+ProtocolName, |
| 1504 | &CGM.getModule()); |
| 1505 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1506 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1507 | UsedGlobals.push_back(Entry); |
| 1508 | // FIXME: Is this necessary? Why only for protocol? |
| 1509 | Entry->setAlignment(4); |
| 1510 | } |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1511 | |
| 1512 | return Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1515 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1516 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1517 | |
| 1518 | if (!Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1519 | // We use the initializer as a marker of whether this is a forward |
| 1520 | // reference or not. At module finalization we add the empty |
| 1521 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1522 | Entry = |
| 1523 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1524 | llvm::GlobalValue::ExternalLinkage, |
| 1525 | 0, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1526 | "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1527 | &CGM.getModule()); |
| 1528 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1529 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1530 | UsedGlobals.push_back(Entry); |
| 1531 | // FIXME: Is this necessary? Why only for protocol? |
| 1532 | Entry->setAlignment(4); |
| 1533 | } |
| 1534 | |
| 1535 | return Entry; |
| 1536 | } |
| 1537 | |
| 1538 | /* |
| 1539 | struct _objc_protocol_extension { |
| 1540 | uint32_t size; |
| 1541 | struct objc_method_description_list *optional_instance_methods; |
| 1542 | struct objc_method_description_list *optional_class_methods; |
| 1543 | struct objc_property_list *instance_properties; |
| 1544 | }; |
| 1545 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1546 | llvm::Constant * |
| 1547 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1548 | const ConstantVector &OptInstanceMethods, |
| 1549 | const ConstantVector &OptClassMethods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1550 | uint64_t Size = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1551 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1552 | std::vector<llvm::Constant*> Values(4); |
| 1553 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1554 | Values[1] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1555 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
| 1556 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1557 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1558 | OptInstanceMethods); |
| 1559 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1560 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" |
| 1561 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1562 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1563 | OptClassMethods); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1564 | Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + |
| 1565 | PD->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1566 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1567 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1568 | // Return null if no extension bits are used. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1569 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
| 1570 | Values[3]->isNullValue()) |
| 1571 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 1572 | |
| 1573 | llvm::Constant *Init = |
| 1574 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1575 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1576 | // No special section, but goes in llvm.used |
| 1577 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(), |
| 1578 | Init, |
| 1579 | 0, 0, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | /* |
| 1583 | struct objc_protocol_list { |
| 1584 | struct objc_protocol_list *next; |
| 1585 | long count; |
| 1586 | Protocol *list[]; |
| 1587 | }; |
| 1588 | */ |
Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1589 | llvm::Constant * |
| 1590 | CGObjCMac::EmitProtocolList(const std::string &Name, |
| 1591 | ObjCProtocolDecl::protocol_iterator begin, |
| 1592 | ObjCProtocolDecl::protocol_iterator end) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1593 | std::vector<llvm::Constant*> ProtocolRefs; |
| 1594 | |
Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1595 | for (; begin != end; ++begin) |
| 1596 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1597 | |
| 1598 | // Just return null for empty protocol lists |
| 1599 | if (ProtocolRefs.empty()) |
| 1600 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1601 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1602 | // This list is null terminated. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1603 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
| 1604 | |
| 1605 | std::vector<llvm::Constant*> Values(3); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1606 | // This field is only used by the runtime. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1607 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1608 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 1609 | Values[2] = |
| 1610 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 1611 | ProtocolRefs.size()), |
| 1612 | ProtocolRefs); |
| 1613 | |
| 1614 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1615 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1616 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1617 | 4, false); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1618 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
| 1619 | } |
| 1620 | |
| 1621 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1622 | struct _objc_property { |
| 1623 | const char * const name; |
| 1624 | const char * const attributes; |
| 1625 | }; |
| 1626 | |
| 1627 | struct _objc_property_list { |
| 1628 | uint32_t entsize; // sizeof (struct _objc_property) |
| 1629 | uint32_t prop_count; |
| 1630 | struct _objc_property[prop_count]; |
| 1631 | }; |
| 1632 | */ |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1633 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name, |
| 1634 | const Decl *Container, |
| 1635 | const ObjCContainerDecl *OCD, |
| 1636 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1637 | std::vector<llvm::Constant*> Properties, Prop(2); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1638 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(CGM.getContext()), |
| 1639 | E = OCD->prop_end(CGM.getContext()); I != E; ++I) { |
Steve Naroff | 93983f8 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 1640 | const ObjCPropertyDecl *PD = *I; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1641 | Prop[0] = GetPropertyName(PD->getIdentifier()); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1642 | Prop[1] = GetPropertyTypeString(PD, Container); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1643 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
| 1644 | Prop)); |
| 1645 | } |
| 1646 | |
| 1647 | // Return null for empty list. |
| 1648 | if (Properties.empty()) |
| 1649 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1650 | |
| 1651 | unsigned PropertySize = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1652 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1653 | std::vector<llvm::Constant*> Values(3); |
| 1654 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 1655 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
| 1656 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
| 1657 | Properties.size()); |
| 1658 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
| 1659 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1660 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1661 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1662 | CreateMetadataVar(Name, Init, |
| 1663 | (ObjCABI == 2) ? "__DATA, __objc_const" : |
| 1664 | "__OBJC,__property,regular,no_dead_strip", |
| 1665 | (ObjCABI == 2) ? 8 : 4, |
| 1666 | true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1667 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1671 | struct objc_method_description_list { |
| 1672 | int count; |
| 1673 | struct objc_method_description list[]; |
| 1674 | }; |
| 1675 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1676 | llvm::Constant * |
| 1677 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 1678 | std::vector<llvm::Constant*> Desc(2); |
| 1679 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1680 | ObjCTypes.SelectorPtrTy); |
| 1681 | Desc[1] = GetMethodVarType(MD); |
| 1682 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
| 1683 | Desc); |
| 1684 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1685 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1686 | llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name, |
| 1687 | const char *Section, |
| 1688 | const ConstantVector &Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1689 | // Return null for empty list. |
| 1690 | if (Methods.empty()) |
| 1691 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 1692 | |
| 1693 | std::vector<llvm::Constant*> Values(2); |
| 1694 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 1695 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
| 1696 | Methods.size()); |
| 1697 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
| 1698 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1699 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1700 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1701 | return llvm::ConstantExpr::getBitCast(GV, |
| 1702 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1705 | /* |
| 1706 | struct _objc_category { |
| 1707 | char *category_name; |
| 1708 | char *class_name; |
| 1709 | struct _objc_method_list *instance_methods; |
| 1710 | struct _objc_method_list *class_methods; |
| 1711 | struct _objc_protocol_list *protocols; |
| 1712 | uint32_t size; // <rdar://4585769> |
| 1713 | struct _objc_property_list *instance_properties; |
| 1714 | }; |
| 1715 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1716 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1717 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1718 | |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1719 | // FIXME: This is poor design, the OCD should have a pointer to the |
| 1720 | // category decl. Additionally, note that Category can be null for |
| 1721 | // the @implementation w/o an @interface case. Sema should just |
| 1722 | // create one for us as it does for @implementation so everyone else |
| 1723 | // can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1724 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1725 | const ObjCCategoryDecl *Category = |
| 1726 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1727 | std::string ExtName(Interface->getNameAsString() + "_" + |
| 1728 | OCD->getNameAsString()); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1729 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1730 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1731 | for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(), |
| 1732 | e = OCD->instmeth_end(); i != e; ++i) { |
| 1733 | // Instance methods should always be defined. |
| 1734 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1735 | } |
| 1736 | for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(), |
| 1737 | e = OCD->classmeth_end(); i != e; ++i) { |
| 1738 | // Class methods should always be defined. |
| 1739 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1740 | } |
| 1741 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1742 | std::vector<llvm::Constant*> Values(7); |
| 1743 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 1744 | Values[1] = GetClassName(Interface->getIdentifier()); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1745 | Values[2] = |
| 1746 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") + |
| 1747 | ExtName, |
| 1748 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1749 | InstanceMethods); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1750 | Values[3] = |
| 1751 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName, |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1752 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1753 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1754 | if (Category) { |
| 1755 | Values[4] = |
| 1756 | EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName, |
| 1757 | Category->protocol_begin(), |
| 1758 | Category->protocol_end()); |
| 1759 | } else { |
| 1760 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1761 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1762 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1763 | |
| 1764 | // If there is no category @interface then there can be no properties. |
| 1765 | if (Category) { |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1766 | Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1767 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1768 | } else { |
| 1769 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1770 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1771 | |
| 1772 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
| 1773 | Values); |
| 1774 | |
| 1775 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1776 | CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init, |
| 1777 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1778 | 4, true); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1779 | DefinedCategories.push_back(GV); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1782 | // FIXME: Get from somewhere? |
| 1783 | enum ClassFlags { |
| 1784 | eClassFlags_Factory = 0x00001, |
| 1785 | eClassFlags_Meta = 0x00002, |
| 1786 | // <rdr://5142207> |
| 1787 | eClassFlags_HasCXXStructors = 0x02000, |
| 1788 | eClassFlags_Hidden = 0x20000, |
| 1789 | eClassFlags_ABI2_Hidden = 0x00010, |
| 1790 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 1791 | }; |
| 1792 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1793 | /* |
| 1794 | struct _objc_class { |
| 1795 | Class isa; |
| 1796 | Class super_class; |
| 1797 | const char *name; |
| 1798 | long version; |
| 1799 | long info; |
| 1800 | long instance_size; |
| 1801 | struct _objc_ivar_list *ivars; |
| 1802 | struct _objc_method_list *methods; |
| 1803 | struct _objc_cache *cache; |
| 1804 | struct _objc_protocol_list *protocols; |
| 1805 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 1806 | const char *ivar_layout; |
| 1807 | struct _objc_class_ext *ext; |
| 1808 | }; |
| 1809 | |
| 1810 | See EmitClassExtension(); |
| 1811 | */ |
| 1812 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1813 | DefinedSymbols.insert(ID->getIdentifier()); |
| 1814 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1815 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1816 | // FIXME: Gross |
| 1817 | ObjCInterfaceDecl *Interface = |
| 1818 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1819 | llvm::Constant *Protocols = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1820 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(), |
Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1821 | Interface->protocol_begin(), |
| 1822 | Interface->protocol_end()); |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 1823 | const llvm::Type *InterfaceTy; |
| 1824 | if (Interface->isForwardDecl()) |
| 1825 | InterfaceTy = llvm::StructType::get(NULL, NULL); |
| 1826 | else |
| 1827 | InterfaceTy = |
Chris Lattner | 03d9f34 | 2009-04-01 06:23:52 +0000 | [diff] [blame] | 1828 | CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1829 | unsigned Flags = eClassFlags_Factory; |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1830 | unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1831 | |
| 1832 | // FIXME: Set CXX-structors flag. |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1833 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1834 | Flags |= eClassFlags_Hidden; |
| 1835 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1836 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1837 | for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(), |
| 1838 | e = ID->instmeth_end(); i != e; ++i) { |
| 1839 | // Instance methods should always be defined. |
| 1840 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1841 | } |
| 1842 | for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(), |
| 1843 | e = ID->classmeth_end(); i != e; ++i) { |
| 1844 | // Class methods should always be defined. |
| 1845 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1846 | } |
| 1847 | |
| 1848 | for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(), |
| 1849 | e = ID->propimpl_end(); i != e; ++i) { |
| 1850 | ObjCPropertyImplDecl *PID = *i; |
| 1851 | |
| 1852 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 1853 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 1854 | |
| 1855 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 1856 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1857 | InstanceMethods.push_back(C); |
| 1858 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 1859 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1860 | InstanceMethods.push_back(C); |
| 1861 | } |
| 1862 | } |
| 1863 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1864 | std::vector<llvm::Constant*> Values(12); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1865 | Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1866 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1867 | // Record a reference to the super class. |
| 1868 | LazySymbols.insert(Super->getIdentifier()); |
| 1869 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1870 | Values[ 1] = |
| 1871 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1872 | ObjCTypes.ClassPtrTy); |
| 1873 | } else { |
| 1874 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1875 | } |
| 1876 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1877 | // Version is always 0. |
| 1878 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1879 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1880 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1881 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1882 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1883 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1884 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1885 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1886 | // cache is always NULL. |
| 1887 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1888 | Values[ 9] = Protocols; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1889 | // FIXME: Set ivar_layout |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 1890 | // Values[10] = BuildIvarLayout(ID, true); |
| 1891 | Values[10] = GetIvarLayoutName(0, ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1892 | Values[11] = EmitClassExtension(ID); |
| 1893 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1894 | Values); |
| 1895 | |
| 1896 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1897 | CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init, |
| 1898 | "__OBJC,__class,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1899 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1900 | DefinedClasses.push_back(GV); |
| 1901 | } |
| 1902 | |
| 1903 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1904 | llvm::Constant *Protocols, |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1905 | const llvm::Type *InterfaceTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1906 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1907 | unsigned Flags = eClassFlags_Meta; |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1908 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1909 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1910 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1911 | Flags |= eClassFlags_Hidden; |
| 1912 | |
| 1913 | std::vector<llvm::Constant*> Values(12); |
| 1914 | // The isa for the metaclass is the root of the hierarchy. |
| 1915 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 1916 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 1917 | Root = Super; |
| 1918 | Values[ 0] = |
| 1919 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
| 1920 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1921 | // The super class for the metaclass is emitted as the name of the |
| 1922 | // super class. The runtime fixes this up to point to the |
| 1923 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1924 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
| 1925 | Values[ 1] = |
| 1926 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1927 | ObjCTypes.ClassPtrTy); |
| 1928 | } else { |
| 1929 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1930 | } |
| 1931 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1932 | // Version is always 0. |
| 1933 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1934 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1935 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1936 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1937 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1938 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1939 | "__OBJC,__cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1940 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1941 | // cache is always NULL. |
| 1942 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1943 | Values[ 9] = Protocols; |
| 1944 | // ivar_layout for metaclass is always NULL. |
| 1945 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 1946 | // The class extension is always unused for metaclasses. |
| 1947 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 1948 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1949 | Values); |
| 1950 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1951 | std::string Name("\01L_OBJC_METACLASS_"); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1952 | Name += ID->getNameAsCString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1953 | |
| 1954 | // Check for a forward reference. |
| 1955 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 1956 | if (GV) { |
| 1957 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 1958 | "Forward metaclass reference has incorrect type."); |
| 1959 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 1960 | GV->setInitializer(Init); |
| 1961 | } else { |
| 1962 | GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1963 | llvm::GlobalValue::InternalLinkage, |
| 1964 | Init, Name, |
| 1965 | &CGM.getModule()); |
| 1966 | } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1967 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1968 | GV->setAlignment(4); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1969 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1970 | |
| 1971 | return GV; |
| 1972 | } |
| 1973 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1974 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1975 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1976 | |
| 1977 | // FIXME: Should we look these up somewhere other than the |
| 1978 | // module. Its a bit silly since we only generate these while |
| 1979 | // processing an implementation, so exactly one pointer would work |
| 1980 | // if know when we entered/exitted an implementation block. |
| 1981 | |
| 1982 | // Check for an existing forward reference. |
Fariborz Jahanian | b0d2794 | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 1983 | // Previously, metaclass with internal linkage may have been defined. |
| 1984 | // pass 'true' as 2nd argument so it is returned. |
| 1985 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) { |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1986 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 1987 | "Forward metaclass reference has incorrect type."); |
| 1988 | return GV; |
| 1989 | } else { |
| 1990 | // Generate as an external reference to keep a consistent |
| 1991 | // module. This will be patched up when we emit the metaclass. |
| 1992 | return new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1993 | llvm::GlobalValue::ExternalLinkage, |
| 1994 | 0, |
| 1995 | Name, |
| 1996 | &CGM.getModule()); |
| 1997 | } |
| 1998 | } |
| 1999 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2000 | /* |
| 2001 | struct objc_class_ext { |
| 2002 | uint32_t size; |
| 2003 | const char *weak_ivar_layout; |
| 2004 | struct _objc_property_list *properties; |
| 2005 | }; |
| 2006 | */ |
| 2007 | llvm::Constant * |
| 2008 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
| 2009 | uint64_t Size = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 2010 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2011 | |
| 2012 | std::vector<llvm::Constant*> Values(3); |
| 2013 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2014 | // FIXME: Output weak_ivar_layout string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2015 | // Values[1] = BuildIvarLayout(ID, false); |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 2016 | Values[1] = GetIvarLayoutName(0, ObjCTypes); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2017 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2018 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2019 | |
| 2020 | // Return null if no extension bits are used. |
| 2021 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
| 2022 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 2023 | |
| 2024 | llvm::Constant *Init = |
| 2025 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2026 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2027 | Init, "__OBJC,__class_ext,regular,no_dead_strip", |
| 2028 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 2031 | /// getInterfaceDeclForIvar - Get the interface declaration node where |
| 2032 | /// this ivar is declared in. |
| 2033 | /// FIXME. Ideally, this info should be in the ivar node. But currently |
| 2034 | /// it is not and prevailing wisdom is that ASTs should not have more |
| 2035 | /// info than is absolutely needed, even though this info reflects the |
| 2036 | /// source language. |
| 2037 | /// |
| 2038 | static const ObjCInterfaceDecl *getInterfaceDeclForIvar( |
| 2039 | const ObjCInterfaceDecl *OI, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2040 | const ObjCIvarDecl *IVD, |
| 2041 | ASTContext &Context) { |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 2042 | if (!OI) |
| 2043 | return 0; |
| 2044 | assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface"); |
| 2045 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 2046 | E = OI->ivar_end(); I != E; ++I) |
| 2047 | if ((*I)->getIdentifier() == IVD->getIdentifier()) |
| 2048 | return OI; |
Fariborz Jahanian | 5a4b453 | 2009-03-31 17:00:52 +0000 | [diff] [blame] | 2049 | // look into properties. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2050 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context), |
| 2051 | E = OI->prop_end(Context); I != E; ++I) { |
Fariborz Jahanian | 5a4b453 | 2009-03-31 17:00:52 +0000 | [diff] [blame] | 2052 | ObjCPropertyDecl *PDecl = (*I); |
| 2053 | if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl()) |
| 2054 | if (IV->getIdentifier() == IVD->getIdentifier()) |
| 2055 | return OI; |
| 2056 | } |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2057 | return getInterfaceDeclForIvar(OI->getSuperClass(), IVD, Context); |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2060 | /* |
| 2061 | struct objc_ivar { |
| 2062 | char *ivar_name; |
| 2063 | char *ivar_type; |
| 2064 | int ivar_offset; |
| 2065 | }; |
| 2066 | |
| 2067 | struct objc_ivar_list { |
| 2068 | int ivar_count; |
| 2069 | struct objc_ivar list[count]; |
| 2070 | }; |
| 2071 | */ |
| 2072 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2073 | bool ForClass) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2074 | std::vector<llvm::Constant*> Ivars, Ivar(3); |
| 2075 | |
| 2076 | // When emitting the root class GCC emits ivar entries for the |
| 2077 | // actual class structure. It is not clear if we need to follow this |
| 2078 | // behavior; for now lets try and get away with not doing it. If so, |
| 2079 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 2080 | // for the class. |
| 2081 | if (ForClass) |
| 2082 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2083 | |
| 2084 | ObjCInterfaceDecl *OID = |
| 2085 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2086 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 2087 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
| 2088 | GetNamedIvarList(OID, OIvars); |
| 2089 | |
| 2090 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 2091 | ObjCIvarDecl *IVD = OIvars[i]; |
| 2092 | const FieldDecl *Field = OID->lookupFieldDeclForIvar(CGM.getContext(), IVD); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 2093 | Ivar[0] = GetMethodVarName(Field->getIdentifier()); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 2094 | Ivar[1] = GetMethodVarType(Field); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 2095 | Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame^] | 2096 | ComputeIvarBaseOffset(CGM, OID, IVD)); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2097 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2098 | } |
| 2099 | |
| 2100 | // Return null for empty list. |
| 2101 | if (Ivars.empty()) |
| 2102 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
| 2103 | |
| 2104 | std::vector<llvm::Constant*> Values(2); |
| 2105 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 2106 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
| 2107 | Ivars.size()); |
| 2108 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
| 2109 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2110 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2111 | llvm::GlobalVariable *GV; |
| 2112 | if (ForClass) |
| 2113 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(), |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2114 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
| 2115 | 4, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2116 | else |
| 2117 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" |
| 2118 | + ID->getNameAsString(), |
| 2119 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2120 | 4, true); |
| 2121 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2122 | } |
| 2123 | |
| 2124 | /* |
| 2125 | struct objc_method { |
| 2126 | SEL method_name; |
| 2127 | char *method_types; |
| 2128 | void *method; |
| 2129 | }; |
| 2130 | |
| 2131 | struct objc_method_list { |
| 2132 | struct objc_method_list *obsolete; |
| 2133 | int count; |
| 2134 | struct objc_method methods_list[count]; |
| 2135 | }; |
| 2136 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2137 | |
| 2138 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 2139 | /// given method if it has been defined. The result is null if the |
| 2140 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2141 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2142 | // FIXME: Use DenseMap::lookup |
| 2143 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 2144 | if (!Fn) |
| 2145 | return 0; |
| 2146 | |
| 2147 | std::vector<llvm::Constant*> Method(3); |
| 2148 | Method[0] = |
| 2149 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 2150 | ObjCTypes.SelectorPtrTy); |
| 2151 | Method[1] = GetMethodVarType(MD); |
| 2152 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 2153 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 2154 | } |
| 2155 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2156 | llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name, |
| 2157 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2158 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2159 | // Return null for empty list. |
| 2160 | if (Methods.empty()) |
| 2161 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
| 2162 | |
| 2163 | std::vector<llvm::Constant*> Values(3); |
| 2164 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 2165 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 2166 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 2167 | Methods.size()); |
| 2168 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 2169 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2170 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2171 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2172 | return llvm::ConstantExpr::getBitCast(GV, |
| 2173 | ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2174 | } |
| 2175 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 2176 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 2177 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2178 | std::string Name; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 2179 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2180 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2181 | CodeGenTypes &Types = CGM.getTypes(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2182 | const llvm::FunctionType *MethodTy = |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2183 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2184 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2185 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2186 | llvm::GlobalValue::InternalLinkage, |
| 2187 | Name, |
| 2188 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2189 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2190 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2191 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
Daniel Dunbar | 48fa064 | 2009-04-19 02:03:42 +0000 | [diff] [blame] | 2194 | /// GetFieldBaseOffset - return the field's byte offset. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2195 | uint64_t CGObjCCommonMac::GetFieldBaseOffset(const ObjCInterfaceDecl *OI, |
| 2196 | const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 2197 | const FieldDecl *Field) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame^] | 2198 | // Is this a C struct? |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 2199 | if (!OI) |
| 2200 | return Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame^] | 2201 | return ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2204 | llvm::GlobalVariable * |
| 2205 | CGObjCCommonMac::CreateMetadataVar(const std::string &Name, |
| 2206 | llvm::Constant *Init, |
| 2207 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2208 | unsigned Align, |
| 2209 | bool AddToUsed) { |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2210 | const llvm::Type *Ty = Init->getType(); |
| 2211 | llvm::GlobalVariable *GV = |
| 2212 | new llvm::GlobalVariable(Ty, false, |
| 2213 | llvm::GlobalValue::InternalLinkage, |
| 2214 | Init, |
| 2215 | Name, |
| 2216 | &CGM.getModule()); |
| 2217 | if (Section) |
| 2218 | GV->setSection(Section); |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2219 | if (Align) |
| 2220 | GV->setAlignment(Align); |
| 2221 | if (AddToUsed) |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2222 | UsedGlobals.push_back(GV); |
| 2223 | return GV; |
| 2224 | } |
| 2225 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2226 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2227 | // Abuse this interface function as a place to finalize. |
| 2228 | FinishModule(); |
| 2229 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2230 | return NULL; |
| 2231 | } |
| 2232 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2233 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2234 | return ObjCTypes.getGetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2235 | } |
| 2236 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2237 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2238 | return ObjCTypes.getSetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2241 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2242 | return ObjCTypes.getEnumerationMutationFn(); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2243 | } |
| 2244 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2245 | /* |
| 2246 | |
| 2247 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 2248 | -- |
| 2249 | |
| 2250 | The basic framework for a @try-catch-finally is as follows: |
| 2251 | { |
| 2252 | objc_exception_data d; |
| 2253 | id _rethrow = null; |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2254 | bool _call_try_exit = true; |
| 2255 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2256 | objc_exception_try_enter(&d); |
| 2257 | if (!setjmp(d.jmp_buf)) { |
| 2258 | ... try body ... |
| 2259 | } else { |
| 2260 | // exception path |
| 2261 | id _caught = objc_exception_extract(&d); |
| 2262 | |
| 2263 | // enter new try scope for handlers |
| 2264 | if (!setjmp(d.jmp_buf)) { |
| 2265 | ... match exception and execute catch blocks ... |
| 2266 | |
| 2267 | // fell off end, rethrow. |
| 2268 | _rethrow = _caught; |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2269 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2270 | } else { |
| 2271 | // exception in catch block |
| 2272 | _rethrow = objc_exception_extract(&d); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2273 | _call_try_exit = false; |
| 2274 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2275 | } |
| 2276 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2277 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2278 | |
| 2279 | finally: |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2280 | if (_call_try_exit) |
| 2281 | objc_exception_try_exit(&d); |
| 2282 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2283 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2284 | ... dispatch to finally destination ... |
| 2285 | |
| 2286 | finally_rethrow: |
| 2287 | objc_exception_throw(_rethrow); |
| 2288 | |
| 2289 | finally_end: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
| 2292 | This framework differs slightly from the one gcc uses, in that gcc |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2293 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 2294 | and if the object should be rethrown. This breaks in the face of |
| 2295 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2296 | |
| 2297 | We specialize this framework for a few particular circumstances: |
| 2298 | |
| 2299 | - If there are no catch blocks, then we avoid emitting the second |
| 2300 | exception handling context. |
| 2301 | |
| 2302 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 2303 | e)) we avoid emitting the code to rethrow an uncaught exception. |
| 2304 | |
| 2305 | - FIXME: If there is no @finally block we can do a few more |
| 2306 | simplifications. |
| 2307 | |
| 2308 | Rethrows and Jumps-Through-Finally |
| 2309 | -- |
| 2310 | |
| 2311 | Support for implicit rethrows and jumping through the finally block is |
| 2312 | handled by storing the current exception-handling context in |
| 2313 | ObjCEHStack. |
| 2314 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2315 | In order to implement proper @finally semantics, we support one basic |
| 2316 | mechanism for jumping through the finally block to an arbitrary |
| 2317 | destination. Constructs which generate exits from a @try or @catch |
| 2318 | block use this mechanism to implement the proper semantics by chaining |
| 2319 | jumps, as necessary. |
| 2320 | |
| 2321 | This mechanism works like the one used for indirect goto: we |
| 2322 | arbitrarily assign an ID to each destination and store the ID for the |
| 2323 | destination in a variable prior to entering the finally block. At the |
| 2324 | end of the finally block we simply create a switch to the proper |
| 2325 | destination. |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2326 | |
| 2327 | Code gen for @synchronized(expr) stmt; |
| 2328 | Effectively generating code for: |
| 2329 | objc_sync_enter(expr); |
| 2330 | @try stmt @finally { objc_sync_exit(expr); } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2331 | */ |
| 2332 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2333 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 2334 | const Stmt &S) { |
| 2335 | bool isTry = isa<ObjCAtTryStmt>(S); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2336 | // Create various blocks we refer to for handling @finally. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2337 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2338 | llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit"); |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2339 | llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit"); |
| 2340 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
| 2341 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2342 | |
| 2343 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 2344 | // evaluation of the expression must occur before we enter the |
| 2345 | // @synchronized. We can safely avoid a temp here because jumps into |
| 2346 | // @synchronized are illegal & this will dominate uses. |
| 2347 | llvm::Value *SyncArg = 0; |
| 2348 | if (!isTry) { |
| 2349 | SyncArg = |
| 2350 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 2351 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 2352 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2353 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2354 | |
| 2355 | // Push an EH context entry, used for handling rethrows and jumps |
| 2356 | // through finally. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2357 | CGF.PushCleanupBlock(FinallyBlock); |
| 2358 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2359 | CGF.ObjCEHValueStack.push_back(0); |
| 2360 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2361 | // Allocate memory for the exception data and rethrow pointer. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2362 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 2363 | "exceptiondata.ptr"); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2364 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy, |
| 2365 | "_rethrow"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2366 | llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty, |
| 2367 | "_call_try_exit"); |
| 2368 | CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr); |
| 2369 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2370 | // Enter a new try block and call setjmp. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2371 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2372 | llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0, |
| 2373 | "jmpbufarray"); |
| 2374 | JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2375 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2376 | JmpBufPtr, "result"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2377 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2378 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 2379 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2380 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2381 | TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2382 | |
| 2383 | // Emit the @try block. |
| 2384 | CGF.EmitBlock(TryBlock); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2385 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 2386 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2387 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2388 | |
| 2389 | // Emit the "exception in @try" block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2390 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2391 | |
| 2392 | // Retrieve the exception object. We may emit multiple blocks but |
| 2393 | // nothing can cross this so the value is already in SSA form. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2394 | llvm::Value *Caught = |
| 2395 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2396 | ExceptionData, "caught"); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2397 | CGF.ObjCEHValueStack.back() = Caught; |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2398 | if (!isTry) |
| 2399 | { |
| 2400 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2401 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2402 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2403 | } |
| 2404 | else if (const ObjCAtCatchStmt* CatchStmt = |
| 2405 | cast<ObjCAtTryStmt>(S).getCatchStmts()) |
| 2406 | { |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2407 | // Enter a new exception try block (in case a @catch block throws |
| 2408 | // an exception). |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2409 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2410 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2411 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2412 | JmpBufPtr, "result"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2413 | llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2414 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2415 | llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch"); |
| 2416 | llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler"); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2417 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2418 | |
| 2419 | CGF.EmitBlock(CatchBlock); |
| 2420 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2421 | // Handle catch list. As a special case we check if everything is |
| 2422 | // matched and avoid generating code for falling off the end if |
| 2423 | // so. |
| 2424 | bool AllMatched = false; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2425 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2426 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2427 | |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2428 | const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2429 | const PointerType *PT = 0; |
| 2430 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2431 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2432 | if (!CatchParam) { |
| 2433 | AllMatched = true; |
| 2434 | } else { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2435 | PT = CatchParam->getType()->getAsPointerType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2436 | |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 2437 | // catch(id e) always matches. |
| 2438 | // FIXME: For the time being we also match id<X>; this should |
| 2439 | // be rejected by Sema instead. |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2440 | if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) || |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2441 | CatchParam->getType()->isObjCQualifiedIdType()) |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2442 | AllMatched = true; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2443 | } |
| 2444 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2445 | if (AllMatched) { |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2446 | if (CatchParam) { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2447 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2448 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2449 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2450 | } |
Anders Carlsson | 1452f55 | 2008-09-11 08:21:54 +0000 | [diff] [blame] | 2451 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2452 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2453 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2454 | break; |
| 2455 | } |
| 2456 | |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2457 | assert(PT && "Unexpected non-pointer type in @catch"); |
| 2458 | QualType T = PT->getPointeeType(); |
Anders Carlsson | 4b7ff6e | 2008-09-11 06:35:14 +0000 | [diff] [blame] | 2459 | const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2460 | assert(ObjCType && "Catch parameter must have Objective-C type!"); |
| 2461 | |
| 2462 | // Check if the @catch block matches the exception object. |
| 2463 | llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl()); |
| 2464 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2465 | llvm::Value *Match = |
| 2466 | CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(), |
| 2467 | Class, Caught, "match"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2468 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2469 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2470 | |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2471 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2472 | MatchedBlock, NextCatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2473 | |
| 2474 | // Emit the @catch block. |
| 2475 | CGF.EmitBlock(MatchedBlock); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2476 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2477 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2478 | |
| 2479 | llvm::Value *Tmp = |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2480 | CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()), |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2481 | "tmp"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2482 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2483 | |
| 2484 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2485 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2486 | |
| 2487 | CGF.EmitBlock(NextCatchBlock); |
| 2488 | } |
| 2489 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2490 | if (!AllMatched) { |
| 2491 | // None of the handlers caught the exception, so store it to be |
| 2492 | // rethrown at the end of the @finally block. |
| 2493 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2494 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2495 | } |
| 2496 | |
| 2497 | // Emit the exception handler for the @catch blocks. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2498 | CGF.EmitBlock(CatchHandler); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2499 | CGF.Builder.CreateStore( |
| 2500 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2501 | ExceptionData), |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2502 | RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2503 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2504 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2505 | } else { |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2506 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2507 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2508 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2509 | } |
| 2510 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2511 | // Pop the exception-handling stack entry. It is important to do |
| 2512 | // this now, because the code in the @finally block is not in this |
| 2513 | // context. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2514 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 2515 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2516 | CGF.ObjCEHValueStack.pop_back(); |
| 2517 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2518 | // Emit the @finally block. |
| 2519 | CGF.EmitBlock(FinallyBlock); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2520 | llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp"); |
| 2521 | |
| 2522 | CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit); |
| 2523 | |
| 2524 | CGF.EmitBlock(FinallyExit); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2525 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2526 | |
| 2527 | CGF.EmitBlock(FinallyNoExit); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2528 | if (isTry) { |
| 2529 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 2530 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 2531 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2532 | } else { |
| 2533 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 2534 | // @synchronized. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2535 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg); |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 2536 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2537 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2538 | // Emit the switch block |
| 2539 | if (Info.SwitchBlock) |
| 2540 | CGF.EmitBlock(Info.SwitchBlock); |
| 2541 | if (Info.EndBlock) |
| 2542 | CGF.EmitBlock(Info.EndBlock); |
| 2543 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2544 | CGF.EmitBlock(FinallyRethrow); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2545 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2546 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2547 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2548 | |
| 2549 | CGF.EmitBlock(FinallyEnd); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2550 | } |
| 2551 | |
| 2552 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2553 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2554 | llvm::Value *ExceptionAsObject; |
| 2555 | |
| 2556 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 2557 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
| 2558 | ExceptionAsObject = |
| 2559 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 2560 | } else { |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2561 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2562 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2563 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2564 | } |
| 2565 | |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2566 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2567 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2568 | |
| 2569 | // Clear the insertion point to indicate we are in unreachable code. |
| 2570 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2571 | } |
| 2572 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2573 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2574 | /// object: objc_read_weak (id *src) |
| 2575 | /// |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2576 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2577 | llvm::Value *AddrWeakObj) |
| 2578 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2579 | const llvm::Type* DestTy = |
| 2580 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2581 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2582 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2583 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2584 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2585 | return read_weak; |
| 2586 | } |
| 2587 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2588 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 2589 | /// objc_assign_weak (id src, id *dst) |
| 2590 | /// |
| 2591 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 2592 | llvm::Value *src, llvm::Value *dst) |
| 2593 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2594 | const llvm::Type * SrcTy = src->getType(); |
| 2595 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2596 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2597 | assert(Size <= 8 && "does not support size > 8"); |
| 2598 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2599 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2600 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2601 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2602 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2603 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 2604 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2605 | src, dst, "weakassign"); |
| 2606 | return; |
| 2607 | } |
| 2608 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2609 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 2610 | /// objc_assign_global (id src, id *dst) |
| 2611 | /// |
| 2612 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 2613 | llvm::Value *src, llvm::Value *dst) |
| 2614 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2615 | const llvm::Type * SrcTy = src->getType(); |
| 2616 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2617 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2618 | assert(Size <= 8 && "does not support size > 8"); |
| 2619 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2620 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2621 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2622 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2623 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2624 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2625 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2626 | src, dst, "globalassign"); |
| 2627 | return; |
| 2628 | } |
| 2629 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2630 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 2631 | /// objc_assign_ivar (id src, id *dst) |
| 2632 | /// |
| 2633 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 2634 | llvm::Value *src, llvm::Value *dst) |
| 2635 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2636 | const llvm::Type * SrcTy = src->getType(); |
| 2637 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2638 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2639 | assert(Size <= 8 && "does not support size > 8"); |
| 2640 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2641 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2642 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2643 | } |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2644 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2645 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2646 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(), |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2647 | src, dst, "assignivar"); |
| 2648 | return; |
| 2649 | } |
| 2650 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2651 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 2652 | /// objc_assign_strongCast (id src, id *dst) |
| 2653 | /// |
| 2654 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 2655 | llvm::Value *src, llvm::Value *dst) |
| 2656 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2657 | const llvm::Type * SrcTy = src->getType(); |
| 2658 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2659 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2660 | assert(Size <= 8 && "does not support size > 8"); |
| 2661 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2662 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2663 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2664 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2665 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2666 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2667 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2668 | src, dst, "weakassign"); |
| 2669 | return; |
| 2670 | } |
| 2671 | |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2672 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 2673 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2674 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 2675 | QualType ObjectTy, |
| 2676 | llvm::Value *BaseValue, |
| 2677 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2678 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 2679 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame^] | 2680 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 2681 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2682 | } |
| 2683 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2684 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 2685 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2686 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame^] | 2687 | uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2688 | return llvm::ConstantInt::get( |
| 2689 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 2690 | Offset); |
| 2691 | } |
| 2692 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2693 | /* *** Private Interface *** */ |
| 2694 | |
| 2695 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 2696 | /// level information. |
| 2697 | /// |
| 2698 | /// See: <rdr://4810609&4810587&4810587> |
| 2699 | /// struct IMAGE_INFO { |
| 2700 | /// unsigned version; |
| 2701 | /// unsigned flags; |
| 2702 | /// }; |
| 2703 | enum ImageInfoFlags { |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2704 | eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what |
| 2705 | // this implies. |
| 2706 | eImageInfo_GarbageCollected = (1 << 1), |
| 2707 | eImageInfo_GCOnly = (1 << 2), |
| 2708 | eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set. |
| 2709 | |
| 2710 | // A flag indicating that the module has no instances of an |
| 2711 | // @synthesize of a superclass variable. <rdar://problem/6803242> |
| 2712 | eImageInfo_CorrectedSynthesize = (1 << 4) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2713 | }; |
| 2714 | |
| 2715 | void CGObjCMac::EmitImageInfo() { |
| 2716 | unsigned version = 0; // Version is unused? |
| 2717 | unsigned flags = 0; |
| 2718 | |
| 2719 | // FIXME: Fix and continue? |
| 2720 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 2721 | flags |= eImageInfo_GarbageCollected; |
| 2722 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 2723 | flags |= eImageInfo_GCOnly; |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2724 | |
| 2725 | // We never allow @synthesize of a superclass property. |
| 2726 | flags |= eImageInfo_CorrectedSynthesize; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2727 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2728 | // Emitted as int[2]; |
| 2729 | llvm::Constant *values[2] = { |
| 2730 | llvm::ConstantInt::get(llvm::Type::Int32Ty, version), |
| 2731 | llvm::ConstantInt::get(llvm::Type::Int32Ty, flags) |
| 2732 | }; |
| 2733 | llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2734 | |
| 2735 | const char *Section; |
| 2736 | if (ObjCABI == 1) |
| 2737 | Section = "__OBJC, __image_info,regular"; |
| 2738 | else |
| 2739 | Section = "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2740 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2741 | CreateMetadataVar("\01L_OBJC_IMAGE_INFO", |
| 2742 | llvm::ConstantArray::get(AT, values, 2), |
| 2743 | Section, |
| 2744 | 0, |
| 2745 | true); |
| 2746 | GV->setConstant(true); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2747 | } |
| 2748 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2749 | |
| 2750 | // struct objc_module { |
| 2751 | // unsigned long version; |
| 2752 | // unsigned long size; |
| 2753 | // const char *name; |
| 2754 | // Symtab symtab; |
| 2755 | // }; |
| 2756 | |
| 2757 | // FIXME: Get from somewhere |
| 2758 | static const int ModuleVersion = 7; |
| 2759 | |
| 2760 | void CGObjCMac::EmitModuleInfo() { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 2761 | uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2762 | |
| 2763 | std::vector<llvm::Constant*> Values(4); |
| 2764 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion); |
| 2765 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2766 | // This used to be the filename, now it is unused. <rdr://4327263> |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2767 | Values[2] = GetClassName(&CGM.getContext().Idents.get("")); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2768 | Values[3] = EmitModuleSymbols(); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2769 | CreateMetadataVar("\01L_OBJC_MODULES", |
| 2770 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
| 2771 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2772 | 4, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2773 | } |
| 2774 | |
| 2775 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2776 | unsigned NumClasses = DefinedClasses.size(); |
| 2777 | unsigned NumCategories = DefinedCategories.size(); |
| 2778 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2779 | // Return null if no symbols were defined. |
| 2780 | if (!NumClasses && !NumCategories) |
| 2781 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
| 2782 | |
| 2783 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2784 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2785 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
| 2786 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 2787 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
| 2788 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2789 | // The runtime expects exactly the list of defined classes followed |
| 2790 | // by the list of defined categories, in a single array. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2791 | std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2792 | for (unsigned i=0; i<NumClasses; i++) |
| 2793 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 2794 | ObjCTypes.Int8PtrTy); |
| 2795 | for (unsigned i=0; i<NumCategories; i++) |
| 2796 | Symbols[NumClasses + i] = |
| 2797 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 2798 | ObjCTypes.Int8PtrTy); |
| 2799 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2800 | Values[4] = |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2801 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2802 | NumClasses + NumCategories), |
| 2803 | Symbols); |
| 2804 | |
| 2805 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2806 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2807 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2808 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 2809 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2810 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2811 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
| 2812 | } |
| 2813 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2814 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2815 | const ObjCInterfaceDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2816 | LazySymbols.insert(ID->getIdentifier()); |
| 2817 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2818 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 2819 | |
| 2820 | if (!Entry) { |
| 2821 | llvm::Constant *Casted = |
| 2822 | llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()), |
| 2823 | ObjCTypes.ClassPtrTy); |
| 2824 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2825 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 2826 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2827 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2828 | } |
| 2829 | |
| 2830 | return Builder.CreateLoad(Entry, false, "tmp"); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2831 | } |
| 2832 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2833 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2834 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 2835 | |
| 2836 | if (!Entry) { |
| 2837 | llvm::Constant *Casted = |
| 2838 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 2839 | ObjCTypes.SelectorPtrTy); |
| 2840 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2841 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 2842 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2843 | 4, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2844 | } |
| 2845 | |
| 2846 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 2847 | } |
| 2848 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 2849 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2850 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2851 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2852 | if (!Entry) |
| 2853 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 2854 | llvm::ConstantArray::get(Ident->getName()), |
| 2855 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 2856 | 1, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2857 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2858 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 2861 | /// GetInterfaceDeclStructLayout - Get layout for ivars of given |
| 2862 | /// interface declaration. |
| 2863 | const llvm::StructLayout *CGObjCCommonMac::GetInterfaceDeclStructLayout( |
| 2864 | const ObjCInterfaceDecl *OID) const { |
Daniel Dunbar | 24c8991 | 2009-04-21 21:41:56 +0000 | [diff] [blame] | 2865 | assert(!OID->isForwardDecl() && "Invalid interface decl!"); |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 2866 | QualType T = CGM.getContext().getObjCInterfaceType(OID); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 2867 | const llvm::StructType *InterfaceTy = |
| 2868 | cast<llvm::StructType>(CGM.getTypes().ConvertType(T)); |
| 2869 | return CGM.getTargetData().getStructLayout(InterfaceTy); |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 2870 | } |
| 2871 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 2872 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 2873 | /// ivar layout bitmap. |
| 2874 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
| 2875 | const ObjCCommonTypesHelper &ObjCTypes) { |
| 2876 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 2877 | } |
| 2878 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2879 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCInterfaceDecl *OI, |
| 2880 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2881 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2882 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2883 | unsigned int BytePos, bool ForStrongLayout, |
| 2884 | int &Index, int &SkIndex, bool &HasUnion) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2885 | bool IsUnion = (RD && RD->isUnion()); |
| 2886 | uint64_t MaxUnionIvarSize = 0; |
| 2887 | uint64_t MaxSkippedUnionIvarSize = 0; |
| 2888 | FieldDecl *MaxField = 0; |
| 2889 | FieldDecl *MaxSkippedField = 0; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2890 | FieldDecl *LastFieldBitfield = 0; |
| 2891 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2892 | unsigned base = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2893 | if (RecFields.empty()) |
| 2894 | return; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2895 | if (IsUnion) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2896 | base = BytePos + GetFieldBaseOffset(OI, Layout, RecFields[0]); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2897 | unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0); |
| 2898 | unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth(); |
| 2899 | |
| 2900 | llvm::SmallVector<FieldDecl*, 16> TmpRecFields; |
| 2901 | |
| 2902 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2903 | FieldDecl *Field = RecFields[i]; |
| 2904 | // Skip over unnamed or bitfields |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2905 | if (!Field->getIdentifier() || Field->isBitField()) { |
| 2906 | LastFieldBitfield = Field; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2907 | continue; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2908 | } |
| 2909 | LastFieldBitfield = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2910 | QualType FQT = Field->getType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2911 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2912 | if (FQT->isUnionType()) |
| 2913 | HasUnion = true; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2914 | else |
| 2915 | assert(FQT->isRecordType() && |
| 2916 | "only union/record is supported for ivar layout bitmap"); |
| 2917 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2918 | const RecordType *RT = FQT->getAsRecordType(); |
| 2919 | const RecordDecl *RD = RT->getDecl(); |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 2920 | // FIXME - Find a more efficient way of passing records down. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2921 | TmpRecFields.append(RD->field_begin(CGM.getContext()), |
| 2922 | RD->field_end(CGM.getContext())); |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 2923 | const llvm::Type *Ty = CGM.getTypes().ConvertType(FQT); |
| 2924 | const llvm::StructLayout *RecLayout = |
| 2925 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); |
| 2926 | |
| 2927 | BuildAggrIvarLayout(0, RecLayout, RD, TmpRecFields, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2928 | BytePos + GetFieldBaseOffset(OI, Layout, Field), |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2929 | ForStrongLayout, Index, SkIndex, |
| 2930 | HasUnion); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2931 | TmpRecFields.clear(); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2932 | continue; |
| 2933 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2934 | |
| 2935 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2936 | const ConstantArrayType *CArray = |
| 2937 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2938 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2939 | assert(CArray && "only array with know element size is supported"); |
| 2940 | FQT = CArray->getElementType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2941 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 2942 | const ConstantArrayType *CArray = |
| 2943 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2944 | ElCount *= CArray->getSize().getZExtValue(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2945 | FQT = CArray->getElementType(); |
| 2946 | } |
| 2947 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2948 | assert(!FQT->isUnionType() && |
| 2949 | "layout for array of unions not supported"); |
| 2950 | if (FQT->isRecordType()) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2951 | int OldIndex = Index; |
| 2952 | int OldSkIndex = SkIndex; |
| 2953 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2954 | // FIXME - Use a common routine with the above! |
| 2955 | const RecordType *RT = FQT->getAsRecordType(); |
| 2956 | const RecordDecl *RD = RT->getDecl(); |
| 2957 | // FIXME - Find a more efficiant way of passing records down. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2958 | TmpRecFields.append(RD->field_begin(CGM.getContext()), |
| 2959 | RD->field_end(CGM.getContext())); |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 2960 | const llvm::Type *Ty = CGM.getTypes().ConvertType(FQT); |
| 2961 | const llvm::StructLayout *RecLayout = |
| 2962 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2963 | |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 2964 | BuildAggrIvarLayout(0, RecLayout, RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2965 | TmpRecFields, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2966 | BytePos + GetFieldBaseOffset(OI, Layout, Field), |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2967 | ForStrongLayout, Index, SkIndex, |
| 2968 | HasUnion); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2969 | TmpRecFields.clear(); |
| 2970 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2971 | // Replicate layout information for each array element. Note that |
| 2972 | // one element is already done. |
| 2973 | uint64_t ElIx = 1; |
| 2974 | for (int FirstIndex = Index, FirstSkIndex = SkIndex; |
| 2975 | ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2976 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2977 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 2978 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2979 | GC_IVAR gcivar; |
| 2980 | gcivar.ivar_bytepos = IvarsInfo[i].ivar_bytepos + Size*ElIx; |
| 2981 | gcivar.ivar_size = IvarsInfo[i].ivar_size; |
| 2982 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2983 | } |
| 2984 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2985 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2986 | GC_IVAR skivar; |
| 2987 | skivar.ivar_bytepos = SkipIvars[i].ivar_bytepos + Size*ElIx; |
| 2988 | skivar.ivar_size = SkipIvars[i].ivar_size; |
| 2989 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2990 | } |
| 2991 | } |
| 2992 | continue; |
| 2993 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2994 | } |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2995 | // At this point, we are done with Record/Union and array there of. |
| 2996 | // For other arrays we are down to its element type. |
| 2997 | QualType::GCAttrTypes GCAttr = QualType::GCNone; |
| 2998 | do { |
| 2999 | if (FQT.isObjCGCStrong() || FQT.isObjCGCWeak()) { |
| 3000 | GCAttr = FQT.isObjCGCStrong() ? QualType::Strong : QualType::Weak; |
| 3001 | break; |
| 3002 | } |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3003 | else if (CGM.getContext().isObjCObjectPointerType(FQT)) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3004 | GCAttr = QualType::Strong; |
| 3005 | break; |
| 3006 | } |
| 3007 | else if (const PointerType *PT = FQT->getAsPointerType()) { |
| 3008 | FQT = PT->getPointeeType(); |
| 3009 | } |
| 3010 | else { |
| 3011 | break; |
| 3012 | } |
| 3013 | } while (true); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3014 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3015 | if ((ForStrongLayout && GCAttr == QualType::Strong) |
| 3016 | || (!ForStrongLayout && GCAttr == QualType::Weak)) { |
| 3017 | if (IsUnion) |
| 3018 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3019 | uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType()) |
| 3020 | / WordSizeInBits; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3021 | if (UnionIvarSize > MaxUnionIvarSize) |
| 3022 | { |
| 3023 | MaxUnionIvarSize = UnionIvarSize; |
| 3024 | MaxField = Field; |
| 3025 | } |
| 3026 | } |
| 3027 | else |
| 3028 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3029 | GC_IVAR gcivar; |
| 3030 | gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field); |
| 3031 | gcivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) / |
| 3032 | WordSizeInBits; |
| 3033 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3034 | } |
| 3035 | } |
| 3036 | else if ((ForStrongLayout && |
| 3037 | (GCAttr == QualType::GCNone || GCAttr == QualType::Weak)) |
| 3038 | || (!ForStrongLayout && GCAttr != QualType::Weak)) { |
| 3039 | if (IsUnion) |
| 3040 | { |
| 3041 | uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType()); |
| 3042 | if (UnionIvarSize > MaxSkippedUnionIvarSize) |
| 3043 | { |
| 3044 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 3045 | MaxSkippedField = Field; |
| 3046 | } |
| 3047 | } |
| 3048 | else |
| 3049 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3050 | GC_IVAR skivar; |
| 3051 | skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field); |
| 3052 | skivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) / |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3053 | ByteSizeInBits; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3054 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3055 | } |
| 3056 | } |
| 3057 | } |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3058 | if (LastFieldBitfield) { |
| 3059 | // Last field was a bitfield. Must update skip info. |
| 3060 | GC_IVAR skivar; |
| 3061 | skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, |
| 3062 | LastFieldBitfield); |
| 3063 | Expr *BitWidth = LastFieldBitfield->getBitWidth(); |
| 3064 | uint64_t BitFieldSize = |
| 3065 | BitWidth->getIntegerConstantExprValue(CGM.getContext()).getZExtValue(); |
| 3066 | skivar.ivar_size = (BitFieldSize / ByteSizeInBits) |
| 3067 | + ((BitFieldSize % ByteSizeInBits) != 0); |
| 3068 | SkipIvars.push_back(skivar); ++SkIndex; |
| 3069 | } |
| 3070 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3071 | if (MaxField) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3072 | GC_IVAR gcivar; |
| 3073 | gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, MaxField); |
| 3074 | gcivar.ivar_size = MaxUnionIvarSize; |
| 3075 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3076 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3077 | |
| 3078 | if (MaxSkippedField) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3079 | GC_IVAR skivar; |
| 3080 | skivar.ivar_bytepos = BytePos + |
| 3081 | GetFieldBaseOffset(OI, Layout, MaxSkippedField); |
| 3082 | skivar.ivar_size = MaxSkippedUnionIvarSize; |
| 3083 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3084 | } |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3085 | } |
| 3086 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3087 | static int |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3088 | IvarBytePosCompare(const void *a, const void *b) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3089 | { |
| 3090 | unsigned int sa = ((CGObjCCommonMac::GC_IVAR *)a)->ivar_bytepos; |
| 3091 | unsigned int sb = ((CGObjCCommonMac::GC_IVAR *)b)->ivar_bytepos; |
| 3092 | |
| 3093 | if (sa < sb) |
| 3094 | return -1; |
| 3095 | if (sa > sb) |
| 3096 | return 1; |
| 3097 | return 0; |
| 3098 | } |
| 3099 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3100 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 3101 | /// implementation for the __strong or __weak case. |
| 3102 | /// The layout map displays which words in ivar list must be skipped |
| 3103 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 3104 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
| 3105 | /// of words to skip and right nibble is count of words to scan. So, each |
| 3106 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
| 3107 | /// represented by a 0x00 byte which also ends the string. |
| 3108 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 3109 | /// - id, Class |
| 3110 | /// - object * |
| 3111 | /// - __strong anything |
| 3112 | /// |
| 3113 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 3114 | /// - __weak anything |
| 3115 | /// |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3116 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3117 | const ObjCImplementationDecl *OMD, |
| 3118 | bool ForStrongLayout) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3119 | int Index = -1; |
| 3120 | int SkIndex = -1; |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3121 | bool hasUnion = false; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3122 | int SkipScan; |
| 3123 | unsigned int WordsToScan, WordsToSkip; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3124 | const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 3125 | if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) |
| 3126 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3127 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3128 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3129 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3130 | CGM.getContext().CollectObjCIvars(OI, RecFields); |
| 3131 | if (RecFields.empty()) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3132 | return llvm::Constant::getNullValue(PtrTy); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3133 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3134 | SkipIvars.clear(); |
| 3135 | IvarsInfo.clear(); |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 3136 | |
| 3137 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OI); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3138 | BuildAggrIvarLayout(OI, Layout, 0, RecFields, 0, ForStrongLayout, |
| 3139 | Index, SkIndex, hasUnion); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3140 | if (Index == -1) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3141 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3142 | |
| 3143 | // Sort on byte position in case we encounterred a union nested in |
| 3144 | // the ivar list. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3145 | if (hasUnion && !IvarsInfo.empty()) |
| 3146 | qsort(&IvarsInfo[0], Index+1, sizeof(GC_IVAR), IvarBytePosCompare); |
| 3147 | if (hasUnion && !SkipIvars.empty()) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3148 | qsort(&SkipIvars[0], Index+1, sizeof(GC_IVAR), IvarBytePosCompare); |
| 3149 | |
| 3150 | // Build the string of skip/scan nibbles |
| 3151 | SkipScan = -1; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3152 | SkipScanIvars.clear(); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3153 | unsigned int WordSize = |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3154 | CGM.getTypes().getTargetData().getTypePaddedSize(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3155 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 3156 | WordsToSkip = 0; |
| 3157 | WordsToScan = IvarsInfo[0].ivar_size; |
| 3158 | } |
| 3159 | else { |
| 3160 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 3161 | WordsToScan = IvarsInfo[0].ivar_size; |
| 3162 | } |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3163 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3164 | { |
| 3165 | unsigned int TailPrevGCObjC = |
| 3166 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
| 3167 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) |
| 3168 | { |
| 3169 | // consecutive 'scanned' object pointers. |
| 3170 | WordsToScan += IvarsInfo[i].ivar_size; |
| 3171 | } |
| 3172 | else |
| 3173 | { |
| 3174 | // Skip over 'gc'able object pointer which lay over each other. |
| 3175 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 3176 | continue; |
| 3177 | // Must skip over 1 or more words. We save current skip/scan values |
| 3178 | // and start a new pair. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3179 | SKIP_SCAN SkScan; |
| 3180 | SkScan.skip = WordsToSkip; |
| 3181 | SkScan.scan = WordsToScan; |
| 3182 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
| 3183 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3184 | // Skip the hole. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3185 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 3186 | SkScan.scan = 0; |
| 3187 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3188 | WordsToSkip = 0; |
| 3189 | WordsToScan = IvarsInfo[i].ivar_size; |
| 3190 | } |
| 3191 | } |
| 3192 | if (WordsToScan > 0) |
| 3193 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3194 | SKIP_SCAN SkScan; |
| 3195 | SkScan.skip = WordsToSkip; |
| 3196 | SkScan.scan = WordsToScan; |
| 3197 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3198 | } |
| 3199 | |
| 3200 | bool BytesSkipped = false; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3201 | if (!SkipIvars.empty()) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3202 | { |
| 3203 | int LastByteSkipped = |
| 3204 | SkipIvars[SkIndex].ivar_bytepos + SkipIvars[SkIndex].ivar_size; |
| 3205 | int LastByteScanned = |
| 3206 | IvarsInfo[Index].ivar_bytepos + IvarsInfo[Index].ivar_size * WordSize; |
| 3207 | BytesSkipped = (LastByteSkipped > LastByteScanned); |
| 3208 | // Compute number of bytes to skip at the tail end of the last ivar scanned. |
| 3209 | if (BytesSkipped) |
| 3210 | { |
| 3211 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3212 | SKIP_SCAN SkScan; |
| 3213 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
| 3214 | SkScan.scan = 0; |
| 3215 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3216 | } |
| 3217 | } |
| 3218 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 3219 | // as 0xMN. |
| 3220 | for (int i = 0; i <= SkipScan; i++) |
| 3221 | { |
| 3222 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 3223 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 3224 | // 0xM0 followed by 0x0N detected. |
| 3225 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 3226 | for (int j = i+1; j < SkipScan; j++) |
| 3227 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 3228 | --SkipScan; |
| 3229 | } |
| 3230 | } |
| 3231 | |
| 3232 | // Generate the string. |
| 3233 | std::string BitMap; |
| 3234 | for (int i = 0; i <= SkipScan; i++) |
| 3235 | { |
| 3236 | unsigned char byte; |
| 3237 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 3238 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 3239 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 3240 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
| 3241 | |
| 3242 | if (skip_small > 0 || skip_big > 0) |
| 3243 | BytesSkipped = true; |
| 3244 | // first skip big. |
| 3245 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 3246 | BitMap += (unsigned char)(0xf0); |
| 3247 | |
| 3248 | // next (skip small, scan) |
| 3249 | if (skip_small) |
| 3250 | { |
| 3251 | byte = skip_small << 4; |
| 3252 | if (scan_big > 0) |
| 3253 | { |
| 3254 | byte |= 0xf; |
| 3255 | --scan_big; |
| 3256 | } |
| 3257 | else if (scan_small) |
| 3258 | { |
| 3259 | byte |= scan_small; |
| 3260 | scan_small = 0; |
| 3261 | } |
| 3262 | BitMap += byte; |
| 3263 | } |
| 3264 | // next scan big |
| 3265 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 3266 | BitMap += (unsigned char)(0x0f); |
| 3267 | // last scan small |
| 3268 | if (scan_small) |
| 3269 | { |
| 3270 | byte = scan_small; |
| 3271 | BitMap += byte; |
| 3272 | } |
| 3273 | } |
| 3274 | // null terminate string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3275 | unsigned char zero = 0; |
| 3276 | BitMap += zero; |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3277 | |
| 3278 | if (CGM.getLangOptions().ObjCGCBitmapPrint) { |
| 3279 | printf("\n%s ivar layout for class '%s': ", |
| 3280 | ForStrongLayout ? "strong" : "weak", |
| 3281 | OMD->getClassInterface()->getNameAsCString()); |
| 3282 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
| 3283 | for (unsigned i = 0; i < BitMap.size(); i++) |
| 3284 | if (!(s[i] & 0xf0)) |
| 3285 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3286 | else |
| 3287 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3288 | printf("\n"); |
| 3289 | } |
| 3290 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3291 | // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as |
| 3292 | // final layout. |
| 3293 | if (ForStrongLayout && !BytesSkipped) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3294 | return llvm::Constant::getNullValue(PtrTy); |
| 3295 | llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 3296 | llvm::ConstantArray::get(BitMap.c_str()), |
| 3297 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3298 | 1, true); |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3299 | return getConstantGEP(Entry, 0, 0); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3300 | } |
| 3301 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3302 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3303 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 3304 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3305 | // FIXME: Avoid std::string copying. |
| 3306 | if (!Entry) |
| 3307 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
| 3308 | llvm::ConstantArray::get(Sel.getAsString()), |
| 3309 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3310 | 1, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3311 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3312 | return getConstantGEP(Entry, 0, 0); |
| 3313 | } |
| 3314 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3315 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3316 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3317 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 3318 | } |
| 3319 | |
| 3320 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3321 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3322 | return GetMethodVarName(&CGM.getContext().Idents.get(Name)); |
| 3323 | } |
| 3324 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 3325 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3326 | std::string TypeStr; |
| 3327 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 3328 | |
| 3329 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3330 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3331 | if (!Entry) |
| 3332 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3333 | llvm::ConstantArray::get(TypeStr), |
| 3334 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3335 | 1, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3336 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3337 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3338 | } |
| 3339 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3340 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3341 | std::string TypeStr; |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3342 | CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D), |
| 3343 | TypeStr); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3344 | |
| 3345 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 3346 | |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3347 | if (!Entry) |
| 3348 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3349 | llvm::ConstantArray::get(TypeStr), |
| 3350 | "__TEXT,__cstring,cstring_literals", |
| 3351 | 1, true); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3352 | |
| 3353 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3354 | } |
| 3355 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3356 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3357 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3358 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
| 3359 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3360 | if (!Entry) |
| 3361 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
| 3362 | llvm::ConstantArray::get(Ident->getName()), |
| 3363 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3364 | 1, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3365 | |
| 3366 | return getConstantGEP(Entry, 0, 0); |
| 3367 | } |
| 3368 | |
| 3369 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3370 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3371 | llvm::Constant * |
| 3372 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 3373 | const Decl *Container) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3374 | std::string TypeStr; |
| 3375 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3376 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 3377 | } |
| 3378 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3379 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
| 3380 | const ObjCContainerDecl *CD, |
| 3381 | std::string &NameOut) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 3382 | NameOut = '\01'; |
| 3383 | NameOut += (D->isInstanceMethod() ? '-' : '+'); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3384 | NameOut += '['; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 3385 | assert (CD && "Missing container decl in GetNameForMethod"); |
| 3386 | NameOut += CD->getNameAsString(); |
Fariborz Jahanian | 1e9aef3 | 2009-04-16 18:34:20 +0000 | [diff] [blame] | 3387 | if (const ObjCCategoryImplDecl *CID = |
| 3388 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) { |
| 3389 | NameOut += '('; |
| 3390 | NameOut += CID->getNameAsString(); |
| 3391 | NameOut+= ')'; |
| 3392 | } |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3393 | NameOut += ' '; |
| 3394 | NameOut += D->getSelector().getAsString(); |
| 3395 | NameOut += ']'; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3396 | } |
| 3397 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3398 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3399 | EmitModuleInfo(); |
| 3400 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3401 | // Emit the dummy bodies for any protocols which were referenced but |
| 3402 | // never defined. |
| 3403 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
| 3404 | i = Protocols.begin(), e = Protocols.end(); i != e; ++i) { |
| 3405 | if (i->second->hasInitializer()) |
| 3406 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3407 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3408 | std::vector<llvm::Constant*> Values(5); |
| 3409 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 3410 | Values[1] = GetClassName(i->first); |
| 3411 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 3412 | Values[3] = Values[4] = |
| 3413 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 3414 | i->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 3415 | i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 3416 | Values)); |
| 3417 | } |
| 3418 | |
| 3419 | std::vector<llvm::Constant*> Used; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3420 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3421 | e = UsedGlobals.end(); i != e; ++i) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3422 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3423 | } |
| 3424 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3425 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3426 | llvm::GlobalValue *GV = |
| 3427 | new llvm::GlobalVariable(AT, false, |
| 3428 | llvm::GlobalValue::AppendingLinkage, |
| 3429 | llvm::ConstantArray::get(AT, Used), |
| 3430 | "llvm.used", |
| 3431 | &CGM.getModule()); |
| 3432 | |
| 3433 | GV->setSection("llvm.metadata"); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3434 | |
| 3435 | // Add assembler directives to add lazy undefined symbol references |
| 3436 | // for classes which are referenced but not defined. This is |
| 3437 | // important for correct linker interaction. |
| 3438 | |
| 3439 | // FIXME: Uh, this isn't particularly portable. |
| 3440 | std::stringstream s; |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3441 | |
| 3442 | if (!CGM.getModule().getModuleInlineAsm().empty()) |
| 3443 | s << "\n"; |
| 3444 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3445 | for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(), |
| 3446 | e = LazySymbols.end(); i != e; ++i) { |
| 3447 | s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n"; |
| 3448 | } |
| 3449 | for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(), |
| 3450 | e = DefinedSymbols.end(); i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3451 | s << "\t.objc_class_name_" << (*i)->getName() << "=0\n" |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3452 | << "\t.globl .objc_class_name_" << (*i)->getName() << "\n"; |
| 3453 | } |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3454 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3455 | CGM.getModule().appendModuleInlineAsm(s.str()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3456 | } |
| 3457 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3458 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3459 | : CGObjCCommonMac(cgm), |
| 3460 | ObjCTypes(cgm) |
| 3461 | { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3462 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3463 | ObjCABI = 2; |
| 3464 | } |
| 3465 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3466 | /* *** */ |
| 3467 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3468 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3469 | : CGM(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3470 | { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3471 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3472 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3473 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3474 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3475 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3476 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3477 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3478 | Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 3479 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3480 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 3481 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3482 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3483 | |
| 3484 | // FIXME: It would be nice to unify this with the opaque type, so |
| 3485 | // that the IR comes out a bit cleaner. |
| 3486 | const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 3487 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3488 | |
| 3489 | // I'm not sure I like this. The implicit coordination is a bit |
| 3490 | // gross. We should solve this in a reasonable fashion because this |
| 3491 | // is a pretty common task (match some runtime data structure with |
| 3492 | // an LLVM data structure). |
| 3493 | |
| 3494 | // FIXME: This is leaked. |
| 3495 | // FIXME: Merge with rewriter code? |
| 3496 | |
| 3497 | // struct _objc_super { |
| 3498 | // id self; |
| 3499 | // Class cls; |
| 3500 | // } |
| 3501 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3502 | SourceLocation(), |
| 3503 | &Ctx.Idents.get("_objc_super")); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3504 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3505 | Ctx.getObjCIdType(), 0, false)); |
| 3506 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3507 | Ctx.getObjCClassType(), 0, false)); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3508 | RD->completeDefinition(Ctx); |
| 3509 | |
| 3510 | SuperCTy = Ctx.getTagDeclType(RD); |
| 3511 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
| 3512 | |
| 3513 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3514 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 3515 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3516 | // struct _prop_t { |
| 3517 | // char *name; |
| 3518 | // char *attributes; |
| 3519 | // } |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 3520 | PropertyTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, NULL); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3521 | CGM.getModule().addTypeName("struct._prop_t", |
| 3522 | PropertyTy); |
| 3523 | |
| 3524 | // struct _prop_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3525 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3526 | // uint32_t count_of_properties; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3527 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3528 | // } |
| 3529 | PropertyListTy = llvm::StructType::get(IntTy, |
| 3530 | IntTy, |
| 3531 | llvm::ArrayType::get(PropertyTy, 0), |
| 3532 | NULL); |
| 3533 | CGM.getModule().addTypeName("struct._prop_list_t", |
| 3534 | PropertyListTy); |
| 3535 | // struct _prop_list_t * |
| 3536 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
| 3537 | |
| 3538 | // struct _objc_method { |
| 3539 | // SEL _cmd; |
| 3540 | // char *method_type; |
| 3541 | // char *_imp; |
| 3542 | // } |
| 3543 | MethodTy = llvm::StructType::get(SelectorPtrTy, |
| 3544 | Int8PtrTy, |
| 3545 | Int8PtrTy, |
| 3546 | NULL); |
| 3547 | CGM.getModule().addTypeName("struct._objc_method", MethodTy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3548 | |
| 3549 | // struct _objc_cache * |
| 3550 | CacheTy = llvm::OpaqueType::get(); |
| 3551 | CGM.getModule().addTypeName("struct._objc_cache", CacheTy); |
| 3552 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3553 | } |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3554 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3555 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3556 | : ObjCCommonTypesHelper(cgm) |
| 3557 | { |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3558 | // struct _objc_method_description { |
| 3559 | // SEL name; |
| 3560 | // char *types; |
| 3561 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3562 | MethodDescriptionTy = |
| 3563 | llvm::StructType::get(SelectorPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3564 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3565 | NULL); |
| 3566 | CGM.getModule().addTypeName("struct._objc_method_description", |
| 3567 | MethodDescriptionTy); |
| 3568 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3569 | // struct _objc_method_description_list { |
| 3570 | // int count; |
| 3571 | // struct _objc_method_description[1]; |
| 3572 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3573 | MethodDescriptionListTy = |
| 3574 | llvm::StructType::get(IntTy, |
| 3575 | llvm::ArrayType::get(MethodDescriptionTy, 0), |
| 3576 | NULL); |
| 3577 | CGM.getModule().addTypeName("struct._objc_method_description_list", |
| 3578 | MethodDescriptionListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3579 | |
| 3580 | // struct _objc_method_description_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3581 | MethodDescriptionListPtrTy = |
| 3582 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
| 3583 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3584 | // Protocol description structures |
| 3585 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3586 | // struct _objc_protocol_extension { |
| 3587 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 3588 | // struct _objc_method_description_list *optional_instance_methods; |
| 3589 | // struct _objc_method_description_list *optional_class_methods; |
| 3590 | // struct _objc_property_list *instance_properties; |
| 3591 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3592 | ProtocolExtensionTy = |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3593 | llvm::StructType::get(IntTy, |
| 3594 | MethodDescriptionListPtrTy, |
| 3595 | MethodDescriptionListPtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3596 | PropertyListPtrTy, |
| 3597 | NULL); |
| 3598 | CGM.getModule().addTypeName("struct._objc_protocol_extension", |
| 3599 | ProtocolExtensionTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3600 | |
| 3601 | // struct _objc_protocol_extension * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3602 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
| 3603 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3604 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3605 | |
| 3606 | llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(); |
| 3607 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3608 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3609 | const llvm::Type *T = |
| 3610 | llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3611 | LongTy, |
| 3612 | llvm::ArrayType::get(ProtocolTyHolder, 0), |
| 3613 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3614 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T); |
| 3615 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3616 | // struct _objc_protocol { |
| 3617 | // struct _objc_protocol_extension *isa; |
| 3618 | // char *protocol_name; |
| 3619 | // struct _objc_protocol **_objc_protocol_list; |
| 3620 | // struct _objc_method_description_list *instance_methods; |
| 3621 | // struct _objc_method_description_list *class_methods; |
| 3622 | // } |
| 3623 | T = llvm::StructType::get(ProtocolExtensionPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3624 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3625 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3626 | MethodDescriptionListPtrTy, |
| 3627 | MethodDescriptionListPtrTy, |
| 3628 | NULL); |
| 3629 | cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T); |
| 3630 | |
| 3631 | ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get()); |
| 3632 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3633 | ProtocolListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3634 | // struct _objc_protocol_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3635 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
| 3636 | |
| 3637 | ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get()); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3638 | CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3639 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3640 | |
| 3641 | // Class description structures |
| 3642 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3643 | // struct _objc_ivar { |
| 3644 | // char *ivar_name; |
| 3645 | // char *ivar_type; |
| 3646 | // int ivar_offset; |
| 3647 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3648 | IvarTy = llvm::StructType::get(Int8PtrTy, |
| 3649 | Int8PtrTy, |
| 3650 | IntTy, |
| 3651 | NULL); |
| 3652 | CGM.getModule().addTypeName("struct._objc_ivar", IvarTy); |
| 3653 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3654 | // struct _objc_ivar_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3655 | IvarListTy = llvm::OpaqueType::get(); |
| 3656 | CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy); |
| 3657 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
| 3658 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3659 | // struct _objc_method_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3660 | MethodListTy = llvm::OpaqueType::get(); |
| 3661 | CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy); |
| 3662 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
| 3663 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3664 | // struct _objc_class_extension * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3665 | ClassExtensionTy = |
| 3666 | llvm::StructType::get(IntTy, |
| 3667 | Int8PtrTy, |
| 3668 | PropertyListPtrTy, |
| 3669 | NULL); |
| 3670 | CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy); |
| 3671 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
| 3672 | |
| 3673 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3674 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3675 | // struct _objc_class { |
| 3676 | // Class isa; |
| 3677 | // Class super_class; |
| 3678 | // char *name; |
| 3679 | // long version; |
| 3680 | // long info; |
| 3681 | // long instance_size; |
| 3682 | // struct _objc_ivar_list *ivars; |
| 3683 | // struct _objc_method_list *methods; |
| 3684 | // struct _objc_cache *cache; |
| 3685 | // struct _objc_protocol_list *protocols; |
| 3686 | // char *ivar_layout; |
| 3687 | // struct _objc_class_ext *ext; |
| 3688 | // }; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3689 | T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3690 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3691 | Int8PtrTy, |
| 3692 | LongTy, |
| 3693 | LongTy, |
| 3694 | LongTy, |
| 3695 | IvarListPtrTy, |
| 3696 | MethodListPtrTy, |
| 3697 | CachePtrTy, |
| 3698 | ProtocolListPtrTy, |
| 3699 | Int8PtrTy, |
| 3700 | ClassExtensionPtrTy, |
| 3701 | NULL); |
| 3702 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T); |
| 3703 | |
| 3704 | ClassTy = cast<llvm::StructType>(ClassTyHolder.get()); |
| 3705 | CGM.getModule().addTypeName("struct._objc_class", ClassTy); |
| 3706 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
| 3707 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3708 | // struct _objc_category { |
| 3709 | // char *category_name; |
| 3710 | // char *class_name; |
| 3711 | // struct _objc_method_list *instance_method; |
| 3712 | // struct _objc_method_list *class_method; |
| 3713 | // uint32_t size; // sizeof(struct _objc_category) |
| 3714 | // struct _objc_property_list *instance_properties;// category's @property |
| 3715 | // } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3716 | CategoryTy = llvm::StructType::get(Int8PtrTy, |
| 3717 | Int8PtrTy, |
| 3718 | MethodListPtrTy, |
| 3719 | MethodListPtrTy, |
| 3720 | ProtocolListPtrTy, |
| 3721 | IntTy, |
| 3722 | PropertyListPtrTy, |
| 3723 | NULL); |
| 3724 | CGM.getModule().addTypeName("struct._objc_category", CategoryTy); |
| 3725 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3726 | // Global metadata structures |
| 3727 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3728 | // struct _objc_symtab { |
| 3729 | // long sel_ref_cnt; |
| 3730 | // SEL *refs; |
| 3731 | // short cls_def_cnt; |
| 3732 | // short cat_def_cnt; |
| 3733 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 3734 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3735 | SymtabTy = llvm::StructType::get(LongTy, |
| 3736 | SelectorPtrTy, |
| 3737 | ShortTy, |
| 3738 | ShortTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3739 | llvm::ArrayType::get(Int8PtrTy, 0), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3740 | NULL); |
| 3741 | CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy); |
| 3742 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
| 3743 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3744 | // struct _objc_module { |
| 3745 | // long version; |
| 3746 | // long size; // sizeof(struct _objc_module) |
| 3747 | // char *name; |
| 3748 | // struct _objc_symtab* symtab; |
| 3749 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3750 | ModuleTy = |
| 3751 | llvm::StructType::get(LongTy, |
| 3752 | LongTy, |
| 3753 | Int8PtrTy, |
| 3754 | SymtabPtrTy, |
| 3755 | NULL); |
| 3756 | CGM.getModule().addTypeName("struct._objc_module", ModuleTy); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3757 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 3758 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3759 | // FIXME: This is the size of the setjmp buffer and should be |
| 3760 | // target specific. 18 is what's used on 32-bit X86. |
| 3761 | uint64_t SetJmpBufferSize = 18; |
| 3762 | |
| 3763 | // Exceptions |
| 3764 | const llvm::Type *StackPtrTy = |
Daniel Dunbar | 1000491 | 2008-09-27 06:32:25 +0000 | [diff] [blame] | 3765 | llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3766 | |
| 3767 | ExceptionDataTy = |
| 3768 | llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty, |
| 3769 | SetJmpBufferSize), |
| 3770 | StackPtrTy, NULL); |
| 3771 | CGM.getModule().addTypeName("struct._objc_exception_data", |
| 3772 | ExceptionDataTy); |
| 3773 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3774 | } |
| 3775 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3776 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3777 | : ObjCCommonTypesHelper(cgm) |
| 3778 | { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3779 | // struct _method_list_t { |
| 3780 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 3781 | // uint32_t method_count; |
| 3782 | // struct _objc_method method_list[method_count]; |
| 3783 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3784 | MethodListnfABITy = llvm::StructType::get(IntTy, |
| 3785 | IntTy, |
| 3786 | llvm::ArrayType::get(MethodTy, 0), |
| 3787 | NULL); |
| 3788 | CGM.getModule().addTypeName("struct.__method_list_t", |
| 3789 | MethodListnfABITy); |
| 3790 | // struct method_list_t * |
| 3791 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3792 | |
| 3793 | // struct _protocol_t { |
| 3794 | // id isa; // NULL |
| 3795 | // const char * const protocol_name; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3796 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3797 | // const struct method_list_t * const instance_methods; |
| 3798 | // const struct method_list_t * const class_methods; |
| 3799 | // const struct method_list_t *optionalInstanceMethods; |
| 3800 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3801 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3802 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 3803 | // const uint32_t flags; // = 0 |
| 3804 | // } |
| 3805 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3806 | // Holder for struct _protocol_list_t * |
| 3807 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3808 | |
| 3809 | ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy, |
| 3810 | Int8PtrTy, |
| 3811 | llvm::PointerType::getUnqual( |
| 3812 | ProtocolListTyHolder), |
| 3813 | MethodListnfABIPtrTy, |
| 3814 | MethodListnfABIPtrTy, |
| 3815 | MethodListnfABIPtrTy, |
| 3816 | MethodListnfABIPtrTy, |
| 3817 | PropertyListPtrTy, |
| 3818 | IntTy, |
| 3819 | IntTy, |
| 3820 | NULL); |
| 3821 | CGM.getModule().addTypeName("struct._protocol_t", |
| 3822 | ProtocolnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3823 | |
| 3824 | // struct _protocol_t* |
| 3825 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3826 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3827 | // struct _protocol_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3828 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3829 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3830 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3831 | ProtocolListnfABITy = llvm::StructType::get(LongTy, |
| 3832 | llvm::ArrayType::get( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3833 | ProtocolnfABIPtrTy, 0), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3834 | NULL); |
| 3835 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3836 | ProtocolListnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3837 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo( |
| 3838 | ProtocolListnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3839 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3840 | // struct _objc_protocol_list* |
| 3841 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3842 | |
| 3843 | // struct _ivar_t { |
| 3844 | // unsigned long int *offset; // pointer to ivar offset location |
| 3845 | // char *name; |
| 3846 | // char *type; |
| 3847 | // uint32_t alignment; |
| 3848 | // uint32_t size; |
| 3849 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3850 | IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy), |
| 3851 | Int8PtrTy, |
| 3852 | Int8PtrTy, |
| 3853 | IntTy, |
| 3854 | IntTy, |
| 3855 | NULL); |
| 3856 | CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy); |
| 3857 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3858 | // struct _ivar_list_t { |
| 3859 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 3860 | // uint32 count; |
| 3861 | // struct _iver_t list[count]; |
| 3862 | // } |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3863 | IvarListnfABITy = llvm::StructType::get(IntTy, |
| 3864 | IntTy, |
| 3865 | llvm::ArrayType::get( |
| 3866 | IvarnfABITy, 0), |
| 3867 | NULL); |
| 3868 | CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy); |
| 3869 | |
| 3870 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3871 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3872 | // struct _class_ro_t { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3873 | // uint32_t const flags; |
| 3874 | // uint32_t const instanceStart; |
| 3875 | // uint32_t const instanceSize; |
| 3876 | // uint32_t const reserved; // only when building for 64bit targets |
| 3877 | // const uint8_t * const ivarLayout; |
| 3878 | // const char *const name; |
| 3879 | // const struct _method_list_t * const baseMethods; |
| 3880 | // const struct _objc_protocol_list *const baseProtocols; |
| 3881 | // const struct _ivar_list_t *const ivars; |
| 3882 | // const uint8_t * const weakIvarLayout; |
| 3883 | // const struct _prop_list_t * const properties; |
| 3884 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3885 | |
| 3886 | // FIXME. Add 'reserved' field in 64bit abi mode! |
| 3887 | ClassRonfABITy = llvm::StructType::get(IntTy, |
| 3888 | IntTy, |
| 3889 | IntTy, |
| 3890 | Int8PtrTy, |
| 3891 | Int8PtrTy, |
| 3892 | MethodListnfABIPtrTy, |
| 3893 | ProtocolListnfABIPtrTy, |
| 3894 | IvarListnfABIPtrTy, |
| 3895 | Int8PtrTy, |
| 3896 | PropertyListPtrTy, |
| 3897 | NULL); |
| 3898 | CGM.getModule().addTypeName("struct._class_ro_t", |
| 3899 | ClassRonfABITy); |
| 3900 | |
| 3901 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 3902 | std::vector<const llvm::Type*> Params; |
| 3903 | Params.push_back(ObjectPtrTy); |
| 3904 | Params.push_back(SelectorPtrTy); |
| 3905 | ImpnfABITy = llvm::PointerType::getUnqual( |
| 3906 | llvm::FunctionType::get(ObjectPtrTy, Params, false)); |
| 3907 | |
| 3908 | // struct _class_t { |
| 3909 | // struct _class_t *isa; |
| 3910 | // struct _class_t * const superclass; |
| 3911 | // void *cache; |
| 3912 | // IMP *vtable; |
| 3913 | // struct class_ro_t *ro; |
| 3914 | // } |
| 3915 | |
| 3916 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3917 | ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3918 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3919 | CachePtrTy, |
| 3920 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 3921 | llvm::PointerType::getUnqual( |
| 3922 | ClassRonfABITy), |
| 3923 | NULL); |
| 3924 | CGM.getModule().addTypeName("struct._class_t", ClassnfABITy); |
| 3925 | |
| 3926 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo( |
| 3927 | ClassnfABITy); |
| 3928 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3929 | // LLVM for struct _class_t * |
| 3930 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
| 3931 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3932 | // struct _category_t { |
| 3933 | // const char * const name; |
| 3934 | // struct _class_t *const cls; |
| 3935 | // const struct _method_list_t * const instance_methods; |
| 3936 | // const struct _method_list_t * const class_methods; |
| 3937 | // const struct _protocol_list_t * const protocols; |
| 3938 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3939 | // } |
| 3940 | CategorynfABITy = llvm::StructType::get(Int8PtrTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3941 | ClassnfABIPtrTy, |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3942 | MethodListnfABIPtrTy, |
| 3943 | MethodListnfABIPtrTy, |
| 3944 | ProtocolListnfABIPtrTy, |
| 3945 | PropertyListPtrTy, |
| 3946 | NULL); |
| 3947 | CGM.getModule().addTypeName("struct._category_t", CategorynfABITy); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3948 | |
| 3949 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3950 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3951 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3952 | |
| 3953 | // MessageRefTy - LLVM for: |
| 3954 | // struct _message_ref_t { |
| 3955 | // IMP messenger; |
| 3956 | // SEL name; |
| 3957 | // }; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3958 | |
| 3959 | // First the clang type for struct _message_ref_t |
| 3960 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3961 | SourceLocation(), |
| 3962 | &Ctx.Idents.get("_message_ref_t")); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3963 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3964 | Ctx.VoidPtrTy, 0, false)); |
| 3965 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3966 | Ctx.getObjCSelType(), 0, false)); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3967 | RD->completeDefinition(Ctx); |
| 3968 | |
| 3969 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 3970 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 3971 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3972 | |
| 3973 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 3974 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
| 3975 | |
| 3976 | // SuperMessageRefTy - LLVM for: |
| 3977 | // struct _super_message_ref_t { |
| 3978 | // SUPER_IMP messenger; |
| 3979 | // SEL name; |
| 3980 | // }; |
| 3981 | SuperMessageRefTy = llvm::StructType::get(ImpnfABITy, |
| 3982 | SelectorPtrTy, |
| 3983 | NULL); |
| 3984 | CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy); |
| 3985 | |
| 3986 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 3987 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
| 3988 | |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 3989 | |
| 3990 | // struct objc_typeinfo { |
| 3991 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 3992 | // const char* name; // c++ typeinfo string |
| 3993 | // Class cls; |
| 3994 | // }; |
| 3995 | EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy), |
| 3996 | Int8PtrTy, |
| 3997 | ClassnfABIPtrTy, |
| 3998 | NULL); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 3999 | CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 4000 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4001 | } |
| 4002 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4003 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
| 4004 | FinishNonFragileABIModule(); |
| 4005 | |
| 4006 | return NULL; |
| 4007 | } |
| 4008 | |
| 4009 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 4010 | // nonfragile abi has no module definition. |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4011 | |
| 4012 | // Build list of all implemented classe addresses in array |
| 4013 | // L_OBJC_LABEL_CLASS_$. |
| 4014 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$ |
| 4015 | // list of 'nonlazy' implementations (defined as those with a +load{} |
| 4016 | // method!!). |
| 4017 | unsigned NumClasses = DefinedClasses.size(); |
| 4018 | if (NumClasses) { |
| 4019 | std::vector<llvm::Constant*> Symbols(NumClasses); |
| 4020 | for (unsigned i=0; i<NumClasses; i++) |
| 4021 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 4022 | ObjCTypes.Int8PtrTy); |
| 4023 | llvm::Constant* Init = |
| 4024 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 4025 | NumClasses), |
| 4026 | Symbols); |
| 4027 | |
| 4028 | llvm::GlobalVariable *GV = |
| 4029 | new llvm::GlobalVariable(Init->getType(), false, |
| 4030 | llvm::GlobalValue::InternalLinkage, |
| 4031 | Init, |
| 4032 | "\01L_OBJC_LABEL_CLASS_$", |
| 4033 | &CGM.getModule()); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 4034 | GV->setAlignment(8); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4035 | GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip"); |
| 4036 | UsedGlobals.push_back(GV); |
| 4037 | } |
| 4038 | |
| 4039 | // Build list of all implemented category addresses in array |
| 4040 | // L_OBJC_LABEL_CATEGORY_$. |
| 4041 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$ |
| 4042 | // list of 'nonlazy' category implementations (defined as those with a +load{} |
| 4043 | // method!!). |
| 4044 | unsigned NumCategory = DefinedCategories.size(); |
| 4045 | if (NumCategory) { |
| 4046 | std::vector<llvm::Constant*> Symbols(NumCategory); |
| 4047 | for (unsigned i=0; i<NumCategory; i++) |
| 4048 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 4049 | ObjCTypes.Int8PtrTy); |
| 4050 | llvm::Constant* Init = |
| 4051 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 4052 | NumCategory), |
| 4053 | Symbols); |
| 4054 | |
| 4055 | llvm::GlobalVariable *GV = |
| 4056 | new llvm::GlobalVariable(Init->getType(), false, |
| 4057 | llvm::GlobalValue::InternalLinkage, |
| 4058 | Init, |
| 4059 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 4060 | &CGM.getModule()); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 4061 | GV->setAlignment(8); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4062 | GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip"); |
| 4063 | UsedGlobals.push_back(GV); |
| 4064 | } |
| 4065 | |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4066 | // static int L_OBJC_IMAGE_INFO[2] = { 0, flags }; |
| 4067 | // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0 |
| 4068 | std::vector<llvm::Constant*> Values(2); |
| 4069 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0); |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4070 | unsigned int flags = 0; |
Fariborz Jahanian | 66a5c2c | 2009-02-24 23:34:44 +0000 | [diff] [blame] | 4071 | // FIXME: Fix and continue? |
| 4072 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 4073 | flags |= eImageInfo_GarbageCollected; |
| 4074 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 4075 | flags |= eImageInfo_GCOnly; |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4076 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4077 | llvm::Constant* Init = llvm::ConstantArray::get( |
| 4078 | llvm::ArrayType::get(ObjCTypes.IntTy, 2), |
| 4079 | Values); |
| 4080 | llvm::GlobalVariable *IMGV = |
| 4081 | new llvm::GlobalVariable(Init->getType(), false, |
| 4082 | llvm::GlobalValue::InternalLinkage, |
| 4083 | Init, |
| 4084 | "\01L_OBJC_IMAGE_INFO", |
| 4085 | &CGM.getModule()); |
| 4086 | IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); |
| 4087 | UsedGlobals.push_back(IMGV); |
| 4088 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4089 | std::vector<llvm::Constant*> Used; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4090 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4091 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
| 4092 | e = UsedGlobals.end(); i != e; ++i) { |
| 4093 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
| 4094 | } |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4095 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4096 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
| 4097 | llvm::GlobalValue *GV = |
| 4098 | new llvm::GlobalVariable(AT, false, |
| 4099 | llvm::GlobalValue::AppendingLinkage, |
| 4100 | llvm::ConstantArray::get(AT, Used), |
| 4101 | "llvm.used", |
| 4102 | &CGM.getModule()); |
| 4103 | |
| 4104 | GV->setSection("llvm.metadata"); |
| 4105 | |
| 4106 | } |
| 4107 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4108 | // Metadata flags |
| 4109 | enum MetaDataDlags { |
| 4110 | CLS = 0x0, |
| 4111 | CLS_META = 0x1, |
| 4112 | CLS_ROOT = 0x2, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4113 | OBJC2_CLS_HIDDEN = 0x10, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4114 | CLS_EXCEPTION = 0x20 |
| 4115 | }; |
| 4116 | /// BuildClassRoTInitializer - generate meta-data for: |
| 4117 | /// struct _class_ro_t { |
| 4118 | /// uint32_t const flags; |
| 4119 | /// uint32_t const instanceStart; |
| 4120 | /// uint32_t const instanceSize; |
| 4121 | /// uint32_t const reserved; // only when building for 64bit targets |
| 4122 | /// const uint8_t * const ivarLayout; |
| 4123 | /// const char *const name; |
| 4124 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4125 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4126 | /// const struct _ivar_list_t *const ivars; |
| 4127 | /// const uint8_t * const weakIvarLayout; |
| 4128 | /// const struct _prop_list_t * const properties; |
| 4129 | /// } |
| 4130 | /// |
| 4131 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
| 4132 | unsigned flags, |
| 4133 | unsigned InstanceStart, |
| 4134 | unsigned InstanceSize, |
| 4135 | const ObjCImplementationDecl *ID) { |
| 4136 | std::string ClassName = ID->getNameAsString(); |
| 4137 | std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets! |
| 4138 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 4139 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 4140 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
| 4141 | // FIXME. For 64bit targets add 0 here. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4142 | // FIXME. ivarLayout is currently null! |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 4143 | // Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4144 | // : BuildIvarLayout(ID, true); |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 4145 | Values[ 3] = GetIvarLayoutName(0, ObjCTypes); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4146 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4147 | // const struct _method_list_t * const baseMethods; |
| 4148 | std::vector<llvm::Constant*> Methods; |
| 4149 | std::string MethodListName("\01l_OBJC_$_"); |
| 4150 | if (flags & CLS_META) { |
| 4151 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
| 4152 | for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(), |
| 4153 | e = ID->classmeth_end(); i != e; ++i) { |
| 4154 | // Class methods should always be defined. |
| 4155 | Methods.push_back(GetMethodConstant(*i)); |
| 4156 | } |
| 4157 | } else { |
| 4158 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
| 4159 | for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(), |
| 4160 | e = ID->instmeth_end(); i != e; ++i) { |
| 4161 | // Instance methods should always be defined. |
| 4162 | Methods.push_back(GetMethodConstant(*i)); |
| 4163 | } |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4164 | for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(), |
| 4165 | e = ID->propimpl_end(); i != e; ++i) { |
| 4166 | ObjCPropertyImplDecl *PID = *i; |
| 4167 | |
| 4168 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 4169 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 4170 | |
| 4171 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 4172 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4173 | Methods.push_back(C); |
| 4174 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 4175 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4176 | Methods.push_back(C); |
| 4177 | } |
| 4178 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4179 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4180 | Values[ 5] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4181 | "__DATA, __objc_const", Methods); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4182 | |
| 4183 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4184 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
| 4185 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
| 4186 | + OID->getNameAsString(), |
| 4187 | OID->protocol_begin(), |
| 4188 | OID->protocol_end()); |
| 4189 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4190 | if (flags & CLS_META) |
| 4191 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4192 | else |
| 4193 | Values[ 7] = EmitIvarList(ID); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4194 | // FIXME. weakIvarLayout is currently null. |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 4195 | // Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4196 | // : BuildIvarLayout(ID, false); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4197 | Values[ 8] = GetIvarLayoutName(0, ObjCTypes); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4198 | if (flags & CLS_META) |
| 4199 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4200 | else |
| 4201 | Values[ 9] = |
| 4202 | EmitPropertyList( |
| 4203 | "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
| 4204 | ID, ID->getClassInterface(), ObjCTypes); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4205 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
| 4206 | Values); |
| 4207 | llvm::GlobalVariable *CLASS_RO_GV = |
| 4208 | new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false, |
| 4209 | llvm::GlobalValue::InternalLinkage, |
| 4210 | Init, |
| 4211 | (flags & CLS_META) ? |
| 4212 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 4213 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName, |
| 4214 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4215 | CLASS_RO_GV->setAlignment( |
| 4216 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4217 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4218 | return CLASS_RO_GV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4219 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4220 | } |
| 4221 | |
| 4222 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 4223 | /// for the given ClassName for: |
| 4224 | /// struct _class_t { |
| 4225 | /// struct _class_t *isa; |
| 4226 | /// struct _class_t * const superclass; |
| 4227 | /// void *cache; |
| 4228 | /// IMP *vtable; |
| 4229 | /// struct class_ro_t *ro; |
| 4230 | /// } |
| 4231 | /// |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4232 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
| 4233 | std::string &ClassName, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4234 | llvm::Constant *IsAGV, |
| 4235 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4236 | llvm::Constant *ClassRoGV, |
| 4237 | bool HiddenVisibility) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4238 | std::vector<llvm::Constant*> Values(5); |
| 4239 | Values[0] = IsAGV; |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4240 | Values[1] = SuperClassGV |
| 4241 | ? SuperClassGV |
| 4242 | : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4243 | Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar |
| 4244 | Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar |
| 4245 | Values[4] = ClassRoGV; // &CLASS_RO_GV |
| 4246 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
| 4247 | Values); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4248 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 4249 | GV->setInitializer(Init); |
Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 4250 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4251 | GV->setAlignment( |
| 4252 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4253 | if (HiddenVisibility) |
| 4254 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4255 | return GV; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4256 | } |
| 4257 | |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4258 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCInterfaceDecl *OID, |
| 4259 | uint32_t &InstanceStart, |
| 4260 | uint32_t &InstanceSize) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame^] | 4261 | // Find first and last (non-padding) ivars in this interface. |
| 4262 | |
| 4263 | // FIXME: Use iterator. |
| 4264 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
| 4265 | GetNamedIvarList(OID, OIvars); |
| 4266 | |
| 4267 | if (OIvars.empty()) { |
| 4268 | InstanceStart = InstanceSize = 0; |
| 4269 | return; |
Daniel Dunbar | d4ae6c0 | 2009-04-22 04:39:47 +0000 | [diff] [blame] | 4270 | } |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame^] | 4271 | |
| 4272 | const ObjCIvarDecl *First = OIvars.front(); |
| 4273 | const ObjCIvarDecl *Last = OIvars.back(); |
| 4274 | |
| 4275 | InstanceStart = ComputeIvarBaseOffset(CGM, OID, First); |
| 4276 | const llvm::Type *FieldTy = |
| 4277 | CGM.getTypes().ConvertTypeForMem(Last->getType()); |
| 4278 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
| 4279 | InstanceSize = ComputeIvarBaseOffset(CGM, OID, Last) + Size; |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4280 | } |
| 4281 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4282 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 4283 | std::string ClassName = ID->getNameAsString(); |
| 4284 | if (!ObjCEmptyCacheVar) { |
| 4285 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4286 | ObjCTypes.CacheTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4287 | false, |
| 4288 | llvm::GlobalValue::ExternalLinkage, |
| 4289 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4290 | "_objc_empty_cache", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4291 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4292 | |
| 4293 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4294 | ObjCTypes.ImpnfABITy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4295 | false, |
| 4296 | llvm::GlobalValue::ExternalLinkage, |
| 4297 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4298 | "_objc_empty_vtable", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4299 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4300 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4301 | assert(ID->getClassInterface() && |
| 4302 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 4303 | // FIXME: Is this correct (that meta class size is never computed)? |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4304 | uint32_t InstanceStart = |
| 4305 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy); |
| 4306 | uint32_t InstanceSize = InstanceStart; |
| 4307 | uint32_t flags = CLS_META; |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4308 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 4309 | std::string ObjCClassName(getClassSymbolPrefix()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4310 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4311 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4312 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4313 | bool classIsHidden = |
| 4314 | CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4315 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4316 | flags |= OBJC2_CLS_HIDDEN; |
| 4317 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4318 | // class is root |
| 4319 | flags |= CLS_ROOT; |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4320 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4321 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4322 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4323 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4324 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 4325 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 4326 | Root = Super; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4327 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4328 | // work on super class metadata symbol. |
| 4329 | std::string SuperClassName = |
| 4330 | ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4331 | SuperClassGV = GetClassGlobal(SuperClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4332 | } |
| 4333 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 4334 | InstanceStart, |
| 4335 | InstanceSize,ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4336 | std::string TClassName = ObjCMetaClassName + ClassName; |
| 4337 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4338 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 4339 | classIsHidden); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4340 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4341 | // Metadata for the class |
| 4342 | flags = CLS; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4343 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4344 | flags |= OBJC2_CLS_HIDDEN; |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4345 | |
| 4346 | if (hasObjCExceptionAttribute(ID->getClassInterface())) |
| 4347 | flags |= CLS_EXCEPTION; |
| 4348 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4349 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4350 | flags |= CLS_ROOT; |
| 4351 | SuperClassGV = 0; |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 4352 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4353 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4354 | std::string RootClassName = |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4355 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4356 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4357 | } |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4358 | GetClassSizeInfo(ID->getClassInterface(), InstanceStart, InstanceSize); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4359 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4360 | InstanceStart, |
| 4361 | InstanceSize, |
| 4362 | ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4363 | |
| 4364 | TClassName = ObjCClassName + ClassName; |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4365 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4366 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 4367 | classIsHidden); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4368 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4369 | |
| 4370 | // Force the definition of the EHType if necessary. |
| 4371 | if (flags & CLS_EXCEPTION) |
| 4372 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4373 | } |
| 4374 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4375 | /// GenerateProtocolRef - This routine is called to generate code for |
| 4376 | /// a protocol reference expression; as in: |
| 4377 | /// @code |
| 4378 | /// @protocol(Proto1); |
| 4379 | /// @endcode |
| 4380 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 4381 | /// which will hold address of the protocol meta-data. |
| 4382 | /// |
| 4383 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
| 4384 | const ObjCProtocolDecl *PD) { |
| 4385 | |
Fariborz Jahanian | 960cd06 | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 4386 | // This routine is called for @protocol only. So, we must build definition |
| 4387 | // of protocol's meta-data (not a reference to it!) |
| 4388 | // |
| 4389 | llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4390 | ObjCTypes.ExternalProtocolPtrTy); |
| 4391 | |
| 4392 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
| 4393 | ProtocolName += PD->getNameAsCString(); |
| 4394 | |
| 4395 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 4396 | if (PTGV) |
| 4397 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4398 | PTGV = new llvm::GlobalVariable( |
| 4399 | Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4400 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4401 | Init, |
| 4402 | ProtocolName, |
| 4403 | &CGM.getModule()); |
| 4404 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 4405 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4406 | UsedGlobals.push_back(PTGV); |
| 4407 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4408 | } |
| 4409 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4410 | /// GenerateCategory - Build metadata for a category implementation. |
| 4411 | /// struct _category_t { |
| 4412 | /// const char * const name; |
| 4413 | /// struct _class_t *const cls; |
| 4414 | /// const struct _method_list_t * const instance_methods; |
| 4415 | /// const struct _method_list_t * const class_methods; |
| 4416 | /// const struct _protocol_list_t * const protocols; |
| 4417 | /// const struct _prop_list_t * const properties; |
| 4418 | /// } |
| 4419 | /// |
| 4420 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) |
| 4421 | { |
| 4422 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4423 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
| 4424 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4425 | "_$_" + OCD->getNameAsString()); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4426 | std::string ExtClassName(getClassSymbolPrefix() + |
| 4427 | Interface->getNameAsString()); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4428 | |
| 4429 | std::vector<llvm::Constant*> Values(6); |
| 4430 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 4431 | // meta-class entry symbol |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4432 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4433 | Values[1] = ClassGV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4434 | std::vector<llvm::Constant*> Methods; |
| 4435 | std::string MethodListName(Prefix); |
| 4436 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
| 4437 | "_$_" + OCD->getNameAsString(); |
| 4438 | |
| 4439 | for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(), |
| 4440 | e = OCD->instmeth_end(); i != e; ++i) { |
| 4441 | // Instance methods should always be defined. |
| 4442 | Methods.push_back(GetMethodConstant(*i)); |
| 4443 | } |
| 4444 | |
| 4445 | Values[2] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4446 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4447 | Methods); |
| 4448 | |
| 4449 | MethodListName = Prefix; |
| 4450 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 4451 | OCD->getNameAsString(); |
| 4452 | Methods.clear(); |
| 4453 | for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(), |
| 4454 | e = OCD->classmeth_end(); i != e; ++i) { |
| 4455 | // Class methods should always be defined. |
| 4456 | Methods.push_back(GetMethodConstant(*i)); |
| 4457 | } |
| 4458 | |
| 4459 | Values[3] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4460 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4461 | Methods); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4462 | const ObjCCategoryDecl *Category = |
| 4463 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4464 | if (Category) { |
| 4465 | std::string ExtName(Interface->getNameAsString() + "_$_" + |
| 4466 | OCD->getNameAsString()); |
| 4467 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
| 4468 | + Interface->getNameAsString() + "_$_" |
| 4469 | + Category->getNameAsString(), |
| 4470 | Category->protocol_begin(), |
| 4471 | Category->protocol_end()); |
| 4472 | Values[5] = |
| 4473 | EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
| 4474 | OCD, Category, ObjCTypes); |
| 4475 | } |
| 4476 | else { |
| 4477 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4478 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4479 | } |
| 4480 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4481 | llvm::Constant *Init = |
| 4482 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
| 4483 | Values); |
| 4484 | llvm::GlobalVariable *GCATV |
| 4485 | = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy, |
| 4486 | false, |
| 4487 | llvm::GlobalValue::InternalLinkage, |
| 4488 | Init, |
| 4489 | ExtCatName, |
| 4490 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4491 | GCATV->setAlignment( |
| 4492 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4493 | GCATV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4494 | UsedGlobals.push_back(GCATV); |
| 4495 | DefinedCategories.push_back(GCATV); |
| 4496 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4497 | |
| 4498 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 4499 | /// given method if it has been defined. The result is null if the |
| 4500 | /// method has not been defined. The return value has type MethodPtrTy. |
| 4501 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
| 4502 | const ObjCMethodDecl *MD) { |
| 4503 | // FIXME: Use DenseMap::lookup |
| 4504 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 4505 | if (!Fn) |
| 4506 | return 0; |
| 4507 | |
| 4508 | std::vector<llvm::Constant*> Method(3); |
| 4509 | Method[0] = |
| 4510 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4511 | ObjCTypes.SelectorPtrTy); |
| 4512 | Method[1] = GetMethodVarType(MD); |
| 4513 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 4514 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 4515 | } |
| 4516 | |
| 4517 | /// EmitMethodList - Build meta-data for method declarations |
| 4518 | /// struct _method_list_t { |
| 4519 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 4520 | /// uint32_t method_count; |
| 4521 | /// struct _objc_method method_list[method_count]; |
| 4522 | /// } |
| 4523 | /// |
| 4524 | llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList( |
| 4525 | const std::string &Name, |
| 4526 | const char *Section, |
| 4527 | const ConstantVector &Methods) { |
| 4528 | // Return null for empty list. |
| 4529 | if (Methods.empty()) |
| 4530 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
| 4531 | |
| 4532 | std::vector<llvm::Constant*> Values(3); |
| 4533 | // sizeof(struct _objc_method) |
| 4534 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy); |
| 4535 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4536 | // method_count |
| 4537 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 4538 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 4539 | Methods.size()); |
| 4540 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 4541 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4542 | |
| 4543 | llvm::GlobalVariable *GV = |
| 4544 | new llvm::GlobalVariable(Init->getType(), false, |
| 4545 | llvm::GlobalValue::InternalLinkage, |
| 4546 | Init, |
| 4547 | Name, |
| 4548 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4549 | GV->setAlignment( |
| 4550 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4551 | GV->setSection(Section); |
| 4552 | UsedGlobals.push_back(GV); |
| 4553 | return llvm::ConstantExpr::getBitCast(GV, |
| 4554 | ObjCTypes.MethodListnfABIPtrTy); |
| 4555 | } |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4556 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4557 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 4558 | /// the given ivar. |
| 4559 | /// |
| 4560 | llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 4561 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4562 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 4563 | std::string Name = "OBJC_IVAR_$_" + |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4564 | getInterfaceDeclForIvar(ID, Ivar, CGM.getContext())->getNameAsString() + |
| 4565 | '.' + Ivar->getNameAsString(); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4566 | llvm::GlobalVariable *IvarOffsetGV = |
| 4567 | CGM.getModule().getGlobalVariable(Name); |
| 4568 | if (!IvarOffsetGV) |
| 4569 | IvarOffsetGV = |
| 4570 | new llvm::GlobalVariable(ObjCTypes.LongTy, |
| 4571 | false, |
| 4572 | llvm::GlobalValue::ExternalLinkage, |
| 4573 | 0, |
| 4574 | Name, |
| 4575 | &CGM.getModule()); |
| 4576 | return IvarOffsetGV; |
| 4577 | } |
| 4578 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4579 | llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar( |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4580 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4581 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4582 | unsigned long int Offset) { |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4583 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
| 4584 | IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy, |
| 4585 | Offset)); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4586 | IvarOffsetGV->setAlignment( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4587 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy)); |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4588 | |
| 4589 | // FIXME: This matches gcc, but shouldn't the visibility be set on |
| 4590 | // the use as well (i.e., in ObjCIvarOffsetVariable). |
| 4591 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 4592 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
| 4593 | CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden) |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4594 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4595 | else |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4596 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4597 | IvarOffsetGV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4598 | return IvarOffsetGV; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4599 | } |
| 4600 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4601 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 4602 | /// implementation. The return value has type |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4603 | /// IvarListnfABIPtrTy. |
| 4604 | /// struct _ivar_t { |
| 4605 | /// unsigned long int *offset; // pointer to ivar offset location |
| 4606 | /// char *name; |
| 4607 | /// char *type; |
| 4608 | /// uint32_t alignment; |
| 4609 | /// uint32_t size; |
| 4610 | /// } |
| 4611 | /// struct _ivar_list_t { |
| 4612 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 4613 | /// uint32 count; |
| 4614 | /// struct _iver_t list[count]; |
| 4615 | /// } |
| 4616 | /// |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4617 | |
| 4618 | void CGObjCCommonMac::GetNamedIvarList(const ObjCInterfaceDecl *OID, |
| 4619 | llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const { |
| 4620 | for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), |
| 4621 | E = OID->ivar_end(); I != E; ++I) { |
| 4622 | // Ignore unnamed bit-fields. |
| 4623 | if (!(*I)->getDeclName()) |
| 4624 | continue; |
| 4625 | |
| 4626 | Res.push_back(*I); |
| 4627 | } |
| 4628 | |
| 4629 | for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(CGM.getContext()), |
| 4630 | E = OID->prop_end(CGM.getContext()); I != E; ++I) |
| 4631 | if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl()) |
| 4632 | Res.push_back(IV); |
| 4633 | } |
| 4634 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4635 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
| 4636 | const ObjCImplementationDecl *ID) { |
| 4637 | |
| 4638 | std::vector<llvm::Constant*> Ivars, Ivar(5); |
| 4639 | |
| 4640 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4641 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
| 4642 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4643 | // FIXME. Consolidate this with similar code in GenerateClass. |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 4644 | |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4645 | // Collect declared and synthesized ivars in a small vector. |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4646 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4647 | GetNamedIvarList(OID, OIvars); |
Fariborz Jahanian | 99eee36 | 2009-04-01 19:37:34 +0000 | [diff] [blame] | 4648 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4649 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 4650 | ObjCIvarDecl *IVD = OIvars[i]; |
| 4651 | const FieldDecl *Field = OID->lookupFieldDeclForIvar(CGM.getContext(), IVD); |
Daniel Dunbar | 3eec8aa | 2009-04-20 05:53:40 +0000 | [diff] [blame] | 4652 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD, |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame^] | 4653 | ComputeIvarBaseOffset(CGM, OID, IVD)); |
Daniel Dunbar | 3eec8aa | 2009-04-20 05:53:40 +0000 | [diff] [blame] | 4654 | Ivar[1] = GetMethodVarName(Field->getIdentifier()); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4655 | Ivar[2] = GetMethodVarType(Field); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4656 | const llvm::Type *FieldTy = |
| 4657 | CGM.getTypes().ConvertTypeForMem(Field->getType()); |
| 4658 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
| 4659 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
| 4660 | Field->getType().getTypePtr()) >> 3; |
| 4661 | Align = llvm::Log2_32(Align); |
| 4662 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4663 | // NOTE. Size of a bitfield does not match gcc's, because of the |
| 4664 | // way bitfields are treated special in each. But I am told that |
| 4665 | // 'size' for bitfield ivars is ignored by the runtime so it does |
| 4666 | // not matter. If it matters, there is enough info to get the |
| 4667 | // bitfield right! |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4668 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4669 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
| 4670 | } |
| 4671 | // Return null for empty list. |
| 4672 | if (Ivars.empty()) |
| 4673 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4674 | std::vector<llvm::Constant*> Values(3); |
| 4675 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy); |
| 4676 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4677 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 4678 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
| 4679 | Ivars.size()); |
| 4680 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
| 4681 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4682 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 4683 | llvm::GlobalVariable *GV = |
| 4684 | new llvm::GlobalVariable(Init->getType(), false, |
| 4685 | llvm::GlobalValue::InternalLinkage, |
| 4686 | Init, |
| 4687 | Prefix + OID->getNameAsString(), |
| 4688 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4689 | GV->setAlignment( |
| 4690 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4691 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4692 | |
| 4693 | UsedGlobals.push_back(GV); |
| 4694 | return llvm::ConstantExpr::getBitCast(GV, |
| 4695 | ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4696 | } |
| 4697 | |
| 4698 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
| 4699 | const ObjCProtocolDecl *PD) { |
| 4700 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4701 | |
| 4702 | if (!Entry) { |
| 4703 | // We use the initializer as a marker of whether this is a forward |
| 4704 | // reference or not. At module finalization we add the empty |
| 4705 | // contents for protocols which were referenced but never defined. |
| 4706 | Entry = |
| 4707 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
| 4708 | llvm::GlobalValue::ExternalLinkage, |
| 4709 | 0, |
| 4710 | "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(), |
| 4711 | &CGM.getModule()); |
| 4712 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 4713 | UsedGlobals.push_back(Entry); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4714 | } |
| 4715 | |
| 4716 | return Entry; |
| 4717 | } |
| 4718 | |
| 4719 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 4720 | /// @code |
| 4721 | /// struct _protocol_t { |
| 4722 | /// id isa; // NULL |
| 4723 | /// const char * const protocol_name; |
| 4724 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 4725 | /// const struct method_list_t * const instance_methods; |
| 4726 | /// const struct method_list_t * const class_methods; |
| 4727 | /// const struct method_list_t *optionalInstanceMethods; |
| 4728 | /// const struct method_list_t *optionalClassMethods; |
| 4729 | /// const struct _prop_list_t * properties; |
| 4730 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 4731 | /// const uint32_t flags; // = 0 |
| 4732 | /// } |
| 4733 | /// @endcode |
| 4734 | /// |
| 4735 | |
| 4736 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
| 4737 | const ObjCProtocolDecl *PD) { |
| 4738 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4739 | |
| 4740 | // Early exit if a defining object has already been generated. |
| 4741 | if (Entry && Entry->hasInitializer()) |
| 4742 | return Entry; |
| 4743 | |
| 4744 | const char *ProtocolName = PD->getNameAsCString(); |
| 4745 | |
| 4746 | // Construct method lists. |
| 4747 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 4748 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4749 | for (ObjCProtocolDecl::instmeth_iterator |
| 4750 | i = PD->instmeth_begin(CGM.getContext()), |
| 4751 | e = PD->instmeth_end(CGM.getContext()); |
| 4752 | i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4753 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4754 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4755 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4756 | OptInstanceMethods.push_back(C); |
| 4757 | } else { |
| 4758 | InstanceMethods.push_back(C); |
| 4759 | } |
| 4760 | } |
| 4761 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4762 | for (ObjCProtocolDecl::classmeth_iterator |
| 4763 | i = PD->classmeth_begin(CGM.getContext()), |
| 4764 | e = PD->classmeth_end(CGM.getContext()); |
| 4765 | i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4766 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4767 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4768 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4769 | OptClassMethods.push_back(C); |
| 4770 | } else { |
| 4771 | ClassMethods.push_back(C); |
| 4772 | } |
| 4773 | } |
| 4774 | |
| 4775 | std::vector<llvm::Constant*> Values(10); |
| 4776 | // isa is NULL |
| 4777 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
| 4778 | Values[1] = GetClassName(PD->getIdentifier()); |
| 4779 | Values[2] = EmitProtocolList( |
| 4780 | "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(), |
| 4781 | PD->protocol_begin(), |
| 4782 | PD->protocol_end()); |
| 4783 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4784 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4785 | + PD->getNameAsString(), |
| 4786 | "__DATA, __objc_const", |
| 4787 | InstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4788 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4789 | + PD->getNameAsString(), |
| 4790 | "__DATA, __objc_const", |
| 4791 | ClassMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4792 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4793 | + PD->getNameAsString(), |
| 4794 | "__DATA, __objc_const", |
| 4795 | OptInstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4796 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4797 | + PD->getNameAsString(), |
| 4798 | "__DATA, __objc_const", |
| 4799 | OptClassMethods); |
| 4800 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(), |
| 4801 | 0, PD, ObjCTypes); |
| 4802 | uint32_t Size = |
| 4803 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy); |
| 4804 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4805 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
| 4806 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
| 4807 | Values); |
| 4808 | |
| 4809 | if (Entry) { |
| 4810 | // Already created, fix the linkage and update the initializer. |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4811 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4812 | Entry->setInitializer(Init); |
| 4813 | } else { |
| 4814 | Entry = |
| 4815 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4816 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4817 | Init, |
| 4818 | std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName, |
| 4819 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4820 | Entry->setAlignment( |
| 4821 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4822 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4823 | } |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4824 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4825 | |
| 4826 | // Use this protocol meta-data to build protocol list table in section |
| 4827 | // __DATA, __objc_protolist |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4828 | llvm::GlobalVariable *PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4829 | ObjCTypes.ProtocolnfABIPtrTy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4830 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4831 | Entry, |
| 4832 | std::string("\01l_OBJC_LABEL_PROTOCOL_$_") |
| 4833 | +ProtocolName, |
| 4834 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4835 | PTGV->setAlignment( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4836 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 4837 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4838 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4839 | UsedGlobals.push_back(PTGV); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4840 | return Entry; |
| 4841 | } |
| 4842 | |
| 4843 | /// EmitProtocolList - Generate protocol list meta-data: |
| 4844 | /// @code |
| 4845 | /// struct _protocol_list_t { |
| 4846 | /// long protocol_count; // Note, this is 32/64 bit |
| 4847 | /// struct _protocol_t[protocol_count]; |
| 4848 | /// } |
| 4849 | /// @endcode |
| 4850 | /// |
| 4851 | llvm::Constant * |
| 4852 | CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name, |
| 4853 | ObjCProtocolDecl::protocol_iterator begin, |
| 4854 | ObjCProtocolDecl::protocol_iterator end) { |
| 4855 | std::vector<llvm::Constant*> ProtocolRefs; |
| 4856 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4857 | // Just return null for empty protocol lists |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4858 | if (begin == end) |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4859 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4860 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4861 | // FIXME: We shouldn't need to do this lookup here, should we? |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4862 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
| 4863 | if (GV) |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4864 | return llvm::ConstantExpr::getBitCast(GV, |
| 4865 | ObjCTypes.ProtocolListnfABIPtrTy); |
| 4866 | |
| 4867 | for (; begin != end; ++begin) |
| 4868 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 4869 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4870 | // This list is null terminated. |
| 4871 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4872 | ObjCTypes.ProtocolnfABIPtrTy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4873 | |
| 4874 | std::vector<llvm::Constant*> Values(2); |
| 4875 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 4876 | Values[1] = |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4877 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4878 | ProtocolRefs.size()), |
| 4879 | ProtocolRefs); |
| 4880 | |
| 4881 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4882 | GV = new llvm::GlobalVariable(Init->getType(), false, |
| 4883 | llvm::GlobalValue::InternalLinkage, |
| 4884 | Init, |
| 4885 | Name, |
| 4886 | &CGM.getModule()); |
| 4887 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4888 | GV->setAlignment( |
| 4889 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4890 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4891 | return llvm::ConstantExpr::getBitCast(GV, |
| 4892 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4893 | } |
| 4894 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4895 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 4896 | /// struct _objc_method { |
| 4897 | /// SEL _cmd; |
| 4898 | /// char *method_type; |
| 4899 | /// char *_imp; |
| 4900 | /// } |
| 4901 | |
| 4902 | llvm::Constant * |
| 4903 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 4904 | std::vector<llvm::Constant*> Desc(3); |
| 4905 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4906 | ObjCTypes.SelectorPtrTy); |
| 4907 | Desc[1] = GetMethodVarType(MD); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4908 | // Protocol methods have no implementation. So, this entry is always NULL. |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4909 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 4910 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
| 4911 | } |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4912 | |
| 4913 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 4914 | /// This code gen. amounts to generating code for: |
| 4915 | /// @code |
| 4916 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 4917 | /// @encode |
| 4918 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4919 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4920 | CodeGen::CodeGenFunction &CGF, |
| 4921 | QualType ObjectTy, |
| 4922 | llvm::Value *BaseValue, |
| 4923 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4924 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 4925 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame^] | 4926 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 4927 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4928 | } |
| 4929 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4930 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
| 4931 | CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 4932 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4933 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 4934 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar), |
| 4935 | false, "ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4936 | } |
| 4937 | |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4938 | CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend( |
| 4939 | CodeGen::CodeGenFunction &CGF, |
| 4940 | QualType ResultType, |
| 4941 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4942 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4943 | QualType Arg0Ty, |
| 4944 | bool IsSuper, |
| 4945 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4946 | // FIXME. Even though IsSuper is passes. This function doese not |
| 4947 | // handle calls to 'super' receivers. |
| 4948 | CodeGenTypes &Types = CGM.getTypes(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4949 | llvm::Value *Arg0 = Receiver; |
| 4950 | if (!IsSuper) |
| 4951 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4952 | |
| 4953 | // Find the message function name. |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 4954 | // FIXME. This is too much work to get the ABI-specific result type |
| 4955 | // needed to find the message name. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4956 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, |
| 4957 | llvm::SmallVector<QualType, 16>()); |
| 4958 | llvm::Constant *Fn; |
| 4959 | std::string Name("\01l_"); |
| 4960 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4961 | #if 0 |
| 4962 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4963 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4964 | Fn = ObjCTypes.getMessageSendIdStretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4965 | // FIXME. Is there a better way of getting these names. |
| 4966 | // They are available in RuntimeFunctions vector pair. |
| 4967 | Name += "objc_msgSendId_stret_fixup"; |
| 4968 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4969 | else |
| 4970 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4971 | if (IsSuper) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4972 | Fn = ObjCTypes.getMessageSendSuper2StretFixupFn(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4973 | Name += "objc_msgSendSuper2_stret_fixup"; |
| 4974 | } |
| 4975 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4976 | { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4977 | Fn = ObjCTypes.getMessageSendStretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4978 | Name += "objc_msgSend_stret_fixup"; |
| 4979 | } |
| 4980 | } |
Fariborz Jahanian | 1a6b368 | 2009-02-05 19:35:43 +0000 | [diff] [blame] | 4981 | else if (ResultType->isFloatingType() && |
| 4982 | // Selection of frret API only happens in 32bit nonfragile ABI. |
| 4983 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4984 | Fn = ObjCTypes.getMessageSendFpretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4985 | Name += "objc_msgSend_fpret_fixup"; |
| 4986 | } |
| 4987 | else { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4988 | #if 0 |
| 4989 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4990 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4991 | Fn = ObjCTypes.getMessageSendIdFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4992 | Name += "objc_msgSendId_fixup"; |
| 4993 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4994 | else |
| 4995 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4996 | if (IsSuper) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4997 | Fn = ObjCTypes.getMessageSendSuper2FixupFn(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4998 | Name += "objc_msgSendSuper2_fixup"; |
| 4999 | } |
| 5000 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5001 | { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5002 | Fn = ObjCTypes.getMessageSendFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5003 | Name += "objc_msgSend_fixup"; |
| 5004 | } |
| 5005 | } |
| 5006 | Name += '_'; |
| 5007 | std::string SelName(Sel.getAsString()); |
| 5008 | // Replace all ':' in selector name with '_' ouch! |
| 5009 | for(unsigned i = 0; i < SelName.size(); i++) |
| 5010 | if (SelName[i] == ':') |
| 5011 | SelName[i] = '_'; |
| 5012 | Name += SelName; |
| 5013 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5014 | if (!GV) { |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5015 | // Build message ref table entry. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5016 | std::vector<llvm::Constant*> Values(2); |
| 5017 | Values[0] = Fn; |
| 5018 | Values[1] = GetMethodVarName(Sel); |
| 5019 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 5020 | GV = new llvm::GlobalVariable(Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 5021 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5022 | Init, |
| 5023 | Name, |
| 5024 | &CGM.getModule()); |
| 5025 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | f59c1a6 | 2009-04-15 19:04:46 +0000 | [diff] [blame] | 5026 | GV->setAlignment(16); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5027 | GV->setSection("__DATA, __objc_msgrefs, coalesced"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5028 | } |
| 5029 | llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5030 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5031 | CallArgList ActualArgs; |
| 5032 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 5033 | ActualArgs.push_back(std::make_pair(RValue::get(Arg1), |
| 5034 | ObjCTypes.MessageRefCPtrTy)); |
| 5035 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5036 | const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs); |
| 5037 | llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0); |
| 5038 | Callee = CGF.Builder.CreateLoad(Callee); |
Fariborz Jahanian | 3ab75bd | 2009-02-14 21:25:36 +0000 | [diff] [blame] | 5039 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5040 | Callee = CGF.Builder.CreateBitCast(Callee, |
| 5041 | llvm::PointerType::getUnqual(FTy)); |
| 5042 | return CGF.EmitCall(FnInfo1, Callee, ActualArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5043 | } |
| 5044 | |
| 5045 | /// Generate code for a message send expression in the nonfragile abi. |
| 5046 | CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend( |
| 5047 | CodeGen::CodeGenFunction &CGF, |
| 5048 | QualType ResultType, |
| 5049 | Selector Sel, |
| 5050 | llvm::Value *Receiver, |
| 5051 | bool IsClassMessage, |
| 5052 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5053 | return EmitMessageSend(CGF, ResultType, Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5054 | Receiver, CGF.getContext().getObjCIdType(), |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5055 | false, CallArgs); |
| 5056 | } |
| 5057 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5058 | llvm::GlobalVariable * |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5059 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5060 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5061 | |
Daniel Dunbar | dfff230 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 5062 | if (!GV) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5063 | GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 5064 | llvm::GlobalValue::ExternalLinkage, |
| 5065 | 0, Name, &CGM.getModule()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5066 | } |
| 5067 | |
| 5068 | return GV; |
| 5069 | } |
| 5070 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5071 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5072 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5073 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 5074 | |
| 5075 | if (!Entry) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5076 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5077 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5078 | Entry = |
| 5079 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5080 | llvm::GlobalValue::InternalLinkage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5081 | ClassGV, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5082 | "\01L_OBJC_CLASSLIST_REFERENCES_$_", |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5083 | &CGM.getModule()); |
| 5084 | Entry->setAlignment( |
| 5085 | CGM.getTargetData().getPrefTypeAlignment( |
| 5086 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5087 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
| 5088 | UsedGlobals.push_back(Entry); |
| 5089 | } |
| 5090 | |
| 5091 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5092 | } |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5093 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5094 | llvm::Value * |
| 5095 | CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder, |
| 5096 | const ObjCInterfaceDecl *ID) { |
| 5097 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
| 5098 | |
| 5099 | if (!Entry) { |
| 5100 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5101 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
| 5102 | Entry = |
| 5103 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5104 | llvm::GlobalValue::InternalLinkage, |
| 5105 | ClassGV, |
| 5106 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 5107 | &CGM.getModule()); |
| 5108 | Entry->setAlignment( |
| 5109 | CGM.getTargetData().getPrefTypeAlignment( |
| 5110 | ObjCTypes.ClassnfABIPtrTy)); |
| 5111 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5112 | UsedGlobals.push_back(Entry); |
| 5113 | } |
| 5114 | |
| 5115 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5116 | } |
| 5117 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5118 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 5119 | /// meta-data |
| 5120 | /// |
| 5121 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 5122 | const ObjCInterfaceDecl *ID) { |
| 5123 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 5124 | if (Entry) |
| 5125 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5126 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5127 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5128 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5129 | Entry = |
| 5130 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5131 | llvm::GlobalValue::InternalLinkage, |
| 5132 | MetaClassGV, |
| 5133 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 5134 | &CGM.getModule()); |
| 5135 | Entry->setAlignment( |
| 5136 | CGM.getTargetData().getPrefTypeAlignment( |
| 5137 | ObjCTypes.ClassnfABIPtrTy)); |
| 5138 | |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5139 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5140 | UsedGlobals.push_back(Entry); |
| 5141 | |
| 5142 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5143 | } |
| 5144 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5145 | /// GetClass - Return a reference to the class for the given interface |
| 5146 | /// decl. |
| 5147 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 5148 | const ObjCInterfaceDecl *ID) { |
| 5149 | return EmitClassRef(Builder, ID); |
| 5150 | } |
| 5151 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5152 | /// Generates a message send where the super is the receiver. This is |
| 5153 | /// a message send to self with special delivery semantics indicating |
| 5154 | /// which class's method should be called. |
| 5155 | CodeGen::RValue |
| 5156 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 5157 | QualType ResultType, |
| 5158 | Selector Sel, |
| 5159 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5160 | bool isCategoryImpl, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5161 | llvm::Value *Receiver, |
| 5162 | bool IsClassMessage, |
| 5163 | const CodeGen::CallArgList &CallArgs) { |
| 5164 | // ... |
| 5165 | // Create and init a super structure; this is a (receiver, class) |
| 5166 | // pair we will pass to objc_msgSendSuper. |
| 5167 | llvm::Value *ObjCSuper = |
| 5168 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 5169 | |
| 5170 | llvm::Value *ReceiverAsObject = |
| 5171 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 5172 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 5173 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
| 5174 | |
| 5175 | // 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] | 5176 | llvm::Value *Target; |
| 5177 | if (IsClassMessage) { |
| 5178 | if (isCategoryImpl) { |
| 5179 | // Message sent to "super' in a class method defined in |
| 5180 | // a category implementation. |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5181 | Target = EmitClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5182 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 5183 | Target = CGF.Builder.CreateLoad(Target); |
| 5184 | } |
| 5185 | else |
| 5186 | Target = EmitMetaClassRef(CGF.Builder, Class); |
| 5187 | } |
| 5188 | else |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5189 | Target = EmitSuperClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5190 | |
| 5191 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 5192 | // and ObjCTypes types. |
| 5193 | const llvm::Type *ClassTy = |
| 5194 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 5195 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 5196 | CGF.Builder.CreateStore(Target, |
| 5197 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 5198 | |
| 5199 | return EmitMessageSend(CGF, ResultType, Sel, |
| 5200 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 5201 | true, CallArgs); |
| 5202 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5203 | |
| 5204 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
| 5205 | Selector Sel) { |
| 5206 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 5207 | |
| 5208 | if (!Entry) { |
| 5209 | llvm::Constant *Casted = |
| 5210 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 5211 | ObjCTypes.SelectorPtrTy); |
| 5212 | Entry = |
| 5213 | new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false, |
| 5214 | llvm::GlobalValue::InternalLinkage, |
| 5215 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_", |
| 5216 | &CGM.getModule()); |
| 5217 | Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip"); |
| 5218 | UsedGlobals.push_back(Entry); |
| 5219 | } |
| 5220 | |
| 5221 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5222 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5223 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 5224 | /// objc_assign_ivar (id src, id *dst) |
| 5225 | /// |
| 5226 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 5227 | llvm::Value *src, llvm::Value *dst) |
| 5228 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5229 | const llvm::Type * SrcTy = src->getType(); |
| 5230 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5231 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5232 | assert(Size <= 8 && "does not support size > 8"); |
| 5233 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5234 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5235 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5236 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5237 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5238 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5239 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5240 | src, dst, "assignivar"); |
| 5241 | return; |
| 5242 | } |
| 5243 | |
| 5244 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 5245 | /// objc_assign_strongCast (id src, id *dst) |
| 5246 | /// |
| 5247 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
| 5248 | CodeGen::CodeGenFunction &CGF, |
| 5249 | llvm::Value *src, llvm::Value *dst) |
| 5250 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5251 | const llvm::Type * SrcTy = src->getType(); |
| 5252 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5253 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5254 | assert(Size <= 8 && "does not support size > 8"); |
| 5255 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5256 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5257 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5258 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5259 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5260 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5261 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5262 | src, dst, "weakassign"); |
| 5263 | return; |
| 5264 | } |
| 5265 | |
| 5266 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 5267 | /// object: objc_read_weak (id *src) |
| 5268 | /// |
| 5269 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
| 5270 | CodeGen::CodeGenFunction &CGF, |
| 5271 | llvm::Value *AddrWeakObj) |
| 5272 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5273 | const llvm::Type* DestTy = |
| 5274 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5275 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 5276 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5277 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5278 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5279 | return read_weak; |
| 5280 | } |
| 5281 | |
| 5282 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 5283 | /// objc_assign_weak (id src, id *dst) |
| 5284 | /// |
| 5285 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 5286 | llvm::Value *src, llvm::Value *dst) |
| 5287 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5288 | const llvm::Type * SrcTy = src->getType(); |
| 5289 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5290 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5291 | assert(Size <= 8 && "does not support size > 8"); |
| 5292 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5293 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5294 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5295 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5296 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5297 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 5298 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5299 | src, dst, "weakassign"); |
| 5300 | return; |
| 5301 | } |
| 5302 | |
| 5303 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 5304 | /// objc_assign_global (id src, id *dst) |
| 5305 | /// |
| 5306 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(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 | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5319 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5320 | src, dst, "globalassign"); |
| 5321 | return; |
| 5322 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5323 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5324 | void |
| 5325 | CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 5326 | const Stmt &S) { |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5327 | bool isTry = isa<ObjCAtTryStmt>(S); |
| 5328 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 5329 | llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5330 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5331 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5332 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5333 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
| 5334 | |
| 5335 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 5336 | // evaluation of the expression must occur before we enter the |
| 5337 | // @synchronized. We can safely avoid a temp here because jumps into |
| 5338 | // @synchronized are illegal & this will dominate uses. |
| 5339 | llvm::Value *SyncArg = 0; |
| 5340 | if (!isTry) { |
| 5341 | SyncArg = |
| 5342 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 5343 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5344 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5345 | } |
| 5346 | |
| 5347 | // Push an EH context entry, used for handling rethrows and jumps |
| 5348 | // through finally. |
| 5349 | CGF.PushCleanupBlock(FinallyBlock); |
| 5350 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5351 | CGF.setInvokeDest(TryHandler); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5352 | |
| 5353 | CGF.EmitBlock(TryBlock); |
| 5354 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 5355 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
| 5356 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5357 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5358 | // Emit the exception handler. |
| 5359 | |
| 5360 | CGF.EmitBlock(TryHandler); |
| 5361 | |
| 5362 | llvm::Value *llvm_eh_exception = |
| 5363 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
| 5364 | llvm::Value *llvm_eh_selector_i64 = |
| 5365 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64); |
| 5366 | llvm::Value *llvm_eh_typeid_for_i64 = |
| 5367 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64); |
| 5368 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5369 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow"); |
| 5370 | |
| 5371 | llvm::SmallVector<llvm::Value*, 8> SelectorArgs; |
| 5372 | SelectorArgs.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5373 | SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5374 | |
| 5375 | // Construct the lists of (type, catch body) to handle. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5376 | llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5377 | bool HasCatchAll = false; |
| 5378 | if (isTry) { |
| 5379 | if (const ObjCAtCatchStmt* CatchStmt = |
| 5380 | cast<ObjCAtTryStmt>(S).getCatchStmts()) { |
| 5381 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5382 | const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5383 | Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody())); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5384 | |
| 5385 | // catch(...) always matches. |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5386 | if (!CatchDecl) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5387 | // Use i8* null here to signal this is a catch all, not a cleanup. |
| 5388 | llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 5389 | SelectorArgs.push_back(Null); |
| 5390 | HasCatchAll = true; |
| 5391 | break; |
| 5392 | } |
| 5393 | |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5394 | if (CGF.getContext().isObjCIdType(CatchDecl->getType()) || |
| 5395 | CatchDecl->getType()->isObjCQualifiedIdType()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5396 | llvm::Value *IDEHType = |
| 5397 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 5398 | if (!IDEHType) |
| 5399 | IDEHType = |
| 5400 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5401 | llvm::GlobalValue::ExternalLinkage, |
| 5402 | 0, "OBJC_EHTYPE_id", &CGM.getModule()); |
| 5403 | SelectorArgs.push_back(IDEHType); |
| 5404 | HasCatchAll = true; |
| 5405 | break; |
| 5406 | } |
| 5407 | |
| 5408 | // All other types should be Objective-C interface pointer types. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5409 | const PointerType *PT = CatchDecl->getType()->getAsPointerType(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5410 | assert(PT && "Invalid @catch type."); |
| 5411 | const ObjCInterfaceType *IT = |
| 5412 | PT->getPointeeType()->getAsObjCInterfaceType(); |
| 5413 | assert(IT && "Invalid @catch type."); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5414 | llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5415 | SelectorArgs.push_back(EHType); |
| 5416 | } |
| 5417 | } |
| 5418 | } |
| 5419 | |
| 5420 | // We use a cleanup unless there was already a catch all. |
| 5421 | if (!HasCatchAll) { |
| 5422 | SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5423 | Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5424 | } |
| 5425 | |
| 5426 | llvm::Value *Selector = |
| 5427 | CGF.Builder.CreateCall(llvm_eh_selector_i64, |
| 5428 | SelectorArgs.begin(), SelectorArgs.end(), |
| 5429 | "selector"); |
| 5430 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5431 | const ParmVarDecl *CatchParam = Handlers[i].first; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5432 | const Stmt *CatchBody = Handlers[i].second; |
| 5433 | |
| 5434 | llvm::BasicBlock *Next = 0; |
| 5435 | |
| 5436 | // The last handler always matches. |
| 5437 | if (i + 1 != e) { |
| 5438 | assert(CatchParam && "Only last handler can be a catch all."); |
| 5439 | |
| 5440 | llvm::BasicBlock *Match = CGF.createBasicBlock("match"); |
| 5441 | Next = CGF.createBasicBlock("catch.next"); |
| 5442 | llvm::Value *Id = |
| 5443 | CGF.Builder.CreateCall(llvm_eh_typeid_for_i64, |
| 5444 | CGF.Builder.CreateBitCast(SelectorArgs[i+2], |
| 5445 | ObjCTypes.Int8PtrTy)); |
| 5446 | CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id), |
| 5447 | Match, Next); |
| 5448 | |
| 5449 | CGF.EmitBlock(Match); |
| 5450 | } |
| 5451 | |
| 5452 | if (CatchBody) { |
| 5453 | llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end"); |
| 5454 | llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler"); |
| 5455 | |
| 5456 | // Cleanups must call objc_end_catch. |
| 5457 | // |
| 5458 | // FIXME: It seems incorrect for objc_begin_catch to be inside |
| 5459 | // this context, but this matches gcc. |
| 5460 | CGF.PushCleanupBlock(MatchEnd); |
| 5461 | CGF.setInvokeDest(MatchHandler); |
| 5462 | |
| 5463 | llvm::Value *ExcObject = |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5464 | CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5465 | |
| 5466 | // Bind the catch parameter if it exists. |
| 5467 | if (CatchParam) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5468 | ExcObject = |
| 5469 | CGF.Builder.CreateBitCast(ExcObject, |
| 5470 | CGF.ConvertType(CatchParam->getType())); |
| 5471 | // CatchParam is a ParmVarDecl because of the grammar |
| 5472 | // construction used to handle this, but for codegen purposes |
| 5473 | // we treat this as a local decl. |
| 5474 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
| 5475 | CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5476 | } |
| 5477 | |
| 5478 | CGF.ObjCEHValueStack.push_back(ExcObject); |
| 5479 | CGF.EmitStmt(CatchBody); |
| 5480 | CGF.ObjCEHValueStack.pop_back(); |
| 5481 | |
| 5482 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5483 | |
| 5484 | CGF.EmitBlock(MatchHandler); |
| 5485 | |
| 5486 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5487 | // We are required to emit this call to satisfy LLVM, even |
| 5488 | // though we don't use the result. |
| 5489 | llvm::SmallVector<llvm::Value*, 8> Args; |
| 5490 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5491 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5492 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5493 | 0)); |
| 5494 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5495 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5496 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5497 | |
| 5498 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5499 | |
| 5500 | CGF.EmitBlock(MatchEnd); |
| 5501 | |
| 5502 | // Unfortunately, we also have to generate another EH frame here |
| 5503 | // in case this throws. |
| 5504 | llvm::BasicBlock *MatchEndHandler = |
| 5505 | CGF.createBasicBlock("match.end.handler"); |
| 5506 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5507 | CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5508 | Cont, MatchEndHandler, |
| 5509 | Args.begin(), Args.begin()); |
| 5510 | |
| 5511 | CGF.EmitBlock(Cont); |
| 5512 | if (Info.SwitchBlock) |
| 5513 | CGF.EmitBlock(Info.SwitchBlock); |
| 5514 | if (Info.EndBlock) |
| 5515 | CGF.EmitBlock(Info.EndBlock); |
| 5516 | |
| 5517 | CGF.EmitBlock(MatchEndHandler); |
| 5518 | Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5519 | // We are required to emit this call to satisfy LLVM, even |
| 5520 | // though we don't use the result. |
| 5521 | Args.clear(); |
| 5522 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5523 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5524 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5525 | 0)); |
| 5526 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5527 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5528 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5529 | |
| 5530 | if (Next) |
| 5531 | CGF.EmitBlock(Next); |
| 5532 | } else { |
| 5533 | assert(!Next && "catchup should be last handler."); |
| 5534 | |
| 5535 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5536 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5537 | } |
| 5538 | } |
| 5539 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5540 | // Pop the cleanup entry, the @finally is outside this cleanup |
| 5541 | // scope. |
| 5542 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5543 | CGF.setInvokeDest(PrevLandingPad); |
| 5544 | |
| 5545 | CGF.EmitBlock(FinallyBlock); |
| 5546 | |
| 5547 | if (isTry) { |
| 5548 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 5549 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 5550 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 5551 | } else { |
| 5552 | // Emit 'objc_sync_exit(expr)' as finally's sole statement for |
| 5553 | // @synchronized. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5554 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5555 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5556 | |
| 5557 | if (Info.SwitchBlock) |
| 5558 | CGF.EmitBlock(Info.SwitchBlock); |
| 5559 | if (Info.EndBlock) |
| 5560 | CGF.EmitBlock(Info.EndBlock); |
| 5561 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5562 | // Branch around the rethrow code. |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5563 | CGF.EmitBranch(FinallyEnd); |
| 5564 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5565 | CGF.EmitBlock(FinallyRethrow); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5566 | CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5567 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5568 | CGF.Builder.CreateUnreachable(); |
| 5569 | |
| 5570 | CGF.EmitBlock(FinallyEnd); |
| 5571 | } |
| 5572 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5573 | /// EmitThrowStmt - Generate code for a throw statement. |
| 5574 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 5575 | const ObjCAtThrowStmt &S) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5576 | llvm::Value *Exception; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5577 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5578 | Exception = CGF.EmitScalarExpr(ThrowExpr); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5579 | } else { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5580 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
| 5581 | "Unexpected rethrow outside @catch block."); |
| 5582 | Exception = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5583 | } |
| 5584 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5585 | llvm::Value *ExceptionAsObject = |
| 5586 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 5587 | llvm::BasicBlock *InvokeDest = CGF.getInvokeDest(); |
| 5588 | if (InvokeDest) { |
| 5589 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5590 | CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5591 | Cont, InvokeDest, |
| 5592 | &ExceptionAsObject, &ExceptionAsObject + 1); |
| 5593 | CGF.EmitBlock(Cont); |
| 5594 | } else |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5595 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5596 | CGF.Builder.CreateUnreachable(); |
| 5597 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5598 | // Clear the insertion point to indicate we are in unreachable code. |
| 5599 | CGF.Builder.ClearInsertionPoint(); |
| 5600 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5601 | |
| 5602 | llvm::Value * |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5603 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 5604 | bool ForDefinition) { |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5605 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5606 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5607 | // If we don't need a definition, return the entry if found or check |
| 5608 | // if we use an external reference. |
| 5609 | if (!ForDefinition) { |
| 5610 | if (Entry) |
| 5611 | return Entry; |
Daniel Dunbar | 7e075cb | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 5612 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5613 | // If this type (or a super class) has the __objc_exception__ |
| 5614 | // attribute, emit an external reference. |
| 5615 | if (hasObjCExceptionAttribute(ID)) |
| 5616 | return Entry = |
| 5617 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5618 | llvm::GlobalValue::ExternalLinkage, |
| 5619 | 0, |
| 5620 | (std::string("OBJC_EHTYPE_$_") + |
| 5621 | ID->getIdentifier()->getName()), |
| 5622 | &CGM.getModule()); |
| 5623 | } |
| 5624 | |
| 5625 | // Otherwise we need to either make a new entry or fill in the |
| 5626 | // initializer. |
| 5627 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5628 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5629 | std::string VTableName = "objc_ehtype_vtable"; |
| 5630 | llvm::GlobalVariable *VTableGV = |
| 5631 | CGM.getModule().getGlobalVariable(VTableName); |
| 5632 | if (!VTableGV) |
| 5633 | VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false, |
| 5634 | llvm::GlobalValue::ExternalLinkage, |
| 5635 | 0, VTableName, &CGM.getModule()); |
| 5636 | |
| 5637 | llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2); |
| 5638 | |
| 5639 | std::vector<llvm::Constant*> Values(3); |
| 5640 | Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1); |
| 5641 | Values[1] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5642 | Values[2] = GetClassGlobal(ClassName); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5643 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
| 5644 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5645 | if (Entry) { |
| 5646 | Entry->setInitializer(Init); |
| 5647 | } else { |
| 5648 | Entry = new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5649 | llvm::GlobalValue::WeakAnyLinkage, |
| 5650 | Init, |
| 5651 | (std::string("OBJC_EHTYPE_$_") + |
| 5652 | ID->getIdentifier()->getName()), |
| 5653 | &CGM.getModule()); |
| 5654 | } |
| 5655 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 5656 | if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden) |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5657 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5658 | Entry->setAlignment(8); |
| 5659 | |
| 5660 | if (ForDefinition) { |
| 5661 | Entry->setSection("__DATA,__objc_const"); |
| 5662 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 5663 | } else { |
| 5664 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 5665 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5666 | |
| 5667 | return Entry; |
| 5668 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5669 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5670 | /* *** */ |
| 5671 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5672 | CodeGen::CGObjCRuntime * |
| 5673 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 5674 | return new CGObjCMac(CGM); |
| 5675 | } |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5676 | |
| 5677 | CodeGen::CGObjCRuntime * |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5678 | CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5679 | return new CGObjCNonFragileABIMac(CGM); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5680 | } |