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 | dbc9337 | 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 | dbc9337 | 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 | dbc9337 | 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 | dbc9337 | 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 | dbc9337 | 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 | dbc9337 | 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 | dbc9337 | 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]; |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame^] | 2092 | Ivar[0] = GetMethodVarName(IVD->getIdentifier()); |
| 2093 | Ivar[1] = GetMethodVarType(IVD); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 2094 | Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2095 | ComputeIvarBaseOffset(CGM, OID, IVD)); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2096 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
| 2099 | // Return null for empty list. |
| 2100 | if (Ivars.empty()) |
| 2101 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
| 2102 | |
| 2103 | std::vector<llvm::Constant*> Values(2); |
| 2104 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 2105 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
| 2106 | Ivars.size()); |
| 2107 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
| 2108 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2109 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2110 | llvm::GlobalVariable *GV; |
| 2111 | if (ForClass) |
| 2112 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(), |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2113 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
| 2114 | 4, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2115 | else |
| 2116 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" |
| 2117 | + ID->getNameAsString(), |
| 2118 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2119 | 4, true); |
| 2120 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2121 | } |
| 2122 | |
| 2123 | /* |
| 2124 | struct objc_method { |
| 2125 | SEL method_name; |
| 2126 | char *method_types; |
| 2127 | void *method; |
| 2128 | }; |
| 2129 | |
| 2130 | struct objc_method_list { |
| 2131 | struct objc_method_list *obsolete; |
| 2132 | int count; |
| 2133 | struct objc_method methods_list[count]; |
| 2134 | }; |
| 2135 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2136 | |
| 2137 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 2138 | /// given method if it has been defined. The result is null if the |
| 2139 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2140 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2141 | // FIXME: Use DenseMap::lookup |
| 2142 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 2143 | if (!Fn) |
| 2144 | return 0; |
| 2145 | |
| 2146 | std::vector<llvm::Constant*> Method(3); |
| 2147 | Method[0] = |
| 2148 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 2149 | ObjCTypes.SelectorPtrTy); |
| 2150 | Method[1] = GetMethodVarType(MD); |
| 2151 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 2152 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 2153 | } |
| 2154 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2155 | llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name, |
| 2156 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2157 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2158 | // Return null for empty list. |
| 2159 | if (Methods.empty()) |
| 2160 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
| 2161 | |
| 2162 | std::vector<llvm::Constant*> Values(3); |
| 2163 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 2164 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 2165 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 2166 | Methods.size()); |
| 2167 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 2168 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2169 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2170 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2171 | return llvm::ConstantExpr::getBitCast(GV, |
| 2172 | ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2173 | } |
| 2174 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 2175 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 2176 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2177 | std::string Name; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 2178 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2179 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2180 | CodeGenTypes &Types = CGM.getTypes(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2181 | const llvm::FunctionType *MethodTy = |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2182 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2183 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2184 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2185 | llvm::GlobalValue::InternalLinkage, |
| 2186 | Name, |
| 2187 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2188 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2189 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2190 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2191 | } |
| 2192 | |
Daniel Dunbar | 48fa064 | 2009-04-19 02:03:42 +0000 | [diff] [blame] | 2193 | /// GetFieldBaseOffset - return the field's byte offset. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2194 | uint64_t CGObjCCommonMac::GetFieldBaseOffset(const ObjCInterfaceDecl *OI, |
| 2195 | const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 2196 | const FieldDecl *Field) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2197 | // Is this a C struct? |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 2198 | if (!OI) |
| 2199 | return Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2200 | return ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2201 | } |
| 2202 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2203 | llvm::GlobalVariable * |
| 2204 | CGObjCCommonMac::CreateMetadataVar(const std::string &Name, |
| 2205 | llvm::Constant *Init, |
| 2206 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2207 | unsigned Align, |
| 2208 | bool AddToUsed) { |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2209 | const llvm::Type *Ty = Init->getType(); |
| 2210 | llvm::GlobalVariable *GV = |
| 2211 | new llvm::GlobalVariable(Ty, false, |
| 2212 | llvm::GlobalValue::InternalLinkage, |
| 2213 | Init, |
| 2214 | Name, |
| 2215 | &CGM.getModule()); |
| 2216 | if (Section) |
| 2217 | GV->setSection(Section); |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2218 | if (Align) |
| 2219 | GV->setAlignment(Align); |
| 2220 | if (AddToUsed) |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2221 | UsedGlobals.push_back(GV); |
| 2222 | return GV; |
| 2223 | } |
| 2224 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2225 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2226 | // Abuse this interface function as a place to finalize. |
| 2227 | FinishModule(); |
| 2228 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2229 | return NULL; |
| 2230 | } |
| 2231 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2232 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2233 | return ObjCTypes.getGetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2234 | } |
| 2235 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2236 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2237 | return ObjCTypes.getSetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2238 | } |
| 2239 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2240 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2241 | return ObjCTypes.getEnumerationMutationFn(); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2244 | /* |
| 2245 | |
| 2246 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 2247 | -- |
| 2248 | |
| 2249 | The basic framework for a @try-catch-finally is as follows: |
| 2250 | { |
| 2251 | objc_exception_data d; |
| 2252 | id _rethrow = null; |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2253 | bool _call_try_exit = true; |
| 2254 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2255 | objc_exception_try_enter(&d); |
| 2256 | if (!setjmp(d.jmp_buf)) { |
| 2257 | ... try body ... |
| 2258 | } else { |
| 2259 | // exception path |
| 2260 | id _caught = objc_exception_extract(&d); |
| 2261 | |
| 2262 | // enter new try scope for handlers |
| 2263 | if (!setjmp(d.jmp_buf)) { |
| 2264 | ... match exception and execute catch blocks ... |
| 2265 | |
| 2266 | // fell off end, rethrow. |
| 2267 | _rethrow = _caught; |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2268 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2269 | } else { |
| 2270 | // exception in catch block |
| 2271 | _rethrow = objc_exception_extract(&d); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2272 | _call_try_exit = false; |
| 2273 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2274 | } |
| 2275 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2276 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2277 | |
| 2278 | finally: |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2279 | if (_call_try_exit) |
| 2280 | objc_exception_try_exit(&d); |
| 2281 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2282 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2283 | ... dispatch to finally destination ... |
| 2284 | |
| 2285 | finally_rethrow: |
| 2286 | objc_exception_throw(_rethrow); |
| 2287 | |
| 2288 | finally_end: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2289 | } |
| 2290 | |
| 2291 | This framework differs slightly from the one gcc uses, in that gcc |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2292 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 2293 | and if the object should be rethrown. This breaks in the face of |
| 2294 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2295 | |
| 2296 | We specialize this framework for a few particular circumstances: |
| 2297 | |
| 2298 | - If there are no catch blocks, then we avoid emitting the second |
| 2299 | exception handling context. |
| 2300 | |
| 2301 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 2302 | e)) we avoid emitting the code to rethrow an uncaught exception. |
| 2303 | |
| 2304 | - FIXME: If there is no @finally block we can do a few more |
| 2305 | simplifications. |
| 2306 | |
| 2307 | Rethrows and Jumps-Through-Finally |
| 2308 | -- |
| 2309 | |
| 2310 | Support for implicit rethrows and jumping through the finally block is |
| 2311 | handled by storing the current exception-handling context in |
| 2312 | ObjCEHStack. |
| 2313 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2314 | In order to implement proper @finally semantics, we support one basic |
| 2315 | mechanism for jumping through the finally block to an arbitrary |
| 2316 | destination. Constructs which generate exits from a @try or @catch |
| 2317 | block use this mechanism to implement the proper semantics by chaining |
| 2318 | jumps, as necessary. |
| 2319 | |
| 2320 | This mechanism works like the one used for indirect goto: we |
| 2321 | arbitrarily assign an ID to each destination and store the ID for the |
| 2322 | destination in a variable prior to entering the finally block. At the |
| 2323 | end of the finally block we simply create a switch to the proper |
| 2324 | destination. |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2325 | |
| 2326 | Code gen for @synchronized(expr) stmt; |
| 2327 | Effectively generating code for: |
| 2328 | objc_sync_enter(expr); |
| 2329 | @try stmt @finally { objc_sync_exit(expr); } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2330 | */ |
| 2331 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2332 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 2333 | const Stmt &S) { |
| 2334 | bool isTry = isa<ObjCAtTryStmt>(S); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2335 | // Create various blocks we refer to for handling @finally. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2336 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2337 | llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit"); |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2338 | llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit"); |
| 2339 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
| 2340 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2341 | |
| 2342 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 2343 | // evaluation of the expression must occur before we enter the |
| 2344 | // @synchronized. We can safely avoid a temp here because jumps into |
| 2345 | // @synchronized are illegal & this will dominate uses. |
| 2346 | llvm::Value *SyncArg = 0; |
| 2347 | if (!isTry) { |
| 2348 | SyncArg = |
| 2349 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 2350 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 2351 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2352 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2353 | |
| 2354 | // Push an EH context entry, used for handling rethrows and jumps |
| 2355 | // through finally. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2356 | CGF.PushCleanupBlock(FinallyBlock); |
| 2357 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2358 | CGF.ObjCEHValueStack.push_back(0); |
| 2359 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2360 | // Allocate memory for the exception data and rethrow pointer. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2361 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 2362 | "exceptiondata.ptr"); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2363 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy, |
| 2364 | "_rethrow"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2365 | llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty, |
| 2366 | "_call_try_exit"); |
| 2367 | CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr); |
| 2368 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2369 | // Enter a new try block and call setjmp. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2370 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2371 | llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0, |
| 2372 | "jmpbufarray"); |
| 2373 | JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2374 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2375 | JmpBufPtr, "result"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2376 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2377 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 2378 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2379 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2380 | TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2381 | |
| 2382 | // Emit the @try block. |
| 2383 | CGF.EmitBlock(TryBlock); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2384 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 2385 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2386 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2387 | |
| 2388 | // Emit the "exception in @try" block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2389 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2390 | |
| 2391 | // Retrieve the exception object. We may emit multiple blocks but |
| 2392 | // nothing can cross this so the value is already in SSA form. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2393 | llvm::Value *Caught = |
| 2394 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2395 | ExceptionData, "caught"); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2396 | CGF.ObjCEHValueStack.back() = Caught; |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2397 | if (!isTry) |
| 2398 | { |
| 2399 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2400 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2401 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2402 | } |
| 2403 | else if (const ObjCAtCatchStmt* CatchStmt = |
| 2404 | cast<ObjCAtTryStmt>(S).getCatchStmts()) |
| 2405 | { |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2406 | // Enter a new exception try block (in case a @catch block throws |
| 2407 | // an exception). |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2408 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2409 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2410 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2411 | JmpBufPtr, "result"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2412 | llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2413 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2414 | llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch"); |
| 2415 | llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler"); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2416 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2417 | |
| 2418 | CGF.EmitBlock(CatchBlock); |
| 2419 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2420 | // Handle catch list. As a special case we check if everything is |
| 2421 | // matched and avoid generating code for falling off the end if |
| 2422 | // so. |
| 2423 | bool AllMatched = false; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2424 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2425 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2426 | |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2427 | const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2428 | const PointerType *PT = 0; |
| 2429 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2430 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2431 | if (!CatchParam) { |
| 2432 | AllMatched = true; |
| 2433 | } else { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2434 | PT = CatchParam->getType()->getAsPointerType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2435 | |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 2436 | // catch(id e) always matches. |
| 2437 | // FIXME: For the time being we also match id<X>; this should |
| 2438 | // be rejected by Sema instead. |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2439 | if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) || |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2440 | CatchParam->getType()->isObjCQualifiedIdType()) |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2441 | AllMatched = true; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2442 | } |
| 2443 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2444 | if (AllMatched) { |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2445 | if (CatchParam) { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2446 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2447 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2448 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2449 | } |
Anders Carlsson | 1452f55 | 2008-09-11 08:21:54 +0000 | [diff] [blame] | 2450 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2451 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2452 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2453 | break; |
| 2454 | } |
| 2455 | |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2456 | assert(PT && "Unexpected non-pointer type in @catch"); |
| 2457 | QualType T = PT->getPointeeType(); |
Anders Carlsson | 4b7ff6e | 2008-09-11 06:35:14 +0000 | [diff] [blame] | 2458 | const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2459 | assert(ObjCType && "Catch parameter must have Objective-C type!"); |
| 2460 | |
| 2461 | // Check if the @catch block matches the exception object. |
| 2462 | llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl()); |
| 2463 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2464 | llvm::Value *Match = |
| 2465 | CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(), |
| 2466 | Class, Caught, "match"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2467 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2468 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2469 | |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2470 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2471 | MatchedBlock, NextCatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2472 | |
| 2473 | // Emit the @catch block. |
| 2474 | CGF.EmitBlock(MatchedBlock); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2475 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2476 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2477 | |
| 2478 | llvm::Value *Tmp = |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2479 | CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()), |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2480 | "tmp"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2481 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2482 | |
| 2483 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2484 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2485 | |
| 2486 | CGF.EmitBlock(NextCatchBlock); |
| 2487 | } |
| 2488 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2489 | if (!AllMatched) { |
| 2490 | // None of the handlers caught the exception, so store it to be |
| 2491 | // rethrown at the end of the @finally block. |
| 2492 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2493 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2494 | } |
| 2495 | |
| 2496 | // Emit the exception handler for the @catch blocks. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2497 | CGF.EmitBlock(CatchHandler); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2498 | CGF.Builder.CreateStore( |
| 2499 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2500 | ExceptionData), |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2501 | RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2502 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2503 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2504 | } else { |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2505 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2506 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2507 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2508 | } |
| 2509 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2510 | // Pop the exception-handling stack entry. It is important to do |
| 2511 | // this now, because the code in the @finally block is not in this |
| 2512 | // context. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2513 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 2514 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2515 | CGF.ObjCEHValueStack.pop_back(); |
| 2516 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2517 | // Emit the @finally block. |
| 2518 | CGF.EmitBlock(FinallyBlock); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2519 | llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp"); |
| 2520 | |
| 2521 | CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit); |
| 2522 | |
| 2523 | CGF.EmitBlock(FinallyExit); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2524 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2525 | |
| 2526 | CGF.EmitBlock(FinallyNoExit); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2527 | if (isTry) { |
| 2528 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 2529 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 2530 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2531 | } else { |
| 2532 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 2533 | // @synchronized. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2534 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg); |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 2535 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2536 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2537 | // Emit the switch block |
| 2538 | if (Info.SwitchBlock) |
| 2539 | CGF.EmitBlock(Info.SwitchBlock); |
| 2540 | if (Info.EndBlock) |
| 2541 | CGF.EmitBlock(Info.EndBlock); |
| 2542 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2543 | CGF.EmitBlock(FinallyRethrow); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2544 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2545 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2546 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2547 | |
| 2548 | CGF.EmitBlock(FinallyEnd); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2549 | } |
| 2550 | |
| 2551 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2552 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2553 | llvm::Value *ExceptionAsObject; |
| 2554 | |
| 2555 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 2556 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
| 2557 | ExceptionAsObject = |
| 2558 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 2559 | } else { |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2560 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2561 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2562 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2563 | } |
| 2564 | |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2565 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2566 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2567 | |
| 2568 | // Clear the insertion point to indicate we are in unreachable code. |
| 2569 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2572 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2573 | /// object: objc_read_weak (id *src) |
| 2574 | /// |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2575 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2576 | llvm::Value *AddrWeakObj) |
| 2577 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2578 | const llvm::Type* DestTy = |
| 2579 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2580 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2581 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2582 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2583 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2584 | return read_weak; |
| 2585 | } |
| 2586 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2587 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 2588 | /// objc_assign_weak (id src, id *dst) |
| 2589 | /// |
| 2590 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 2591 | llvm::Value *src, llvm::Value *dst) |
| 2592 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2593 | const llvm::Type * SrcTy = src->getType(); |
| 2594 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2595 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2596 | assert(Size <= 8 && "does not support size > 8"); |
| 2597 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2598 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2599 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2600 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2601 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2602 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 2603 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2604 | src, dst, "weakassign"); |
| 2605 | return; |
| 2606 | } |
| 2607 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2608 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 2609 | /// objc_assign_global (id src, id *dst) |
| 2610 | /// |
| 2611 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 2612 | llvm::Value *src, llvm::Value *dst) |
| 2613 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2614 | const llvm::Type * SrcTy = src->getType(); |
| 2615 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2616 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2617 | assert(Size <= 8 && "does not support size > 8"); |
| 2618 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2619 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2620 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2621 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2622 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2623 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2624 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2625 | src, dst, "globalassign"); |
| 2626 | return; |
| 2627 | } |
| 2628 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2629 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 2630 | /// objc_assign_ivar (id src, id *dst) |
| 2631 | /// |
| 2632 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 2633 | llvm::Value *src, llvm::Value *dst) |
| 2634 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2635 | const llvm::Type * SrcTy = src->getType(); |
| 2636 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2637 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2638 | assert(Size <= 8 && "does not support size > 8"); |
| 2639 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2640 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2641 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2642 | } |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2643 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2644 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2645 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(), |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2646 | src, dst, "assignivar"); |
| 2647 | return; |
| 2648 | } |
| 2649 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2650 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 2651 | /// objc_assign_strongCast (id src, id *dst) |
| 2652 | /// |
| 2653 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 2654 | llvm::Value *src, llvm::Value *dst) |
| 2655 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2656 | const llvm::Type * SrcTy = src->getType(); |
| 2657 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2658 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2659 | assert(Size <= 8 && "does not support size > 8"); |
| 2660 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2661 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2662 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2663 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2664 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2665 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2666 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2667 | src, dst, "weakassign"); |
| 2668 | return; |
| 2669 | } |
| 2670 | |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2671 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 2672 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2673 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 2674 | QualType ObjectTy, |
| 2675 | llvm::Value *BaseValue, |
| 2676 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2677 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 2678 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2679 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 2680 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2681 | } |
| 2682 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2683 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 2684 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2685 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2686 | uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2687 | return llvm::ConstantInt::get( |
| 2688 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 2689 | Offset); |
| 2690 | } |
| 2691 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2692 | /* *** Private Interface *** */ |
| 2693 | |
| 2694 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 2695 | /// level information. |
| 2696 | /// |
| 2697 | /// See: <rdr://4810609&4810587&4810587> |
| 2698 | /// struct IMAGE_INFO { |
| 2699 | /// unsigned version; |
| 2700 | /// unsigned flags; |
| 2701 | /// }; |
| 2702 | enum ImageInfoFlags { |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2703 | eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what |
| 2704 | // this implies. |
| 2705 | eImageInfo_GarbageCollected = (1 << 1), |
| 2706 | eImageInfo_GCOnly = (1 << 2), |
| 2707 | eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set. |
| 2708 | |
| 2709 | // A flag indicating that the module has no instances of an |
| 2710 | // @synthesize of a superclass variable. <rdar://problem/6803242> |
| 2711 | eImageInfo_CorrectedSynthesize = (1 << 4) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2712 | }; |
| 2713 | |
| 2714 | void CGObjCMac::EmitImageInfo() { |
| 2715 | unsigned version = 0; // Version is unused? |
| 2716 | unsigned flags = 0; |
| 2717 | |
| 2718 | // FIXME: Fix and continue? |
| 2719 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 2720 | flags |= eImageInfo_GarbageCollected; |
| 2721 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 2722 | flags |= eImageInfo_GCOnly; |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2723 | |
| 2724 | // We never allow @synthesize of a superclass property. |
| 2725 | flags |= eImageInfo_CorrectedSynthesize; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2726 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2727 | // Emitted as int[2]; |
| 2728 | llvm::Constant *values[2] = { |
| 2729 | llvm::ConstantInt::get(llvm::Type::Int32Ty, version), |
| 2730 | llvm::ConstantInt::get(llvm::Type::Int32Ty, flags) |
| 2731 | }; |
| 2732 | llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2733 | |
| 2734 | const char *Section; |
| 2735 | if (ObjCABI == 1) |
| 2736 | Section = "__OBJC, __image_info,regular"; |
| 2737 | else |
| 2738 | Section = "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2739 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2740 | CreateMetadataVar("\01L_OBJC_IMAGE_INFO", |
| 2741 | llvm::ConstantArray::get(AT, values, 2), |
| 2742 | Section, |
| 2743 | 0, |
| 2744 | true); |
| 2745 | GV->setConstant(true); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2746 | } |
| 2747 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2748 | |
| 2749 | // struct objc_module { |
| 2750 | // unsigned long version; |
| 2751 | // unsigned long size; |
| 2752 | // const char *name; |
| 2753 | // Symtab symtab; |
| 2754 | // }; |
| 2755 | |
| 2756 | // FIXME: Get from somewhere |
| 2757 | static const int ModuleVersion = 7; |
| 2758 | |
| 2759 | void CGObjCMac::EmitModuleInfo() { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 2760 | uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2761 | |
| 2762 | std::vector<llvm::Constant*> Values(4); |
| 2763 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion); |
| 2764 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2765 | // This used to be the filename, now it is unused. <rdr://4327263> |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2766 | Values[2] = GetClassName(&CGM.getContext().Idents.get("")); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2767 | Values[3] = EmitModuleSymbols(); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2768 | CreateMetadataVar("\01L_OBJC_MODULES", |
| 2769 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
| 2770 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2771 | 4, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
| 2774 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2775 | unsigned NumClasses = DefinedClasses.size(); |
| 2776 | unsigned NumCategories = DefinedCategories.size(); |
| 2777 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2778 | // Return null if no symbols were defined. |
| 2779 | if (!NumClasses && !NumCategories) |
| 2780 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
| 2781 | |
| 2782 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2783 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2784 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
| 2785 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 2786 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
| 2787 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2788 | // The runtime expects exactly the list of defined classes followed |
| 2789 | // by the list of defined categories, in a single array. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2790 | std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2791 | for (unsigned i=0; i<NumClasses; i++) |
| 2792 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 2793 | ObjCTypes.Int8PtrTy); |
| 2794 | for (unsigned i=0; i<NumCategories; i++) |
| 2795 | Symbols[NumClasses + i] = |
| 2796 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 2797 | ObjCTypes.Int8PtrTy); |
| 2798 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2799 | Values[4] = |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2800 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2801 | NumClasses + NumCategories), |
| 2802 | Symbols); |
| 2803 | |
| 2804 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2805 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2806 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2807 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 2808 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2809 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2810 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
| 2811 | } |
| 2812 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2813 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2814 | const ObjCInterfaceDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2815 | LazySymbols.insert(ID->getIdentifier()); |
| 2816 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2817 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 2818 | |
| 2819 | if (!Entry) { |
| 2820 | llvm::Constant *Casted = |
| 2821 | llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()), |
| 2822 | ObjCTypes.ClassPtrTy); |
| 2823 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2824 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 2825 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2826 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2827 | } |
| 2828 | |
| 2829 | return Builder.CreateLoad(Entry, false, "tmp"); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2832 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2833 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 2834 | |
| 2835 | if (!Entry) { |
| 2836 | llvm::Constant *Casted = |
| 2837 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 2838 | ObjCTypes.SelectorPtrTy); |
| 2839 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2840 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 2841 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2842 | 4, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2843 | } |
| 2844 | |
| 2845 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 2846 | } |
| 2847 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 2848 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2849 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2850 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2851 | if (!Entry) |
| 2852 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 2853 | llvm::ConstantArray::get(Ident->getName()), |
| 2854 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 2855 | 1, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2856 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2857 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 2860 | /// GetInterfaceDeclStructLayout - Get layout for ivars of given |
| 2861 | /// interface declaration. |
| 2862 | const llvm::StructLayout *CGObjCCommonMac::GetInterfaceDeclStructLayout( |
| 2863 | const ObjCInterfaceDecl *OID) const { |
Daniel Dunbar | 24c8991 | 2009-04-21 21:41:56 +0000 | [diff] [blame] | 2864 | assert(!OID->isForwardDecl() && "Invalid interface decl!"); |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 2865 | QualType T = CGM.getContext().getObjCInterfaceType(OID); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 2866 | const llvm::StructType *InterfaceTy = |
| 2867 | cast<llvm::StructType>(CGM.getTypes().ConvertType(T)); |
| 2868 | return CGM.getTargetData().getStructLayout(InterfaceTy); |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 2869 | } |
| 2870 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 2871 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 2872 | /// ivar layout bitmap. |
| 2873 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
| 2874 | const ObjCCommonTypesHelper &ObjCTypes) { |
| 2875 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 2876 | } |
| 2877 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2878 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCInterfaceDecl *OI, |
| 2879 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2880 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2881 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2882 | unsigned int BytePos, bool ForStrongLayout, |
| 2883 | int &Index, int &SkIndex, bool &HasUnion) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2884 | bool IsUnion = (RD && RD->isUnion()); |
| 2885 | uint64_t MaxUnionIvarSize = 0; |
| 2886 | uint64_t MaxSkippedUnionIvarSize = 0; |
| 2887 | FieldDecl *MaxField = 0; |
| 2888 | FieldDecl *MaxSkippedField = 0; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2889 | FieldDecl *LastFieldBitfield = 0; |
| 2890 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2891 | unsigned base = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2892 | if (RecFields.empty()) |
| 2893 | return; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2894 | if (IsUnion) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2895 | base = BytePos + GetFieldBaseOffset(OI, Layout, RecFields[0]); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2896 | unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0); |
| 2897 | unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth(); |
| 2898 | |
| 2899 | llvm::SmallVector<FieldDecl*, 16> TmpRecFields; |
| 2900 | |
| 2901 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2902 | FieldDecl *Field = RecFields[i]; |
| 2903 | // Skip over unnamed or bitfields |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2904 | if (!Field->getIdentifier() || Field->isBitField()) { |
| 2905 | LastFieldBitfield = Field; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2906 | continue; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2907 | } |
| 2908 | LastFieldBitfield = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2909 | QualType FQT = Field->getType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2910 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2911 | if (FQT->isUnionType()) |
| 2912 | HasUnion = true; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2913 | else |
| 2914 | assert(FQT->isRecordType() && |
| 2915 | "only union/record is supported for ivar layout bitmap"); |
| 2916 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2917 | const RecordType *RT = FQT->getAsRecordType(); |
| 2918 | const RecordDecl *RD = RT->getDecl(); |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 2919 | // FIXME - Find a more efficient way of passing records down. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2920 | TmpRecFields.append(RD->field_begin(CGM.getContext()), |
| 2921 | RD->field_end(CGM.getContext())); |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 2922 | const llvm::Type *Ty = CGM.getTypes().ConvertType(FQT); |
| 2923 | const llvm::StructLayout *RecLayout = |
| 2924 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); |
| 2925 | |
| 2926 | BuildAggrIvarLayout(0, RecLayout, RD, TmpRecFields, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2927 | BytePos + GetFieldBaseOffset(OI, Layout, Field), |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2928 | ForStrongLayout, Index, SkIndex, |
| 2929 | HasUnion); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2930 | TmpRecFields.clear(); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2931 | continue; |
| 2932 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2933 | |
| 2934 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2935 | const ConstantArrayType *CArray = |
| 2936 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2937 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2938 | assert(CArray && "only array with know element size is supported"); |
| 2939 | FQT = CArray->getElementType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2940 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 2941 | const ConstantArrayType *CArray = |
| 2942 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2943 | ElCount *= CArray->getSize().getZExtValue(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2944 | FQT = CArray->getElementType(); |
| 2945 | } |
| 2946 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2947 | assert(!FQT->isUnionType() && |
| 2948 | "layout for array of unions not supported"); |
| 2949 | if (FQT->isRecordType()) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2950 | int OldIndex = Index; |
| 2951 | int OldSkIndex = SkIndex; |
| 2952 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2953 | // FIXME - Use a common routine with the above! |
| 2954 | const RecordType *RT = FQT->getAsRecordType(); |
| 2955 | const RecordDecl *RD = RT->getDecl(); |
| 2956 | // FIXME - Find a more efficiant way of passing records down. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2957 | TmpRecFields.append(RD->field_begin(CGM.getContext()), |
| 2958 | RD->field_end(CGM.getContext())); |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 2959 | const llvm::Type *Ty = CGM.getTypes().ConvertType(FQT); |
| 2960 | const llvm::StructLayout *RecLayout = |
| 2961 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2962 | |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 2963 | BuildAggrIvarLayout(0, RecLayout, RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2964 | TmpRecFields, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2965 | BytePos + GetFieldBaseOffset(OI, Layout, Field), |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2966 | ForStrongLayout, Index, SkIndex, |
| 2967 | HasUnion); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2968 | TmpRecFields.clear(); |
| 2969 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2970 | // Replicate layout information for each array element. Note that |
| 2971 | // one element is already done. |
| 2972 | uint64_t ElIx = 1; |
| 2973 | for (int FirstIndex = Index, FirstSkIndex = SkIndex; |
| 2974 | ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2975 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2976 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 2977 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2978 | GC_IVAR gcivar; |
| 2979 | gcivar.ivar_bytepos = IvarsInfo[i].ivar_bytepos + Size*ElIx; |
| 2980 | gcivar.ivar_size = IvarsInfo[i].ivar_size; |
| 2981 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2982 | } |
| 2983 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2984 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2985 | GC_IVAR skivar; |
| 2986 | skivar.ivar_bytepos = SkipIvars[i].ivar_bytepos + Size*ElIx; |
| 2987 | skivar.ivar_size = SkipIvars[i].ivar_size; |
| 2988 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2989 | } |
| 2990 | } |
| 2991 | continue; |
| 2992 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2993 | } |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2994 | // At this point, we are done with Record/Union and array there of. |
| 2995 | // For other arrays we are down to its element type. |
| 2996 | QualType::GCAttrTypes GCAttr = QualType::GCNone; |
| 2997 | do { |
| 2998 | if (FQT.isObjCGCStrong() || FQT.isObjCGCWeak()) { |
| 2999 | GCAttr = FQT.isObjCGCStrong() ? QualType::Strong : QualType::Weak; |
| 3000 | break; |
| 3001 | } |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3002 | else if (CGM.getContext().isObjCObjectPointerType(FQT)) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3003 | GCAttr = QualType::Strong; |
| 3004 | break; |
| 3005 | } |
| 3006 | else if (const PointerType *PT = FQT->getAsPointerType()) { |
| 3007 | FQT = PT->getPointeeType(); |
| 3008 | } |
| 3009 | else { |
| 3010 | break; |
| 3011 | } |
| 3012 | } while (true); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3013 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3014 | if ((ForStrongLayout && GCAttr == QualType::Strong) |
| 3015 | || (!ForStrongLayout && GCAttr == QualType::Weak)) { |
| 3016 | if (IsUnion) |
| 3017 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3018 | uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType()) |
| 3019 | / WordSizeInBits; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3020 | if (UnionIvarSize > MaxUnionIvarSize) |
| 3021 | { |
| 3022 | MaxUnionIvarSize = UnionIvarSize; |
| 3023 | MaxField = Field; |
| 3024 | } |
| 3025 | } |
| 3026 | else |
| 3027 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3028 | GC_IVAR gcivar; |
| 3029 | gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field); |
| 3030 | gcivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) / |
| 3031 | WordSizeInBits; |
| 3032 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3033 | } |
| 3034 | } |
| 3035 | else if ((ForStrongLayout && |
| 3036 | (GCAttr == QualType::GCNone || GCAttr == QualType::Weak)) |
| 3037 | || (!ForStrongLayout && GCAttr != QualType::Weak)) { |
| 3038 | if (IsUnion) |
| 3039 | { |
| 3040 | uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType()); |
| 3041 | if (UnionIvarSize > MaxSkippedUnionIvarSize) |
| 3042 | { |
| 3043 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 3044 | MaxSkippedField = Field; |
| 3045 | } |
| 3046 | } |
| 3047 | else |
| 3048 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3049 | GC_IVAR skivar; |
| 3050 | skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field); |
| 3051 | skivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) / |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3052 | ByteSizeInBits; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3053 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3054 | } |
| 3055 | } |
| 3056 | } |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3057 | if (LastFieldBitfield) { |
| 3058 | // Last field was a bitfield. Must update skip info. |
| 3059 | GC_IVAR skivar; |
| 3060 | skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, |
| 3061 | LastFieldBitfield); |
| 3062 | Expr *BitWidth = LastFieldBitfield->getBitWidth(); |
| 3063 | uint64_t BitFieldSize = |
| 3064 | BitWidth->getIntegerConstantExprValue(CGM.getContext()).getZExtValue(); |
| 3065 | skivar.ivar_size = (BitFieldSize / ByteSizeInBits) |
| 3066 | + ((BitFieldSize % ByteSizeInBits) != 0); |
| 3067 | SkipIvars.push_back(skivar); ++SkIndex; |
| 3068 | } |
| 3069 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3070 | if (MaxField) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3071 | GC_IVAR gcivar; |
| 3072 | gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, MaxField); |
| 3073 | gcivar.ivar_size = MaxUnionIvarSize; |
| 3074 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3075 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3076 | |
| 3077 | if (MaxSkippedField) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3078 | GC_IVAR skivar; |
| 3079 | skivar.ivar_bytepos = BytePos + |
| 3080 | GetFieldBaseOffset(OI, Layout, MaxSkippedField); |
| 3081 | skivar.ivar_size = MaxSkippedUnionIvarSize; |
| 3082 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3083 | } |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3084 | } |
| 3085 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3086 | static int |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3087 | IvarBytePosCompare(const void *a, const void *b) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3088 | { |
| 3089 | unsigned int sa = ((CGObjCCommonMac::GC_IVAR *)a)->ivar_bytepos; |
| 3090 | unsigned int sb = ((CGObjCCommonMac::GC_IVAR *)b)->ivar_bytepos; |
| 3091 | |
| 3092 | if (sa < sb) |
| 3093 | return -1; |
| 3094 | if (sa > sb) |
| 3095 | return 1; |
| 3096 | return 0; |
| 3097 | } |
| 3098 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3099 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 3100 | /// implementation for the __strong or __weak case. |
| 3101 | /// The layout map displays which words in ivar list must be skipped |
| 3102 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 3103 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
| 3104 | /// of words to skip and right nibble is count of words to scan. So, each |
| 3105 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
| 3106 | /// represented by a 0x00 byte which also ends the string. |
| 3107 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 3108 | /// - id, Class |
| 3109 | /// - object * |
| 3110 | /// - __strong anything |
| 3111 | /// |
| 3112 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 3113 | /// - __weak anything |
| 3114 | /// |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3115 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3116 | const ObjCImplementationDecl *OMD, |
| 3117 | bool ForStrongLayout) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3118 | int Index = -1; |
| 3119 | int SkIndex = -1; |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3120 | bool hasUnion = false; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3121 | int SkipScan; |
| 3122 | unsigned int WordsToScan, WordsToSkip; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3123 | const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 3124 | if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) |
| 3125 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3126 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3127 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3128 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3129 | CGM.getContext().CollectObjCIvars(OI, RecFields); |
| 3130 | if (RecFields.empty()) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3131 | return llvm::Constant::getNullValue(PtrTy); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3132 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3133 | SkipIvars.clear(); |
| 3134 | IvarsInfo.clear(); |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 3135 | |
| 3136 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OI); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3137 | BuildAggrIvarLayout(OI, Layout, 0, RecFields, 0, ForStrongLayout, |
| 3138 | Index, SkIndex, hasUnion); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3139 | if (Index == -1) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3140 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3141 | |
| 3142 | // Sort on byte position in case we encounterred a union nested in |
| 3143 | // the ivar list. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3144 | if (hasUnion && !IvarsInfo.empty()) |
| 3145 | qsort(&IvarsInfo[0], Index+1, sizeof(GC_IVAR), IvarBytePosCompare); |
| 3146 | if (hasUnion && !SkipIvars.empty()) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3147 | qsort(&SkipIvars[0], Index+1, sizeof(GC_IVAR), IvarBytePosCompare); |
| 3148 | |
| 3149 | // Build the string of skip/scan nibbles |
| 3150 | SkipScan = -1; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3151 | SkipScanIvars.clear(); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3152 | unsigned int WordSize = |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3153 | CGM.getTypes().getTargetData().getTypePaddedSize(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3154 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 3155 | WordsToSkip = 0; |
| 3156 | WordsToScan = IvarsInfo[0].ivar_size; |
| 3157 | } |
| 3158 | else { |
| 3159 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 3160 | WordsToScan = IvarsInfo[0].ivar_size; |
| 3161 | } |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3162 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3163 | { |
| 3164 | unsigned int TailPrevGCObjC = |
| 3165 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
| 3166 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) |
| 3167 | { |
| 3168 | // consecutive 'scanned' object pointers. |
| 3169 | WordsToScan += IvarsInfo[i].ivar_size; |
| 3170 | } |
| 3171 | else |
| 3172 | { |
| 3173 | // Skip over 'gc'able object pointer which lay over each other. |
| 3174 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 3175 | continue; |
| 3176 | // Must skip over 1 or more words. We save current skip/scan values |
| 3177 | // and start a new pair. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3178 | SKIP_SCAN SkScan; |
| 3179 | SkScan.skip = WordsToSkip; |
| 3180 | SkScan.scan = WordsToScan; |
| 3181 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
| 3182 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3183 | // Skip the hole. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3184 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 3185 | SkScan.scan = 0; |
| 3186 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3187 | WordsToSkip = 0; |
| 3188 | WordsToScan = IvarsInfo[i].ivar_size; |
| 3189 | } |
| 3190 | } |
| 3191 | if (WordsToScan > 0) |
| 3192 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3193 | SKIP_SCAN SkScan; |
| 3194 | SkScan.skip = WordsToSkip; |
| 3195 | SkScan.scan = WordsToScan; |
| 3196 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3197 | } |
| 3198 | |
| 3199 | bool BytesSkipped = false; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3200 | if (!SkipIvars.empty()) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3201 | { |
| 3202 | int LastByteSkipped = |
| 3203 | SkipIvars[SkIndex].ivar_bytepos + SkipIvars[SkIndex].ivar_size; |
| 3204 | int LastByteScanned = |
| 3205 | IvarsInfo[Index].ivar_bytepos + IvarsInfo[Index].ivar_size * WordSize; |
| 3206 | BytesSkipped = (LastByteSkipped > LastByteScanned); |
| 3207 | // Compute number of bytes to skip at the tail end of the last ivar scanned. |
| 3208 | if (BytesSkipped) |
| 3209 | { |
| 3210 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3211 | SKIP_SCAN SkScan; |
| 3212 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
| 3213 | SkScan.scan = 0; |
| 3214 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3215 | } |
| 3216 | } |
| 3217 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 3218 | // as 0xMN. |
| 3219 | for (int i = 0; i <= SkipScan; i++) |
| 3220 | { |
| 3221 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 3222 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 3223 | // 0xM0 followed by 0x0N detected. |
| 3224 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 3225 | for (int j = i+1; j < SkipScan; j++) |
| 3226 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 3227 | --SkipScan; |
| 3228 | } |
| 3229 | } |
| 3230 | |
| 3231 | // Generate the string. |
| 3232 | std::string BitMap; |
| 3233 | for (int i = 0; i <= SkipScan; i++) |
| 3234 | { |
| 3235 | unsigned char byte; |
| 3236 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 3237 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 3238 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 3239 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
| 3240 | |
| 3241 | if (skip_small > 0 || skip_big > 0) |
| 3242 | BytesSkipped = true; |
| 3243 | // first skip big. |
| 3244 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 3245 | BitMap += (unsigned char)(0xf0); |
| 3246 | |
| 3247 | // next (skip small, scan) |
| 3248 | if (skip_small) |
| 3249 | { |
| 3250 | byte = skip_small << 4; |
| 3251 | if (scan_big > 0) |
| 3252 | { |
| 3253 | byte |= 0xf; |
| 3254 | --scan_big; |
| 3255 | } |
| 3256 | else if (scan_small) |
| 3257 | { |
| 3258 | byte |= scan_small; |
| 3259 | scan_small = 0; |
| 3260 | } |
| 3261 | BitMap += byte; |
| 3262 | } |
| 3263 | // next scan big |
| 3264 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 3265 | BitMap += (unsigned char)(0x0f); |
| 3266 | // last scan small |
| 3267 | if (scan_small) |
| 3268 | { |
| 3269 | byte = scan_small; |
| 3270 | BitMap += byte; |
| 3271 | } |
| 3272 | } |
| 3273 | // null terminate string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3274 | unsigned char zero = 0; |
| 3275 | BitMap += zero; |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3276 | |
| 3277 | if (CGM.getLangOptions().ObjCGCBitmapPrint) { |
| 3278 | printf("\n%s ivar layout for class '%s': ", |
| 3279 | ForStrongLayout ? "strong" : "weak", |
| 3280 | OMD->getClassInterface()->getNameAsCString()); |
| 3281 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
| 3282 | for (unsigned i = 0; i < BitMap.size(); i++) |
| 3283 | if (!(s[i] & 0xf0)) |
| 3284 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3285 | else |
| 3286 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3287 | printf("\n"); |
| 3288 | } |
| 3289 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3290 | // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as |
| 3291 | // final layout. |
| 3292 | if (ForStrongLayout && !BytesSkipped) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3293 | return llvm::Constant::getNullValue(PtrTy); |
| 3294 | llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 3295 | llvm::ConstantArray::get(BitMap.c_str()), |
| 3296 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3297 | 1, true); |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3298 | return getConstantGEP(Entry, 0, 0); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3299 | } |
| 3300 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3301 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3302 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 3303 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3304 | // FIXME: Avoid std::string copying. |
| 3305 | if (!Entry) |
| 3306 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
| 3307 | llvm::ConstantArray::get(Sel.getAsString()), |
| 3308 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3309 | 1, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3310 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3311 | return getConstantGEP(Entry, 0, 0); |
| 3312 | } |
| 3313 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3314 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3315 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3316 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 3317 | } |
| 3318 | |
| 3319 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3320 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3321 | return GetMethodVarName(&CGM.getContext().Idents.get(Name)); |
| 3322 | } |
| 3323 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 3324 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3325 | std::string TypeStr; |
| 3326 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 3327 | |
| 3328 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3329 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3330 | if (!Entry) |
| 3331 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3332 | llvm::ConstantArray::get(TypeStr), |
| 3333 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3334 | 1, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3335 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3336 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3337 | } |
| 3338 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3339 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3340 | std::string TypeStr; |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3341 | CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D), |
| 3342 | TypeStr); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3343 | |
| 3344 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 3345 | |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3346 | if (!Entry) |
| 3347 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3348 | llvm::ConstantArray::get(TypeStr), |
| 3349 | "__TEXT,__cstring,cstring_literals", |
| 3350 | 1, true); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3351 | |
| 3352 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3353 | } |
| 3354 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3355 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3356 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3357 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
| 3358 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3359 | if (!Entry) |
| 3360 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
| 3361 | llvm::ConstantArray::get(Ident->getName()), |
| 3362 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3363 | 1, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3364 | |
| 3365 | return getConstantGEP(Entry, 0, 0); |
| 3366 | } |
| 3367 | |
| 3368 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3369 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3370 | llvm::Constant * |
| 3371 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 3372 | const Decl *Container) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3373 | std::string TypeStr; |
| 3374 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3375 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 3376 | } |
| 3377 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3378 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
| 3379 | const ObjCContainerDecl *CD, |
| 3380 | std::string &NameOut) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 3381 | NameOut = '\01'; |
| 3382 | NameOut += (D->isInstanceMethod() ? '-' : '+'); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3383 | NameOut += '['; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 3384 | assert (CD && "Missing container decl in GetNameForMethod"); |
| 3385 | NameOut += CD->getNameAsString(); |
Fariborz Jahanian | 1e9aef3 | 2009-04-16 18:34:20 +0000 | [diff] [blame] | 3386 | if (const ObjCCategoryImplDecl *CID = |
| 3387 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) { |
| 3388 | NameOut += '('; |
| 3389 | NameOut += CID->getNameAsString(); |
| 3390 | NameOut+= ')'; |
| 3391 | } |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3392 | NameOut += ' '; |
| 3393 | NameOut += D->getSelector().getAsString(); |
| 3394 | NameOut += ']'; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3395 | } |
| 3396 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3397 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3398 | EmitModuleInfo(); |
| 3399 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3400 | // Emit the dummy bodies for any protocols which were referenced but |
| 3401 | // never defined. |
| 3402 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
| 3403 | i = Protocols.begin(), e = Protocols.end(); i != e; ++i) { |
| 3404 | if (i->second->hasInitializer()) |
| 3405 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3406 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3407 | std::vector<llvm::Constant*> Values(5); |
| 3408 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 3409 | Values[1] = GetClassName(i->first); |
| 3410 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 3411 | Values[3] = Values[4] = |
| 3412 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 3413 | i->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 3414 | i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 3415 | Values)); |
| 3416 | } |
| 3417 | |
| 3418 | std::vector<llvm::Constant*> Used; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3419 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3420 | e = UsedGlobals.end(); i != e; ++i) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3421 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3422 | } |
| 3423 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3424 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3425 | llvm::GlobalValue *GV = |
| 3426 | new llvm::GlobalVariable(AT, false, |
| 3427 | llvm::GlobalValue::AppendingLinkage, |
| 3428 | llvm::ConstantArray::get(AT, Used), |
| 3429 | "llvm.used", |
| 3430 | &CGM.getModule()); |
| 3431 | |
| 3432 | GV->setSection("llvm.metadata"); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3433 | |
| 3434 | // Add assembler directives to add lazy undefined symbol references |
| 3435 | // for classes which are referenced but not defined. This is |
| 3436 | // important for correct linker interaction. |
| 3437 | |
| 3438 | // FIXME: Uh, this isn't particularly portable. |
| 3439 | std::stringstream s; |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3440 | |
| 3441 | if (!CGM.getModule().getModuleInlineAsm().empty()) |
| 3442 | s << "\n"; |
| 3443 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3444 | for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(), |
| 3445 | e = LazySymbols.end(); i != e; ++i) { |
| 3446 | s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n"; |
| 3447 | } |
| 3448 | for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(), |
| 3449 | e = DefinedSymbols.end(); i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3450 | s << "\t.objc_class_name_" << (*i)->getName() << "=0\n" |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3451 | << "\t.globl .objc_class_name_" << (*i)->getName() << "\n"; |
| 3452 | } |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3453 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3454 | CGM.getModule().appendModuleInlineAsm(s.str()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3455 | } |
| 3456 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3457 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3458 | : CGObjCCommonMac(cgm), |
| 3459 | ObjCTypes(cgm) |
| 3460 | { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3461 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3462 | ObjCABI = 2; |
| 3463 | } |
| 3464 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3465 | /* *** */ |
| 3466 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3467 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3468 | : CGM(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3469 | { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3470 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3471 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3472 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3473 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3474 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3475 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3476 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3477 | Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 3478 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3479 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 3480 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3481 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3482 | |
| 3483 | // FIXME: It would be nice to unify this with the opaque type, so |
| 3484 | // that the IR comes out a bit cleaner. |
| 3485 | const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 3486 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3487 | |
| 3488 | // I'm not sure I like this. The implicit coordination is a bit |
| 3489 | // gross. We should solve this in a reasonable fashion because this |
| 3490 | // is a pretty common task (match some runtime data structure with |
| 3491 | // an LLVM data structure). |
| 3492 | |
| 3493 | // FIXME: This is leaked. |
| 3494 | // FIXME: Merge with rewriter code? |
| 3495 | |
| 3496 | // struct _objc_super { |
| 3497 | // id self; |
| 3498 | // Class cls; |
| 3499 | // } |
| 3500 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3501 | SourceLocation(), |
| 3502 | &Ctx.Idents.get("_objc_super")); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3503 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3504 | Ctx.getObjCIdType(), 0, false)); |
| 3505 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3506 | Ctx.getObjCClassType(), 0, false)); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3507 | RD->completeDefinition(Ctx); |
| 3508 | |
| 3509 | SuperCTy = Ctx.getTagDeclType(RD); |
| 3510 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
| 3511 | |
| 3512 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3513 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 3514 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3515 | // struct _prop_t { |
| 3516 | // char *name; |
| 3517 | // char *attributes; |
| 3518 | // } |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 3519 | PropertyTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, NULL); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3520 | CGM.getModule().addTypeName("struct._prop_t", |
| 3521 | PropertyTy); |
| 3522 | |
| 3523 | // struct _prop_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3524 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3525 | // uint32_t count_of_properties; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3526 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3527 | // } |
| 3528 | PropertyListTy = llvm::StructType::get(IntTy, |
| 3529 | IntTy, |
| 3530 | llvm::ArrayType::get(PropertyTy, 0), |
| 3531 | NULL); |
| 3532 | CGM.getModule().addTypeName("struct._prop_list_t", |
| 3533 | PropertyListTy); |
| 3534 | // struct _prop_list_t * |
| 3535 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
| 3536 | |
| 3537 | // struct _objc_method { |
| 3538 | // SEL _cmd; |
| 3539 | // char *method_type; |
| 3540 | // char *_imp; |
| 3541 | // } |
| 3542 | MethodTy = llvm::StructType::get(SelectorPtrTy, |
| 3543 | Int8PtrTy, |
| 3544 | Int8PtrTy, |
| 3545 | NULL); |
| 3546 | CGM.getModule().addTypeName("struct._objc_method", MethodTy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3547 | |
| 3548 | // struct _objc_cache * |
| 3549 | CacheTy = llvm::OpaqueType::get(); |
| 3550 | CGM.getModule().addTypeName("struct._objc_cache", CacheTy); |
| 3551 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3552 | } |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3553 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3554 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3555 | : ObjCCommonTypesHelper(cgm) |
| 3556 | { |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3557 | // struct _objc_method_description { |
| 3558 | // SEL name; |
| 3559 | // char *types; |
| 3560 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3561 | MethodDescriptionTy = |
| 3562 | llvm::StructType::get(SelectorPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3563 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3564 | NULL); |
| 3565 | CGM.getModule().addTypeName("struct._objc_method_description", |
| 3566 | MethodDescriptionTy); |
| 3567 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3568 | // struct _objc_method_description_list { |
| 3569 | // int count; |
| 3570 | // struct _objc_method_description[1]; |
| 3571 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3572 | MethodDescriptionListTy = |
| 3573 | llvm::StructType::get(IntTy, |
| 3574 | llvm::ArrayType::get(MethodDescriptionTy, 0), |
| 3575 | NULL); |
| 3576 | CGM.getModule().addTypeName("struct._objc_method_description_list", |
| 3577 | MethodDescriptionListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3578 | |
| 3579 | // struct _objc_method_description_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3580 | MethodDescriptionListPtrTy = |
| 3581 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
| 3582 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3583 | // Protocol description structures |
| 3584 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3585 | // struct _objc_protocol_extension { |
| 3586 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 3587 | // struct _objc_method_description_list *optional_instance_methods; |
| 3588 | // struct _objc_method_description_list *optional_class_methods; |
| 3589 | // struct _objc_property_list *instance_properties; |
| 3590 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3591 | ProtocolExtensionTy = |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3592 | llvm::StructType::get(IntTy, |
| 3593 | MethodDescriptionListPtrTy, |
| 3594 | MethodDescriptionListPtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3595 | PropertyListPtrTy, |
| 3596 | NULL); |
| 3597 | CGM.getModule().addTypeName("struct._objc_protocol_extension", |
| 3598 | ProtocolExtensionTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3599 | |
| 3600 | // struct _objc_protocol_extension * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3601 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
| 3602 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3603 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3604 | |
| 3605 | llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(); |
| 3606 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3607 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3608 | const llvm::Type *T = |
| 3609 | llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3610 | LongTy, |
| 3611 | llvm::ArrayType::get(ProtocolTyHolder, 0), |
| 3612 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3613 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T); |
| 3614 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3615 | // struct _objc_protocol { |
| 3616 | // struct _objc_protocol_extension *isa; |
| 3617 | // char *protocol_name; |
| 3618 | // struct _objc_protocol **_objc_protocol_list; |
| 3619 | // struct _objc_method_description_list *instance_methods; |
| 3620 | // struct _objc_method_description_list *class_methods; |
| 3621 | // } |
| 3622 | T = llvm::StructType::get(ProtocolExtensionPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3623 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3624 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3625 | MethodDescriptionListPtrTy, |
| 3626 | MethodDescriptionListPtrTy, |
| 3627 | NULL); |
| 3628 | cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T); |
| 3629 | |
| 3630 | ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get()); |
| 3631 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3632 | ProtocolListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3633 | // struct _objc_protocol_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3634 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
| 3635 | |
| 3636 | ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get()); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3637 | CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3638 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3639 | |
| 3640 | // Class description structures |
| 3641 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3642 | // struct _objc_ivar { |
| 3643 | // char *ivar_name; |
| 3644 | // char *ivar_type; |
| 3645 | // int ivar_offset; |
| 3646 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3647 | IvarTy = llvm::StructType::get(Int8PtrTy, |
| 3648 | Int8PtrTy, |
| 3649 | IntTy, |
| 3650 | NULL); |
| 3651 | CGM.getModule().addTypeName("struct._objc_ivar", IvarTy); |
| 3652 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3653 | // struct _objc_ivar_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3654 | IvarListTy = llvm::OpaqueType::get(); |
| 3655 | CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy); |
| 3656 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
| 3657 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3658 | // struct _objc_method_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3659 | MethodListTy = llvm::OpaqueType::get(); |
| 3660 | CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy); |
| 3661 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
| 3662 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3663 | // struct _objc_class_extension * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3664 | ClassExtensionTy = |
| 3665 | llvm::StructType::get(IntTy, |
| 3666 | Int8PtrTy, |
| 3667 | PropertyListPtrTy, |
| 3668 | NULL); |
| 3669 | CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy); |
| 3670 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
| 3671 | |
| 3672 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3673 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3674 | // struct _objc_class { |
| 3675 | // Class isa; |
| 3676 | // Class super_class; |
| 3677 | // char *name; |
| 3678 | // long version; |
| 3679 | // long info; |
| 3680 | // long instance_size; |
| 3681 | // struct _objc_ivar_list *ivars; |
| 3682 | // struct _objc_method_list *methods; |
| 3683 | // struct _objc_cache *cache; |
| 3684 | // struct _objc_protocol_list *protocols; |
| 3685 | // char *ivar_layout; |
| 3686 | // struct _objc_class_ext *ext; |
| 3687 | // }; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3688 | T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3689 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3690 | Int8PtrTy, |
| 3691 | LongTy, |
| 3692 | LongTy, |
| 3693 | LongTy, |
| 3694 | IvarListPtrTy, |
| 3695 | MethodListPtrTy, |
| 3696 | CachePtrTy, |
| 3697 | ProtocolListPtrTy, |
| 3698 | Int8PtrTy, |
| 3699 | ClassExtensionPtrTy, |
| 3700 | NULL); |
| 3701 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T); |
| 3702 | |
| 3703 | ClassTy = cast<llvm::StructType>(ClassTyHolder.get()); |
| 3704 | CGM.getModule().addTypeName("struct._objc_class", ClassTy); |
| 3705 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
| 3706 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3707 | // struct _objc_category { |
| 3708 | // char *category_name; |
| 3709 | // char *class_name; |
| 3710 | // struct _objc_method_list *instance_method; |
| 3711 | // struct _objc_method_list *class_method; |
| 3712 | // uint32_t size; // sizeof(struct _objc_category) |
| 3713 | // struct _objc_property_list *instance_properties;// category's @property |
| 3714 | // } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3715 | CategoryTy = llvm::StructType::get(Int8PtrTy, |
| 3716 | Int8PtrTy, |
| 3717 | MethodListPtrTy, |
| 3718 | MethodListPtrTy, |
| 3719 | ProtocolListPtrTy, |
| 3720 | IntTy, |
| 3721 | PropertyListPtrTy, |
| 3722 | NULL); |
| 3723 | CGM.getModule().addTypeName("struct._objc_category", CategoryTy); |
| 3724 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3725 | // Global metadata structures |
| 3726 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3727 | // struct _objc_symtab { |
| 3728 | // long sel_ref_cnt; |
| 3729 | // SEL *refs; |
| 3730 | // short cls_def_cnt; |
| 3731 | // short cat_def_cnt; |
| 3732 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 3733 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3734 | SymtabTy = llvm::StructType::get(LongTy, |
| 3735 | SelectorPtrTy, |
| 3736 | ShortTy, |
| 3737 | ShortTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3738 | llvm::ArrayType::get(Int8PtrTy, 0), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3739 | NULL); |
| 3740 | CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy); |
| 3741 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
| 3742 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3743 | // struct _objc_module { |
| 3744 | // long version; |
| 3745 | // long size; // sizeof(struct _objc_module) |
| 3746 | // char *name; |
| 3747 | // struct _objc_symtab* symtab; |
| 3748 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3749 | ModuleTy = |
| 3750 | llvm::StructType::get(LongTy, |
| 3751 | LongTy, |
| 3752 | Int8PtrTy, |
| 3753 | SymtabPtrTy, |
| 3754 | NULL); |
| 3755 | CGM.getModule().addTypeName("struct._objc_module", ModuleTy); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3756 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 3757 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3758 | // FIXME: This is the size of the setjmp buffer and should be |
| 3759 | // target specific. 18 is what's used on 32-bit X86. |
| 3760 | uint64_t SetJmpBufferSize = 18; |
| 3761 | |
| 3762 | // Exceptions |
| 3763 | const llvm::Type *StackPtrTy = |
Daniel Dunbar | 1000491 | 2008-09-27 06:32:25 +0000 | [diff] [blame] | 3764 | llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3765 | |
| 3766 | ExceptionDataTy = |
| 3767 | llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty, |
| 3768 | SetJmpBufferSize), |
| 3769 | StackPtrTy, NULL); |
| 3770 | CGM.getModule().addTypeName("struct._objc_exception_data", |
| 3771 | ExceptionDataTy); |
| 3772 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3773 | } |
| 3774 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3775 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3776 | : ObjCCommonTypesHelper(cgm) |
| 3777 | { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3778 | // struct _method_list_t { |
| 3779 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 3780 | // uint32_t method_count; |
| 3781 | // struct _objc_method method_list[method_count]; |
| 3782 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3783 | MethodListnfABITy = llvm::StructType::get(IntTy, |
| 3784 | IntTy, |
| 3785 | llvm::ArrayType::get(MethodTy, 0), |
| 3786 | NULL); |
| 3787 | CGM.getModule().addTypeName("struct.__method_list_t", |
| 3788 | MethodListnfABITy); |
| 3789 | // struct method_list_t * |
| 3790 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3791 | |
| 3792 | // struct _protocol_t { |
| 3793 | // id isa; // NULL |
| 3794 | // const char * const protocol_name; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3795 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3796 | // const struct method_list_t * const instance_methods; |
| 3797 | // const struct method_list_t * const class_methods; |
| 3798 | // const struct method_list_t *optionalInstanceMethods; |
| 3799 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3800 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3801 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 3802 | // const uint32_t flags; // = 0 |
| 3803 | // } |
| 3804 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3805 | // Holder for struct _protocol_list_t * |
| 3806 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3807 | |
| 3808 | ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy, |
| 3809 | Int8PtrTy, |
| 3810 | llvm::PointerType::getUnqual( |
| 3811 | ProtocolListTyHolder), |
| 3812 | MethodListnfABIPtrTy, |
| 3813 | MethodListnfABIPtrTy, |
| 3814 | MethodListnfABIPtrTy, |
| 3815 | MethodListnfABIPtrTy, |
| 3816 | PropertyListPtrTy, |
| 3817 | IntTy, |
| 3818 | IntTy, |
| 3819 | NULL); |
| 3820 | CGM.getModule().addTypeName("struct._protocol_t", |
| 3821 | ProtocolnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3822 | |
| 3823 | // struct _protocol_t* |
| 3824 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3825 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3826 | // struct _protocol_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3827 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3828 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3829 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3830 | ProtocolListnfABITy = llvm::StructType::get(LongTy, |
| 3831 | llvm::ArrayType::get( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3832 | ProtocolnfABIPtrTy, 0), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3833 | NULL); |
| 3834 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3835 | ProtocolListnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3836 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo( |
| 3837 | ProtocolListnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3838 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3839 | // struct _objc_protocol_list* |
| 3840 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3841 | |
| 3842 | // struct _ivar_t { |
| 3843 | // unsigned long int *offset; // pointer to ivar offset location |
| 3844 | // char *name; |
| 3845 | // char *type; |
| 3846 | // uint32_t alignment; |
| 3847 | // uint32_t size; |
| 3848 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3849 | IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy), |
| 3850 | Int8PtrTy, |
| 3851 | Int8PtrTy, |
| 3852 | IntTy, |
| 3853 | IntTy, |
| 3854 | NULL); |
| 3855 | CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy); |
| 3856 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3857 | // struct _ivar_list_t { |
| 3858 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 3859 | // uint32 count; |
| 3860 | // struct _iver_t list[count]; |
| 3861 | // } |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3862 | IvarListnfABITy = llvm::StructType::get(IntTy, |
| 3863 | IntTy, |
| 3864 | llvm::ArrayType::get( |
| 3865 | IvarnfABITy, 0), |
| 3866 | NULL); |
| 3867 | CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy); |
| 3868 | |
| 3869 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3870 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3871 | // struct _class_ro_t { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3872 | // uint32_t const flags; |
| 3873 | // uint32_t const instanceStart; |
| 3874 | // uint32_t const instanceSize; |
| 3875 | // uint32_t const reserved; // only when building for 64bit targets |
| 3876 | // const uint8_t * const ivarLayout; |
| 3877 | // const char *const name; |
| 3878 | // const struct _method_list_t * const baseMethods; |
| 3879 | // const struct _objc_protocol_list *const baseProtocols; |
| 3880 | // const struct _ivar_list_t *const ivars; |
| 3881 | // const uint8_t * const weakIvarLayout; |
| 3882 | // const struct _prop_list_t * const properties; |
| 3883 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3884 | |
| 3885 | // FIXME. Add 'reserved' field in 64bit abi mode! |
| 3886 | ClassRonfABITy = llvm::StructType::get(IntTy, |
| 3887 | IntTy, |
| 3888 | IntTy, |
| 3889 | Int8PtrTy, |
| 3890 | Int8PtrTy, |
| 3891 | MethodListnfABIPtrTy, |
| 3892 | ProtocolListnfABIPtrTy, |
| 3893 | IvarListnfABIPtrTy, |
| 3894 | Int8PtrTy, |
| 3895 | PropertyListPtrTy, |
| 3896 | NULL); |
| 3897 | CGM.getModule().addTypeName("struct._class_ro_t", |
| 3898 | ClassRonfABITy); |
| 3899 | |
| 3900 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 3901 | std::vector<const llvm::Type*> Params; |
| 3902 | Params.push_back(ObjectPtrTy); |
| 3903 | Params.push_back(SelectorPtrTy); |
| 3904 | ImpnfABITy = llvm::PointerType::getUnqual( |
| 3905 | llvm::FunctionType::get(ObjectPtrTy, Params, false)); |
| 3906 | |
| 3907 | // struct _class_t { |
| 3908 | // struct _class_t *isa; |
| 3909 | // struct _class_t * const superclass; |
| 3910 | // void *cache; |
| 3911 | // IMP *vtable; |
| 3912 | // struct class_ro_t *ro; |
| 3913 | // } |
| 3914 | |
| 3915 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3916 | ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3917 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3918 | CachePtrTy, |
| 3919 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 3920 | llvm::PointerType::getUnqual( |
| 3921 | ClassRonfABITy), |
| 3922 | NULL); |
| 3923 | CGM.getModule().addTypeName("struct._class_t", ClassnfABITy); |
| 3924 | |
| 3925 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo( |
| 3926 | ClassnfABITy); |
| 3927 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3928 | // LLVM for struct _class_t * |
| 3929 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
| 3930 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3931 | // struct _category_t { |
| 3932 | // const char * const name; |
| 3933 | // struct _class_t *const cls; |
| 3934 | // const struct _method_list_t * const instance_methods; |
| 3935 | // const struct _method_list_t * const class_methods; |
| 3936 | // const struct _protocol_list_t * const protocols; |
| 3937 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3938 | // } |
| 3939 | CategorynfABITy = llvm::StructType::get(Int8PtrTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3940 | ClassnfABIPtrTy, |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3941 | MethodListnfABIPtrTy, |
| 3942 | MethodListnfABIPtrTy, |
| 3943 | ProtocolListnfABIPtrTy, |
| 3944 | PropertyListPtrTy, |
| 3945 | NULL); |
| 3946 | CGM.getModule().addTypeName("struct._category_t", CategorynfABITy); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3947 | |
| 3948 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3949 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3950 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3951 | |
| 3952 | // MessageRefTy - LLVM for: |
| 3953 | // struct _message_ref_t { |
| 3954 | // IMP messenger; |
| 3955 | // SEL name; |
| 3956 | // }; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3957 | |
| 3958 | // First the clang type for struct _message_ref_t |
| 3959 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3960 | SourceLocation(), |
| 3961 | &Ctx.Idents.get("_message_ref_t")); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3962 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3963 | Ctx.VoidPtrTy, 0, false)); |
| 3964 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3965 | Ctx.getObjCSelType(), 0, false)); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3966 | RD->completeDefinition(Ctx); |
| 3967 | |
| 3968 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 3969 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 3970 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3971 | |
| 3972 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 3973 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
| 3974 | |
| 3975 | // SuperMessageRefTy - LLVM for: |
| 3976 | // struct _super_message_ref_t { |
| 3977 | // SUPER_IMP messenger; |
| 3978 | // SEL name; |
| 3979 | // }; |
| 3980 | SuperMessageRefTy = llvm::StructType::get(ImpnfABITy, |
| 3981 | SelectorPtrTy, |
| 3982 | NULL); |
| 3983 | CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy); |
| 3984 | |
| 3985 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 3986 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
| 3987 | |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 3988 | |
| 3989 | // struct objc_typeinfo { |
| 3990 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 3991 | // const char* name; // c++ typeinfo string |
| 3992 | // Class cls; |
| 3993 | // }; |
| 3994 | EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy), |
| 3995 | Int8PtrTy, |
| 3996 | ClassnfABIPtrTy, |
| 3997 | NULL); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 3998 | CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 3999 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4000 | } |
| 4001 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4002 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
| 4003 | FinishNonFragileABIModule(); |
| 4004 | |
| 4005 | return NULL; |
| 4006 | } |
| 4007 | |
| 4008 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 4009 | // nonfragile abi has no module definition. |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4010 | |
| 4011 | // Build list of all implemented classe addresses in array |
| 4012 | // L_OBJC_LABEL_CLASS_$. |
| 4013 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$ |
| 4014 | // list of 'nonlazy' implementations (defined as those with a +load{} |
| 4015 | // method!!). |
| 4016 | unsigned NumClasses = DefinedClasses.size(); |
| 4017 | if (NumClasses) { |
| 4018 | std::vector<llvm::Constant*> Symbols(NumClasses); |
| 4019 | for (unsigned i=0; i<NumClasses; i++) |
| 4020 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 4021 | ObjCTypes.Int8PtrTy); |
| 4022 | llvm::Constant* Init = |
| 4023 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 4024 | NumClasses), |
| 4025 | Symbols); |
| 4026 | |
| 4027 | llvm::GlobalVariable *GV = |
| 4028 | new llvm::GlobalVariable(Init->getType(), false, |
| 4029 | llvm::GlobalValue::InternalLinkage, |
| 4030 | Init, |
| 4031 | "\01L_OBJC_LABEL_CLASS_$", |
| 4032 | &CGM.getModule()); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 4033 | GV->setAlignment(8); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4034 | GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip"); |
| 4035 | UsedGlobals.push_back(GV); |
| 4036 | } |
| 4037 | |
| 4038 | // Build list of all implemented category addresses in array |
| 4039 | // L_OBJC_LABEL_CATEGORY_$. |
| 4040 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$ |
| 4041 | // list of 'nonlazy' category implementations (defined as those with a +load{} |
| 4042 | // method!!). |
| 4043 | unsigned NumCategory = DefinedCategories.size(); |
| 4044 | if (NumCategory) { |
| 4045 | std::vector<llvm::Constant*> Symbols(NumCategory); |
| 4046 | for (unsigned i=0; i<NumCategory; i++) |
| 4047 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 4048 | ObjCTypes.Int8PtrTy); |
| 4049 | llvm::Constant* Init = |
| 4050 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 4051 | NumCategory), |
| 4052 | Symbols); |
| 4053 | |
| 4054 | llvm::GlobalVariable *GV = |
| 4055 | new llvm::GlobalVariable(Init->getType(), false, |
| 4056 | llvm::GlobalValue::InternalLinkage, |
| 4057 | Init, |
| 4058 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 4059 | &CGM.getModule()); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 4060 | GV->setAlignment(8); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4061 | GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip"); |
| 4062 | UsedGlobals.push_back(GV); |
| 4063 | } |
| 4064 | |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4065 | // static int L_OBJC_IMAGE_INFO[2] = { 0, flags }; |
| 4066 | // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0 |
| 4067 | std::vector<llvm::Constant*> Values(2); |
| 4068 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0); |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4069 | unsigned int flags = 0; |
Fariborz Jahanian | 66a5c2c | 2009-02-24 23:34:44 +0000 | [diff] [blame] | 4070 | // FIXME: Fix and continue? |
| 4071 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 4072 | flags |= eImageInfo_GarbageCollected; |
| 4073 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 4074 | flags |= eImageInfo_GCOnly; |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4075 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4076 | llvm::Constant* Init = llvm::ConstantArray::get( |
| 4077 | llvm::ArrayType::get(ObjCTypes.IntTy, 2), |
| 4078 | Values); |
| 4079 | llvm::GlobalVariable *IMGV = |
| 4080 | new llvm::GlobalVariable(Init->getType(), false, |
| 4081 | llvm::GlobalValue::InternalLinkage, |
| 4082 | Init, |
| 4083 | "\01L_OBJC_IMAGE_INFO", |
| 4084 | &CGM.getModule()); |
| 4085 | IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); |
| 4086 | UsedGlobals.push_back(IMGV); |
| 4087 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4088 | std::vector<llvm::Constant*> Used; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4089 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4090 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
| 4091 | e = UsedGlobals.end(); i != e; ++i) { |
| 4092 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
| 4093 | } |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4094 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4095 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
| 4096 | llvm::GlobalValue *GV = |
| 4097 | new llvm::GlobalVariable(AT, false, |
| 4098 | llvm::GlobalValue::AppendingLinkage, |
| 4099 | llvm::ConstantArray::get(AT, Used), |
| 4100 | "llvm.used", |
| 4101 | &CGM.getModule()); |
| 4102 | |
| 4103 | GV->setSection("llvm.metadata"); |
| 4104 | |
| 4105 | } |
| 4106 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4107 | // Metadata flags |
| 4108 | enum MetaDataDlags { |
| 4109 | CLS = 0x0, |
| 4110 | CLS_META = 0x1, |
| 4111 | CLS_ROOT = 0x2, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4112 | OBJC2_CLS_HIDDEN = 0x10, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4113 | CLS_EXCEPTION = 0x20 |
| 4114 | }; |
| 4115 | /// BuildClassRoTInitializer - generate meta-data for: |
| 4116 | /// struct _class_ro_t { |
| 4117 | /// uint32_t const flags; |
| 4118 | /// uint32_t const instanceStart; |
| 4119 | /// uint32_t const instanceSize; |
| 4120 | /// uint32_t const reserved; // only when building for 64bit targets |
| 4121 | /// const uint8_t * const ivarLayout; |
| 4122 | /// const char *const name; |
| 4123 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4124 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4125 | /// const struct _ivar_list_t *const ivars; |
| 4126 | /// const uint8_t * const weakIvarLayout; |
| 4127 | /// const struct _prop_list_t * const properties; |
| 4128 | /// } |
| 4129 | /// |
| 4130 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
| 4131 | unsigned flags, |
| 4132 | unsigned InstanceStart, |
| 4133 | unsigned InstanceSize, |
| 4134 | const ObjCImplementationDecl *ID) { |
| 4135 | std::string ClassName = ID->getNameAsString(); |
| 4136 | std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets! |
| 4137 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 4138 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 4139 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
| 4140 | // FIXME. For 64bit targets add 0 here. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4141 | // FIXME. ivarLayout is currently null! |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 4142 | // Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4143 | // : BuildIvarLayout(ID, true); |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 4144 | Values[ 3] = GetIvarLayoutName(0, ObjCTypes); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4145 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4146 | // const struct _method_list_t * const baseMethods; |
| 4147 | std::vector<llvm::Constant*> Methods; |
| 4148 | std::string MethodListName("\01l_OBJC_$_"); |
| 4149 | if (flags & CLS_META) { |
| 4150 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
| 4151 | for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(), |
| 4152 | e = ID->classmeth_end(); i != e; ++i) { |
| 4153 | // Class methods should always be defined. |
| 4154 | Methods.push_back(GetMethodConstant(*i)); |
| 4155 | } |
| 4156 | } else { |
| 4157 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
| 4158 | for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(), |
| 4159 | e = ID->instmeth_end(); i != e; ++i) { |
| 4160 | // Instance methods should always be defined. |
| 4161 | Methods.push_back(GetMethodConstant(*i)); |
| 4162 | } |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4163 | for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(), |
| 4164 | e = ID->propimpl_end(); i != e; ++i) { |
| 4165 | ObjCPropertyImplDecl *PID = *i; |
| 4166 | |
| 4167 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 4168 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 4169 | |
| 4170 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 4171 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4172 | Methods.push_back(C); |
| 4173 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 4174 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4175 | Methods.push_back(C); |
| 4176 | } |
| 4177 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4178 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4179 | Values[ 5] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4180 | "__DATA, __objc_const", Methods); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4181 | |
| 4182 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4183 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
| 4184 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
| 4185 | + OID->getNameAsString(), |
| 4186 | OID->protocol_begin(), |
| 4187 | OID->protocol_end()); |
| 4188 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4189 | if (flags & CLS_META) |
| 4190 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4191 | else |
| 4192 | Values[ 7] = EmitIvarList(ID); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4193 | // FIXME. weakIvarLayout is currently null. |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 4194 | // Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4195 | // : BuildIvarLayout(ID, false); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4196 | Values[ 8] = GetIvarLayoutName(0, ObjCTypes); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4197 | if (flags & CLS_META) |
| 4198 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4199 | else |
| 4200 | Values[ 9] = |
| 4201 | EmitPropertyList( |
| 4202 | "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
| 4203 | ID, ID->getClassInterface(), ObjCTypes); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4204 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
| 4205 | Values); |
| 4206 | llvm::GlobalVariable *CLASS_RO_GV = |
| 4207 | new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false, |
| 4208 | llvm::GlobalValue::InternalLinkage, |
| 4209 | Init, |
| 4210 | (flags & CLS_META) ? |
| 4211 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 4212 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName, |
| 4213 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4214 | CLASS_RO_GV->setAlignment( |
| 4215 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4216 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4217 | return CLASS_RO_GV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4218 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4219 | } |
| 4220 | |
| 4221 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 4222 | /// for the given ClassName for: |
| 4223 | /// struct _class_t { |
| 4224 | /// struct _class_t *isa; |
| 4225 | /// struct _class_t * const superclass; |
| 4226 | /// void *cache; |
| 4227 | /// IMP *vtable; |
| 4228 | /// struct class_ro_t *ro; |
| 4229 | /// } |
| 4230 | /// |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4231 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
| 4232 | std::string &ClassName, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4233 | llvm::Constant *IsAGV, |
| 4234 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4235 | llvm::Constant *ClassRoGV, |
| 4236 | bool HiddenVisibility) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4237 | std::vector<llvm::Constant*> Values(5); |
| 4238 | Values[0] = IsAGV; |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4239 | Values[1] = SuperClassGV |
| 4240 | ? SuperClassGV |
| 4241 | : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4242 | Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar |
| 4243 | Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar |
| 4244 | Values[4] = ClassRoGV; // &CLASS_RO_GV |
| 4245 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
| 4246 | Values); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4247 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 4248 | GV->setInitializer(Init); |
Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 4249 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4250 | GV->setAlignment( |
| 4251 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4252 | if (HiddenVisibility) |
| 4253 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4254 | return GV; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4255 | } |
| 4256 | |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4257 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCInterfaceDecl *OID, |
| 4258 | uint32_t &InstanceStart, |
| 4259 | uint32_t &InstanceSize) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4260 | // Find first and last (non-padding) ivars in this interface. |
| 4261 | |
| 4262 | // FIXME: Use iterator. |
| 4263 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
| 4264 | GetNamedIvarList(OID, OIvars); |
| 4265 | |
| 4266 | if (OIvars.empty()) { |
| 4267 | InstanceStart = InstanceSize = 0; |
| 4268 | return; |
Daniel Dunbar | d4ae6c0 | 2009-04-22 04:39:47 +0000 | [diff] [blame] | 4269 | } |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4270 | |
| 4271 | const ObjCIvarDecl *First = OIvars.front(); |
| 4272 | const ObjCIvarDecl *Last = OIvars.back(); |
| 4273 | |
| 4274 | InstanceStart = ComputeIvarBaseOffset(CGM, OID, First); |
| 4275 | const llvm::Type *FieldTy = |
| 4276 | CGM.getTypes().ConvertTypeForMem(Last->getType()); |
| 4277 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
| 4278 | InstanceSize = ComputeIvarBaseOffset(CGM, OID, Last) + Size; |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4279 | } |
| 4280 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4281 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 4282 | std::string ClassName = ID->getNameAsString(); |
| 4283 | if (!ObjCEmptyCacheVar) { |
| 4284 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4285 | ObjCTypes.CacheTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4286 | false, |
| 4287 | llvm::GlobalValue::ExternalLinkage, |
| 4288 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4289 | "_objc_empty_cache", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4290 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4291 | |
| 4292 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4293 | ObjCTypes.ImpnfABITy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4294 | false, |
| 4295 | llvm::GlobalValue::ExternalLinkage, |
| 4296 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4297 | "_objc_empty_vtable", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4298 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4299 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4300 | assert(ID->getClassInterface() && |
| 4301 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 4302 | // FIXME: Is this correct (that meta class size is never computed)? |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4303 | uint32_t InstanceStart = |
| 4304 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy); |
| 4305 | uint32_t InstanceSize = InstanceStart; |
| 4306 | uint32_t flags = CLS_META; |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4307 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 4308 | std::string ObjCClassName(getClassSymbolPrefix()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4309 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4310 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4311 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4312 | bool classIsHidden = |
| 4313 | CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4314 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4315 | flags |= OBJC2_CLS_HIDDEN; |
| 4316 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4317 | // class is root |
| 4318 | flags |= CLS_ROOT; |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4319 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4320 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4321 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4322 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4323 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 4324 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 4325 | Root = Super; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4326 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4327 | // work on super class metadata symbol. |
| 4328 | std::string SuperClassName = |
| 4329 | ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4330 | SuperClassGV = GetClassGlobal(SuperClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4331 | } |
| 4332 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 4333 | InstanceStart, |
| 4334 | InstanceSize,ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4335 | std::string TClassName = ObjCMetaClassName + ClassName; |
| 4336 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4337 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 4338 | classIsHidden); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4339 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4340 | // Metadata for the class |
| 4341 | flags = CLS; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4342 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4343 | flags |= OBJC2_CLS_HIDDEN; |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4344 | |
| 4345 | if (hasObjCExceptionAttribute(ID->getClassInterface())) |
| 4346 | flags |= CLS_EXCEPTION; |
| 4347 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4348 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4349 | flags |= CLS_ROOT; |
| 4350 | SuperClassGV = 0; |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 4351 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4352 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4353 | std::string RootClassName = |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4354 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4355 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4356 | } |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4357 | GetClassSizeInfo(ID->getClassInterface(), InstanceStart, InstanceSize); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4358 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4359 | InstanceStart, |
| 4360 | InstanceSize, |
| 4361 | ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4362 | |
| 4363 | TClassName = ObjCClassName + ClassName; |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4364 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4365 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 4366 | classIsHidden); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4367 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4368 | |
| 4369 | // Force the definition of the EHType if necessary. |
| 4370 | if (flags & CLS_EXCEPTION) |
| 4371 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4372 | } |
| 4373 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4374 | /// GenerateProtocolRef - This routine is called to generate code for |
| 4375 | /// a protocol reference expression; as in: |
| 4376 | /// @code |
| 4377 | /// @protocol(Proto1); |
| 4378 | /// @endcode |
| 4379 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 4380 | /// which will hold address of the protocol meta-data. |
| 4381 | /// |
| 4382 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
| 4383 | const ObjCProtocolDecl *PD) { |
| 4384 | |
Fariborz Jahanian | 960cd06 | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 4385 | // This routine is called for @protocol only. So, we must build definition |
| 4386 | // of protocol's meta-data (not a reference to it!) |
| 4387 | // |
| 4388 | llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4389 | ObjCTypes.ExternalProtocolPtrTy); |
| 4390 | |
| 4391 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
| 4392 | ProtocolName += PD->getNameAsCString(); |
| 4393 | |
| 4394 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 4395 | if (PTGV) |
| 4396 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4397 | PTGV = new llvm::GlobalVariable( |
| 4398 | Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4399 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4400 | Init, |
| 4401 | ProtocolName, |
| 4402 | &CGM.getModule()); |
| 4403 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 4404 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4405 | UsedGlobals.push_back(PTGV); |
| 4406 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4407 | } |
| 4408 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4409 | /// GenerateCategory - Build metadata for a category implementation. |
| 4410 | /// struct _category_t { |
| 4411 | /// const char * const name; |
| 4412 | /// struct _class_t *const cls; |
| 4413 | /// const struct _method_list_t * const instance_methods; |
| 4414 | /// const struct _method_list_t * const class_methods; |
| 4415 | /// const struct _protocol_list_t * const protocols; |
| 4416 | /// const struct _prop_list_t * const properties; |
| 4417 | /// } |
| 4418 | /// |
| 4419 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) |
| 4420 | { |
| 4421 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4422 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
| 4423 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4424 | "_$_" + OCD->getNameAsString()); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4425 | std::string ExtClassName(getClassSymbolPrefix() + |
| 4426 | Interface->getNameAsString()); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4427 | |
| 4428 | std::vector<llvm::Constant*> Values(6); |
| 4429 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 4430 | // meta-class entry symbol |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4431 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4432 | Values[1] = ClassGV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4433 | std::vector<llvm::Constant*> Methods; |
| 4434 | std::string MethodListName(Prefix); |
| 4435 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
| 4436 | "_$_" + OCD->getNameAsString(); |
| 4437 | |
| 4438 | for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(), |
| 4439 | e = OCD->instmeth_end(); i != e; ++i) { |
| 4440 | // Instance methods should always be defined. |
| 4441 | Methods.push_back(GetMethodConstant(*i)); |
| 4442 | } |
| 4443 | |
| 4444 | Values[2] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4445 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4446 | Methods); |
| 4447 | |
| 4448 | MethodListName = Prefix; |
| 4449 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 4450 | OCD->getNameAsString(); |
| 4451 | Methods.clear(); |
| 4452 | for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(), |
| 4453 | e = OCD->classmeth_end(); i != e; ++i) { |
| 4454 | // Class methods should always be defined. |
| 4455 | Methods.push_back(GetMethodConstant(*i)); |
| 4456 | } |
| 4457 | |
| 4458 | Values[3] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4459 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4460 | Methods); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4461 | const ObjCCategoryDecl *Category = |
| 4462 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4463 | if (Category) { |
| 4464 | std::string ExtName(Interface->getNameAsString() + "_$_" + |
| 4465 | OCD->getNameAsString()); |
| 4466 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
| 4467 | + Interface->getNameAsString() + "_$_" |
| 4468 | + Category->getNameAsString(), |
| 4469 | Category->protocol_begin(), |
| 4470 | Category->protocol_end()); |
| 4471 | Values[5] = |
| 4472 | EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
| 4473 | OCD, Category, ObjCTypes); |
| 4474 | } |
| 4475 | else { |
| 4476 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4477 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4478 | } |
| 4479 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4480 | llvm::Constant *Init = |
| 4481 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
| 4482 | Values); |
| 4483 | llvm::GlobalVariable *GCATV |
| 4484 | = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy, |
| 4485 | false, |
| 4486 | llvm::GlobalValue::InternalLinkage, |
| 4487 | Init, |
| 4488 | ExtCatName, |
| 4489 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4490 | GCATV->setAlignment( |
| 4491 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4492 | GCATV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4493 | UsedGlobals.push_back(GCATV); |
| 4494 | DefinedCategories.push_back(GCATV); |
| 4495 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4496 | |
| 4497 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 4498 | /// given method if it has been defined. The result is null if the |
| 4499 | /// method has not been defined. The return value has type MethodPtrTy. |
| 4500 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
| 4501 | const ObjCMethodDecl *MD) { |
| 4502 | // FIXME: Use DenseMap::lookup |
| 4503 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 4504 | if (!Fn) |
| 4505 | return 0; |
| 4506 | |
| 4507 | std::vector<llvm::Constant*> Method(3); |
| 4508 | Method[0] = |
| 4509 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4510 | ObjCTypes.SelectorPtrTy); |
| 4511 | Method[1] = GetMethodVarType(MD); |
| 4512 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 4513 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 4514 | } |
| 4515 | |
| 4516 | /// EmitMethodList - Build meta-data for method declarations |
| 4517 | /// struct _method_list_t { |
| 4518 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 4519 | /// uint32_t method_count; |
| 4520 | /// struct _objc_method method_list[method_count]; |
| 4521 | /// } |
| 4522 | /// |
| 4523 | llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList( |
| 4524 | const std::string &Name, |
| 4525 | const char *Section, |
| 4526 | const ConstantVector &Methods) { |
| 4527 | // Return null for empty list. |
| 4528 | if (Methods.empty()) |
| 4529 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
| 4530 | |
| 4531 | std::vector<llvm::Constant*> Values(3); |
| 4532 | // sizeof(struct _objc_method) |
| 4533 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy); |
| 4534 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4535 | // method_count |
| 4536 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 4537 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 4538 | Methods.size()); |
| 4539 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 4540 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4541 | |
| 4542 | llvm::GlobalVariable *GV = |
| 4543 | new llvm::GlobalVariable(Init->getType(), false, |
| 4544 | llvm::GlobalValue::InternalLinkage, |
| 4545 | Init, |
| 4546 | Name, |
| 4547 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4548 | GV->setAlignment( |
| 4549 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4550 | GV->setSection(Section); |
| 4551 | UsedGlobals.push_back(GV); |
| 4552 | return llvm::ConstantExpr::getBitCast(GV, |
| 4553 | ObjCTypes.MethodListnfABIPtrTy); |
| 4554 | } |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4555 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4556 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 4557 | /// the given ivar. |
| 4558 | /// |
| 4559 | llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 4560 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4561 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 4562 | std::string Name = "OBJC_IVAR_$_" + |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4563 | getInterfaceDeclForIvar(ID, Ivar, CGM.getContext())->getNameAsString() + |
| 4564 | '.' + Ivar->getNameAsString(); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4565 | llvm::GlobalVariable *IvarOffsetGV = |
| 4566 | CGM.getModule().getGlobalVariable(Name); |
| 4567 | if (!IvarOffsetGV) |
| 4568 | IvarOffsetGV = |
| 4569 | new llvm::GlobalVariable(ObjCTypes.LongTy, |
| 4570 | false, |
| 4571 | llvm::GlobalValue::ExternalLinkage, |
| 4572 | 0, |
| 4573 | Name, |
| 4574 | &CGM.getModule()); |
| 4575 | return IvarOffsetGV; |
| 4576 | } |
| 4577 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4578 | llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar( |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4579 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4580 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4581 | unsigned long int Offset) { |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4582 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
| 4583 | IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy, |
| 4584 | Offset)); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4585 | IvarOffsetGV->setAlignment( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4586 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy)); |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4587 | |
| 4588 | // FIXME: This matches gcc, but shouldn't the visibility be set on |
| 4589 | // the use as well (i.e., in ObjCIvarOffsetVariable). |
| 4590 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 4591 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
| 4592 | CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden) |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4593 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4594 | else |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4595 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4596 | IvarOffsetGV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4597 | return IvarOffsetGV; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4598 | } |
| 4599 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4600 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 4601 | /// implementation. The return value has type |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4602 | /// IvarListnfABIPtrTy. |
| 4603 | /// struct _ivar_t { |
| 4604 | /// unsigned long int *offset; // pointer to ivar offset location |
| 4605 | /// char *name; |
| 4606 | /// char *type; |
| 4607 | /// uint32_t alignment; |
| 4608 | /// uint32_t size; |
| 4609 | /// } |
| 4610 | /// struct _ivar_list_t { |
| 4611 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 4612 | /// uint32 count; |
| 4613 | /// struct _iver_t list[count]; |
| 4614 | /// } |
| 4615 | /// |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4616 | |
| 4617 | void CGObjCCommonMac::GetNamedIvarList(const ObjCInterfaceDecl *OID, |
| 4618 | llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const { |
| 4619 | for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), |
| 4620 | E = OID->ivar_end(); I != E; ++I) { |
| 4621 | // Ignore unnamed bit-fields. |
| 4622 | if (!(*I)->getDeclName()) |
| 4623 | continue; |
| 4624 | |
| 4625 | Res.push_back(*I); |
| 4626 | } |
| 4627 | |
| 4628 | for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(CGM.getContext()), |
| 4629 | E = OID->prop_end(CGM.getContext()); I != E; ++I) |
| 4630 | if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl()) |
| 4631 | Res.push_back(IV); |
| 4632 | } |
| 4633 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4634 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
| 4635 | const ObjCImplementationDecl *ID) { |
| 4636 | |
| 4637 | std::vector<llvm::Constant*> Ivars, Ivar(5); |
| 4638 | |
| 4639 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4640 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
| 4641 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4642 | // FIXME. Consolidate this with similar code in GenerateClass. |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 4643 | |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4644 | // Collect declared and synthesized ivars in a small vector. |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4645 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4646 | GetNamedIvarList(OID, OIvars); |
Fariborz Jahanian | 99eee36 | 2009-04-01 19:37:34 +0000 | [diff] [blame] | 4647 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4648 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 4649 | ObjCIvarDecl *IVD = OIvars[i]; |
Daniel Dunbar | 3eec8aa | 2009-04-20 05:53:40 +0000 | [diff] [blame] | 4650 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD, |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4651 | ComputeIvarBaseOffset(CGM, OID, IVD)); |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame^] | 4652 | Ivar[1] = GetMethodVarName(IVD->getIdentifier()); |
| 4653 | Ivar[2] = GetMethodVarType(IVD); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4654 | const llvm::Type *FieldTy = |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame^] | 4655 | CGM.getTypes().ConvertTypeForMem(IVD->getType()); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4656 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
| 4657 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame^] | 4658 | IVD->getType().getTypePtr()) >> 3; |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4659 | Align = llvm::Log2_32(Align); |
| 4660 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4661 | // NOTE. Size of a bitfield does not match gcc's, because of the |
| 4662 | // way bitfields are treated special in each. But I am told that |
| 4663 | // 'size' for bitfield ivars is ignored by the runtime so it does |
| 4664 | // not matter. If it matters, there is enough info to get the |
| 4665 | // bitfield right! |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4666 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4667 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
| 4668 | } |
| 4669 | // Return null for empty list. |
| 4670 | if (Ivars.empty()) |
| 4671 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4672 | std::vector<llvm::Constant*> Values(3); |
| 4673 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy); |
| 4674 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4675 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 4676 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
| 4677 | Ivars.size()); |
| 4678 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
| 4679 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4680 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 4681 | llvm::GlobalVariable *GV = |
| 4682 | new llvm::GlobalVariable(Init->getType(), false, |
| 4683 | llvm::GlobalValue::InternalLinkage, |
| 4684 | Init, |
| 4685 | Prefix + OID->getNameAsString(), |
| 4686 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4687 | GV->setAlignment( |
| 4688 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4689 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4690 | |
| 4691 | UsedGlobals.push_back(GV); |
| 4692 | return llvm::ConstantExpr::getBitCast(GV, |
| 4693 | ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4694 | } |
| 4695 | |
| 4696 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
| 4697 | const ObjCProtocolDecl *PD) { |
| 4698 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4699 | |
| 4700 | if (!Entry) { |
| 4701 | // We use the initializer as a marker of whether this is a forward |
| 4702 | // reference or not. At module finalization we add the empty |
| 4703 | // contents for protocols which were referenced but never defined. |
| 4704 | Entry = |
| 4705 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
| 4706 | llvm::GlobalValue::ExternalLinkage, |
| 4707 | 0, |
| 4708 | "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(), |
| 4709 | &CGM.getModule()); |
| 4710 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 4711 | UsedGlobals.push_back(Entry); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4712 | } |
| 4713 | |
| 4714 | return Entry; |
| 4715 | } |
| 4716 | |
| 4717 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 4718 | /// @code |
| 4719 | /// struct _protocol_t { |
| 4720 | /// id isa; // NULL |
| 4721 | /// const char * const protocol_name; |
| 4722 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 4723 | /// const struct method_list_t * const instance_methods; |
| 4724 | /// const struct method_list_t * const class_methods; |
| 4725 | /// const struct method_list_t *optionalInstanceMethods; |
| 4726 | /// const struct method_list_t *optionalClassMethods; |
| 4727 | /// const struct _prop_list_t * properties; |
| 4728 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 4729 | /// const uint32_t flags; // = 0 |
| 4730 | /// } |
| 4731 | /// @endcode |
| 4732 | /// |
| 4733 | |
| 4734 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
| 4735 | const ObjCProtocolDecl *PD) { |
| 4736 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4737 | |
| 4738 | // Early exit if a defining object has already been generated. |
| 4739 | if (Entry && Entry->hasInitializer()) |
| 4740 | return Entry; |
| 4741 | |
| 4742 | const char *ProtocolName = PD->getNameAsCString(); |
| 4743 | |
| 4744 | // Construct method lists. |
| 4745 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 4746 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4747 | for (ObjCProtocolDecl::instmeth_iterator |
| 4748 | i = PD->instmeth_begin(CGM.getContext()), |
| 4749 | e = PD->instmeth_end(CGM.getContext()); |
| 4750 | i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4751 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4752 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4753 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4754 | OptInstanceMethods.push_back(C); |
| 4755 | } else { |
| 4756 | InstanceMethods.push_back(C); |
| 4757 | } |
| 4758 | } |
| 4759 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4760 | for (ObjCProtocolDecl::classmeth_iterator |
| 4761 | i = PD->classmeth_begin(CGM.getContext()), |
| 4762 | e = PD->classmeth_end(CGM.getContext()); |
| 4763 | i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4764 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4765 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4766 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4767 | OptClassMethods.push_back(C); |
| 4768 | } else { |
| 4769 | ClassMethods.push_back(C); |
| 4770 | } |
| 4771 | } |
| 4772 | |
| 4773 | std::vector<llvm::Constant*> Values(10); |
| 4774 | // isa is NULL |
| 4775 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
| 4776 | Values[1] = GetClassName(PD->getIdentifier()); |
| 4777 | Values[2] = EmitProtocolList( |
| 4778 | "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(), |
| 4779 | PD->protocol_begin(), |
| 4780 | PD->protocol_end()); |
| 4781 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4782 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4783 | + PD->getNameAsString(), |
| 4784 | "__DATA, __objc_const", |
| 4785 | InstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4786 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4787 | + PD->getNameAsString(), |
| 4788 | "__DATA, __objc_const", |
| 4789 | ClassMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4790 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4791 | + PD->getNameAsString(), |
| 4792 | "__DATA, __objc_const", |
| 4793 | OptInstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4794 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4795 | + PD->getNameAsString(), |
| 4796 | "__DATA, __objc_const", |
| 4797 | OptClassMethods); |
| 4798 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(), |
| 4799 | 0, PD, ObjCTypes); |
| 4800 | uint32_t Size = |
| 4801 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy); |
| 4802 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4803 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
| 4804 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
| 4805 | Values); |
| 4806 | |
| 4807 | if (Entry) { |
| 4808 | // Already created, fix the linkage and update the initializer. |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4809 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4810 | Entry->setInitializer(Init); |
| 4811 | } else { |
| 4812 | Entry = |
| 4813 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4814 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4815 | Init, |
| 4816 | std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName, |
| 4817 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4818 | Entry->setAlignment( |
| 4819 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4820 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4821 | } |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4822 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4823 | |
| 4824 | // Use this protocol meta-data to build protocol list table in section |
| 4825 | // __DATA, __objc_protolist |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4826 | llvm::GlobalVariable *PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4827 | ObjCTypes.ProtocolnfABIPtrTy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4828 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4829 | Entry, |
| 4830 | std::string("\01l_OBJC_LABEL_PROTOCOL_$_") |
| 4831 | +ProtocolName, |
| 4832 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4833 | PTGV->setAlignment( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4834 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 4835 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4836 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4837 | UsedGlobals.push_back(PTGV); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4838 | return Entry; |
| 4839 | } |
| 4840 | |
| 4841 | /// EmitProtocolList - Generate protocol list meta-data: |
| 4842 | /// @code |
| 4843 | /// struct _protocol_list_t { |
| 4844 | /// long protocol_count; // Note, this is 32/64 bit |
| 4845 | /// struct _protocol_t[protocol_count]; |
| 4846 | /// } |
| 4847 | /// @endcode |
| 4848 | /// |
| 4849 | llvm::Constant * |
| 4850 | CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name, |
| 4851 | ObjCProtocolDecl::protocol_iterator begin, |
| 4852 | ObjCProtocolDecl::protocol_iterator end) { |
| 4853 | std::vector<llvm::Constant*> ProtocolRefs; |
| 4854 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4855 | // Just return null for empty protocol lists |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4856 | if (begin == end) |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4857 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4858 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4859 | // FIXME: We shouldn't need to do this lookup here, should we? |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4860 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
| 4861 | if (GV) |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4862 | return llvm::ConstantExpr::getBitCast(GV, |
| 4863 | ObjCTypes.ProtocolListnfABIPtrTy); |
| 4864 | |
| 4865 | for (; begin != end; ++begin) |
| 4866 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 4867 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4868 | // This list is null terminated. |
| 4869 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4870 | ObjCTypes.ProtocolnfABIPtrTy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4871 | |
| 4872 | std::vector<llvm::Constant*> Values(2); |
| 4873 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 4874 | Values[1] = |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4875 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4876 | ProtocolRefs.size()), |
| 4877 | ProtocolRefs); |
| 4878 | |
| 4879 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4880 | GV = new llvm::GlobalVariable(Init->getType(), false, |
| 4881 | llvm::GlobalValue::InternalLinkage, |
| 4882 | Init, |
| 4883 | Name, |
| 4884 | &CGM.getModule()); |
| 4885 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4886 | GV->setAlignment( |
| 4887 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4888 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4889 | return llvm::ConstantExpr::getBitCast(GV, |
| 4890 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4891 | } |
| 4892 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4893 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 4894 | /// struct _objc_method { |
| 4895 | /// SEL _cmd; |
| 4896 | /// char *method_type; |
| 4897 | /// char *_imp; |
| 4898 | /// } |
| 4899 | |
| 4900 | llvm::Constant * |
| 4901 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 4902 | std::vector<llvm::Constant*> Desc(3); |
| 4903 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4904 | ObjCTypes.SelectorPtrTy); |
| 4905 | Desc[1] = GetMethodVarType(MD); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4906 | // Protocol methods have no implementation. So, this entry is always NULL. |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4907 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 4908 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
| 4909 | } |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4910 | |
| 4911 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 4912 | /// This code gen. amounts to generating code for: |
| 4913 | /// @code |
| 4914 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 4915 | /// @encode |
| 4916 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4917 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4918 | CodeGen::CodeGenFunction &CGF, |
| 4919 | QualType ObjectTy, |
| 4920 | llvm::Value *BaseValue, |
| 4921 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4922 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 4923 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4924 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 4925 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4926 | } |
| 4927 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4928 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
| 4929 | CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 4930 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4931 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 4932 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar), |
| 4933 | false, "ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4934 | } |
| 4935 | |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4936 | CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend( |
| 4937 | CodeGen::CodeGenFunction &CGF, |
| 4938 | QualType ResultType, |
| 4939 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4940 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4941 | QualType Arg0Ty, |
| 4942 | bool IsSuper, |
| 4943 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4944 | // FIXME. Even though IsSuper is passes. This function doese not |
| 4945 | // handle calls to 'super' receivers. |
| 4946 | CodeGenTypes &Types = CGM.getTypes(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4947 | llvm::Value *Arg0 = Receiver; |
| 4948 | if (!IsSuper) |
| 4949 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4950 | |
| 4951 | // Find the message function name. |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 4952 | // FIXME. This is too much work to get the ABI-specific result type |
| 4953 | // needed to find the message name. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4954 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, |
| 4955 | llvm::SmallVector<QualType, 16>()); |
| 4956 | llvm::Constant *Fn; |
| 4957 | std::string Name("\01l_"); |
| 4958 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4959 | #if 0 |
| 4960 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4961 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4962 | Fn = ObjCTypes.getMessageSendIdStretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4963 | // FIXME. Is there a better way of getting these names. |
| 4964 | // They are available in RuntimeFunctions vector pair. |
| 4965 | Name += "objc_msgSendId_stret_fixup"; |
| 4966 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4967 | else |
| 4968 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4969 | if (IsSuper) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4970 | Fn = ObjCTypes.getMessageSendSuper2StretFixupFn(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4971 | Name += "objc_msgSendSuper2_stret_fixup"; |
| 4972 | } |
| 4973 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4974 | { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4975 | Fn = ObjCTypes.getMessageSendStretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4976 | Name += "objc_msgSend_stret_fixup"; |
| 4977 | } |
| 4978 | } |
Fariborz Jahanian | 1a6b368 | 2009-02-05 19:35:43 +0000 | [diff] [blame] | 4979 | else if (ResultType->isFloatingType() && |
| 4980 | // Selection of frret API only happens in 32bit nonfragile ABI. |
| 4981 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4982 | Fn = ObjCTypes.getMessageSendFpretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4983 | Name += "objc_msgSend_fpret_fixup"; |
| 4984 | } |
| 4985 | else { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4986 | #if 0 |
| 4987 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4988 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4989 | Fn = ObjCTypes.getMessageSendIdFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4990 | Name += "objc_msgSendId_fixup"; |
| 4991 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4992 | else |
| 4993 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4994 | if (IsSuper) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 4995 | Fn = ObjCTypes.getMessageSendSuper2FixupFn(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4996 | Name += "objc_msgSendSuper2_fixup"; |
| 4997 | } |
| 4998 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4999 | { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5000 | Fn = ObjCTypes.getMessageSendFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5001 | Name += "objc_msgSend_fixup"; |
| 5002 | } |
| 5003 | } |
| 5004 | Name += '_'; |
| 5005 | std::string SelName(Sel.getAsString()); |
| 5006 | // Replace all ':' in selector name with '_' ouch! |
| 5007 | for(unsigned i = 0; i < SelName.size(); i++) |
| 5008 | if (SelName[i] == ':') |
| 5009 | SelName[i] = '_'; |
| 5010 | Name += SelName; |
| 5011 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5012 | if (!GV) { |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5013 | // Build message ref table entry. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5014 | std::vector<llvm::Constant*> Values(2); |
| 5015 | Values[0] = Fn; |
| 5016 | Values[1] = GetMethodVarName(Sel); |
| 5017 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 5018 | GV = new llvm::GlobalVariable(Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 5019 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5020 | Init, |
| 5021 | Name, |
| 5022 | &CGM.getModule()); |
| 5023 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | f59c1a6 | 2009-04-15 19:04:46 +0000 | [diff] [blame] | 5024 | GV->setAlignment(16); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5025 | GV->setSection("__DATA, __objc_msgrefs, coalesced"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5026 | } |
| 5027 | llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5028 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5029 | CallArgList ActualArgs; |
| 5030 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 5031 | ActualArgs.push_back(std::make_pair(RValue::get(Arg1), |
| 5032 | ObjCTypes.MessageRefCPtrTy)); |
| 5033 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5034 | const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs); |
| 5035 | llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0); |
| 5036 | Callee = CGF.Builder.CreateLoad(Callee); |
Fariborz Jahanian | 3ab75bd | 2009-02-14 21:25:36 +0000 | [diff] [blame] | 5037 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5038 | Callee = CGF.Builder.CreateBitCast(Callee, |
| 5039 | llvm::PointerType::getUnqual(FTy)); |
| 5040 | return CGF.EmitCall(FnInfo1, Callee, ActualArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5041 | } |
| 5042 | |
| 5043 | /// Generate code for a message send expression in the nonfragile abi. |
| 5044 | CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend( |
| 5045 | CodeGen::CodeGenFunction &CGF, |
| 5046 | QualType ResultType, |
| 5047 | Selector Sel, |
| 5048 | llvm::Value *Receiver, |
| 5049 | bool IsClassMessage, |
| 5050 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5051 | return EmitMessageSend(CGF, ResultType, Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5052 | Receiver, CGF.getContext().getObjCIdType(), |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5053 | false, CallArgs); |
| 5054 | } |
| 5055 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5056 | llvm::GlobalVariable * |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5057 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5058 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5059 | |
Daniel Dunbar | dfff230 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 5060 | if (!GV) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5061 | GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 5062 | llvm::GlobalValue::ExternalLinkage, |
| 5063 | 0, Name, &CGM.getModule()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5064 | } |
| 5065 | |
| 5066 | return GV; |
| 5067 | } |
| 5068 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5069 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5070 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5071 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 5072 | |
| 5073 | if (!Entry) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5074 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5075 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5076 | Entry = |
| 5077 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5078 | llvm::GlobalValue::InternalLinkage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5079 | ClassGV, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5080 | "\01L_OBJC_CLASSLIST_REFERENCES_$_", |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5081 | &CGM.getModule()); |
| 5082 | Entry->setAlignment( |
| 5083 | CGM.getTargetData().getPrefTypeAlignment( |
| 5084 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5085 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
| 5086 | UsedGlobals.push_back(Entry); |
| 5087 | } |
| 5088 | |
| 5089 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5090 | } |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5091 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5092 | llvm::Value * |
| 5093 | CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder, |
| 5094 | const ObjCInterfaceDecl *ID) { |
| 5095 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
| 5096 | |
| 5097 | if (!Entry) { |
| 5098 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5099 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
| 5100 | Entry = |
| 5101 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5102 | llvm::GlobalValue::InternalLinkage, |
| 5103 | ClassGV, |
| 5104 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 5105 | &CGM.getModule()); |
| 5106 | Entry->setAlignment( |
| 5107 | CGM.getTargetData().getPrefTypeAlignment( |
| 5108 | ObjCTypes.ClassnfABIPtrTy)); |
| 5109 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5110 | UsedGlobals.push_back(Entry); |
| 5111 | } |
| 5112 | |
| 5113 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5114 | } |
| 5115 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5116 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 5117 | /// meta-data |
| 5118 | /// |
| 5119 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 5120 | const ObjCInterfaceDecl *ID) { |
| 5121 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 5122 | if (Entry) |
| 5123 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5124 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5125 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5126 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5127 | Entry = |
| 5128 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5129 | llvm::GlobalValue::InternalLinkage, |
| 5130 | MetaClassGV, |
| 5131 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 5132 | &CGM.getModule()); |
| 5133 | Entry->setAlignment( |
| 5134 | CGM.getTargetData().getPrefTypeAlignment( |
| 5135 | ObjCTypes.ClassnfABIPtrTy)); |
| 5136 | |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5137 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5138 | UsedGlobals.push_back(Entry); |
| 5139 | |
| 5140 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5141 | } |
| 5142 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5143 | /// GetClass - Return a reference to the class for the given interface |
| 5144 | /// decl. |
| 5145 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 5146 | const ObjCInterfaceDecl *ID) { |
| 5147 | return EmitClassRef(Builder, ID); |
| 5148 | } |
| 5149 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5150 | /// Generates a message send where the super is the receiver. This is |
| 5151 | /// a message send to self with special delivery semantics indicating |
| 5152 | /// which class's method should be called. |
| 5153 | CodeGen::RValue |
| 5154 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 5155 | QualType ResultType, |
| 5156 | Selector Sel, |
| 5157 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5158 | bool isCategoryImpl, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5159 | llvm::Value *Receiver, |
| 5160 | bool IsClassMessage, |
| 5161 | const CodeGen::CallArgList &CallArgs) { |
| 5162 | // ... |
| 5163 | // Create and init a super structure; this is a (receiver, class) |
| 5164 | // pair we will pass to objc_msgSendSuper. |
| 5165 | llvm::Value *ObjCSuper = |
| 5166 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 5167 | |
| 5168 | llvm::Value *ReceiverAsObject = |
| 5169 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 5170 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 5171 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
| 5172 | |
| 5173 | // 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] | 5174 | llvm::Value *Target; |
| 5175 | if (IsClassMessage) { |
| 5176 | if (isCategoryImpl) { |
| 5177 | // Message sent to "super' in a class method defined in |
| 5178 | // a category implementation. |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5179 | Target = EmitClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5180 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 5181 | Target = CGF.Builder.CreateLoad(Target); |
| 5182 | } |
| 5183 | else |
| 5184 | Target = EmitMetaClassRef(CGF.Builder, Class); |
| 5185 | } |
| 5186 | else |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5187 | Target = EmitSuperClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5188 | |
| 5189 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 5190 | // and ObjCTypes types. |
| 5191 | const llvm::Type *ClassTy = |
| 5192 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 5193 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 5194 | CGF.Builder.CreateStore(Target, |
| 5195 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 5196 | |
| 5197 | return EmitMessageSend(CGF, ResultType, Sel, |
| 5198 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 5199 | true, CallArgs); |
| 5200 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5201 | |
| 5202 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
| 5203 | Selector Sel) { |
| 5204 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 5205 | |
| 5206 | if (!Entry) { |
| 5207 | llvm::Constant *Casted = |
| 5208 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 5209 | ObjCTypes.SelectorPtrTy); |
| 5210 | Entry = |
| 5211 | new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false, |
| 5212 | llvm::GlobalValue::InternalLinkage, |
| 5213 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_", |
| 5214 | &CGM.getModule()); |
| 5215 | Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip"); |
| 5216 | UsedGlobals.push_back(Entry); |
| 5217 | } |
| 5218 | |
| 5219 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5220 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5221 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 5222 | /// objc_assign_ivar (id src, id *dst) |
| 5223 | /// |
| 5224 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 5225 | llvm::Value *src, llvm::Value *dst) |
| 5226 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5227 | const llvm::Type * SrcTy = src->getType(); |
| 5228 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5229 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5230 | assert(Size <= 8 && "does not support size > 8"); |
| 5231 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5232 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5233 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5234 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5235 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5236 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5237 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5238 | src, dst, "assignivar"); |
| 5239 | return; |
| 5240 | } |
| 5241 | |
| 5242 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 5243 | /// objc_assign_strongCast (id src, id *dst) |
| 5244 | /// |
| 5245 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
| 5246 | CodeGen::CodeGenFunction &CGF, |
| 5247 | llvm::Value *src, llvm::Value *dst) |
| 5248 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5249 | const llvm::Type * SrcTy = src->getType(); |
| 5250 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5251 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5252 | assert(Size <= 8 && "does not support size > 8"); |
| 5253 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5254 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5255 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5256 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5257 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5258 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5259 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5260 | src, dst, "weakassign"); |
| 5261 | return; |
| 5262 | } |
| 5263 | |
| 5264 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 5265 | /// object: objc_read_weak (id *src) |
| 5266 | /// |
| 5267 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
| 5268 | CodeGen::CodeGenFunction &CGF, |
| 5269 | llvm::Value *AddrWeakObj) |
| 5270 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5271 | const llvm::Type* DestTy = |
| 5272 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5273 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 5274 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5275 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5276 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5277 | return read_weak; |
| 5278 | } |
| 5279 | |
| 5280 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 5281 | /// objc_assign_weak (id src, id *dst) |
| 5282 | /// |
| 5283 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 5284 | llvm::Value *src, llvm::Value *dst) |
| 5285 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5286 | const llvm::Type * SrcTy = src->getType(); |
| 5287 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5288 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5289 | assert(Size <= 8 && "does not support size > 8"); |
| 5290 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5291 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5292 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5293 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5294 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5295 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 5296 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5297 | src, dst, "weakassign"); |
| 5298 | return; |
| 5299 | } |
| 5300 | |
| 5301 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 5302 | /// objc_assign_global (id src, id *dst) |
| 5303 | /// |
| 5304 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 5305 | llvm::Value *src, llvm::Value *dst) |
| 5306 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5307 | const llvm::Type * SrcTy = src->getType(); |
| 5308 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5309 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5310 | assert(Size <= 8 && "does not support size > 8"); |
| 5311 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5312 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5313 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5314 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5315 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5316 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5317 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5318 | src, dst, "globalassign"); |
| 5319 | return; |
| 5320 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5321 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5322 | void |
| 5323 | CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 5324 | const Stmt &S) { |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5325 | bool isTry = isa<ObjCAtTryStmt>(S); |
| 5326 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 5327 | llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5328 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5329 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5330 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5331 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
| 5332 | |
| 5333 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 5334 | // evaluation of the expression must occur before we enter the |
| 5335 | // @synchronized. We can safely avoid a temp here because jumps into |
| 5336 | // @synchronized are illegal & this will dominate uses. |
| 5337 | llvm::Value *SyncArg = 0; |
| 5338 | if (!isTry) { |
| 5339 | SyncArg = |
| 5340 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 5341 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5342 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5343 | } |
| 5344 | |
| 5345 | // Push an EH context entry, used for handling rethrows and jumps |
| 5346 | // through finally. |
| 5347 | CGF.PushCleanupBlock(FinallyBlock); |
| 5348 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5349 | CGF.setInvokeDest(TryHandler); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5350 | |
| 5351 | CGF.EmitBlock(TryBlock); |
| 5352 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 5353 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
| 5354 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5355 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5356 | // Emit the exception handler. |
| 5357 | |
| 5358 | CGF.EmitBlock(TryHandler); |
| 5359 | |
| 5360 | llvm::Value *llvm_eh_exception = |
| 5361 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
| 5362 | llvm::Value *llvm_eh_selector_i64 = |
| 5363 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64); |
| 5364 | llvm::Value *llvm_eh_typeid_for_i64 = |
| 5365 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64); |
| 5366 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5367 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow"); |
| 5368 | |
| 5369 | llvm::SmallVector<llvm::Value*, 8> SelectorArgs; |
| 5370 | SelectorArgs.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5371 | SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5372 | |
| 5373 | // Construct the lists of (type, catch body) to handle. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5374 | llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5375 | bool HasCatchAll = false; |
| 5376 | if (isTry) { |
| 5377 | if (const ObjCAtCatchStmt* CatchStmt = |
| 5378 | cast<ObjCAtTryStmt>(S).getCatchStmts()) { |
| 5379 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5380 | const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5381 | Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody())); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5382 | |
| 5383 | // catch(...) always matches. |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5384 | if (!CatchDecl) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5385 | // Use i8* null here to signal this is a catch all, not a cleanup. |
| 5386 | llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 5387 | SelectorArgs.push_back(Null); |
| 5388 | HasCatchAll = true; |
| 5389 | break; |
| 5390 | } |
| 5391 | |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5392 | if (CGF.getContext().isObjCIdType(CatchDecl->getType()) || |
| 5393 | CatchDecl->getType()->isObjCQualifiedIdType()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5394 | llvm::Value *IDEHType = |
| 5395 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 5396 | if (!IDEHType) |
| 5397 | IDEHType = |
| 5398 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5399 | llvm::GlobalValue::ExternalLinkage, |
| 5400 | 0, "OBJC_EHTYPE_id", &CGM.getModule()); |
| 5401 | SelectorArgs.push_back(IDEHType); |
| 5402 | HasCatchAll = true; |
| 5403 | break; |
| 5404 | } |
| 5405 | |
| 5406 | // All other types should be Objective-C interface pointer types. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5407 | const PointerType *PT = CatchDecl->getType()->getAsPointerType(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5408 | assert(PT && "Invalid @catch type."); |
| 5409 | const ObjCInterfaceType *IT = |
| 5410 | PT->getPointeeType()->getAsObjCInterfaceType(); |
| 5411 | assert(IT && "Invalid @catch type."); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5412 | llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5413 | SelectorArgs.push_back(EHType); |
| 5414 | } |
| 5415 | } |
| 5416 | } |
| 5417 | |
| 5418 | // We use a cleanup unless there was already a catch all. |
| 5419 | if (!HasCatchAll) { |
| 5420 | SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5421 | Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5422 | } |
| 5423 | |
| 5424 | llvm::Value *Selector = |
| 5425 | CGF.Builder.CreateCall(llvm_eh_selector_i64, |
| 5426 | SelectorArgs.begin(), SelectorArgs.end(), |
| 5427 | "selector"); |
| 5428 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5429 | const ParmVarDecl *CatchParam = Handlers[i].first; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5430 | const Stmt *CatchBody = Handlers[i].second; |
| 5431 | |
| 5432 | llvm::BasicBlock *Next = 0; |
| 5433 | |
| 5434 | // The last handler always matches. |
| 5435 | if (i + 1 != e) { |
| 5436 | assert(CatchParam && "Only last handler can be a catch all."); |
| 5437 | |
| 5438 | llvm::BasicBlock *Match = CGF.createBasicBlock("match"); |
| 5439 | Next = CGF.createBasicBlock("catch.next"); |
| 5440 | llvm::Value *Id = |
| 5441 | CGF.Builder.CreateCall(llvm_eh_typeid_for_i64, |
| 5442 | CGF.Builder.CreateBitCast(SelectorArgs[i+2], |
| 5443 | ObjCTypes.Int8PtrTy)); |
| 5444 | CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id), |
| 5445 | Match, Next); |
| 5446 | |
| 5447 | CGF.EmitBlock(Match); |
| 5448 | } |
| 5449 | |
| 5450 | if (CatchBody) { |
| 5451 | llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end"); |
| 5452 | llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler"); |
| 5453 | |
| 5454 | // Cleanups must call objc_end_catch. |
| 5455 | // |
| 5456 | // FIXME: It seems incorrect for objc_begin_catch to be inside |
| 5457 | // this context, but this matches gcc. |
| 5458 | CGF.PushCleanupBlock(MatchEnd); |
| 5459 | CGF.setInvokeDest(MatchHandler); |
| 5460 | |
| 5461 | llvm::Value *ExcObject = |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5462 | CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5463 | |
| 5464 | // Bind the catch parameter if it exists. |
| 5465 | if (CatchParam) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5466 | ExcObject = |
| 5467 | CGF.Builder.CreateBitCast(ExcObject, |
| 5468 | CGF.ConvertType(CatchParam->getType())); |
| 5469 | // CatchParam is a ParmVarDecl because of the grammar |
| 5470 | // construction used to handle this, but for codegen purposes |
| 5471 | // we treat this as a local decl. |
| 5472 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
| 5473 | CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5474 | } |
| 5475 | |
| 5476 | CGF.ObjCEHValueStack.push_back(ExcObject); |
| 5477 | CGF.EmitStmt(CatchBody); |
| 5478 | CGF.ObjCEHValueStack.pop_back(); |
| 5479 | |
| 5480 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5481 | |
| 5482 | CGF.EmitBlock(MatchHandler); |
| 5483 | |
| 5484 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5485 | // We are required to emit this call to satisfy LLVM, even |
| 5486 | // though we don't use the result. |
| 5487 | llvm::SmallVector<llvm::Value*, 8> Args; |
| 5488 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5489 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5490 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5491 | 0)); |
| 5492 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5493 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5494 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5495 | |
| 5496 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5497 | |
| 5498 | CGF.EmitBlock(MatchEnd); |
| 5499 | |
| 5500 | // Unfortunately, we also have to generate another EH frame here |
| 5501 | // in case this throws. |
| 5502 | llvm::BasicBlock *MatchEndHandler = |
| 5503 | CGF.createBasicBlock("match.end.handler"); |
| 5504 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5505 | CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5506 | Cont, MatchEndHandler, |
| 5507 | Args.begin(), Args.begin()); |
| 5508 | |
| 5509 | CGF.EmitBlock(Cont); |
| 5510 | if (Info.SwitchBlock) |
| 5511 | CGF.EmitBlock(Info.SwitchBlock); |
| 5512 | if (Info.EndBlock) |
| 5513 | CGF.EmitBlock(Info.EndBlock); |
| 5514 | |
| 5515 | CGF.EmitBlock(MatchEndHandler); |
| 5516 | Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5517 | // We are required to emit this call to satisfy LLVM, even |
| 5518 | // though we don't use the result. |
| 5519 | Args.clear(); |
| 5520 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5521 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5522 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5523 | 0)); |
| 5524 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5525 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5526 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5527 | |
| 5528 | if (Next) |
| 5529 | CGF.EmitBlock(Next); |
| 5530 | } else { |
| 5531 | assert(!Next && "catchup should be last handler."); |
| 5532 | |
| 5533 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5534 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5535 | } |
| 5536 | } |
| 5537 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5538 | // Pop the cleanup entry, the @finally is outside this cleanup |
| 5539 | // scope. |
| 5540 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5541 | CGF.setInvokeDest(PrevLandingPad); |
| 5542 | |
| 5543 | CGF.EmitBlock(FinallyBlock); |
| 5544 | |
| 5545 | if (isTry) { |
| 5546 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 5547 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 5548 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 5549 | } else { |
| 5550 | // Emit 'objc_sync_exit(expr)' as finally's sole statement for |
| 5551 | // @synchronized. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5552 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5553 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5554 | |
| 5555 | if (Info.SwitchBlock) |
| 5556 | CGF.EmitBlock(Info.SwitchBlock); |
| 5557 | if (Info.EndBlock) |
| 5558 | CGF.EmitBlock(Info.EndBlock); |
| 5559 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5560 | // Branch around the rethrow code. |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5561 | CGF.EmitBranch(FinallyEnd); |
| 5562 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5563 | CGF.EmitBlock(FinallyRethrow); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5564 | CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5565 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5566 | CGF.Builder.CreateUnreachable(); |
| 5567 | |
| 5568 | CGF.EmitBlock(FinallyEnd); |
| 5569 | } |
| 5570 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5571 | /// EmitThrowStmt - Generate code for a throw statement. |
| 5572 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 5573 | const ObjCAtThrowStmt &S) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5574 | llvm::Value *Exception; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5575 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5576 | Exception = CGF.EmitScalarExpr(ThrowExpr); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5577 | } else { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5578 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
| 5579 | "Unexpected rethrow outside @catch block."); |
| 5580 | Exception = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5581 | } |
| 5582 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5583 | llvm::Value *ExceptionAsObject = |
| 5584 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 5585 | llvm::BasicBlock *InvokeDest = CGF.getInvokeDest(); |
| 5586 | if (InvokeDest) { |
| 5587 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5588 | CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5589 | Cont, InvokeDest, |
| 5590 | &ExceptionAsObject, &ExceptionAsObject + 1); |
| 5591 | CGF.EmitBlock(Cont); |
| 5592 | } else |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5593 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5594 | CGF.Builder.CreateUnreachable(); |
| 5595 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5596 | // Clear the insertion point to indicate we are in unreachable code. |
| 5597 | CGF.Builder.ClearInsertionPoint(); |
| 5598 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5599 | |
| 5600 | llvm::Value * |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5601 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 5602 | bool ForDefinition) { |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5603 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5604 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5605 | // If we don't need a definition, return the entry if found or check |
| 5606 | // if we use an external reference. |
| 5607 | if (!ForDefinition) { |
| 5608 | if (Entry) |
| 5609 | return Entry; |
Daniel Dunbar | 7e075cb | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 5610 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5611 | // If this type (or a super class) has the __objc_exception__ |
| 5612 | // attribute, emit an external reference. |
| 5613 | if (hasObjCExceptionAttribute(ID)) |
| 5614 | return Entry = |
| 5615 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5616 | llvm::GlobalValue::ExternalLinkage, |
| 5617 | 0, |
| 5618 | (std::string("OBJC_EHTYPE_$_") + |
| 5619 | ID->getIdentifier()->getName()), |
| 5620 | &CGM.getModule()); |
| 5621 | } |
| 5622 | |
| 5623 | // Otherwise we need to either make a new entry or fill in the |
| 5624 | // initializer. |
| 5625 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5626 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5627 | std::string VTableName = "objc_ehtype_vtable"; |
| 5628 | llvm::GlobalVariable *VTableGV = |
| 5629 | CGM.getModule().getGlobalVariable(VTableName); |
| 5630 | if (!VTableGV) |
| 5631 | VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false, |
| 5632 | llvm::GlobalValue::ExternalLinkage, |
| 5633 | 0, VTableName, &CGM.getModule()); |
| 5634 | |
| 5635 | llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2); |
| 5636 | |
| 5637 | std::vector<llvm::Constant*> Values(3); |
| 5638 | Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1); |
| 5639 | Values[1] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5640 | Values[2] = GetClassGlobal(ClassName); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5641 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
| 5642 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5643 | if (Entry) { |
| 5644 | Entry->setInitializer(Init); |
| 5645 | } else { |
| 5646 | Entry = new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5647 | llvm::GlobalValue::WeakAnyLinkage, |
| 5648 | Init, |
| 5649 | (std::string("OBJC_EHTYPE_$_") + |
| 5650 | ID->getIdentifier()->getName()), |
| 5651 | &CGM.getModule()); |
| 5652 | } |
| 5653 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 5654 | if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden) |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5655 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5656 | Entry->setAlignment(8); |
| 5657 | |
| 5658 | if (ForDefinition) { |
| 5659 | Entry->setSection("__DATA,__objc_const"); |
| 5660 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 5661 | } else { |
| 5662 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 5663 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5664 | |
| 5665 | return Entry; |
| 5666 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5667 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5668 | /* *** */ |
| 5669 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5670 | CodeGen::CGObjCRuntime * |
| 5671 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 5672 | return new CGObjCMac(CGM); |
| 5673 | } |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5674 | |
| 5675 | CodeGen::CGObjCRuntime * |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5676 | CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5677 | return new CGObjCNonFragileABIMac(CGM); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5678 | } |