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 | |
| 32 | namespace { |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 33 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 34 | typedef std::vector<llvm::Constant*> ConstantVector; |
| 35 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 36 | // FIXME: We should find a nicer way to make the labels for |
| 37 | // metadata, string concatenation is lame. |
| 38 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 39 | class ObjCCommonTypesHelper { |
| 40 | protected: |
| 41 | CodeGen::CodeGenModule &CGM; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 42 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 43 | public: |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 44 | const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 45 | const llvm::Type *Int8PtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 46 | |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 47 | /// ObjectPtrTy - LLVM type for object handles (typeof(id)) |
| 48 | const llvm::Type *ObjectPtrTy; |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 49 | |
| 50 | /// PtrObjectPtrTy - LLVM type for id * |
| 51 | const llvm::Type *PtrObjectPtrTy; |
| 52 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 53 | /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL)) |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 54 | const llvm::Type *SelectorPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 55 | /// ProtocolPtrTy - LLVM type for external protocol handles |
| 56 | /// (typeof(Protocol)) |
| 57 | const llvm::Type *ExternalProtocolPtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 58 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 59 | // SuperCTy - clang type for struct objc_super. |
| 60 | QualType SuperCTy; |
| 61 | // SuperPtrCTy - clang type for struct objc_super *. |
| 62 | QualType SuperPtrCTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 63 | |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 64 | /// SuperTy - LLVM type for struct objc_super. |
| 65 | const llvm::StructType *SuperTy; |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 66 | /// SuperPtrTy - LLVM type for struct objc_super *. |
| 67 | const llvm::Type *SuperPtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 68 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 69 | /// PropertyTy - LLVM type for struct objc_property (struct _prop_t |
| 70 | /// in GCC parlance). |
| 71 | const llvm::StructType *PropertyTy; |
| 72 | |
| 73 | /// PropertyListTy - LLVM type for struct objc_property_list |
| 74 | /// (_prop_list_t in GCC parlance). |
| 75 | const llvm::StructType *PropertyListTy; |
| 76 | /// PropertyListPtrTy - LLVM type for struct objc_property_list*. |
| 77 | const llvm::Type *PropertyListPtrTy; |
| 78 | |
| 79 | // MethodTy - LLVM type for struct objc_method. |
| 80 | const llvm::StructType *MethodTy; |
| 81 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 82 | /// CacheTy - LLVM type for struct objc_cache. |
| 83 | const llvm::Type *CacheTy; |
| 84 | /// CachePtrTy - LLVM type for struct objc_cache *. |
| 85 | const llvm::Type *CachePtrTy; |
| 86 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 87 | llvm::Constant *GetPropertyFn, *SetPropertyFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 89 | llvm::Constant *EnumerationMutationFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 90 | |
| 91 | /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 92 | llvm::Constant *GcReadWeakFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 93 | |
| 94 | /// GcAssignWeakFn -- LLVM objc_assign_weak function. |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 95 | llvm::Constant *getGcAssignWeakFn() { |
| 96 | // id objc_assign_weak (id, id *) |
| 97 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 98 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 99 | llvm::FunctionType *FTy = |
| 100 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 101 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 102 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 103 | |
| 104 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 105 | llvm::Constant *GcAssignGlobalFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 106 | |
| 107 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 108 | llvm::Constant *GcAssignIvarFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 109 | |
| 110 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 111 | llvm::Constant *GcAssignStrongCastFn; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 112 | |
| 113 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 114 | llvm::Constant *ExceptionThrowFn; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 115 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 116 | /// SyncEnterFn - LLVM object_sync_enter function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 117 | llvm::Constant *getSyncEnterFn() { |
| 118 | // void objc_sync_enter (id) |
| 119 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 120 | llvm::FunctionType *FTy = |
| 121 | llvm::FunctionType::get(llvm::Type::VoidTy, Args, false); |
| 122 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 123 | } |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 124 | |
| 125 | /// SyncExitFn - LLVM object_sync_exit function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 126 | llvm::Constant *SyncExitFn; |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 127 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 128 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
| 129 | ~ObjCCommonTypesHelper(){} |
| 130 | }; |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 131 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 132 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 133 | /// construction of varies types used during ObjC generation. |
| 134 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
| 135 | private: |
| 136 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 137 | llvm::Constant *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn; |
| 138 | llvm::Constant *MessageSendSuperFn, *MessageSendSuperStretFn, |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 139 | *MessageSendSuperFpretFn; |
| 140 | |
| 141 | public: |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 142 | /// SymtabTy - LLVM type for struct objc_symtab. |
| 143 | const llvm::StructType *SymtabTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 144 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
| 145 | const llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 146 | /// ModuleTy - LLVM type for struct objc_module. |
| 147 | const llvm::StructType *ModuleTy; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 148 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 149 | /// ProtocolTy - LLVM type for struct objc_protocol. |
| 150 | const llvm::StructType *ProtocolTy; |
| 151 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
| 152 | const llvm::Type *ProtocolPtrTy; |
| 153 | /// ProtocolExtensionTy - LLVM type for struct |
| 154 | /// objc_protocol_extension. |
| 155 | const llvm::StructType *ProtocolExtensionTy; |
| 156 | /// ProtocolExtensionTy - LLVM type for struct |
| 157 | /// objc_protocol_extension *. |
| 158 | const llvm::Type *ProtocolExtensionPtrTy; |
| 159 | /// MethodDescriptionTy - LLVM type for struct |
| 160 | /// objc_method_description. |
| 161 | const llvm::StructType *MethodDescriptionTy; |
| 162 | /// MethodDescriptionListTy - LLVM type for struct |
| 163 | /// objc_method_description_list. |
| 164 | const llvm::StructType *MethodDescriptionListTy; |
| 165 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 166 | /// objc_method_description_list *. |
| 167 | const llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 168 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
| 169 | const llvm::Type *ProtocolListTy; |
| 170 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
| 171 | const llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 172 | /// CategoryTy - LLVM type for struct objc_category. |
| 173 | const llvm::StructType *CategoryTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 174 | /// ClassTy - LLVM type for struct objc_class. |
| 175 | const llvm::StructType *ClassTy; |
| 176 | /// ClassPtrTy - LLVM type for struct objc_class *. |
| 177 | const llvm::Type *ClassPtrTy; |
| 178 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
| 179 | const llvm::StructType *ClassExtensionTy; |
| 180 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
| 181 | const llvm::Type *ClassExtensionPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 182 | // IvarTy - LLVM type for struct objc_ivar. |
| 183 | const llvm::StructType *IvarTy; |
| 184 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
| 185 | const llvm::Type *IvarListTy; |
| 186 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
| 187 | const llvm::Type *IvarListPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 188 | /// MethodListTy - LLVM type for struct objc_method_list. |
| 189 | const llvm::Type *MethodListTy; |
| 190 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
| 191 | const llvm::Type *MethodListPtrTy; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 192 | |
| 193 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
| 194 | const llvm::Type *ExceptionDataTy; |
| 195 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 196 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 197 | llvm::Constant *ExceptionTryEnterFn; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 198 | |
| 199 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 200 | llvm::Constant *ExceptionTryExitFn; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 201 | |
| 202 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 203 | llvm::Constant *ExceptionExtractFn; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 204 | |
| 205 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 206 | llvm::Constant *ExceptionMatchFn; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 207 | |
| 208 | /// SetJmpFn - LLVM _setjmp function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 209 | llvm::Constant *SetJmpFn; |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 210 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 211 | public: |
| 212 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 213 | ~ObjCTypesHelper() {} |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 214 | |
| 215 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 216 | llvm::Constant *getSendFn(bool IsSuper) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 217 | return IsSuper ? MessageSendSuperFn : MessageSendFn; |
| 218 | } |
| 219 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 220 | llvm::Constant *getSendStretFn(bool IsSuper) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 221 | return IsSuper ? MessageSendSuperStretFn : MessageSendStretFn; |
| 222 | } |
| 223 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 224 | llvm::Constant *getSendFpretFn(bool IsSuper) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 225 | return IsSuper ? MessageSendSuperFpretFn : MessageSendFpretFn; |
| 226 | } |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 227 | }; |
| 228 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 229 | /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 230 | /// modern abi |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 231 | class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 232 | public: |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 233 | llvm::Constant *MessageSendFixupFn, *MessageSendFpretFixupFn, |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 234 | *MessageSendStretFixupFn, *MessageSendIdFixupFn, |
| 235 | *MessageSendIdStretFixupFn, *MessageSendSuper2FixupFn, |
| 236 | *MessageSendSuper2StretFixupFn; |
| 237 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 238 | // MethodListnfABITy - LLVM for struct _method_list_t |
| 239 | const llvm::StructType *MethodListnfABITy; |
| 240 | |
| 241 | // MethodListnfABIPtrTy - LLVM for struct _method_list_t* |
| 242 | const llvm::Type *MethodListnfABIPtrTy; |
| 243 | |
| 244 | // ProtocolnfABITy = LLVM for struct _protocol_t |
| 245 | const llvm::StructType *ProtocolnfABITy; |
| 246 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 247 | // ProtocolnfABIPtrTy = LLVM for struct _protocol_t* |
| 248 | const llvm::Type *ProtocolnfABIPtrTy; |
| 249 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 250 | // ProtocolListnfABITy - LLVM for struct _objc_protocol_list |
| 251 | const llvm::StructType *ProtocolListnfABITy; |
| 252 | |
| 253 | // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list* |
| 254 | const llvm::Type *ProtocolListnfABIPtrTy; |
| 255 | |
| 256 | // ClassnfABITy - LLVM for struct _class_t |
| 257 | const llvm::StructType *ClassnfABITy; |
| 258 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 259 | // ClassnfABIPtrTy - LLVM for struct _class_t* |
| 260 | const llvm::Type *ClassnfABIPtrTy; |
| 261 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 262 | // IvarnfABITy - LLVM for struct _ivar_t |
| 263 | const llvm::StructType *IvarnfABITy; |
| 264 | |
| 265 | // IvarListnfABITy - LLVM for struct _ivar_list_t |
| 266 | const llvm::StructType *IvarListnfABITy; |
| 267 | |
| 268 | // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t* |
| 269 | const llvm::Type *IvarListnfABIPtrTy; |
| 270 | |
| 271 | // ClassRonfABITy - LLVM for struct _class_ro_t |
| 272 | const llvm::StructType *ClassRonfABITy; |
| 273 | |
| 274 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 275 | const llvm::Type *ImpnfABITy; |
| 276 | |
| 277 | // CategorynfABITy - LLVM for struct _category_t |
| 278 | const llvm::StructType *CategorynfABITy; |
| 279 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 280 | // New types for nonfragile abi messaging. |
| 281 | |
| 282 | // MessageRefTy - LLVM for: |
| 283 | // struct _message_ref_t { |
| 284 | // IMP messenger; |
| 285 | // SEL name; |
| 286 | // }; |
| 287 | const llvm::StructType *MessageRefTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 288 | // MessageRefCTy - clang type for struct _message_ref_t |
| 289 | QualType MessageRefCTy; |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 290 | |
| 291 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 292 | const llvm::Type *MessageRefPtrTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 293 | // MessageRefCPtrTy - clang type for struct _message_ref_t* |
| 294 | QualType MessageRefCPtrTy; |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 295 | |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 296 | // MessengerTy - Type of the messenger (shown as IMP above) |
| 297 | const llvm::FunctionType *MessengerTy; |
| 298 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 299 | // SuperMessageRefTy - LLVM for: |
| 300 | // struct _super_message_ref_t { |
| 301 | // SUPER_IMP messenger; |
| 302 | // SEL name; |
| 303 | // }; |
| 304 | const llvm::StructType *SuperMessageRefTy; |
| 305 | |
| 306 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 307 | const llvm::Type *SuperMessageRefPtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 308 | |
| 309 | /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C |
| 310 | /// exception personality function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 311 | llvm::Value *getEHPersonalityPtr() { |
| 312 | llvm::Constant *Personality = |
| 313 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 314 | std::vector<const llvm::Type*>(), |
| 315 | true), |
| 316 | "__objc_personality_v0"); |
| 317 | return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy); |
| 318 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 319 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 320 | llvm::Constant *UnwindResumeOrRethrowFn, *ObjCBeginCatchFn, *ObjCEndCatchFn; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 321 | |
| 322 | const llvm::StructType *EHTypeTy; |
| 323 | const llvm::Type *EHTypePtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 324 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 325 | ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); |
| 326 | ~ObjCNonFragileABITypesHelper(){} |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 327 | }; |
| 328 | |
| 329 | class CGObjCCommonMac : public CodeGen::CGObjCRuntime { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 330 | public: |
| 331 | // FIXME - accessibility |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 332 | class GC_IVAR { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 333 | public: |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 334 | unsigned int ivar_bytepos; |
| 335 | unsigned int ivar_size; |
| 336 | GC_IVAR() : ivar_bytepos(0), ivar_size(0) {} |
| 337 | }; |
| 338 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 339 | class SKIP_SCAN { |
| 340 | public: |
| 341 | unsigned int skip; |
| 342 | unsigned int scan; |
| 343 | SKIP_SCAN() : skip(0), scan(0) {} |
| 344 | }; |
| 345 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 346 | protected: |
| 347 | CodeGen::CodeGenModule &CGM; |
| 348 | // FIXME! May not be needing this after all. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 349 | unsigned ObjCABI; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 350 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 351 | // gc ivar layout bitmap calculation helper caches. |
| 352 | llvm::SmallVector<GC_IVAR, 16> SkipIvars; |
| 353 | llvm::SmallVector<GC_IVAR, 16> IvarsInfo; |
| 354 | llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 355 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 356 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 357 | /// DefinedSymbols and FinishModule(). |
| 358 | std::set<IdentifierInfo*> LazySymbols; |
| 359 | |
| 360 | /// DefinedSymbols - External symbols which are defined by this |
| 361 | /// module. The symbols in this list and LazySymbols are used to add |
| 362 | /// special linker symbols which ensure that Objective-C modules are |
| 363 | /// linked properly. |
| 364 | std::set<IdentifierInfo*> DefinedSymbols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 365 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 366 | /// ClassNames - uniqued class names. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 367 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 368 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 369 | /// MethodVarNames - uniqued method variable names. |
| 370 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 371 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 372 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 373 | /// a StringMap here because have no other unique reference. |
| 374 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 375 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 376 | /// MethodDefinitions - map of methods which have been defined in |
| 377 | /// this translation unit. |
| 378 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 379 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 380 | /// PropertyNames - uniqued method variable names. |
| 381 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 382 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 383 | /// ClassReferences - uniqued class references. |
| 384 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 385 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 386 | /// SelectorReferences - uniqued selector references. |
| 387 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 388 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 389 | /// Protocols - Protocols for which an objc_protocol structure has |
| 390 | /// been emitted. Forward declarations are handled by creating an |
| 391 | /// empty structure whose initializer is filled in when/if defined. |
| 392 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 393 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 394 | /// DefinedProtocols - Protocols which have actually been |
| 395 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 396 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 397 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 398 | /// DefinedClasses - List of defined classes. |
| 399 | std::vector<llvm::GlobalValue*> DefinedClasses; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 400 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 401 | /// DefinedCategories - List of defined categories. |
| 402 | std::vector<llvm::GlobalValue*> DefinedCategories; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 403 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 404 | /// UsedGlobals - List of globals to pack into the llvm.used metadata |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 405 | /// to prevent them from being clobbered. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 406 | std::vector<llvm::GlobalVariable*> UsedGlobals; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 407 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 408 | /// GetNameForMethod - Return a name for the given method. |
| 409 | /// \param[out] NameOut - The return value. |
| 410 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 411 | const ObjCContainerDecl *CD, |
| 412 | std::string &NameOut); |
| 413 | |
| 414 | /// GetMethodVarName - Return a unique constant for the given |
| 415 | /// selector's name. The return value has type char *. |
| 416 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 417 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
| 418 | llvm::Constant *GetMethodVarName(const std::string &Name); |
| 419 | |
| 420 | /// GetMethodVarType - Return a unique constant for the given |
| 421 | /// selector's name. The return value has type char *. |
| 422 | |
| 423 | // FIXME: This is a horrible name. |
| 424 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame^] | 425 | llvm::Constant *GetMethodVarType(const FieldDecl *D); |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 426 | |
| 427 | /// GetPropertyName - Return a unique constant for the given |
| 428 | /// name. The return value has type char *. |
| 429 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
| 430 | |
| 431 | // FIXME: This can be dropped once string functions are unified. |
| 432 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 433 | const Decl *Container); |
| 434 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 435 | /// GetClassName - Return a unique constant for the given selector's |
| 436 | /// name. The return value has type char *. |
| 437 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
| 438 | |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 439 | /// GetInterfaceDeclStructLayout - Get layout for ivars of given |
| 440 | /// interface declaration. |
| 441 | const llvm::StructLayout *GetInterfaceDeclStructLayout( |
| 442 | const ObjCInterfaceDecl *ID) const; |
| 443 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 444 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 445 | /// implementation for the __strong or __weak case. |
| 446 | /// |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 447 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
| 448 | bool ForStrongLayout); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 449 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 450 | void BuildAggrIvarLayout(const ObjCInterfaceDecl *OI, |
| 451 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 452 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 453 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 454 | unsigned int BytePos, bool ForStrongLayout, |
| 455 | int &Index, int &SkIndex, bool &HasUnion); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 456 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 457 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 458 | /// ivar layout bitmap. |
| 459 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 460 | const ObjCCommonTypesHelper &ObjCTypes); |
| 461 | |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 462 | const RecordDecl *GetFirstIvarInRecord(const ObjCInterfaceDecl *OID, |
| 463 | RecordDecl::field_iterator &FIV, |
| 464 | RecordDecl::field_iterator &PIV); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 465 | /// EmitPropertyList - Emit the given property list. The return |
| 466 | /// value has type PropertyListPtrTy. |
| 467 | llvm::Constant *EmitPropertyList(const std::string &Name, |
| 468 | const Decl *Container, |
| 469 | const ObjCContainerDecl *OCD, |
| 470 | const ObjCCommonTypesHelper &ObjCTypes); |
| 471 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 472 | /// GetProtocolRef - Return a reference to the internal protocol |
| 473 | /// description, creating an empty one if it has not been |
| 474 | /// defined. The return value has type ProtocolPtrTy. |
| 475 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 476 | |
| 477 | /// GetIvarBaseOffset - returns ivars byte offset. |
| 478 | uint64_t GetIvarBaseOffset(const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 479 | const FieldDecl *Field); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 480 | |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 481 | /// GetFieldBaseOffset - return's field byte offset. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 482 | uint64_t GetFieldBaseOffset(const ObjCInterfaceDecl *OI, |
| 483 | const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 484 | const FieldDecl *Field); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 485 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 486 | /// CreateMetadataVar - Create a global variable with internal |
| 487 | /// linkage for use by the Objective-C runtime. |
| 488 | /// |
| 489 | /// This is a convenience wrapper which not only creates the |
| 490 | /// variable, but also sets the section and alignment and adds the |
| 491 | /// global to the UsedGlobals list. |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 492 | /// |
| 493 | /// \param Name - The variable name. |
| 494 | /// \param Init - The variable initializer; this is also used to |
| 495 | /// define the type of the variable. |
| 496 | /// \param Section - The section the variable should go into, or 0. |
| 497 | /// \param Align - The alignment for the variable, or 0. |
| 498 | /// \param AddToUsed - Whether the variable should be added to |
Daniel Dunbar | c158306 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 499 | /// "llvm.used". |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 500 | llvm::GlobalVariable *CreateMetadataVar(const std::string &Name, |
| 501 | llvm::Constant *Init, |
| 502 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 503 | unsigned Align, |
| 504 | bool AddToUsed); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 505 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame^] | 506 | /// GetNamedIvarList - Return the list of ivars in the interface |
| 507 | /// itself (not including super classes and not including unnamed |
| 508 | /// bitfields). |
| 509 | /// |
| 510 | /// For the non-fragile ABI, this also includes synthesized property |
| 511 | /// ivars. |
| 512 | void GetNamedIvarList(const ObjCInterfaceDecl *OID, |
| 513 | llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const; |
| 514 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 515 | public: |
| 516 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm) |
| 517 | { } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 518 | |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 519 | virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 520 | |
| 521 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 522 | const ObjCContainerDecl *CD=0); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 523 | |
| 524 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
| 525 | |
| 526 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 527 | /// declaration, emitting it if necessary. The return value has type |
| 528 | /// ProtocolPtrTy. |
| 529 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
| 530 | |
| 531 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 532 | /// object for the given declaration, emitting it if needed. These |
| 533 | /// forward references will be filled in with empty bodies if no |
| 534 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 535 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 536 | }; |
| 537 | |
| 538 | class CGObjCMac : public CGObjCCommonMac { |
| 539 | private: |
| 540 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 541 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 542 | /// level information. |
| 543 | void EmitImageInfo(); |
| 544 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 545 | /// EmitModuleInfo - Another marker encoding module level |
| 546 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 547 | void EmitModuleInfo(); |
| 548 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 549 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 550 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 551 | llvm::Constant *EmitModuleSymbols(); |
| 552 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 553 | /// FinishModule - Write out global data structures at the end of |
| 554 | /// processing a translation unit. |
| 555 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 556 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 557 | /// EmitClassExtension - Generate the class extension structure used |
| 558 | /// to store the weak ivar layout and properties. The return value |
| 559 | /// has type ClassExtensionPtrTy. |
| 560 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 561 | |
| 562 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 563 | /// for the given class. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 564 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 565 | const ObjCInterfaceDecl *ID); |
| 566 | |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 567 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 568 | QualType ResultType, |
| 569 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 570 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 571 | QualType Arg0Ty, |
| 572 | bool IsSuper, |
| 573 | const CallArgList &CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 574 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 575 | /// EmitIvarList - Emit the ivar list for the given |
| 576 | /// implementation. If ForClass is true the list of class ivars |
| 577 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 578 | /// interface ivars will be emitted. The return value has type |
| 579 | /// IvarListPtrTy. |
| 580 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 581 | bool ForClass); |
| 582 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 583 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 584 | /// for the metaclass of the given interface. The return value has |
| 585 | /// type ClassPtrTy. |
| 586 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 587 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 588 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 589 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 590 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 591 | llvm::Constant *Protocols, |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 592 | const llvm::Type *InterfaceTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 593 | const ConstantVector &Methods); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 594 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 595 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 596 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 597 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 598 | |
| 599 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 600 | /// implementation. The return value has type MethodListPtrTy. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 601 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 602 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 603 | const ConstantVector &Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 604 | |
| 605 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 606 | /// method declarations. |
| 607 | /// - TypeName: The name for the type containing the methods. |
| 608 | /// - IsProtocol: True iff these methods are for a protocol. |
| 609 | /// - ClassMethds: True iff these are class methods. |
| 610 | /// - Required: When true, only "required" methods are |
| 611 | /// listed. Similarly, when false only "optional" methods are |
| 612 | /// listed. For classes this should always be true. |
| 613 | /// - begin, end: The method list to output. |
| 614 | /// |
| 615 | /// The return value has type MethodDescriptionListPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 616 | llvm::Constant *EmitMethodDescList(const std::string &Name, |
| 617 | const char *Section, |
| 618 | const ConstantVector &Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 619 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 620 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 621 | /// declaration, emitting it if necessary. The return value has type |
| 622 | /// ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 623 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 624 | |
| 625 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 626 | /// object for the given declaration, emitting it if needed. These |
| 627 | /// forward references will be filled in with empty bodies if no |
| 628 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 629 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 630 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 631 | /// EmitProtocolExtension - Generate the protocol extension |
| 632 | /// structure used to store optional instance and class methods, and |
| 633 | /// protocol properties. The return value has type |
| 634 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 635 | llvm::Constant * |
| 636 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 637 | const ConstantVector &OptInstanceMethods, |
| 638 | const ConstantVector &OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 639 | |
| 640 | /// EmitProtocolList - Generate the list of referenced |
| 641 | /// protocols. The return value has type ProtocolListPtrTy. |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 642 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 643 | ObjCProtocolDecl::protocol_iterator begin, |
| 644 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 645 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 646 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 647 | /// for the given selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 648 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 649 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 650 | public: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 651 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 652 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 653 | virtual llvm::Function *ModuleInitFunction(); |
| 654 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 655 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 656 | QualType ResultType, |
| 657 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 658 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 659 | bool IsClassMessage, |
| 660 | const CallArgList &CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 661 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 662 | virtual CodeGen::RValue |
| 663 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 664 | QualType ResultType, |
| 665 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 666 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 667 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 668 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 669 | bool IsClassMessage, |
| 670 | const CallArgList &CallArgs); |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 671 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 672 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 673 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 674 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 675 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 676 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 677 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 678 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 679 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 680 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 681 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 682 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 683 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 684 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 685 | virtual llvm::Constant *GetPropertySetFunction(); |
| 686 | virtual llvm::Constant *EnumerationMutationFunction(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 687 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 688 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 689 | const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 690 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 691 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 692 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 693 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 694 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 695 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 696 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 697 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 698 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 699 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 700 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 701 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 702 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 703 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 704 | QualType ObjectTy, |
| 705 | llvm::Value *BaseValue, |
| 706 | const ObjCIvarDecl *Ivar, |
| 707 | const FieldDecl *Field, |
| 708 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 709 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
| 710 | ObjCInterfaceDecl *Interface, |
| 711 | const ObjCIvarDecl *Ivar); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 712 | }; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 713 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 714 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 715 | private: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 716 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 717 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 718 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 719 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 720 | /// SuperClassReferences - uniqued super class references. |
| 721 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; |
| 722 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 723 | /// MetaClassReferences - uniqued meta class references. |
| 724 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 725 | |
| 726 | /// EHTypeReferences - uniqued class ehtype references. |
| 727 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 728 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 729 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 730 | /// processing a translation unit. |
| 731 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 732 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 733 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 734 | unsigned InstanceStart, |
| 735 | unsigned InstanceSize, |
| 736 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 737 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
| 738 | llvm::Constant *IsAGV, |
| 739 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 740 | llvm::Constant *ClassRoGV, |
| 741 | bool HiddenVisibility); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 742 | |
| 743 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
| 744 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 745 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
| 746 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 747 | /// EmitMethodList - Emit the method list for the given |
| 748 | /// implementation. The return value has type MethodListnfABITy. |
| 749 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 750 | const char *Section, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 751 | const ConstantVector &Methods); |
| 752 | /// EmitIvarList - Emit the ivar list for the given |
| 753 | /// implementation. If ForClass is true the list of class ivars |
| 754 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 755 | /// interface ivars will be emitted. The return value has type |
| 756 | /// IvarListnfABIPtrTy. |
| 757 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 758 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 759 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 760 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 761 | unsigned long int offset); |
| 762 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 763 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 764 | /// declaration, emitting it if necessary. The return value has type |
| 765 | /// ProtocolPtrTy. |
| 766 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
| 767 | |
| 768 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 769 | /// object for the given declaration, emitting it if needed. These |
| 770 | /// forward references will be filled in with empty bodies if no |
| 771 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 772 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
| 773 | |
| 774 | /// EmitProtocolList - Generate the list of referenced |
| 775 | /// protocols. The return value has type ProtocolListPtrTy. |
| 776 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 777 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 778 | ObjCProtocolDecl::protocol_iterator end); |
| 779 | |
| 780 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 781 | QualType ResultType, |
| 782 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 783 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 784 | QualType Arg0Ty, |
| 785 | bool IsSuper, |
| 786 | const CallArgList &CallArgs); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 787 | |
| 788 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 789 | /// class of the given name. |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 790 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name); |
| 791 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 792 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 793 | /// for the given class reference. |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 794 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 795 | const ObjCInterfaceDecl *ID); |
| 796 | |
| 797 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 798 | /// for the given super class reference. |
| 799 | llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder, |
| 800 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 801 | |
| 802 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 803 | /// meta-data |
| 804 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
| 805 | const ObjCInterfaceDecl *ID); |
| 806 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 807 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 808 | /// the given ivar. |
| 809 | /// |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 810 | llvm::GlobalVariable * ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 811 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 812 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 813 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 814 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 815 | /// for the given selector. |
| 816 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 817 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 818 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 819 | /// interface. The return value has type EHTypePtrTy. |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 820 | llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 821 | bool ForDefinition); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 822 | |
| 823 | const char *getMetaclassSymbolPrefix() const { |
| 824 | return "OBJC_METACLASS_$_"; |
| 825 | } |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 826 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 827 | const char *getClassSymbolPrefix() const { |
| 828 | return "OBJC_CLASS_$_"; |
| 829 | } |
| 830 | |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 831 | void GetClassSizeInfo(const ObjCInterfaceDecl *OID, |
| 832 | uint32_t &InstanceStart, |
| 833 | uint32_t &InstanceSize); |
| 834 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 835 | public: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 836 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 837 | // FIXME. All stubs for now! |
| 838 | virtual llvm::Function *ModuleInitFunction(); |
| 839 | |
| 840 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
| 841 | QualType ResultType, |
| 842 | Selector Sel, |
| 843 | llvm::Value *Receiver, |
| 844 | bool IsClassMessage, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 845 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 846 | |
| 847 | virtual CodeGen::RValue |
| 848 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 849 | QualType ResultType, |
| 850 | Selector Sel, |
| 851 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 852 | bool isCategoryImpl, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 853 | llvm::Value *Receiver, |
| 854 | bool IsClassMessage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 855 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 856 | |
| 857 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 858 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 859 | |
| 860 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel) |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 861 | { return EmitSelector(Builder, Sel); } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 862 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 863 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 864 | |
| 865 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 866 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 867 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 868 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 869 | virtual llvm::Constant *GetPropertyGetFunction() { |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 870 | return ObjCTypes.GetPropertyFn; |
| 871 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 872 | virtual llvm::Constant *GetPropertySetFunction() { |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 873 | return ObjCTypes.SetPropertyFn; |
| 874 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 875 | virtual llvm::Constant *EnumerationMutationFunction() { |
Daniel Dunbar | 28ed084 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 876 | return ObjCTypes.EnumerationMutationFn; |
| 877 | } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 878 | |
| 879 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 880 | const Stmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 881 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 882 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 883 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 884 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 885 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 886 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 887 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 888 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 889 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 890 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 891 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 892 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 893 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 894 | QualType ObjectTy, |
| 895 | llvm::Value *BaseValue, |
| 896 | const ObjCIvarDecl *Ivar, |
| 897 | const FieldDecl *Field, |
| 898 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 899 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
| 900 | ObjCInterfaceDecl *Interface, |
| 901 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 902 | }; |
| 903 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 904 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 905 | |
| 906 | /* *** Helper Functions *** */ |
| 907 | |
| 908 | /// getConstantGEP() - Help routine to construct simple GEPs. |
| 909 | static llvm::Constant *getConstantGEP(llvm::Constant *C, |
| 910 | unsigned idx0, |
| 911 | unsigned idx1) { |
| 912 | llvm::Value *Idxs[] = { |
| 913 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0), |
| 914 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1) |
| 915 | }; |
| 916 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2); |
| 917 | } |
| 918 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 919 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 920 | /// class has the __objc_exception__ attribute. |
| 921 | static bool hasObjCExceptionAttribute(const ObjCInterfaceDecl *OID) { |
Daniel Dunbar | b11fa0d | 2009-04-13 21:08:27 +0000 | [diff] [blame] | 922 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 923 | return true; |
| 924 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
| 925 | return hasObjCExceptionAttribute(Super); |
| 926 | return false; |
| 927 | } |
| 928 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 929 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 930 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 931 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
| 932 | ObjCTypes(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 933 | { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 934 | ObjCABI = 1; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 935 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 938 | /// GetClass - Return a reference to the class for the given interface |
| 939 | /// decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 940 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 941 | const ObjCInterfaceDecl *ID) { |
| 942 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 946 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 947 | return EmitSelector(Builder, Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 950 | /// Generate a constant CFString object. |
| 951 | /* |
| 952 | struct __builtin_CFString { |
| 953 | const int *isa; // point to __CFConstantStringClassReference |
| 954 | int flags; |
| 955 | const char *str; |
| 956 | long length; |
| 957 | }; |
| 958 | */ |
| 959 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 960 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 961 | const ObjCStringLiteral *SL) { |
Steve Naroff | 8d4141f | 2009-04-01 13:55:36 +0000 | [diff] [blame] | 962 | return CGM.GetAddrOfConstantCFString(SL->getString()); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | /// Generates a message send where the super is the receiver. This is |
| 966 | /// a message send to self with special delivery semantics indicating |
| 967 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 968 | CodeGen::RValue |
| 969 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 970 | QualType ResultType, |
| 971 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 972 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 973 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 974 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 975 | bool IsClassMessage, |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 976 | const CodeGen::CallArgList &CallArgs) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 977 | // Create and init a super structure; this is a (receiver, class) |
| 978 | // pair we will pass to objc_msgSendSuper. |
| 979 | llvm::Value *ObjCSuper = |
| 980 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 981 | llvm::Value *ReceiverAsObject = |
| 982 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 983 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 984 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 985 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 986 | // If this is a class message the metaclass is passed as the target. |
| 987 | llvm::Value *Target; |
| 988 | if (IsClassMessage) { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 989 | if (isCategoryImpl) { |
| 990 | // Message sent to 'super' in a class method defined in a category |
| 991 | // implementation requires an odd treatment. |
| 992 | // If we are in a class method, we must retrieve the |
| 993 | // _metaclass_ for the current class, pointed at by |
| 994 | // the class's "isa" pointer. The following assumes that |
| 995 | // isa" is the first ivar in a class (which it must be). |
| 996 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 997 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 998 | Target = CGF.Builder.CreateLoad(Target); |
| 999 | } |
| 1000 | else { |
| 1001 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 1002 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 1003 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 1004 | Target = Super; |
| 1005 | } |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1006 | } else { |
| 1007 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1008 | } |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1009 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 1010 | // and ObjCTypes types. |
| 1011 | const llvm::Type *ClassTy = |
| 1012 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1013 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1014 | CGF.Builder.CreateStore(Target, |
| 1015 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 1016 | |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1017 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1018 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 1019 | true, CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1020 | } |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1021 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1022 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1023 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1024 | QualType ResultType, |
| 1025 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1026 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1027 | bool IsClassMessage, |
| 1028 | const CallArgList &CallArgs) { |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1029 | llvm::Value *Arg0 = |
| 1030 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1031 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1032 | Arg0, CGF.getContext().getObjCIdType(), |
| 1033 | false, CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1037 | QualType ResultType, |
| 1038 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1039 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1040 | QualType Arg0Ty, |
| 1041 | bool IsSuper, |
| 1042 | const CallArgList &CallArgs) { |
| 1043 | CallArgList ActualArgs; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1044 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 1045 | ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder, |
| 1046 | Sel)), |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1047 | CGF.getContext().getObjCSelType())); |
| 1048 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1049 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1050 | CodeGenTypes &Types = CGM.getTypes(); |
| 1051 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); |
| 1052 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1053 | |
| 1054 | llvm::Constant *Fn; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1055 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1056 | Fn = ObjCTypes.getSendStretFn(IsSuper); |
| 1057 | } else if (ResultType->isFloatingType()) { |
| 1058 | // FIXME: Sadly, this is wrong. This actually depends on the |
| 1059 | // architecture. This happens to be right for x86-32 though. |
| 1060 | Fn = ObjCTypes.getSendFpretFn(IsSuper); |
| 1061 | } else { |
| 1062 | Fn = ObjCTypes.getSendFn(IsSuper); |
| 1063 | } |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 1064 | Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy)); |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1065 | return CGF.EmitCall(FnInfo, Fn, ActualArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1068 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1069 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1070 | // FIXME: I don't understand why gcc generates this, or where it is |
| 1071 | // resolved. Investigate. Its also wasteful to look this up over and |
| 1072 | // over. |
| 1073 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1074 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1075 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
| 1076 | ObjCTypes.ExternalProtocolPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1079 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1080 | // FIXME: We shouldn't need this, the protocol decl should contain |
| 1081 | // enough information to tell us whether this was a declaration or a |
| 1082 | // definition. |
| 1083 | DefinedProtocols.insert(PD->getIdentifier()); |
| 1084 | |
| 1085 | // If we have generated a forward reference to this protocol, emit |
| 1086 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 1087 | // emitted. |
| 1088 | if (Protocols.count(PD->getIdentifier())) |
| 1089 | GetOrEmitProtocol(PD); |
| 1090 | } |
| 1091 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1092 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1093 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 1094 | return GetOrEmitProtocol(PD); |
| 1095 | return GetOrEmitProtocolRef(PD); |
| 1096 | } |
| 1097 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1098 | /* |
| 1099 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 1100 | struct _objc_protocol { |
| 1101 | struct _objc_protocol_extension *isa; |
| 1102 | char *protocol_name; |
| 1103 | struct _objc_protocol_list *protocol_list; |
| 1104 | struct _objc__method_prototype_list *instance_methods; |
| 1105 | struct _objc__method_prototype_list *class_methods |
| 1106 | }; |
| 1107 | |
| 1108 | See EmitProtocolExtension(). |
| 1109 | */ |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1110 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
| 1111 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1112 | |
| 1113 | // Early exit if a defining object has already been generated. |
| 1114 | if (Entry && Entry->hasInitializer()) |
| 1115 | return Entry; |
| 1116 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1117 | // FIXME: I don't understand why gcc generates this, or where it is |
| 1118 | // resolved. Investigate. Its also wasteful to look this up over and |
| 1119 | // over. |
| 1120 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1121 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1122 | const char *ProtocolName = PD->getNameAsCString(); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1123 | |
| 1124 | // Construct method lists. |
| 1125 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1126 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1127 | for (ObjCProtocolDecl::instmeth_iterator |
| 1128 | i = PD->instmeth_begin(CGM.getContext()), |
| 1129 | e = PD->instmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1130 | ObjCMethodDecl *MD = *i; |
| 1131 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1132 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1133 | OptInstanceMethods.push_back(C); |
| 1134 | } else { |
| 1135 | InstanceMethods.push_back(C); |
| 1136 | } |
| 1137 | } |
| 1138 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1139 | for (ObjCProtocolDecl::classmeth_iterator |
| 1140 | i = PD->classmeth_begin(CGM.getContext()), |
| 1141 | e = PD->classmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1142 | ObjCMethodDecl *MD = *i; |
| 1143 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1144 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1145 | OptClassMethods.push_back(C); |
| 1146 | } else { |
| 1147 | ClassMethods.push_back(C); |
| 1148 | } |
| 1149 | } |
| 1150 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1151 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1152 | Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1153 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1154 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1155 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1156 | PD->protocol_begin(), |
| 1157 | PD->protocol_end()); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1158 | Values[3] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1159 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" |
| 1160 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1161 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1162 | InstanceMethods); |
| 1163 | Values[4] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1164 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" |
| 1165 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1166 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1167 | ClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1168 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 1169 | Values); |
| 1170 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1171 | if (Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1172 | // Already created, fix the linkage and update the initializer. |
| 1173 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1174 | Entry->setInitializer(Init); |
| 1175 | } else { |
| 1176 | Entry = |
| 1177 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
| 1178 | llvm::GlobalValue::InternalLinkage, |
| 1179 | Init, |
| 1180 | std::string("\01L_OBJC_PROTOCOL_")+ProtocolName, |
| 1181 | &CGM.getModule()); |
| 1182 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1183 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1184 | UsedGlobals.push_back(Entry); |
| 1185 | // FIXME: Is this necessary? Why only for protocol? |
| 1186 | Entry->setAlignment(4); |
| 1187 | } |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1188 | |
| 1189 | return Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1192 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1193 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1194 | |
| 1195 | if (!Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1196 | // We use the initializer as a marker of whether this is a forward |
| 1197 | // reference or not. At module finalization we add the empty |
| 1198 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1199 | Entry = |
| 1200 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1201 | llvm::GlobalValue::ExternalLinkage, |
| 1202 | 0, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1203 | "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1204 | &CGM.getModule()); |
| 1205 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1206 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1207 | UsedGlobals.push_back(Entry); |
| 1208 | // FIXME: Is this necessary? Why only for protocol? |
| 1209 | Entry->setAlignment(4); |
| 1210 | } |
| 1211 | |
| 1212 | return Entry; |
| 1213 | } |
| 1214 | |
| 1215 | /* |
| 1216 | struct _objc_protocol_extension { |
| 1217 | uint32_t size; |
| 1218 | struct objc_method_description_list *optional_instance_methods; |
| 1219 | struct objc_method_description_list *optional_class_methods; |
| 1220 | struct objc_property_list *instance_properties; |
| 1221 | }; |
| 1222 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1223 | llvm::Constant * |
| 1224 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1225 | const ConstantVector &OptInstanceMethods, |
| 1226 | const ConstantVector &OptClassMethods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1227 | uint64_t Size = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1228 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1229 | std::vector<llvm::Constant*> Values(4); |
| 1230 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1231 | Values[1] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1232 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
| 1233 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1234 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1235 | OptInstanceMethods); |
| 1236 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1237 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" |
| 1238 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1239 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1240 | OptClassMethods); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1241 | Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + |
| 1242 | PD->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1243 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1244 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1245 | // Return null if no extension bits are used. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1246 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
| 1247 | Values[3]->isNullValue()) |
| 1248 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 1249 | |
| 1250 | llvm::Constant *Init = |
| 1251 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1252 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1253 | // No special section, but goes in llvm.used |
| 1254 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(), |
| 1255 | Init, |
| 1256 | 0, 0, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | /* |
| 1260 | struct objc_protocol_list { |
| 1261 | struct objc_protocol_list *next; |
| 1262 | long count; |
| 1263 | Protocol *list[]; |
| 1264 | }; |
| 1265 | */ |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1266 | llvm::Constant * |
| 1267 | CGObjCMac::EmitProtocolList(const std::string &Name, |
| 1268 | ObjCProtocolDecl::protocol_iterator begin, |
| 1269 | ObjCProtocolDecl::protocol_iterator end) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1270 | std::vector<llvm::Constant*> ProtocolRefs; |
| 1271 | |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1272 | for (; begin != end; ++begin) |
| 1273 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1274 | |
| 1275 | // Just return null for empty protocol lists |
| 1276 | if (ProtocolRefs.empty()) |
| 1277 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1278 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1279 | // This list is null terminated. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1280 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
| 1281 | |
| 1282 | std::vector<llvm::Constant*> Values(3); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1283 | // This field is only used by the runtime. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1284 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1285 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 1286 | Values[2] = |
| 1287 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 1288 | ProtocolRefs.size()), |
| 1289 | ProtocolRefs); |
| 1290 | |
| 1291 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1292 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1293 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1294 | 4, false); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1295 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
| 1296 | } |
| 1297 | |
| 1298 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1299 | struct _objc_property { |
| 1300 | const char * const name; |
| 1301 | const char * const attributes; |
| 1302 | }; |
| 1303 | |
| 1304 | struct _objc_property_list { |
| 1305 | uint32_t entsize; // sizeof (struct _objc_property) |
| 1306 | uint32_t prop_count; |
| 1307 | struct _objc_property[prop_count]; |
| 1308 | }; |
| 1309 | */ |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1310 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name, |
| 1311 | const Decl *Container, |
| 1312 | const ObjCContainerDecl *OCD, |
| 1313 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1314 | std::vector<llvm::Constant*> Properties, Prop(2); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1315 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(CGM.getContext()), |
| 1316 | E = OCD->prop_end(CGM.getContext()); I != E; ++I) { |
Steve Naroff | 93983f8 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 1317 | const ObjCPropertyDecl *PD = *I; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1318 | Prop[0] = GetPropertyName(PD->getIdentifier()); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1319 | Prop[1] = GetPropertyTypeString(PD, Container); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1320 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
| 1321 | Prop)); |
| 1322 | } |
| 1323 | |
| 1324 | // Return null for empty list. |
| 1325 | if (Properties.empty()) |
| 1326 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1327 | |
| 1328 | unsigned PropertySize = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1329 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1330 | std::vector<llvm::Constant*> Values(3); |
| 1331 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 1332 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
| 1333 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
| 1334 | Properties.size()); |
| 1335 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
| 1336 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1337 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1338 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1339 | CreateMetadataVar(Name, Init, |
| 1340 | (ObjCABI == 2) ? "__DATA, __objc_const" : |
| 1341 | "__OBJC,__property,regular,no_dead_strip", |
| 1342 | (ObjCABI == 2) ? 8 : 4, |
| 1343 | true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1344 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1348 | struct objc_method_description_list { |
| 1349 | int count; |
| 1350 | struct objc_method_description list[]; |
| 1351 | }; |
| 1352 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1353 | llvm::Constant * |
| 1354 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 1355 | std::vector<llvm::Constant*> Desc(2); |
| 1356 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1357 | ObjCTypes.SelectorPtrTy); |
| 1358 | Desc[1] = GetMethodVarType(MD); |
| 1359 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
| 1360 | Desc); |
| 1361 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1362 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1363 | llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name, |
| 1364 | const char *Section, |
| 1365 | const ConstantVector &Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1366 | // Return null for empty list. |
| 1367 | if (Methods.empty()) |
| 1368 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 1369 | |
| 1370 | std::vector<llvm::Constant*> Values(2); |
| 1371 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 1372 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
| 1373 | Methods.size()); |
| 1374 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
| 1375 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1376 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1377 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1378 | return llvm::ConstantExpr::getBitCast(GV, |
| 1379 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1382 | /* |
| 1383 | struct _objc_category { |
| 1384 | char *category_name; |
| 1385 | char *class_name; |
| 1386 | struct _objc_method_list *instance_methods; |
| 1387 | struct _objc_method_list *class_methods; |
| 1388 | struct _objc_protocol_list *protocols; |
| 1389 | uint32_t size; // <rdar://4585769> |
| 1390 | struct _objc_property_list *instance_properties; |
| 1391 | }; |
| 1392 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1393 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1394 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1395 | |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1396 | // FIXME: This is poor design, the OCD should have a pointer to the |
| 1397 | // category decl. Additionally, note that Category can be null for |
| 1398 | // the @implementation w/o an @interface case. Sema should just |
| 1399 | // create one for us as it does for @implementation so everyone else |
| 1400 | // can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1401 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1402 | const ObjCCategoryDecl *Category = |
| 1403 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1404 | std::string ExtName(Interface->getNameAsString() + "_" + |
| 1405 | OCD->getNameAsString()); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1406 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1407 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1408 | for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(), |
| 1409 | e = OCD->instmeth_end(); i != e; ++i) { |
| 1410 | // Instance methods should always be defined. |
| 1411 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1412 | } |
| 1413 | for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(), |
| 1414 | e = OCD->classmeth_end(); i != e; ++i) { |
| 1415 | // Class methods should always be defined. |
| 1416 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1417 | } |
| 1418 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1419 | std::vector<llvm::Constant*> Values(7); |
| 1420 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 1421 | Values[1] = GetClassName(Interface->getIdentifier()); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1422 | Values[2] = |
| 1423 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") + |
| 1424 | ExtName, |
| 1425 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1426 | InstanceMethods); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1427 | Values[3] = |
| 1428 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName, |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1429 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1430 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1431 | if (Category) { |
| 1432 | Values[4] = |
| 1433 | EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName, |
| 1434 | Category->protocol_begin(), |
| 1435 | Category->protocol_end()); |
| 1436 | } else { |
| 1437 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1438 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1439 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1440 | |
| 1441 | // If there is no category @interface then there can be no properties. |
| 1442 | if (Category) { |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1443 | Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1444 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1445 | } else { |
| 1446 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1447 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1448 | |
| 1449 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
| 1450 | Values); |
| 1451 | |
| 1452 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1453 | CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init, |
| 1454 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1455 | 4, true); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1456 | DefinedCategories.push_back(GV); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1459 | // FIXME: Get from somewhere? |
| 1460 | enum ClassFlags { |
| 1461 | eClassFlags_Factory = 0x00001, |
| 1462 | eClassFlags_Meta = 0x00002, |
| 1463 | // <rdr://5142207> |
| 1464 | eClassFlags_HasCXXStructors = 0x02000, |
| 1465 | eClassFlags_Hidden = 0x20000, |
| 1466 | eClassFlags_ABI2_Hidden = 0x00010, |
| 1467 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 1468 | }; |
| 1469 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1470 | /* |
| 1471 | struct _objc_class { |
| 1472 | Class isa; |
| 1473 | Class super_class; |
| 1474 | const char *name; |
| 1475 | long version; |
| 1476 | long info; |
| 1477 | long instance_size; |
| 1478 | struct _objc_ivar_list *ivars; |
| 1479 | struct _objc_method_list *methods; |
| 1480 | struct _objc_cache *cache; |
| 1481 | struct _objc_protocol_list *protocols; |
| 1482 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 1483 | const char *ivar_layout; |
| 1484 | struct _objc_class_ext *ext; |
| 1485 | }; |
| 1486 | |
| 1487 | See EmitClassExtension(); |
| 1488 | */ |
| 1489 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1490 | DefinedSymbols.insert(ID->getIdentifier()); |
| 1491 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1492 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1493 | // FIXME: Gross |
| 1494 | ObjCInterfaceDecl *Interface = |
| 1495 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1496 | llvm::Constant *Protocols = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1497 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1498 | Interface->protocol_begin(), |
| 1499 | Interface->protocol_end()); |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 1500 | const llvm::Type *InterfaceTy; |
| 1501 | if (Interface->isForwardDecl()) |
| 1502 | InterfaceTy = llvm::StructType::get(NULL, NULL); |
| 1503 | else |
| 1504 | InterfaceTy = |
Chris Lattner | 03d9f34 | 2009-04-01 06:23:52 +0000 | [diff] [blame] | 1505 | CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1506 | unsigned Flags = eClassFlags_Factory; |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1507 | unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1508 | |
| 1509 | // FIXME: Set CXX-structors flag. |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1510 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1511 | Flags |= eClassFlags_Hidden; |
| 1512 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1513 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1514 | for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(), |
| 1515 | e = ID->instmeth_end(); i != e; ++i) { |
| 1516 | // Instance methods should always be defined. |
| 1517 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1518 | } |
| 1519 | for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(), |
| 1520 | e = ID->classmeth_end(); i != e; ++i) { |
| 1521 | // Class methods should always be defined. |
| 1522 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1523 | } |
| 1524 | |
| 1525 | for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(), |
| 1526 | e = ID->propimpl_end(); i != e; ++i) { |
| 1527 | ObjCPropertyImplDecl *PID = *i; |
| 1528 | |
| 1529 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 1530 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 1531 | |
| 1532 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 1533 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1534 | InstanceMethods.push_back(C); |
| 1535 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 1536 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1537 | InstanceMethods.push_back(C); |
| 1538 | } |
| 1539 | } |
| 1540 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1541 | std::vector<llvm::Constant*> Values(12); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1542 | Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1543 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1544 | // Record a reference to the super class. |
| 1545 | LazySymbols.insert(Super->getIdentifier()); |
| 1546 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1547 | Values[ 1] = |
| 1548 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1549 | ObjCTypes.ClassPtrTy); |
| 1550 | } else { |
| 1551 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1552 | } |
| 1553 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1554 | // Version is always 0. |
| 1555 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1556 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1557 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1558 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1559 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1560 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1561 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1562 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1563 | // cache is always NULL. |
| 1564 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1565 | Values[ 9] = Protocols; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1566 | // FIXME: Set ivar_layout |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 1567 | // Values[10] = BuildIvarLayout(ID, true); |
| 1568 | Values[10] = GetIvarLayoutName(0, ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1569 | Values[11] = EmitClassExtension(ID); |
| 1570 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1571 | Values); |
| 1572 | |
| 1573 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1574 | CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init, |
| 1575 | "__OBJC,__class,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1576 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1577 | DefinedClasses.push_back(GV); |
| 1578 | } |
| 1579 | |
| 1580 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1581 | llvm::Constant *Protocols, |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1582 | const llvm::Type *InterfaceTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1583 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1584 | unsigned Flags = eClassFlags_Meta; |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1585 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1586 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1587 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1588 | Flags |= eClassFlags_Hidden; |
| 1589 | |
| 1590 | std::vector<llvm::Constant*> Values(12); |
| 1591 | // The isa for the metaclass is the root of the hierarchy. |
| 1592 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 1593 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 1594 | Root = Super; |
| 1595 | Values[ 0] = |
| 1596 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
| 1597 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1598 | // The super class for the metaclass is emitted as the name of the |
| 1599 | // super class. The runtime fixes this up to point to the |
| 1600 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1601 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
| 1602 | Values[ 1] = |
| 1603 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1604 | ObjCTypes.ClassPtrTy); |
| 1605 | } else { |
| 1606 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1607 | } |
| 1608 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1609 | // Version is always 0. |
| 1610 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1611 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1612 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1613 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1614 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1615 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1616 | "__OBJC,__cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1617 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1618 | // cache is always NULL. |
| 1619 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1620 | Values[ 9] = Protocols; |
| 1621 | // ivar_layout for metaclass is always NULL. |
| 1622 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 1623 | // The class extension is always unused for metaclasses. |
| 1624 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 1625 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1626 | Values); |
| 1627 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1628 | std::string Name("\01L_OBJC_METACLASS_"); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1629 | Name += ID->getNameAsCString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1630 | |
| 1631 | // Check for a forward reference. |
| 1632 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 1633 | if (GV) { |
| 1634 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 1635 | "Forward metaclass reference has incorrect type."); |
| 1636 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 1637 | GV->setInitializer(Init); |
| 1638 | } else { |
| 1639 | GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1640 | llvm::GlobalValue::InternalLinkage, |
| 1641 | Init, Name, |
| 1642 | &CGM.getModule()); |
| 1643 | } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1644 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1645 | GV->setAlignment(4); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1646 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1647 | |
| 1648 | return GV; |
| 1649 | } |
| 1650 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1651 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1652 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1653 | |
| 1654 | // FIXME: Should we look these up somewhere other than the |
| 1655 | // module. Its a bit silly since we only generate these while |
| 1656 | // processing an implementation, so exactly one pointer would work |
| 1657 | // if know when we entered/exitted an implementation block. |
| 1658 | |
| 1659 | // Check for an existing forward reference. |
Fariborz Jahanian | b0d2794 | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 1660 | // Previously, metaclass with internal linkage may have been defined. |
| 1661 | // pass 'true' as 2nd argument so it is returned. |
| 1662 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) { |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1663 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 1664 | "Forward metaclass reference has incorrect type."); |
| 1665 | return GV; |
| 1666 | } else { |
| 1667 | // Generate as an external reference to keep a consistent |
| 1668 | // module. This will be patched up when we emit the metaclass. |
| 1669 | return new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1670 | llvm::GlobalValue::ExternalLinkage, |
| 1671 | 0, |
| 1672 | Name, |
| 1673 | &CGM.getModule()); |
| 1674 | } |
| 1675 | } |
| 1676 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1677 | /* |
| 1678 | struct objc_class_ext { |
| 1679 | uint32_t size; |
| 1680 | const char *weak_ivar_layout; |
| 1681 | struct _objc_property_list *properties; |
| 1682 | }; |
| 1683 | */ |
| 1684 | llvm::Constant * |
| 1685 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
| 1686 | uint64_t Size = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1687 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1688 | |
| 1689 | std::vector<llvm::Constant*> Values(3); |
| 1690 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1691 | // FIXME: Output weak_ivar_layout string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 1692 | // Values[1] = BuildIvarLayout(ID, false); |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 1693 | Values[1] = GetIvarLayoutName(0, ObjCTypes); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1694 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1695 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1696 | |
| 1697 | // Return null if no extension bits are used. |
| 1698 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
| 1699 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 1700 | |
| 1701 | llvm::Constant *Init = |
| 1702 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1703 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1704 | Init, "__OBJC,__class_ext,regular,no_dead_strip", |
| 1705 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
Fariborz Jahanian | f54b194 | 2009-01-17 19:36:33 +0000 | [diff] [blame] | 1708 | /// countInheritedIvars - count number of ivars in class and its super class(s) |
| 1709 | /// |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1710 | static int countInheritedIvars(const ObjCInterfaceDecl *OI, |
| 1711 | ASTContext &Context) { |
Fariborz Jahanian | f54b194 | 2009-01-17 19:36:33 +0000 | [diff] [blame] | 1712 | int count = 0; |
| 1713 | if (!OI) |
| 1714 | return 0; |
| 1715 | const ObjCInterfaceDecl *SuperClass = OI->getSuperClass(); |
| 1716 | if (SuperClass) |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1717 | count += countInheritedIvars(SuperClass, Context); |
Fariborz Jahanian | f54b194 | 2009-01-17 19:36:33 +0000 | [diff] [blame] | 1718 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1719 | E = OI->ivar_end(); I != E; ++I) |
| 1720 | ++count; |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 1721 | // look into properties. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1722 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context), |
| 1723 | E = OI->prop_end(Context); I != E; ++I) { |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 1724 | if ((*I)->getPropertyIvarDecl()) |
| 1725 | ++count; |
| 1726 | } |
Fariborz Jahanian | f54b194 | 2009-01-17 19:36:33 +0000 | [diff] [blame] | 1727 | return count; |
| 1728 | } |
| 1729 | |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1730 | /// getInterfaceDeclForIvar - Get the interface declaration node where |
| 1731 | /// this ivar is declared in. |
| 1732 | /// FIXME. Ideally, this info should be in the ivar node. But currently |
| 1733 | /// it is not and prevailing wisdom is that ASTs should not have more |
| 1734 | /// info than is absolutely needed, even though this info reflects the |
| 1735 | /// source language. |
| 1736 | /// |
| 1737 | static const ObjCInterfaceDecl *getInterfaceDeclForIvar( |
| 1738 | const ObjCInterfaceDecl *OI, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1739 | const ObjCIvarDecl *IVD, |
| 1740 | ASTContext &Context) { |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1741 | if (!OI) |
| 1742 | return 0; |
| 1743 | assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface"); |
| 1744 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1745 | E = OI->ivar_end(); I != E; ++I) |
| 1746 | if ((*I)->getIdentifier() == IVD->getIdentifier()) |
| 1747 | return OI; |
Fariborz Jahanian | 5a4b453 | 2009-03-31 17:00:52 +0000 | [diff] [blame] | 1748 | // look into properties. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1749 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context), |
| 1750 | E = OI->prop_end(Context); I != E; ++I) { |
Fariborz Jahanian | 5a4b453 | 2009-03-31 17:00:52 +0000 | [diff] [blame] | 1751 | ObjCPropertyDecl *PDecl = (*I); |
| 1752 | if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl()) |
| 1753 | if (IV->getIdentifier() == IVD->getIdentifier()) |
| 1754 | return OI; |
| 1755 | } |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1756 | return getInterfaceDeclForIvar(OI->getSuperClass(), IVD, Context); |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1759 | /* |
| 1760 | struct objc_ivar { |
| 1761 | char *ivar_name; |
| 1762 | char *ivar_type; |
| 1763 | int ivar_offset; |
| 1764 | }; |
| 1765 | |
| 1766 | struct objc_ivar_list { |
| 1767 | int ivar_count; |
| 1768 | struct objc_ivar list[count]; |
| 1769 | }; |
| 1770 | */ |
| 1771 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1772 | bool ForClass) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1773 | std::vector<llvm::Constant*> Ivars, Ivar(3); |
| 1774 | |
| 1775 | // When emitting the root class GCC emits ivar entries for the |
| 1776 | // actual class structure. It is not clear if we need to follow this |
| 1777 | // behavior; for now lets try and get away with not doing it. If so, |
| 1778 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 1779 | // for the class. |
| 1780 | if (ForClass) |
| 1781 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1782 | |
| 1783 | ObjCInterfaceDecl *OID = |
| 1784 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 1785 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1786 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame^] | 1787 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
| 1788 | GetNamedIvarList(OID, OIvars); |
| 1789 | |
| 1790 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 1791 | ObjCIvarDecl *IVD = OIvars[i]; |
| 1792 | const FieldDecl *Field = OID->lookupFieldDeclForIvar(CGM.getContext(), IVD); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 1793 | uint64_t Offset = GetIvarBaseOffset(Layout, Field); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame^] | 1794 | Ivar[0] = GetMethodVarName(Field->getIdentifier()); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 1795 | Ivar[1] = GetMethodVarType(Field); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1796 | Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1797 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
| 1800 | // Return null for empty list. |
| 1801 | if (Ivars.empty()) |
| 1802 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
| 1803 | |
| 1804 | std::vector<llvm::Constant*> Values(2); |
| 1805 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 1806 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
| 1807 | Ivars.size()); |
| 1808 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
| 1809 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1810 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1811 | llvm::GlobalVariable *GV; |
| 1812 | if (ForClass) |
| 1813 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(), |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1814 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
| 1815 | 4, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1816 | else |
| 1817 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" |
| 1818 | + ID->getNameAsString(), |
| 1819 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1820 | 4, true); |
| 1821 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1822 | } |
| 1823 | |
| 1824 | /* |
| 1825 | struct objc_method { |
| 1826 | SEL method_name; |
| 1827 | char *method_types; |
| 1828 | void *method; |
| 1829 | }; |
| 1830 | |
| 1831 | struct objc_method_list { |
| 1832 | struct objc_method_list *obsolete; |
| 1833 | int count; |
| 1834 | struct objc_method methods_list[count]; |
| 1835 | }; |
| 1836 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1837 | |
| 1838 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 1839 | /// given method if it has been defined. The result is null if the |
| 1840 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1841 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1842 | // FIXME: Use DenseMap::lookup |
| 1843 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 1844 | if (!Fn) |
| 1845 | return 0; |
| 1846 | |
| 1847 | std::vector<llvm::Constant*> Method(3); |
| 1848 | Method[0] = |
| 1849 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1850 | ObjCTypes.SelectorPtrTy); |
| 1851 | Method[1] = GetMethodVarType(MD); |
| 1852 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 1853 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 1854 | } |
| 1855 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1856 | llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name, |
| 1857 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1858 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1859 | // Return null for empty list. |
| 1860 | if (Methods.empty()) |
| 1861 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
| 1862 | |
| 1863 | std::vector<llvm::Constant*> Values(3); |
| 1864 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 1865 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 1866 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 1867 | Methods.size()); |
| 1868 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 1869 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1870 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1871 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1872 | return llvm::ConstantExpr::getBitCast(GV, |
| 1873 | ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1876 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 1877 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1878 | std::string Name; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 1879 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1880 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1881 | CodeGenTypes &Types = CGM.getTypes(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1882 | const llvm::FunctionType *MethodTy = |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1883 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1884 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1885 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1886 | llvm::GlobalValue::InternalLinkage, |
| 1887 | Name, |
| 1888 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1889 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1890 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1891 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 1894 | uint64_t CGObjCCommonMac::GetIvarBaseOffset(const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 1895 | const FieldDecl *Field) { |
Fariborz Jahanian | a6681ae | 2009-03-09 20:44:22 +0000 | [diff] [blame] | 1896 | if (!Field->isBitField()) |
Daniel Dunbar | 48fa064 | 2009-04-19 02:03:42 +0000 | [diff] [blame] | 1897 | return Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); |
| 1898 | |
Fariborz Jahanian | a6681ae | 2009-03-09 20:44:22 +0000 | [diff] [blame] | 1899 | // FIXME. Must be a better way of getting a bitfield base offset. |
Daniel Dunbar | 48fa064 | 2009-04-19 02:03:42 +0000 | [diff] [blame] | 1900 | CodeGenTypes::BitFieldInfo BFI = CGM.getTypes().getBitFieldInfo(Field); |
| 1901 | // FIXME: The "field no" for bitfields is something completely |
| 1902 | // different; it is the offset in multiples of the base type size! |
| 1903 | uint64_t Offset = CGM.getTypes().getLLVMFieldNo(Field); |
| 1904 | const llvm::Type *Ty = |
| 1905 | CGM.getTypes().ConvertTypeForMemRecursive(Field->getType()); |
| 1906 | Offset *= CGM.getTypes().getTargetData().getTypePaddedSizeInBits(Ty); |
| 1907 | return (Offset + BFI.Begin) / 8; |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
Daniel Dunbar | 48fa064 | 2009-04-19 02:03:42 +0000 | [diff] [blame] | 1910 | /// GetFieldBaseOffset - return the field's byte offset. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 1911 | uint64_t CGObjCCommonMac::GetFieldBaseOffset(const ObjCInterfaceDecl *OI, |
| 1912 | const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 1913 | const FieldDecl *Field) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 1914 | const ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field); |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 1915 | const FieldDecl *FD = OI->lookupFieldDeclForIvar(CGM.getContext(), Ivar); |
| 1916 | return GetIvarBaseOffset(Layout, FD); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1919 | llvm::GlobalVariable * |
| 1920 | CGObjCCommonMac::CreateMetadataVar(const std::string &Name, |
| 1921 | llvm::Constant *Init, |
| 1922 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 1923 | unsigned Align, |
| 1924 | bool AddToUsed) { |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1925 | const llvm::Type *Ty = Init->getType(); |
| 1926 | llvm::GlobalVariable *GV = |
| 1927 | new llvm::GlobalVariable(Ty, false, |
| 1928 | llvm::GlobalValue::InternalLinkage, |
| 1929 | Init, |
| 1930 | Name, |
| 1931 | &CGM.getModule()); |
| 1932 | if (Section) |
| 1933 | GV->setSection(Section); |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 1934 | if (Align) |
| 1935 | GV->setAlignment(Align); |
| 1936 | if (AddToUsed) |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1937 | UsedGlobals.push_back(GV); |
| 1938 | return GV; |
| 1939 | } |
| 1940 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1941 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1942 | // Abuse this interface function as a place to finalize. |
| 1943 | FinishModule(); |
| 1944 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1945 | return NULL; |
| 1946 | } |
| 1947 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1948 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 1949 | return ObjCTypes.GetPropertyFn; |
| 1950 | } |
| 1951 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1952 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 1953 | return ObjCTypes.SetPropertyFn; |
| 1954 | } |
| 1955 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1956 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1957 | return ObjCTypes.EnumerationMutationFn; |
| 1958 | } |
| 1959 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1960 | /* |
| 1961 | |
| 1962 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 1963 | -- |
| 1964 | |
| 1965 | The basic framework for a @try-catch-finally is as follows: |
| 1966 | { |
| 1967 | objc_exception_data d; |
| 1968 | id _rethrow = null; |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 1969 | bool _call_try_exit = true; |
| 1970 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1971 | objc_exception_try_enter(&d); |
| 1972 | if (!setjmp(d.jmp_buf)) { |
| 1973 | ... try body ... |
| 1974 | } else { |
| 1975 | // exception path |
| 1976 | id _caught = objc_exception_extract(&d); |
| 1977 | |
| 1978 | // enter new try scope for handlers |
| 1979 | if (!setjmp(d.jmp_buf)) { |
| 1980 | ... match exception and execute catch blocks ... |
| 1981 | |
| 1982 | // fell off end, rethrow. |
| 1983 | _rethrow = _caught; |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1984 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1985 | } else { |
| 1986 | // exception in catch block |
| 1987 | _rethrow = objc_exception_extract(&d); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 1988 | _call_try_exit = false; |
| 1989 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1990 | } |
| 1991 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1992 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1993 | |
| 1994 | finally: |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 1995 | if (_call_try_exit) |
| 1996 | objc_exception_try_exit(&d); |
| 1997 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1998 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1999 | ... dispatch to finally destination ... |
| 2000 | |
| 2001 | finally_rethrow: |
| 2002 | objc_exception_throw(_rethrow); |
| 2003 | |
| 2004 | finally_end: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
| 2007 | This framework differs slightly from the one gcc uses, in that gcc |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2008 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 2009 | and if the object should be rethrown. This breaks in the face of |
| 2010 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2011 | |
| 2012 | We specialize this framework for a few particular circumstances: |
| 2013 | |
| 2014 | - If there are no catch blocks, then we avoid emitting the second |
| 2015 | exception handling context. |
| 2016 | |
| 2017 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 2018 | e)) we avoid emitting the code to rethrow an uncaught exception. |
| 2019 | |
| 2020 | - FIXME: If there is no @finally block we can do a few more |
| 2021 | simplifications. |
| 2022 | |
| 2023 | Rethrows and Jumps-Through-Finally |
| 2024 | -- |
| 2025 | |
| 2026 | Support for implicit rethrows and jumping through the finally block is |
| 2027 | handled by storing the current exception-handling context in |
| 2028 | ObjCEHStack. |
| 2029 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2030 | In order to implement proper @finally semantics, we support one basic |
| 2031 | mechanism for jumping through the finally block to an arbitrary |
| 2032 | destination. Constructs which generate exits from a @try or @catch |
| 2033 | block use this mechanism to implement the proper semantics by chaining |
| 2034 | jumps, as necessary. |
| 2035 | |
| 2036 | This mechanism works like the one used for indirect goto: we |
| 2037 | arbitrarily assign an ID to each destination and store the ID for the |
| 2038 | destination in a variable prior to entering the finally block. At the |
| 2039 | end of the finally block we simply create a switch to the proper |
| 2040 | destination. |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2041 | |
| 2042 | Code gen for @synchronized(expr) stmt; |
| 2043 | Effectively generating code for: |
| 2044 | objc_sync_enter(expr); |
| 2045 | @try stmt @finally { objc_sync_exit(expr); } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2046 | */ |
| 2047 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2048 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 2049 | const Stmt &S) { |
| 2050 | bool isTry = isa<ObjCAtTryStmt>(S); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2051 | // Create various blocks we refer to for handling @finally. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2052 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2053 | llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit"); |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2054 | llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit"); |
| 2055 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
| 2056 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2057 | |
| 2058 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 2059 | // evaluation of the expression must occur before we enter the |
| 2060 | // @synchronized. We can safely avoid a temp here because jumps into |
| 2061 | // @synchronized are illegal & this will dominate uses. |
| 2062 | llvm::Value *SyncArg = 0; |
| 2063 | if (!isTry) { |
| 2064 | SyncArg = |
| 2065 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 2066 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 2067 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2068 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2069 | |
| 2070 | // Push an EH context entry, used for handling rethrows and jumps |
| 2071 | // through finally. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2072 | CGF.PushCleanupBlock(FinallyBlock); |
| 2073 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2074 | CGF.ObjCEHValueStack.push_back(0); |
| 2075 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2076 | // Allocate memory for the exception data and rethrow pointer. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2077 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 2078 | "exceptiondata.ptr"); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2079 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy, |
| 2080 | "_rethrow"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2081 | llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty, |
| 2082 | "_call_try_exit"); |
| 2083 | CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr); |
| 2084 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2085 | // Enter a new try block and call setjmp. |
| 2086 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData); |
| 2087 | llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0, |
| 2088 | "jmpbufarray"); |
| 2089 | JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp"); |
| 2090 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn, |
| 2091 | JmpBufPtr, "result"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2092 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2093 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 2094 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2095 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2096 | TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2097 | |
| 2098 | // Emit the @try block. |
| 2099 | CGF.EmitBlock(TryBlock); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2100 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 2101 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2102 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2103 | |
| 2104 | // Emit the "exception in @try" block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2105 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2106 | |
| 2107 | // Retrieve the exception object. We may emit multiple blocks but |
| 2108 | // nothing can cross this so the value is already in SSA form. |
| 2109 | llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn, |
| 2110 | ExceptionData, |
| 2111 | "caught"); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2112 | CGF.ObjCEHValueStack.back() = Caught; |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2113 | if (!isTry) |
| 2114 | { |
| 2115 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2116 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2117 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2118 | } |
| 2119 | else if (const ObjCAtCatchStmt* CatchStmt = |
| 2120 | cast<ObjCAtTryStmt>(S).getCatchStmts()) |
| 2121 | { |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2122 | // Enter a new exception try block (in case a @catch block throws |
| 2123 | // an exception). |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2124 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2125 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2126 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn, |
| 2127 | JmpBufPtr, "result"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2128 | llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2129 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2130 | llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch"); |
| 2131 | llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler"); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2132 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2133 | |
| 2134 | CGF.EmitBlock(CatchBlock); |
| 2135 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2136 | // Handle catch list. As a special case we check if everything is |
| 2137 | // matched and avoid generating code for falling off the end if |
| 2138 | // so. |
| 2139 | bool AllMatched = false; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2140 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2141 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2142 | |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2143 | const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2144 | const PointerType *PT = 0; |
| 2145 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2146 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2147 | if (!CatchParam) { |
| 2148 | AllMatched = true; |
| 2149 | } else { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2150 | PT = CatchParam->getType()->getAsPointerType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2151 | |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 2152 | // catch(id e) always matches. |
| 2153 | // FIXME: For the time being we also match id<X>; this should |
| 2154 | // be rejected by Sema instead. |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2155 | if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) || |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2156 | CatchParam->getType()->isObjCQualifiedIdType()) |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2157 | AllMatched = true; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2158 | } |
| 2159 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2160 | if (AllMatched) { |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2161 | if (CatchParam) { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2162 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2163 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2164 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2165 | } |
Anders Carlsson | 1452f55 | 2008-09-11 08:21:54 +0000 | [diff] [blame] | 2166 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2167 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2168 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2169 | break; |
| 2170 | } |
| 2171 | |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2172 | assert(PT && "Unexpected non-pointer type in @catch"); |
| 2173 | QualType T = PT->getPointeeType(); |
Anders Carlsson | 4b7ff6e | 2008-09-11 06:35:14 +0000 | [diff] [blame] | 2174 | const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2175 | assert(ObjCType && "Catch parameter must have Objective-C type!"); |
| 2176 | |
| 2177 | // Check if the @catch block matches the exception object. |
| 2178 | llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl()); |
| 2179 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2180 | llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn, |
| 2181 | Class, Caught, "match"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2182 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2183 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2184 | |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2185 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2186 | MatchedBlock, NextCatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2187 | |
| 2188 | // Emit the @catch block. |
| 2189 | CGF.EmitBlock(MatchedBlock); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2190 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2191 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2192 | |
| 2193 | llvm::Value *Tmp = |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2194 | CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()), |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2195 | "tmp"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2196 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2197 | |
| 2198 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2199 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2200 | |
| 2201 | CGF.EmitBlock(NextCatchBlock); |
| 2202 | } |
| 2203 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2204 | if (!AllMatched) { |
| 2205 | // None of the handlers caught the exception, so store it to be |
| 2206 | // rethrown at the end of the @finally block. |
| 2207 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2208 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
| 2211 | // Emit the exception handler for the @catch blocks. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2212 | CGF.EmitBlock(CatchHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2213 | CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn, |
| 2214 | ExceptionData), |
| 2215 | RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2216 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2217 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2218 | } else { |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2219 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2220 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2221 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2222 | } |
| 2223 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2224 | // Pop the exception-handling stack entry. It is important to do |
| 2225 | // this now, because the code in the @finally block is not in this |
| 2226 | // context. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2227 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 2228 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2229 | CGF.ObjCEHValueStack.pop_back(); |
| 2230 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2231 | // Emit the @finally block. |
| 2232 | CGF.EmitBlock(FinallyBlock); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2233 | llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp"); |
| 2234 | |
| 2235 | CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit); |
| 2236 | |
| 2237 | CGF.EmitBlock(FinallyExit); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2238 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2239 | |
| 2240 | CGF.EmitBlock(FinallyNoExit); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2241 | if (isTry) { |
| 2242 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 2243 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 2244 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2245 | } else { |
| 2246 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 2247 | // @synchronized. |
| 2248 | CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg); |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 2249 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2250 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2251 | // Emit the switch block |
| 2252 | if (Info.SwitchBlock) |
| 2253 | CGF.EmitBlock(Info.SwitchBlock); |
| 2254 | if (Info.EndBlock) |
| 2255 | CGF.EmitBlock(Info.EndBlock); |
| 2256 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2257 | CGF.EmitBlock(FinallyRethrow); |
| 2258 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, |
| 2259 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2260 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2261 | |
| 2262 | CGF.EmitBlock(FinallyEnd); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2263 | } |
| 2264 | |
| 2265 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2266 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2267 | llvm::Value *ExceptionAsObject; |
| 2268 | |
| 2269 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 2270 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
| 2271 | ExceptionAsObject = |
| 2272 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 2273 | } else { |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2274 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2275 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2276 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2280 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2281 | |
| 2282 | // Clear the insertion point to indicate we are in unreachable code. |
| 2283 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2284 | } |
| 2285 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2286 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2287 | /// object: objc_read_weak (id *src) |
| 2288 | /// |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2289 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2290 | llvm::Value *AddrWeakObj) |
| 2291 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2292 | const llvm::Type* DestTy = |
| 2293 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2294 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2295 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn, |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2296 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2297 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2298 | return read_weak; |
| 2299 | } |
| 2300 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2301 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 2302 | /// objc_assign_weak (id src, id *dst) |
| 2303 | /// |
| 2304 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 2305 | llvm::Value *src, llvm::Value *dst) |
| 2306 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2307 | const llvm::Type * SrcTy = src->getType(); |
| 2308 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2309 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2310 | assert(Size <= 8 && "does not support size > 8"); |
| 2311 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2312 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2313 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2314 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2315 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2316 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 2317 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2318 | src, dst, "weakassign"); |
| 2319 | return; |
| 2320 | } |
| 2321 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2322 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 2323 | /// objc_assign_global (id src, id *dst) |
| 2324 | /// |
| 2325 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 2326 | llvm::Value *src, llvm::Value *dst) |
| 2327 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2328 | const llvm::Type * SrcTy = src->getType(); |
| 2329 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2330 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2331 | assert(Size <= 8 && "does not support size > 8"); |
| 2332 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2333 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2334 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2335 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2336 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2337 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2338 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn, |
| 2339 | src, dst, "globalassign"); |
| 2340 | return; |
| 2341 | } |
| 2342 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2343 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 2344 | /// objc_assign_ivar (id src, id *dst) |
| 2345 | /// |
| 2346 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 2347 | llvm::Value *src, llvm::Value *dst) |
| 2348 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2349 | const llvm::Type * SrcTy = src->getType(); |
| 2350 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2351 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2352 | assert(Size <= 8 && "does not support size > 8"); |
| 2353 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2354 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2355 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2356 | } |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2357 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2358 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 2359 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn, |
| 2360 | src, dst, "assignivar"); |
| 2361 | return; |
| 2362 | } |
| 2363 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2364 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 2365 | /// objc_assign_strongCast (id src, id *dst) |
| 2366 | /// |
| 2367 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 2368 | llvm::Value *src, llvm::Value *dst) |
| 2369 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2370 | const llvm::Type * SrcTy = src->getType(); |
| 2371 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2372 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2373 | assert(Size <= 8 && "does not support size > 8"); |
| 2374 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2375 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2376 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2377 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2378 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2379 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2380 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn, |
| 2381 | src, dst, "weakassign"); |
| 2382 | return; |
| 2383 | } |
| 2384 | |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2385 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 2386 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2387 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 2388 | QualType ObjectTy, |
| 2389 | llvm::Value *BaseValue, |
| 2390 | const ObjCIvarDecl *Ivar, |
| 2391 | const FieldDecl *Field, |
| 2392 | unsigned CVRQualifiers) { |
| 2393 | if (Ivar->isBitField()) |
| 2394 | return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field), |
| 2395 | CVRQualifiers); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2396 | // TODO: Add a special case for isa (index 0) |
| 2397 | unsigned Index = CGM.getTypes().getLLVMFieldNo(Field); |
| 2398 | llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp"); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2399 | LValue LV = LValue::MakeAddr(V, |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 2400 | Ivar->getType().getCVRQualifiers()|CVRQualifiers, |
| 2401 | CGM.getContext().getObjCGCAttrKind(Ivar->getType())); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2402 | LValue::SetObjCIvar(LV, true); |
| 2403 | return LV; |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2404 | } |
| 2405 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2406 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
| 2407 | ObjCInterfaceDecl *Interface, |
| 2408 | const ObjCIvarDecl *Ivar) { |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 2409 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(Interface); |
Daniel Dunbar | 60952f9 | 2009-04-20 00:37:55 +0000 | [diff] [blame] | 2410 | const FieldDecl *Field = |
| 2411 | Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 2412 | uint64_t Offset = GetIvarBaseOffset(Layout, Field); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2413 | return llvm::ConstantInt::get( |
| 2414 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 2415 | Offset); |
| 2416 | } |
| 2417 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2418 | /* *** Private Interface *** */ |
| 2419 | |
| 2420 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 2421 | /// level information. |
| 2422 | /// |
| 2423 | /// See: <rdr://4810609&4810587&4810587> |
| 2424 | /// struct IMAGE_INFO { |
| 2425 | /// unsigned version; |
| 2426 | /// unsigned flags; |
| 2427 | /// }; |
| 2428 | enum ImageInfoFlags { |
| 2429 | eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies |
| 2430 | eImageInfo_GarbageCollected = (1 << 1), |
| 2431 | eImageInfo_GCOnly = (1 << 2) |
| 2432 | }; |
| 2433 | |
| 2434 | void CGObjCMac::EmitImageInfo() { |
| 2435 | unsigned version = 0; // Version is unused? |
| 2436 | unsigned flags = 0; |
| 2437 | |
| 2438 | // FIXME: Fix and continue? |
| 2439 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 2440 | flags |= eImageInfo_GarbageCollected; |
| 2441 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 2442 | flags |= eImageInfo_GCOnly; |
| 2443 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2444 | // Emitted as int[2]; |
| 2445 | llvm::Constant *values[2] = { |
| 2446 | llvm::ConstantInt::get(llvm::Type::Int32Ty, version), |
| 2447 | llvm::ConstantInt::get(llvm::Type::Int32Ty, flags) |
| 2448 | }; |
| 2449 | llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2450 | |
| 2451 | const char *Section; |
| 2452 | if (ObjCABI == 1) |
| 2453 | Section = "__OBJC, __image_info,regular"; |
| 2454 | else |
| 2455 | Section = "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2456 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2457 | CreateMetadataVar("\01L_OBJC_IMAGE_INFO", |
| 2458 | llvm::ConstantArray::get(AT, values, 2), |
| 2459 | Section, |
| 2460 | 0, |
| 2461 | true); |
| 2462 | GV->setConstant(true); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2463 | } |
| 2464 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2465 | |
| 2466 | // struct objc_module { |
| 2467 | // unsigned long version; |
| 2468 | // unsigned long size; |
| 2469 | // const char *name; |
| 2470 | // Symtab symtab; |
| 2471 | // }; |
| 2472 | |
| 2473 | // FIXME: Get from somewhere |
| 2474 | static const int ModuleVersion = 7; |
| 2475 | |
| 2476 | void CGObjCMac::EmitModuleInfo() { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 2477 | uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2478 | |
| 2479 | std::vector<llvm::Constant*> Values(4); |
| 2480 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion); |
| 2481 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2482 | // This used to be the filename, now it is unused. <rdr://4327263> |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2483 | Values[2] = GetClassName(&CGM.getContext().Idents.get("")); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2484 | Values[3] = EmitModuleSymbols(); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2485 | CreateMetadataVar("\01L_OBJC_MODULES", |
| 2486 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
| 2487 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2488 | 4, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2489 | } |
| 2490 | |
| 2491 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2492 | unsigned NumClasses = DefinedClasses.size(); |
| 2493 | unsigned NumCategories = DefinedCategories.size(); |
| 2494 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2495 | // Return null if no symbols were defined. |
| 2496 | if (!NumClasses && !NumCategories) |
| 2497 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
| 2498 | |
| 2499 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2500 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2501 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
| 2502 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 2503 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
| 2504 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2505 | // The runtime expects exactly the list of defined classes followed |
| 2506 | // by the list of defined categories, in a single array. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2507 | std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2508 | for (unsigned i=0; i<NumClasses; i++) |
| 2509 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 2510 | ObjCTypes.Int8PtrTy); |
| 2511 | for (unsigned i=0; i<NumCategories; i++) |
| 2512 | Symbols[NumClasses + i] = |
| 2513 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 2514 | ObjCTypes.Int8PtrTy); |
| 2515 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2516 | Values[4] = |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2517 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2518 | NumClasses + NumCategories), |
| 2519 | Symbols); |
| 2520 | |
| 2521 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2522 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2523 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2524 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 2525 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2526 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2527 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
| 2528 | } |
| 2529 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2530 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2531 | const ObjCInterfaceDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2532 | LazySymbols.insert(ID->getIdentifier()); |
| 2533 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2534 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 2535 | |
| 2536 | if (!Entry) { |
| 2537 | llvm::Constant *Casted = |
| 2538 | llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()), |
| 2539 | ObjCTypes.ClassPtrTy); |
| 2540 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2541 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 2542 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2543 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2544 | } |
| 2545 | |
| 2546 | return Builder.CreateLoad(Entry, false, "tmp"); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2547 | } |
| 2548 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2549 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2550 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 2551 | |
| 2552 | if (!Entry) { |
| 2553 | llvm::Constant *Casted = |
| 2554 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 2555 | ObjCTypes.SelectorPtrTy); |
| 2556 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2557 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 2558 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2559 | 4, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2560 | } |
| 2561 | |
| 2562 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 2563 | } |
| 2564 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 2565 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2566 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2567 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2568 | if (!Entry) |
| 2569 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 2570 | llvm::ConstantArray::get(Ident->getName()), |
| 2571 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 2572 | 1, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2573 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2574 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2575 | } |
| 2576 | |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 2577 | /// GetInterfaceDeclStructLayout - Get layout for ivars of given |
| 2578 | /// interface declaration. |
| 2579 | const llvm::StructLayout *CGObjCCommonMac::GetInterfaceDeclStructLayout( |
| 2580 | const ObjCInterfaceDecl *OID) const { |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 2581 | const llvm::Type *InterfaceTy; |
| 2582 | |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 2583 | // FIXME: When does this happen? It seems pretty bad to do this... |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 2584 | if (OID->isForwardDecl()) { |
| 2585 | InterfaceTy = llvm::StructType::get(NULL, NULL); |
| 2586 | } else { |
| 2587 | QualType T = CGM.getContext().getObjCInterfaceType( |
| 2588 | const_cast<ObjCInterfaceDecl*>(OID)); |
| 2589 | InterfaceTy = CGM.getTypes().ConvertType(T); |
| 2590 | } |
| 2591 | |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 2592 | const llvm::StructLayout *Layout = |
| 2593 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy)); |
| 2594 | return Layout; |
| 2595 | } |
| 2596 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 2597 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 2598 | /// ivar layout bitmap. |
| 2599 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
| 2600 | const ObjCCommonTypesHelper &ObjCTypes) { |
| 2601 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 2602 | } |
| 2603 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2604 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCInterfaceDecl *OI, |
| 2605 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2606 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2607 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2608 | unsigned int BytePos, bool ForStrongLayout, |
| 2609 | int &Index, int &SkIndex, bool &HasUnion) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2610 | bool IsUnion = (RD && RD->isUnion()); |
| 2611 | uint64_t MaxUnionIvarSize = 0; |
| 2612 | uint64_t MaxSkippedUnionIvarSize = 0; |
| 2613 | FieldDecl *MaxField = 0; |
| 2614 | FieldDecl *MaxSkippedField = 0; |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2615 | unsigned base = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2616 | if (RecFields.empty()) |
| 2617 | return; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2618 | if (IsUnion) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2619 | base = BytePos + GetFieldBaseOffset(OI, Layout, RecFields[0]); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2620 | unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0); |
| 2621 | unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth(); |
| 2622 | |
| 2623 | llvm::SmallVector<FieldDecl*, 16> TmpRecFields; |
| 2624 | |
| 2625 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2626 | FieldDecl *Field = RecFields[i]; |
| 2627 | // Skip over unnamed or bitfields |
| 2628 | if (!Field->getIdentifier() || Field->isBitField()) |
| 2629 | continue; |
| 2630 | QualType FQT = Field->getType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2631 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2632 | if (FQT->isUnionType()) |
| 2633 | HasUnion = true; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2634 | else |
| 2635 | assert(FQT->isRecordType() && |
| 2636 | "only union/record is supported for ivar layout bitmap"); |
| 2637 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2638 | const RecordType *RT = FQT->getAsRecordType(); |
| 2639 | const RecordDecl *RD = RT->getDecl(); |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 2640 | // FIXME - Find a more efficient way of passing records down. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2641 | TmpRecFields.append(RD->field_begin(CGM.getContext()), |
| 2642 | RD->field_end(CGM.getContext())); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2643 | // FIXME - Is Layout correct? |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2644 | BuildAggrIvarLayout(OI, Layout, RD, TmpRecFields, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2645 | BytePos + GetFieldBaseOffset(OI, Layout, Field), |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2646 | ForStrongLayout, Index, SkIndex, |
| 2647 | HasUnion); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2648 | TmpRecFields.clear(); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2649 | continue; |
| 2650 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2651 | |
| 2652 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2653 | const ConstantArrayType *CArray = |
| 2654 | dyn_cast_or_null<ConstantArrayType>(Array); |
| 2655 | assert(CArray && "only array with know element size is supported"); |
| 2656 | FQT = CArray->getElementType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2657 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 2658 | const ConstantArrayType *CArray = |
| 2659 | dyn_cast_or_null<ConstantArrayType>(Array); |
| 2660 | FQT = CArray->getElementType(); |
| 2661 | } |
| 2662 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2663 | assert(!FQT->isUnionType() && |
| 2664 | "layout for array of unions not supported"); |
| 2665 | if (FQT->isRecordType()) { |
| 2666 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
| 2667 | int OldIndex = Index; |
| 2668 | int OldSkIndex = SkIndex; |
| 2669 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2670 | // FIXME - Use a common routine with the above! |
| 2671 | const RecordType *RT = FQT->getAsRecordType(); |
| 2672 | const RecordDecl *RD = RT->getDecl(); |
| 2673 | // FIXME - Find a more efficiant way of passing records down. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2674 | TmpRecFields.append(RD->field_begin(CGM.getContext()), |
| 2675 | RD->field_end(CGM.getContext())); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2676 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2677 | BuildAggrIvarLayout(OI, Layout, RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2678 | TmpRecFields, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2679 | BytePos + GetFieldBaseOffset(OI, Layout, Field), |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2680 | ForStrongLayout, Index, SkIndex, |
| 2681 | HasUnion); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2682 | TmpRecFields.clear(); |
| 2683 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2684 | // Replicate layout information for each array element. Note that |
| 2685 | // one element is already done. |
| 2686 | uint64_t ElIx = 1; |
| 2687 | for (int FirstIndex = Index, FirstSkIndex = SkIndex; |
| 2688 | ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2689 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2690 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 2691 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2692 | GC_IVAR gcivar; |
| 2693 | gcivar.ivar_bytepos = IvarsInfo[i].ivar_bytepos + Size*ElIx; |
| 2694 | gcivar.ivar_size = IvarsInfo[i].ivar_size; |
| 2695 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2696 | } |
| 2697 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2698 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2699 | GC_IVAR skivar; |
| 2700 | skivar.ivar_bytepos = SkipIvars[i].ivar_bytepos + Size*ElIx; |
| 2701 | skivar.ivar_size = SkipIvars[i].ivar_size; |
| 2702 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2703 | } |
| 2704 | } |
| 2705 | continue; |
| 2706 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2707 | } |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2708 | // At this point, we are done with Record/Union and array there of. |
| 2709 | // For other arrays we are down to its element type. |
| 2710 | QualType::GCAttrTypes GCAttr = QualType::GCNone; |
| 2711 | do { |
| 2712 | if (FQT.isObjCGCStrong() || FQT.isObjCGCWeak()) { |
| 2713 | GCAttr = FQT.isObjCGCStrong() ? QualType::Strong : QualType::Weak; |
| 2714 | break; |
| 2715 | } |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2716 | else if (CGM.getContext().isObjCObjectPointerType(FQT)) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2717 | GCAttr = QualType::Strong; |
| 2718 | break; |
| 2719 | } |
| 2720 | else if (const PointerType *PT = FQT->getAsPointerType()) { |
| 2721 | FQT = PT->getPointeeType(); |
| 2722 | } |
| 2723 | else { |
| 2724 | break; |
| 2725 | } |
| 2726 | } while (true); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2727 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2728 | if ((ForStrongLayout && GCAttr == QualType::Strong) |
| 2729 | || (!ForStrongLayout && GCAttr == QualType::Weak)) { |
| 2730 | if (IsUnion) |
| 2731 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2732 | uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType()) |
| 2733 | / WordSizeInBits; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2734 | if (UnionIvarSize > MaxUnionIvarSize) |
| 2735 | { |
| 2736 | MaxUnionIvarSize = UnionIvarSize; |
| 2737 | MaxField = Field; |
| 2738 | } |
| 2739 | } |
| 2740 | else |
| 2741 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2742 | GC_IVAR gcivar; |
| 2743 | gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field); |
| 2744 | gcivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) / |
| 2745 | WordSizeInBits; |
| 2746 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2747 | } |
| 2748 | } |
| 2749 | else if ((ForStrongLayout && |
| 2750 | (GCAttr == QualType::GCNone || GCAttr == QualType::Weak)) |
| 2751 | || (!ForStrongLayout && GCAttr != QualType::Weak)) { |
| 2752 | if (IsUnion) |
| 2753 | { |
| 2754 | uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType()); |
| 2755 | if (UnionIvarSize > MaxSkippedUnionIvarSize) |
| 2756 | { |
| 2757 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 2758 | MaxSkippedField = Field; |
| 2759 | } |
| 2760 | } |
| 2761 | else |
| 2762 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2763 | GC_IVAR skivar; |
| 2764 | skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field); |
| 2765 | skivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) / |
| 2766 | WordSizeInBits; |
| 2767 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2768 | } |
| 2769 | } |
| 2770 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2771 | if (MaxField) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2772 | GC_IVAR gcivar; |
| 2773 | gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, MaxField); |
| 2774 | gcivar.ivar_size = MaxUnionIvarSize; |
| 2775 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2776 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2777 | |
| 2778 | if (MaxSkippedField) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2779 | GC_IVAR skivar; |
| 2780 | skivar.ivar_bytepos = BytePos + |
| 2781 | GetFieldBaseOffset(OI, Layout, MaxSkippedField); |
| 2782 | skivar.ivar_size = MaxSkippedUnionIvarSize; |
| 2783 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2784 | } |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2785 | } |
| 2786 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2787 | static int |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2788 | IvarBytePosCompare(const void *a, const void *b) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2789 | { |
| 2790 | unsigned int sa = ((CGObjCCommonMac::GC_IVAR *)a)->ivar_bytepos; |
| 2791 | unsigned int sb = ((CGObjCCommonMac::GC_IVAR *)b)->ivar_bytepos; |
| 2792 | |
| 2793 | if (sa < sb) |
| 2794 | return -1; |
| 2795 | if (sa > sb) |
| 2796 | return 1; |
| 2797 | return 0; |
| 2798 | } |
| 2799 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2800 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 2801 | /// implementation for the __strong or __weak case. |
| 2802 | /// The layout map displays which words in ivar list must be skipped |
| 2803 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 2804 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
| 2805 | /// of words to skip and right nibble is count of words to scan. So, each |
| 2806 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
| 2807 | /// represented by a 0x00 byte which also ends the string. |
| 2808 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 2809 | /// - id, Class |
| 2810 | /// - object * |
| 2811 | /// - __strong anything |
| 2812 | /// |
| 2813 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 2814 | /// - __weak anything |
| 2815 | /// |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2816 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2817 | const ObjCImplementationDecl *OMD, |
| 2818 | bool ForStrongLayout) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2819 | int Index = -1; |
| 2820 | int SkIndex = -1; |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2821 | bool hasUnion = false; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2822 | int SkipScan; |
| 2823 | unsigned int WordsToScan, WordsToSkip; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2824 | const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 2825 | if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) |
| 2826 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2827 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2828 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2829 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2830 | CGM.getContext().CollectObjCIvars(OI, RecFields); |
| 2831 | if (RecFields.empty()) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2832 | return llvm::Constant::getNullValue(PtrTy); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2833 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2834 | SkipIvars.clear(); |
| 2835 | IvarsInfo.clear(); |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 2836 | |
| 2837 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OI); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2838 | BuildAggrIvarLayout(OI, Layout, 0, RecFields, 0, ForStrongLayout, |
| 2839 | Index, SkIndex, hasUnion); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2840 | if (Index == -1) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2841 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2842 | |
| 2843 | // Sort on byte position in case we encounterred a union nested in |
| 2844 | // the ivar list. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2845 | if (hasUnion && !IvarsInfo.empty()) |
| 2846 | qsort(&IvarsInfo[0], Index+1, sizeof(GC_IVAR), IvarBytePosCompare); |
| 2847 | if (hasUnion && !SkipIvars.empty()) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2848 | qsort(&SkipIvars[0], Index+1, sizeof(GC_IVAR), IvarBytePosCompare); |
| 2849 | |
| 2850 | // Build the string of skip/scan nibbles |
| 2851 | SkipScan = -1; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2852 | SkipScanIvars.clear(); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2853 | unsigned int WordSize = |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2854 | CGM.getTypes().getTargetData().getTypePaddedSize(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2855 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 2856 | WordsToSkip = 0; |
| 2857 | WordsToScan = IvarsInfo[0].ivar_size; |
| 2858 | } |
| 2859 | else { |
| 2860 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 2861 | WordsToScan = IvarsInfo[0].ivar_size; |
| 2862 | } |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2863 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2864 | { |
| 2865 | unsigned int TailPrevGCObjC = |
| 2866 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
| 2867 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) |
| 2868 | { |
| 2869 | // consecutive 'scanned' object pointers. |
| 2870 | WordsToScan += IvarsInfo[i].ivar_size; |
| 2871 | } |
| 2872 | else |
| 2873 | { |
| 2874 | // Skip over 'gc'able object pointer which lay over each other. |
| 2875 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 2876 | continue; |
| 2877 | // Must skip over 1 or more words. We save current skip/scan values |
| 2878 | // and start a new pair. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2879 | SKIP_SCAN SkScan; |
| 2880 | SkScan.skip = WordsToSkip; |
| 2881 | SkScan.scan = WordsToScan; |
| 2882 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
| 2883 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2884 | // Skip the hole. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2885 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 2886 | SkScan.scan = 0; |
| 2887 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2888 | WordsToSkip = 0; |
| 2889 | WordsToScan = IvarsInfo[i].ivar_size; |
| 2890 | } |
| 2891 | } |
| 2892 | if (WordsToScan > 0) |
| 2893 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2894 | SKIP_SCAN SkScan; |
| 2895 | SkScan.skip = WordsToSkip; |
| 2896 | SkScan.scan = WordsToScan; |
| 2897 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2898 | } |
| 2899 | |
| 2900 | bool BytesSkipped = false; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2901 | if (!SkipIvars.empty()) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2902 | { |
| 2903 | int LastByteSkipped = |
| 2904 | SkipIvars[SkIndex].ivar_bytepos + SkipIvars[SkIndex].ivar_size; |
| 2905 | int LastByteScanned = |
| 2906 | IvarsInfo[Index].ivar_bytepos + IvarsInfo[Index].ivar_size * WordSize; |
| 2907 | BytesSkipped = (LastByteSkipped > LastByteScanned); |
| 2908 | // Compute number of bytes to skip at the tail end of the last ivar scanned. |
| 2909 | if (BytesSkipped) |
| 2910 | { |
| 2911 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2912 | SKIP_SCAN SkScan; |
| 2913 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
| 2914 | SkScan.scan = 0; |
| 2915 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2916 | } |
| 2917 | } |
| 2918 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 2919 | // as 0xMN. |
| 2920 | for (int i = 0; i <= SkipScan; i++) |
| 2921 | { |
| 2922 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 2923 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 2924 | // 0xM0 followed by 0x0N detected. |
| 2925 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 2926 | for (int j = i+1; j < SkipScan; j++) |
| 2927 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 2928 | --SkipScan; |
| 2929 | } |
| 2930 | } |
| 2931 | |
| 2932 | // Generate the string. |
| 2933 | std::string BitMap; |
| 2934 | for (int i = 0; i <= SkipScan; i++) |
| 2935 | { |
| 2936 | unsigned char byte; |
| 2937 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 2938 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 2939 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 2940 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
| 2941 | |
| 2942 | if (skip_small > 0 || skip_big > 0) |
| 2943 | BytesSkipped = true; |
| 2944 | // first skip big. |
| 2945 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 2946 | BitMap += (unsigned char)(0xf0); |
| 2947 | |
| 2948 | // next (skip small, scan) |
| 2949 | if (skip_small) |
| 2950 | { |
| 2951 | byte = skip_small << 4; |
| 2952 | if (scan_big > 0) |
| 2953 | { |
| 2954 | byte |= 0xf; |
| 2955 | --scan_big; |
| 2956 | } |
| 2957 | else if (scan_small) |
| 2958 | { |
| 2959 | byte |= scan_small; |
| 2960 | scan_small = 0; |
| 2961 | } |
| 2962 | BitMap += byte; |
| 2963 | } |
| 2964 | // next scan big |
| 2965 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 2966 | BitMap += (unsigned char)(0x0f); |
| 2967 | // last scan small |
| 2968 | if (scan_small) |
| 2969 | { |
| 2970 | byte = scan_small; |
| 2971 | BitMap += byte; |
| 2972 | } |
| 2973 | } |
| 2974 | // null terminate string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2975 | unsigned char zero = 0; |
| 2976 | BitMap += zero; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2977 | // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as |
| 2978 | // final layout. |
| 2979 | if (ForStrongLayout && !BytesSkipped) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2980 | return llvm::Constant::getNullValue(PtrTy); |
| 2981 | llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 2982 | llvm::ConstantArray::get(BitMap.c_str()), |
| 2983 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 2984 | 1, true); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2985 | // FIXME. Need a commomand-line option for this eventually. |
| 2986 | if (ForStrongLayout) |
| 2987 | printf("\nstrong ivar layout: "); |
| 2988 | else |
| 2989 | printf("\nweak ivar layout: "); |
| 2990 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
| 2991 | for (unsigned i = 0; i < BitMap.size(); i++) |
Fariborz Jahanian | dbf15cb | 2009-03-26 19:10:36 +0000 | [diff] [blame] | 2992 | if (!(s[i] & 0xf0)) |
| 2993 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 2994 | else |
| 2995 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2996 | printf("\n"); |
| 2997 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2998 | return getConstantGEP(Entry, 0, 0); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2999 | } |
| 3000 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3001 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3002 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 3003 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3004 | // FIXME: Avoid std::string copying. |
| 3005 | if (!Entry) |
| 3006 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
| 3007 | llvm::ConstantArray::get(Sel.getAsString()), |
| 3008 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3009 | 1, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3010 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3011 | return getConstantGEP(Entry, 0, 0); |
| 3012 | } |
| 3013 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3014 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3015 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3016 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 3017 | } |
| 3018 | |
| 3019 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3020 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3021 | return GetMethodVarName(&CGM.getContext().Idents.get(Name)); |
| 3022 | } |
| 3023 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame^] | 3024 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3025 | std::string TypeStr; |
| 3026 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 3027 | |
| 3028 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3029 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3030 | if (!Entry) |
| 3031 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3032 | llvm::ConstantArray::get(TypeStr), |
| 3033 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3034 | 1, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3035 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3036 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3037 | } |
| 3038 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3039 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3040 | std::string TypeStr; |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3041 | CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D), |
| 3042 | TypeStr); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3043 | |
| 3044 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 3045 | |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3046 | if (!Entry) |
| 3047 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3048 | llvm::ConstantArray::get(TypeStr), |
| 3049 | "__TEXT,__cstring,cstring_literals", |
| 3050 | 1, true); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3051 | |
| 3052 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3053 | } |
| 3054 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3055 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3056 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3057 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
| 3058 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3059 | if (!Entry) |
| 3060 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
| 3061 | llvm::ConstantArray::get(Ident->getName()), |
| 3062 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3063 | 1, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3064 | |
| 3065 | return getConstantGEP(Entry, 0, 0); |
| 3066 | } |
| 3067 | |
| 3068 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3069 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3070 | llvm::Constant * |
| 3071 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 3072 | const Decl *Container) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3073 | std::string TypeStr; |
| 3074 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3075 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 3076 | } |
| 3077 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3078 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
| 3079 | const ObjCContainerDecl *CD, |
| 3080 | std::string &NameOut) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 3081 | NameOut = '\01'; |
| 3082 | NameOut += (D->isInstanceMethod() ? '-' : '+'); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3083 | NameOut += '['; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 3084 | assert (CD && "Missing container decl in GetNameForMethod"); |
| 3085 | NameOut += CD->getNameAsString(); |
Fariborz Jahanian | 1e9aef3 | 2009-04-16 18:34:20 +0000 | [diff] [blame] | 3086 | if (const ObjCCategoryImplDecl *CID = |
| 3087 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) { |
| 3088 | NameOut += '('; |
| 3089 | NameOut += CID->getNameAsString(); |
| 3090 | NameOut+= ')'; |
| 3091 | } |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3092 | NameOut += ' '; |
| 3093 | NameOut += D->getSelector().getAsString(); |
| 3094 | NameOut += ']'; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3095 | } |
| 3096 | |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 3097 | /// GetFirstIvarInRecord - This routine returns the record for the |
| 3098 | /// implementation of the fiven class OID. It also returns field |
| 3099 | /// corresponding to the first ivar in the class in FIV. It also |
| 3100 | /// returns the one before the first ivar. |
| 3101 | /// |
| 3102 | const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord( |
| 3103 | const ObjCInterfaceDecl *OID, |
| 3104 | RecordDecl::field_iterator &FIV, |
| 3105 | RecordDecl::field_iterator &PIV) { |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3106 | int countSuperClassIvars = countInheritedIvars(OID->getSuperClass(), |
| 3107 | CGM.getContext()); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 3108 | const RecordDecl *RD = CGM.getContext().addRecordToClass(OID); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3109 | RecordDecl::field_iterator ifield = RD->field_begin(CGM.getContext()); |
| 3110 | RecordDecl::field_iterator pfield = RD->field_end(CGM.getContext()); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 3111 | while (countSuperClassIvars-- > 0) { |
| 3112 | pfield = ifield; |
| 3113 | ++ifield; |
| 3114 | } |
| 3115 | FIV = ifield; |
| 3116 | PIV = pfield; |
| 3117 | return RD; |
| 3118 | } |
| 3119 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3120 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3121 | EmitModuleInfo(); |
| 3122 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3123 | // Emit the dummy bodies for any protocols which were referenced but |
| 3124 | // never defined. |
| 3125 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
| 3126 | i = Protocols.begin(), e = Protocols.end(); i != e; ++i) { |
| 3127 | if (i->second->hasInitializer()) |
| 3128 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3129 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3130 | std::vector<llvm::Constant*> Values(5); |
| 3131 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 3132 | Values[1] = GetClassName(i->first); |
| 3133 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 3134 | Values[3] = Values[4] = |
| 3135 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 3136 | i->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 3137 | i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 3138 | Values)); |
| 3139 | } |
| 3140 | |
| 3141 | std::vector<llvm::Constant*> Used; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3142 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3143 | e = UsedGlobals.end(); i != e; ++i) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3144 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3145 | } |
| 3146 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3147 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3148 | llvm::GlobalValue *GV = |
| 3149 | new llvm::GlobalVariable(AT, false, |
| 3150 | llvm::GlobalValue::AppendingLinkage, |
| 3151 | llvm::ConstantArray::get(AT, Used), |
| 3152 | "llvm.used", |
| 3153 | &CGM.getModule()); |
| 3154 | |
| 3155 | GV->setSection("llvm.metadata"); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3156 | |
| 3157 | // Add assembler directives to add lazy undefined symbol references |
| 3158 | // for classes which are referenced but not defined. This is |
| 3159 | // important for correct linker interaction. |
| 3160 | |
| 3161 | // FIXME: Uh, this isn't particularly portable. |
| 3162 | std::stringstream s; |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3163 | |
| 3164 | if (!CGM.getModule().getModuleInlineAsm().empty()) |
| 3165 | s << "\n"; |
| 3166 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3167 | for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(), |
| 3168 | e = LazySymbols.end(); i != e; ++i) { |
| 3169 | s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n"; |
| 3170 | } |
| 3171 | for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(), |
| 3172 | e = DefinedSymbols.end(); i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3173 | s << "\t.objc_class_name_" << (*i)->getName() << "=0\n" |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3174 | << "\t.globl .objc_class_name_" << (*i)->getName() << "\n"; |
| 3175 | } |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3176 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3177 | CGM.getModule().appendModuleInlineAsm(s.str()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3178 | } |
| 3179 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3180 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3181 | : CGObjCCommonMac(cgm), |
| 3182 | ObjCTypes(cgm) |
| 3183 | { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3184 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3185 | ObjCABI = 2; |
| 3186 | } |
| 3187 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3188 | /* *** */ |
| 3189 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3190 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3191 | : CGM(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3192 | { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3193 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3194 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3195 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3196 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3197 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3198 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3199 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3200 | Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 3201 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3202 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 3203 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3204 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3205 | |
| 3206 | // FIXME: It would be nice to unify this with the opaque type, so |
| 3207 | // that the IR comes out a bit cleaner. |
| 3208 | const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 3209 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3210 | |
| 3211 | // I'm not sure I like this. The implicit coordination is a bit |
| 3212 | // gross. We should solve this in a reasonable fashion because this |
| 3213 | // is a pretty common task (match some runtime data structure with |
| 3214 | // an LLVM data structure). |
| 3215 | |
| 3216 | // FIXME: This is leaked. |
| 3217 | // FIXME: Merge with rewriter code? |
| 3218 | |
| 3219 | // struct _objc_super { |
| 3220 | // id self; |
| 3221 | // Class cls; |
| 3222 | // } |
| 3223 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3224 | SourceLocation(), |
| 3225 | &Ctx.Idents.get("_objc_super")); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3226 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3227 | Ctx.getObjCIdType(), 0, false)); |
| 3228 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3229 | Ctx.getObjCClassType(), 0, false)); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3230 | RD->completeDefinition(Ctx); |
| 3231 | |
| 3232 | SuperCTy = Ctx.getTagDeclType(RD); |
| 3233 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
| 3234 | |
| 3235 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3236 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 3237 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3238 | // struct _prop_t { |
| 3239 | // char *name; |
| 3240 | // char *attributes; |
| 3241 | // } |
| 3242 | PropertyTy = llvm::StructType::get(Int8PtrTy, |
| 3243 | Int8PtrTy, |
| 3244 | NULL); |
| 3245 | CGM.getModule().addTypeName("struct._prop_t", |
| 3246 | PropertyTy); |
| 3247 | |
| 3248 | // struct _prop_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3249 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3250 | // uint32_t count_of_properties; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3251 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3252 | // } |
| 3253 | PropertyListTy = llvm::StructType::get(IntTy, |
| 3254 | IntTy, |
| 3255 | llvm::ArrayType::get(PropertyTy, 0), |
| 3256 | NULL); |
| 3257 | CGM.getModule().addTypeName("struct._prop_list_t", |
| 3258 | PropertyListTy); |
| 3259 | // struct _prop_list_t * |
| 3260 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
| 3261 | |
| 3262 | // struct _objc_method { |
| 3263 | // SEL _cmd; |
| 3264 | // char *method_type; |
| 3265 | // char *_imp; |
| 3266 | // } |
| 3267 | MethodTy = llvm::StructType::get(SelectorPtrTy, |
| 3268 | Int8PtrTy, |
| 3269 | Int8PtrTy, |
| 3270 | NULL); |
| 3271 | CGM.getModule().addTypeName("struct._objc_method", MethodTy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3272 | |
| 3273 | // struct _objc_cache * |
| 3274 | CacheTy = llvm::OpaqueType::get(); |
| 3275 | CGM.getModule().addTypeName("struct._objc_cache", CacheTy); |
| 3276 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3277 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3278 | // Property manipulation functions. |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3279 | |
| 3280 | QualType IdType = Ctx.getObjCIdType(); |
| 3281 | QualType SelType = Ctx.getObjCSelType(); |
| 3282 | llvm::SmallVector<QualType,16> Params; |
| 3283 | const llvm::FunctionType *FTy; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3284 | |
| 3285 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3286 | Params.push_back(IdType); |
| 3287 | Params.push_back(SelType); |
| 3288 | Params.push_back(Ctx.LongTy); |
| 3289 | Params.push_back(Ctx.BoolTy); |
| 3290 | FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), |
| 3291 | false); |
| 3292 | GetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_getProperty"); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3293 | |
| 3294 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
| 3295 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3296 | Params.push_back(IdType); |
| 3297 | Params.push_back(SelType); |
| 3298 | Params.push_back(Ctx.LongTy); |
| 3299 | Params.push_back(IdType); |
| 3300 | Params.push_back(Ctx.BoolTy); |
| 3301 | Params.push_back(Ctx.BoolTy); |
| 3302 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
| 3303 | SetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); |
| 3304 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3305 | // Enumeration mutation. |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3306 | |
| 3307 | // void objc_enumerationMutation (id) |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3308 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3309 | Params.push_back(IdType); |
| 3310 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
| 3311 | EnumerationMutationFn = CGM.CreateRuntimeFunction(FTy, |
| 3312 | "objc_enumerationMutation"); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3313 | |
| 3314 | // gc's API |
| 3315 | // id objc_read_weak (id *) |
| 3316 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3317 | Params.push_back(Ctx.getPointerType(IdType)); |
| 3318 | FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false); |
| 3319 | GcReadWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
| 3320 | |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 3321 | // id objc_assign_global (id, id *) |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3322 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3323 | Params.push_back(IdType); |
| 3324 | Params.push_back(Ctx.getPointerType(IdType)); |
| 3325 | |
| 3326 | FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3327 | GcAssignGlobalFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 3328 | GcAssignIvarFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 3329 | GcAssignStrongCastFn = |
| 3330 | CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 3331 | |
| 3332 | // void objc_exception_throw(id) |
| 3333 | Params.clear(); |
| 3334 | Params.push_back(IdType); |
| 3335 | |
| 3336 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 3337 | ExceptionThrowFn = |
| 3338 | CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3339 | |
| 3340 | // synchronized APIs |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3341 | // void objc_sync_exit (id) |
| 3342 | Params.clear(); |
| 3343 | Params.push_back(IdType); |
| 3344 | |
| 3345 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3346 | SyncExitFn = CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3347 | } |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3348 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3349 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3350 | : ObjCCommonTypesHelper(cgm) |
| 3351 | { |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3352 | // struct _objc_method_description { |
| 3353 | // SEL name; |
| 3354 | // char *types; |
| 3355 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3356 | MethodDescriptionTy = |
| 3357 | llvm::StructType::get(SelectorPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3358 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3359 | NULL); |
| 3360 | CGM.getModule().addTypeName("struct._objc_method_description", |
| 3361 | MethodDescriptionTy); |
| 3362 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3363 | // struct _objc_method_description_list { |
| 3364 | // int count; |
| 3365 | // struct _objc_method_description[1]; |
| 3366 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3367 | MethodDescriptionListTy = |
| 3368 | llvm::StructType::get(IntTy, |
| 3369 | llvm::ArrayType::get(MethodDescriptionTy, 0), |
| 3370 | NULL); |
| 3371 | CGM.getModule().addTypeName("struct._objc_method_description_list", |
| 3372 | MethodDescriptionListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3373 | |
| 3374 | // struct _objc_method_description_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3375 | MethodDescriptionListPtrTy = |
| 3376 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
| 3377 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3378 | // Protocol description structures |
| 3379 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3380 | // struct _objc_protocol_extension { |
| 3381 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 3382 | // struct _objc_method_description_list *optional_instance_methods; |
| 3383 | // struct _objc_method_description_list *optional_class_methods; |
| 3384 | // struct _objc_property_list *instance_properties; |
| 3385 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3386 | ProtocolExtensionTy = |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3387 | llvm::StructType::get(IntTy, |
| 3388 | MethodDescriptionListPtrTy, |
| 3389 | MethodDescriptionListPtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3390 | PropertyListPtrTy, |
| 3391 | NULL); |
| 3392 | CGM.getModule().addTypeName("struct._objc_protocol_extension", |
| 3393 | ProtocolExtensionTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3394 | |
| 3395 | // struct _objc_protocol_extension * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3396 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
| 3397 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3398 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3399 | |
| 3400 | llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(); |
| 3401 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3402 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3403 | const llvm::Type *T = |
| 3404 | llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3405 | LongTy, |
| 3406 | llvm::ArrayType::get(ProtocolTyHolder, 0), |
| 3407 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3408 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T); |
| 3409 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3410 | // struct _objc_protocol { |
| 3411 | // struct _objc_protocol_extension *isa; |
| 3412 | // char *protocol_name; |
| 3413 | // struct _objc_protocol **_objc_protocol_list; |
| 3414 | // struct _objc_method_description_list *instance_methods; |
| 3415 | // struct _objc_method_description_list *class_methods; |
| 3416 | // } |
| 3417 | T = llvm::StructType::get(ProtocolExtensionPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3418 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3419 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3420 | MethodDescriptionListPtrTy, |
| 3421 | MethodDescriptionListPtrTy, |
| 3422 | NULL); |
| 3423 | cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T); |
| 3424 | |
| 3425 | ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get()); |
| 3426 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3427 | ProtocolListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3428 | // struct _objc_protocol_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3429 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
| 3430 | |
| 3431 | ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get()); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3432 | CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3433 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3434 | |
| 3435 | // Class description structures |
| 3436 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3437 | // struct _objc_ivar { |
| 3438 | // char *ivar_name; |
| 3439 | // char *ivar_type; |
| 3440 | // int ivar_offset; |
| 3441 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3442 | IvarTy = llvm::StructType::get(Int8PtrTy, |
| 3443 | Int8PtrTy, |
| 3444 | IntTy, |
| 3445 | NULL); |
| 3446 | CGM.getModule().addTypeName("struct._objc_ivar", IvarTy); |
| 3447 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3448 | // struct _objc_ivar_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3449 | IvarListTy = llvm::OpaqueType::get(); |
| 3450 | CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy); |
| 3451 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
| 3452 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3453 | // struct _objc_method_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3454 | MethodListTy = llvm::OpaqueType::get(); |
| 3455 | CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy); |
| 3456 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
| 3457 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3458 | // struct _objc_class_extension * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3459 | ClassExtensionTy = |
| 3460 | llvm::StructType::get(IntTy, |
| 3461 | Int8PtrTy, |
| 3462 | PropertyListPtrTy, |
| 3463 | NULL); |
| 3464 | CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy); |
| 3465 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
| 3466 | |
| 3467 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3468 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3469 | // struct _objc_class { |
| 3470 | // Class isa; |
| 3471 | // Class super_class; |
| 3472 | // char *name; |
| 3473 | // long version; |
| 3474 | // long info; |
| 3475 | // long instance_size; |
| 3476 | // struct _objc_ivar_list *ivars; |
| 3477 | // struct _objc_method_list *methods; |
| 3478 | // struct _objc_cache *cache; |
| 3479 | // struct _objc_protocol_list *protocols; |
| 3480 | // char *ivar_layout; |
| 3481 | // struct _objc_class_ext *ext; |
| 3482 | // }; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3483 | T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3484 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3485 | Int8PtrTy, |
| 3486 | LongTy, |
| 3487 | LongTy, |
| 3488 | LongTy, |
| 3489 | IvarListPtrTy, |
| 3490 | MethodListPtrTy, |
| 3491 | CachePtrTy, |
| 3492 | ProtocolListPtrTy, |
| 3493 | Int8PtrTy, |
| 3494 | ClassExtensionPtrTy, |
| 3495 | NULL); |
| 3496 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T); |
| 3497 | |
| 3498 | ClassTy = cast<llvm::StructType>(ClassTyHolder.get()); |
| 3499 | CGM.getModule().addTypeName("struct._objc_class", ClassTy); |
| 3500 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
| 3501 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3502 | // struct _objc_category { |
| 3503 | // char *category_name; |
| 3504 | // char *class_name; |
| 3505 | // struct _objc_method_list *instance_method; |
| 3506 | // struct _objc_method_list *class_method; |
| 3507 | // uint32_t size; // sizeof(struct _objc_category) |
| 3508 | // struct _objc_property_list *instance_properties;// category's @property |
| 3509 | // } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3510 | CategoryTy = llvm::StructType::get(Int8PtrTy, |
| 3511 | Int8PtrTy, |
| 3512 | MethodListPtrTy, |
| 3513 | MethodListPtrTy, |
| 3514 | ProtocolListPtrTy, |
| 3515 | IntTy, |
| 3516 | PropertyListPtrTy, |
| 3517 | NULL); |
| 3518 | CGM.getModule().addTypeName("struct._objc_category", CategoryTy); |
| 3519 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3520 | // Global metadata structures |
| 3521 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3522 | // struct _objc_symtab { |
| 3523 | // long sel_ref_cnt; |
| 3524 | // SEL *refs; |
| 3525 | // short cls_def_cnt; |
| 3526 | // short cat_def_cnt; |
| 3527 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 3528 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3529 | SymtabTy = llvm::StructType::get(LongTy, |
| 3530 | SelectorPtrTy, |
| 3531 | ShortTy, |
| 3532 | ShortTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3533 | llvm::ArrayType::get(Int8PtrTy, 0), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3534 | NULL); |
| 3535 | CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy); |
| 3536 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
| 3537 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3538 | // struct _objc_module { |
| 3539 | // long version; |
| 3540 | // long size; // sizeof(struct _objc_module) |
| 3541 | // char *name; |
| 3542 | // struct _objc_symtab* symtab; |
| 3543 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3544 | ModuleTy = |
| 3545 | llvm::StructType::get(LongTy, |
| 3546 | LongTy, |
| 3547 | Int8PtrTy, |
| 3548 | SymtabPtrTy, |
| 3549 | NULL); |
| 3550 | CGM.getModule().addTypeName("struct._objc_module", ModuleTy); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3551 | |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 3552 | // Message send functions. |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3553 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3554 | // id objc_msgSend (id, SEL, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3555 | std::vector<const llvm::Type*> Params; |
| 3556 | Params.push_back(ObjectPtrTy); |
| 3557 | Params.push_back(SelectorPtrTy); |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3558 | MessageSendFn = |
| 3559 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3560 | Params, |
| 3561 | true), |
| 3562 | "objc_msgSend"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3563 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3564 | // id objc_msgSend_stret (id, SEL, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3565 | Params.clear(); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3566 | Params.push_back(ObjectPtrTy); |
| 3567 | Params.push_back(SelectorPtrTy); |
| 3568 | MessageSendStretFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3569 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3570 | Params, |
| 3571 | true), |
| 3572 | "objc_msgSend_stret"); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3573 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3574 | // |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3575 | Params.clear(); |
| 3576 | Params.push_back(ObjectPtrTy); |
| 3577 | Params.push_back(SelectorPtrTy); |
| 3578 | // FIXME: This should be long double on x86_64? |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3579 | // [double | long double] objc_msgSend_fpret(id self, SEL op, ...) |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3580 | MessageSendFpretFn = |
| 3581 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy, |
| 3582 | Params, |
| 3583 | true), |
| 3584 | "objc_msgSend_fpret"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3585 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3586 | // id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3587 | Params.clear(); |
| 3588 | Params.push_back(SuperPtrTy); |
| 3589 | Params.push_back(SelectorPtrTy); |
| 3590 | MessageSendSuperFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3591 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3592 | Params, |
| 3593 | true), |
| 3594 | "objc_msgSendSuper"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3595 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3596 | // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super, |
| 3597 | // SEL op, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3598 | Params.clear(); |
| 3599 | Params.push_back(Int8PtrTy); |
| 3600 | Params.push_back(SuperPtrTy); |
| 3601 | Params.push_back(SelectorPtrTy); |
| 3602 | MessageSendSuperStretFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3603 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3604 | Params, |
| 3605 | true), |
| 3606 | "objc_msgSendSuper_stret"); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3607 | |
| 3608 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 3609 | MessageSendSuperFpretFn = MessageSendSuperFn; |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 3610 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3611 | // FIXME: This is the size of the setjmp buffer and should be |
| 3612 | // target specific. 18 is what's used on 32-bit X86. |
| 3613 | uint64_t SetJmpBufferSize = 18; |
| 3614 | |
| 3615 | // Exceptions |
| 3616 | const llvm::Type *StackPtrTy = |
Daniel Dunbar | 1000491 | 2008-09-27 06:32:25 +0000 | [diff] [blame] | 3617 | llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3618 | |
| 3619 | ExceptionDataTy = |
| 3620 | llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty, |
| 3621 | SetJmpBufferSize), |
| 3622 | StackPtrTy, NULL); |
| 3623 | CGM.getModule().addTypeName("struct._objc_exception_data", |
| 3624 | ExceptionDataTy); |
| 3625 | |
| 3626 | Params.clear(); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3627 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 3628 | ExceptionTryEnterFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3629 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3630 | Params, |
| 3631 | false), |
| 3632 | "objc_exception_try_enter"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3633 | ExceptionTryExitFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3634 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3635 | Params, |
| 3636 | false), |
| 3637 | "objc_exception_try_exit"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3638 | ExceptionExtractFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3639 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3640 | Params, |
| 3641 | false), |
| 3642 | "objc_exception_extract"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3643 | |
| 3644 | Params.clear(); |
| 3645 | Params.push_back(ClassPtrTy); |
| 3646 | Params.push_back(ObjectPtrTy); |
| 3647 | ExceptionMatchFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3648 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 3649 | Params, |
| 3650 | false), |
| 3651 | "objc_exception_match"); |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 3652 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3653 | Params.clear(); |
| 3654 | Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty)); |
| 3655 | SetJmpFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3656 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 3657 | Params, |
| 3658 | false), |
| 3659 | "_setjmp"); |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 3660 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3661 | } |
| 3662 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3663 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3664 | : ObjCCommonTypesHelper(cgm) |
| 3665 | { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3666 | // struct _method_list_t { |
| 3667 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 3668 | // uint32_t method_count; |
| 3669 | // struct _objc_method method_list[method_count]; |
| 3670 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3671 | MethodListnfABITy = llvm::StructType::get(IntTy, |
| 3672 | IntTy, |
| 3673 | llvm::ArrayType::get(MethodTy, 0), |
| 3674 | NULL); |
| 3675 | CGM.getModule().addTypeName("struct.__method_list_t", |
| 3676 | MethodListnfABITy); |
| 3677 | // struct method_list_t * |
| 3678 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3679 | |
| 3680 | // struct _protocol_t { |
| 3681 | // id isa; // NULL |
| 3682 | // const char * const protocol_name; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3683 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3684 | // const struct method_list_t * const instance_methods; |
| 3685 | // const struct method_list_t * const class_methods; |
| 3686 | // const struct method_list_t *optionalInstanceMethods; |
| 3687 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3688 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3689 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 3690 | // const uint32_t flags; // = 0 |
| 3691 | // } |
| 3692 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3693 | // Holder for struct _protocol_list_t * |
| 3694 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3695 | |
| 3696 | ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy, |
| 3697 | Int8PtrTy, |
| 3698 | llvm::PointerType::getUnqual( |
| 3699 | ProtocolListTyHolder), |
| 3700 | MethodListnfABIPtrTy, |
| 3701 | MethodListnfABIPtrTy, |
| 3702 | MethodListnfABIPtrTy, |
| 3703 | MethodListnfABIPtrTy, |
| 3704 | PropertyListPtrTy, |
| 3705 | IntTy, |
| 3706 | IntTy, |
| 3707 | NULL); |
| 3708 | CGM.getModule().addTypeName("struct._protocol_t", |
| 3709 | ProtocolnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3710 | |
| 3711 | // struct _protocol_t* |
| 3712 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3713 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3714 | // struct _protocol_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3715 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3716 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3717 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3718 | ProtocolListnfABITy = llvm::StructType::get(LongTy, |
| 3719 | llvm::ArrayType::get( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3720 | ProtocolnfABIPtrTy, 0), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3721 | NULL); |
| 3722 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3723 | ProtocolListnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3724 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo( |
| 3725 | ProtocolListnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3726 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3727 | // struct _objc_protocol_list* |
| 3728 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3729 | |
| 3730 | // struct _ivar_t { |
| 3731 | // unsigned long int *offset; // pointer to ivar offset location |
| 3732 | // char *name; |
| 3733 | // char *type; |
| 3734 | // uint32_t alignment; |
| 3735 | // uint32_t size; |
| 3736 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3737 | IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy), |
| 3738 | Int8PtrTy, |
| 3739 | Int8PtrTy, |
| 3740 | IntTy, |
| 3741 | IntTy, |
| 3742 | NULL); |
| 3743 | CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy); |
| 3744 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3745 | // struct _ivar_list_t { |
| 3746 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 3747 | // uint32 count; |
| 3748 | // struct _iver_t list[count]; |
| 3749 | // } |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3750 | IvarListnfABITy = llvm::StructType::get(IntTy, |
| 3751 | IntTy, |
| 3752 | llvm::ArrayType::get( |
| 3753 | IvarnfABITy, 0), |
| 3754 | NULL); |
| 3755 | CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy); |
| 3756 | |
| 3757 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3758 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3759 | // struct _class_ro_t { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3760 | // uint32_t const flags; |
| 3761 | // uint32_t const instanceStart; |
| 3762 | // uint32_t const instanceSize; |
| 3763 | // uint32_t const reserved; // only when building for 64bit targets |
| 3764 | // const uint8_t * const ivarLayout; |
| 3765 | // const char *const name; |
| 3766 | // const struct _method_list_t * const baseMethods; |
| 3767 | // const struct _objc_protocol_list *const baseProtocols; |
| 3768 | // const struct _ivar_list_t *const ivars; |
| 3769 | // const uint8_t * const weakIvarLayout; |
| 3770 | // const struct _prop_list_t * const properties; |
| 3771 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3772 | |
| 3773 | // FIXME. Add 'reserved' field in 64bit abi mode! |
| 3774 | ClassRonfABITy = llvm::StructType::get(IntTy, |
| 3775 | IntTy, |
| 3776 | IntTy, |
| 3777 | Int8PtrTy, |
| 3778 | Int8PtrTy, |
| 3779 | MethodListnfABIPtrTy, |
| 3780 | ProtocolListnfABIPtrTy, |
| 3781 | IvarListnfABIPtrTy, |
| 3782 | Int8PtrTy, |
| 3783 | PropertyListPtrTy, |
| 3784 | NULL); |
| 3785 | CGM.getModule().addTypeName("struct._class_ro_t", |
| 3786 | ClassRonfABITy); |
| 3787 | |
| 3788 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 3789 | std::vector<const llvm::Type*> Params; |
| 3790 | Params.push_back(ObjectPtrTy); |
| 3791 | Params.push_back(SelectorPtrTy); |
| 3792 | ImpnfABITy = llvm::PointerType::getUnqual( |
| 3793 | llvm::FunctionType::get(ObjectPtrTy, Params, false)); |
| 3794 | |
| 3795 | // struct _class_t { |
| 3796 | // struct _class_t *isa; |
| 3797 | // struct _class_t * const superclass; |
| 3798 | // void *cache; |
| 3799 | // IMP *vtable; |
| 3800 | // struct class_ro_t *ro; |
| 3801 | // } |
| 3802 | |
| 3803 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3804 | ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3805 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3806 | CachePtrTy, |
| 3807 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 3808 | llvm::PointerType::getUnqual( |
| 3809 | ClassRonfABITy), |
| 3810 | NULL); |
| 3811 | CGM.getModule().addTypeName("struct._class_t", ClassnfABITy); |
| 3812 | |
| 3813 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo( |
| 3814 | ClassnfABITy); |
| 3815 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3816 | // LLVM for struct _class_t * |
| 3817 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
| 3818 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3819 | // struct _category_t { |
| 3820 | // const char * const name; |
| 3821 | // struct _class_t *const cls; |
| 3822 | // const struct _method_list_t * const instance_methods; |
| 3823 | // const struct _method_list_t * const class_methods; |
| 3824 | // const struct _protocol_list_t * const protocols; |
| 3825 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3826 | // } |
| 3827 | CategorynfABITy = llvm::StructType::get(Int8PtrTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3828 | ClassnfABIPtrTy, |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3829 | MethodListnfABIPtrTy, |
| 3830 | MethodListnfABIPtrTy, |
| 3831 | ProtocolListnfABIPtrTy, |
| 3832 | PropertyListPtrTy, |
| 3833 | NULL); |
| 3834 | CGM.getModule().addTypeName("struct._category_t", CategorynfABITy); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3835 | |
| 3836 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3837 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3838 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3839 | |
| 3840 | // MessageRefTy - LLVM for: |
| 3841 | // struct _message_ref_t { |
| 3842 | // IMP messenger; |
| 3843 | // SEL name; |
| 3844 | // }; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3845 | |
| 3846 | // First the clang type for struct _message_ref_t |
| 3847 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3848 | SourceLocation(), |
| 3849 | &Ctx.Idents.get("_message_ref_t")); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3850 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3851 | Ctx.VoidPtrTy, 0, false)); |
| 3852 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3853 | Ctx.getObjCSelType(), 0, false)); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3854 | RD->completeDefinition(Ctx); |
| 3855 | |
| 3856 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 3857 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 3858 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3859 | |
| 3860 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 3861 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
| 3862 | |
| 3863 | // SuperMessageRefTy - LLVM for: |
| 3864 | // struct _super_message_ref_t { |
| 3865 | // SUPER_IMP messenger; |
| 3866 | // SEL name; |
| 3867 | // }; |
| 3868 | SuperMessageRefTy = llvm::StructType::get(ImpnfABITy, |
| 3869 | SelectorPtrTy, |
| 3870 | NULL); |
| 3871 | CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy); |
| 3872 | |
| 3873 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 3874 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
| 3875 | |
| 3876 | // id objc_msgSend_fixup (id, struct message_ref_t*, ...) |
| 3877 | Params.clear(); |
| 3878 | Params.push_back(ObjectPtrTy); |
| 3879 | Params.push_back(MessageRefPtrTy); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 3880 | MessengerTy = llvm::FunctionType::get(ObjectPtrTy, |
| 3881 | Params, |
| 3882 | true); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3883 | MessageSendFixupFn = |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 3884 | CGM.CreateRuntimeFunction(MessengerTy, |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3885 | "objc_msgSend_fixup"); |
| 3886 | |
| 3887 | // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...) |
| 3888 | MessageSendFpretFixupFn = |
| 3889 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3890 | Params, |
| 3891 | true), |
| 3892 | "objc_msgSend_fpret_fixup"); |
| 3893 | |
| 3894 | // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...) |
| 3895 | MessageSendStretFixupFn = |
| 3896 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3897 | Params, |
| 3898 | true), |
| 3899 | "objc_msgSend_stret_fixup"); |
| 3900 | |
| 3901 | // id objc_msgSendId_fixup (id, struct message_ref_t*, ...) |
| 3902 | MessageSendIdFixupFn = |
| 3903 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3904 | Params, |
| 3905 | true), |
| 3906 | "objc_msgSendId_fixup"); |
| 3907 | |
| 3908 | |
| 3909 | // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...) |
| 3910 | MessageSendIdStretFixupFn = |
| 3911 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3912 | Params, |
| 3913 | true), |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3914 | "objc_msgSendId_stret_fixup"); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3915 | |
| 3916 | // id objc_msgSendSuper2_fixup (struct objc_super *, |
| 3917 | // struct _super_message_ref_t*, ...) |
| 3918 | Params.clear(); |
| 3919 | Params.push_back(SuperPtrTy); |
| 3920 | Params.push_back(SuperMessageRefPtrTy); |
| 3921 | MessageSendSuper2FixupFn = |
| 3922 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3923 | Params, |
| 3924 | true), |
| 3925 | "objc_msgSendSuper2_fixup"); |
| 3926 | |
| 3927 | |
| 3928 | // id objc_msgSendSuper2_stret_fixup (struct objc_super *, |
| 3929 | // struct _super_message_ref_t*, ...) |
| 3930 | MessageSendSuper2StretFixupFn = |
| 3931 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3932 | Params, |
| 3933 | true), |
| 3934 | "objc_msgSendSuper2_stret_fixup"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 3935 | |
| 3936 | Params.clear(); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 3937 | Params.push_back(Int8PtrTy); |
| 3938 | UnwindResumeOrRethrowFn = |
| 3939 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3940 | Params, |
| 3941 | false), |
| 3942 | "_Unwind_Resume_or_Rethrow"); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 3943 | ObjCBeginCatchFn = |
| 3944 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy, |
| 3945 | Params, |
| 3946 | false), |
| 3947 | "objc_begin_catch"); |
| 3948 | |
| 3949 | Params.clear(); |
| 3950 | ObjCEndCatchFn = |
| 3951 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3952 | Params, |
| 3953 | false), |
| 3954 | "objc_end_catch"); |
| 3955 | |
| 3956 | // struct objc_typeinfo { |
| 3957 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 3958 | // const char* name; // c++ typeinfo string |
| 3959 | // Class cls; |
| 3960 | // }; |
| 3961 | EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy), |
| 3962 | Int8PtrTy, |
| 3963 | ClassnfABIPtrTy, |
| 3964 | NULL); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 3965 | CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 3966 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3967 | } |
| 3968 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3969 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
| 3970 | FinishNonFragileABIModule(); |
| 3971 | |
| 3972 | return NULL; |
| 3973 | } |
| 3974 | |
| 3975 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 3976 | // nonfragile abi has no module definition. |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 3977 | |
| 3978 | // Build list of all implemented classe addresses in array |
| 3979 | // L_OBJC_LABEL_CLASS_$. |
| 3980 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$ |
| 3981 | // list of 'nonlazy' implementations (defined as those with a +load{} |
| 3982 | // method!!). |
| 3983 | unsigned NumClasses = DefinedClasses.size(); |
| 3984 | if (NumClasses) { |
| 3985 | std::vector<llvm::Constant*> Symbols(NumClasses); |
| 3986 | for (unsigned i=0; i<NumClasses; i++) |
| 3987 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 3988 | ObjCTypes.Int8PtrTy); |
| 3989 | llvm::Constant* Init = |
| 3990 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 3991 | NumClasses), |
| 3992 | Symbols); |
| 3993 | |
| 3994 | llvm::GlobalVariable *GV = |
| 3995 | new llvm::GlobalVariable(Init->getType(), false, |
| 3996 | llvm::GlobalValue::InternalLinkage, |
| 3997 | Init, |
| 3998 | "\01L_OBJC_LABEL_CLASS_$", |
| 3999 | &CGM.getModule()); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 4000 | GV->setAlignment(8); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4001 | GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip"); |
| 4002 | UsedGlobals.push_back(GV); |
| 4003 | } |
| 4004 | |
| 4005 | // Build list of all implemented category addresses in array |
| 4006 | // L_OBJC_LABEL_CATEGORY_$. |
| 4007 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$ |
| 4008 | // list of 'nonlazy' category implementations (defined as those with a +load{} |
| 4009 | // method!!). |
| 4010 | unsigned NumCategory = DefinedCategories.size(); |
| 4011 | if (NumCategory) { |
| 4012 | std::vector<llvm::Constant*> Symbols(NumCategory); |
| 4013 | for (unsigned i=0; i<NumCategory; i++) |
| 4014 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 4015 | ObjCTypes.Int8PtrTy); |
| 4016 | llvm::Constant* Init = |
| 4017 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 4018 | NumCategory), |
| 4019 | Symbols); |
| 4020 | |
| 4021 | llvm::GlobalVariable *GV = |
| 4022 | new llvm::GlobalVariable(Init->getType(), false, |
| 4023 | llvm::GlobalValue::InternalLinkage, |
| 4024 | Init, |
| 4025 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 4026 | &CGM.getModule()); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 4027 | GV->setAlignment(8); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4028 | GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip"); |
| 4029 | UsedGlobals.push_back(GV); |
| 4030 | } |
| 4031 | |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4032 | // static int L_OBJC_IMAGE_INFO[2] = { 0, flags }; |
| 4033 | // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0 |
| 4034 | std::vector<llvm::Constant*> Values(2); |
| 4035 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0); |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4036 | unsigned int flags = 0; |
Fariborz Jahanian | 66a5c2c | 2009-02-24 23:34:44 +0000 | [diff] [blame] | 4037 | // FIXME: Fix and continue? |
| 4038 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 4039 | flags |= eImageInfo_GarbageCollected; |
| 4040 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 4041 | flags |= eImageInfo_GCOnly; |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4042 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4043 | llvm::Constant* Init = llvm::ConstantArray::get( |
| 4044 | llvm::ArrayType::get(ObjCTypes.IntTy, 2), |
| 4045 | Values); |
| 4046 | llvm::GlobalVariable *IMGV = |
| 4047 | new llvm::GlobalVariable(Init->getType(), false, |
| 4048 | llvm::GlobalValue::InternalLinkage, |
| 4049 | Init, |
| 4050 | "\01L_OBJC_IMAGE_INFO", |
| 4051 | &CGM.getModule()); |
| 4052 | IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); |
| 4053 | UsedGlobals.push_back(IMGV); |
| 4054 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4055 | std::vector<llvm::Constant*> Used; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4056 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4057 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
| 4058 | e = UsedGlobals.end(); i != e; ++i) { |
| 4059 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
| 4060 | } |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4061 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4062 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
| 4063 | llvm::GlobalValue *GV = |
| 4064 | new llvm::GlobalVariable(AT, false, |
| 4065 | llvm::GlobalValue::AppendingLinkage, |
| 4066 | llvm::ConstantArray::get(AT, Used), |
| 4067 | "llvm.used", |
| 4068 | &CGM.getModule()); |
| 4069 | |
| 4070 | GV->setSection("llvm.metadata"); |
| 4071 | |
| 4072 | } |
| 4073 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4074 | // Metadata flags |
| 4075 | enum MetaDataDlags { |
| 4076 | CLS = 0x0, |
| 4077 | CLS_META = 0x1, |
| 4078 | CLS_ROOT = 0x2, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4079 | OBJC2_CLS_HIDDEN = 0x10, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4080 | CLS_EXCEPTION = 0x20 |
| 4081 | }; |
| 4082 | /// BuildClassRoTInitializer - generate meta-data for: |
| 4083 | /// struct _class_ro_t { |
| 4084 | /// uint32_t const flags; |
| 4085 | /// uint32_t const instanceStart; |
| 4086 | /// uint32_t const instanceSize; |
| 4087 | /// uint32_t const reserved; // only when building for 64bit targets |
| 4088 | /// const uint8_t * const ivarLayout; |
| 4089 | /// const char *const name; |
| 4090 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4091 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4092 | /// const struct _ivar_list_t *const ivars; |
| 4093 | /// const uint8_t * const weakIvarLayout; |
| 4094 | /// const struct _prop_list_t * const properties; |
| 4095 | /// } |
| 4096 | /// |
| 4097 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
| 4098 | unsigned flags, |
| 4099 | unsigned InstanceStart, |
| 4100 | unsigned InstanceSize, |
| 4101 | const ObjCImplementationDecl *ID) { |
| 4102 | std::string ClassName = ID->getNameAsString(); |
| 4103 | std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets! |
| 4104 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 4105 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 4106 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
| 4107 | // FIXME. For 64bit targets add 0 here. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4108 | // FIXME. ivarLayout is currently null! |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 4109 | Values[ 3] = GetIvarLayoutName(0, ObjCTypes); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4110 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4111 | // const struct _method_list_t * const baseMethods; |
| 4112 | std::vector<llvm::Constant*> Methods; |
| 4113 | std::string MethodListName("\01l_OBJC_$_"); |
| 4114 | if (flags & CLS_META) { |
| 4115 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
| 4116 | for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(), |
| 4117 | e = ID->classmeth_end(); i != e; ++i) { |
| 4118 | // Class methods should always be defined. |
| 4119 | Methods.push_back(GetMethodConstant(*i)); |
| 4120 | } |
| 4121 | } else { |
| 4122 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
| 4123 | for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(), |
| 4124 | e = ID->instmeth_end(); i != e; ++i) { |
| 4125 | // Instance methods should always be defined. |
| 4126 | Methods.push_back(GetMethodConstant(*i)); |
| 4127 | } |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4128 | for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(), |
| 4129 | e = ID->propimpl_end(); i != e; ++i) { |
| 4130 | ObjCPropertyImplDecl *PID = *i; |
| 4131 | |
| 4132 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 4133 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 4134 | |
| 4135 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 4136 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4137 | Methods.push_back(C); |
| 4138 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 4139 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4140 | Methods.push_back(C); |
| 4141 | } |
| 4142 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4143 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4144 | Values[ 5] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4145 | "__DATA, __objc_const", Methods); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4146 | |
| 4147 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4148 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
| 4149 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
| 4150 | + OID->getNameAsString(), |
| 4151 | OID->protocol_begin(), |
| 4152 | OID->protocol_end()); |
| 4153 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4154 | if (flags & CLS_META) |
| 4155 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4156 | else |
| 4157 | Values[ 7] = EmitIvarList(ID); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4158 | // FIXME. weakIvarLayout is currently null. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4159 | Values[ 8] = GetIvarLayoutName(0, ObjCTypes); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4160 | if (flags & CLS_META) |
| 4161 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4162 | else |
| 4163 | Values[ 9] = |
| 4164 | EmitPropertyList( |
| 4165 | "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
| 4166 | ID, ID->getClassInterface(), ObjCTypes); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4167 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
| 4168 | Values); |
| 4169 | llvm::GlobalVariable *CLASS_RO_GV = |
| 4170 | new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false, |
| 4171 | llvm::GlobalValue::InternalLinkage, |
| 4172 | Init, |
| 4173 | (flags & CLS_META) ? |
| 4174 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 4175 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName, |
| 4176 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4177 | CLASS_RO_GV->setAlignment( |
| 4178 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4179 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4180 | return CLASS_RO_GV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4181 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4182 | } |
| 4183 | |
| 4184 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 4185 | /// for the given ClassName for: |
| 4186 | /// struct _class_t { |
| 4187 | /// struct _class_t *isa; |
| 4188 | /// struct _class_t * const superclass; |
| 4189 | /// void *cache; |
| 4190 | /// IMP *vtable; |
| 4191 | /// struct class_ro_t *ro; |
| 4192 | /// } |
| 4193 | /// |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4194 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
| 4195 | std::string &ClassName, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4196 | llvm::Constant *IsAGV, |
| 4197 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4198 | llvm::Constant *ClassRoGV, |
| 4199 | bool HiddenVisibility) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4200 | std::vector<llvm::Constant*> Values(5); |
| 4201 | Values[0] = IsAGV; |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4202 | Values[1] = SuperClassGV |
| 4203 | ? SuperClassGV |
| 4204 | : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4205 | Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar |
| 4206 | Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar |
| 4207 | Values[4] = ClassRoGV; // &CLASS_RO_GV |
| 4208 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
| 4209 | Values); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4210 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 4211 | GV->setInitializer(Init); |
Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 4212 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4213 | GV->setAlignment( |
| 4214 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4215 | if (HiddenVisibility) |
| 4216 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4217 | return GV; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4218 | } |
| 4219 | |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4220 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCInterfaceDecl *OID, |
| 4221 | uint32_t &InstanceStart, |
| 4222 | uint32_t &InstanceSize) { |
| 4223 | // FIXME. Share this with the one in EmitIvarList. |
| 4224 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID); |
| 4225 | |
| 4226 | RecordDecl::field_iterator firstField, lastField; |
| 4227 | const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField); |
| 4228 | |
| 4229 | for (RecordDecl::field_iterator e = RD->field_end(CGM.getContext()), |
| 4230 | ifield = firstField; ifield != e; ++ifield) |
| 4231 | lastField = ifield; |
| 4232 | |
| 4233 | InstanceStart = InstanceSize = 0; |
| 4234 | if (lastField != RD->field_end(CGM.getContext())) { |
| 4235 | FieldDecl *Field = *lastField; |
| 4236 | const llvm::Type *FieldTy = |
| 4237 | CGM.getTypes().ConvertTypeForMem(Field->getType()); |
| 4238 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
| 4239 | InstanceSize = GetIvarBaseOffset(Layout, Field) + Size; |
| 4240 | if (firstField == RD->field_end(CGM.getContext())) |
| 4241 | InstanceStart = InstanceSize; |
| 4242 | else { |
| 4243 | Field = *firstField; |
| 4244 | InstanceStart = GetIvarBaseOffset(Layout, Field); |
| 4245 | } |
| 4246 | } |
| 4247 | } |
| 4248 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4249 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 4250 | std::string ClassName = ID->getNameAsString(); |
| 4251 | if (!ObjCEmptyCacheVar) { |
| 4252 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4253 | ObjCTypes.CacheTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4254 | false, |
| 4255 | llvm::GlobalValue::ExternalLinkage, |
| 4256 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4257 | "_objc_empty_cache", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4258 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4259 | |
| 4260 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4261 | ObjCTypes.ImpnfABITy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4262 | false, |
| 4263 | llvm::GlobalValue::ExternalLinkage, |
| 4264 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4265 | "_objc_empty_vtable", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4266 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4267 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4268 | assert(ID->getClassInterface() && |
| 4269 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4270 | // FIXME: Is this correct (That meta class size is never computed)? |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4271 | uint32_t InstanceStart = |
| 4272 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy); |
| 4273 | uint32_t InstanceSize = InstanceStart; |
| 4274 | uint32_t flags = CLS_META; |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4275 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 4276 | std::string ObjCClassName(getClassSymbolPrefix()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4277 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4278 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4279 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4280 | bool classIsHidden = |
| 4281 | CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4282 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4283 | flags |= OBJC2_CLS_HIDDEN; |
| 4284 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4285 | // class is root |
| 4286 | flags |= CLS_ROOT; |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4287 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4288 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4289 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4290 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4291 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 4292 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 4293 | Root = Super; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4294 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4295 | // work on super class metadata symbol. |
| 4296 | std::string SuperClassName = |
| 4297 | ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4298 | SuperClassGV = GetClassGlobal(SuperClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4299 | } |
| 4300 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 4301 | InstanceStart, |
| 4302 | InstanceSize,ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4303 | std::string TClassName = ObjCMetaClassName + ClassName; |
| 4304 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4305 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 4306 | classIsHidden); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4307 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4308 | // Metadata for the class |
| 4309 | flags = CLS; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4310 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4311 | flags |= OBJC2_CLS_HIDDEN; |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4312 | |
| 4313 | if (hasObjCExceptionAttribute(ID->getClassInterface())) |
| 4314 | flags |= CLS_EXCEPTION; |
| 4315 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4316 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4317 | flags |= CLS_ROOT; |
| 4318 | SuperClassGV = 0; |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 4319 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4320 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4321 | std::string RootClassName = |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4322 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4323 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4324 | } |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4325 | GetClassSizeInfo(ID->getClassInterface(), InstanceStart, InstanceSize); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4326 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4327 | InstanceStart, |
| 4328 | InstanceSize, |
| 4329 | ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4330 | |
| 4331 | TClassName = ObjCClassName + ClassName; |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4332 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4333 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 4334 | classIsHidden); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4335 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4336 | |
| 4337 | // Force the definition of the EHType if necessary. |
| 4338 | if (flags & CLS_EXCEPTION) |
| 4339 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4340 | } |
| 4341 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4342 | /// GenerateProtocolRef - This routine is called to generate code for |
| 4343 | /// a protocol reference expression; as in: |
| 4344 | /// @code |
| 4345 | /// @protocol(Proto1); |
| 4346 | /// @endcode |
| 4347 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 4348 | /// which will hold address of the protocol meta-data. |
| 4349 | /// |
| 4350 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
| 4351 | const ObjCProtocolDecl *PD) { |
| 4352 | |
Fariborz Jahanian | 960cd06 | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 4353 | // This routine is called for @protocol only. So, we must build definition |
| 4354 | // of protocol's meta-data (not a reference to it!) |
| 4355 | // |
| 4356 | llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4357 | ObjCTypes.ExternalProtocolPtrTy); |
| 4358 | |
| 4359 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
| 4360 | ProtocolName += PD->getNameAsCString(); |
| 4361 | |
| 4362 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 4363 | if (PTGV) |
| 4364 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4365 | PTGV = new llvm::GlobalVariable( |
| 4366 | Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4367 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4368 | Init, |
| 4369 | ProtocolName, |
| 4370 | &CGM.getModule()); |
| 4371 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 4372 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4373 | UsedGlobals.push_back(PTGV); |
| 4374 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4375 | } |
| 4376 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4377 | /// GenerateCategory - Build metadata for a category implementation. |
| 4378 | /// struct _category_t { |
| 4379 | /// const char * const name; |
| 4380 | /// struct _class_t *const cls; |
| 4381 | /// const struct _method_list_t * const instance_methods; |
| 4382 | /// const struct _method_list_t * const class_methods; |
| 4383 | /// const struct _protocol_list_t * const protocols; |
| 4384 | /// const struct _prop_list_t * const properties; |
| 4385 | /// } |
| 4386 | /// |
| 4387 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) |
| 4388 | { |
| 4389 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4390 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
| 4391 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4392 | "_$_" + OCD->getNameAsString()); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4393 | std::string ExtClassName(getClassSymbolPrefix() + |
| 4394 | Interface->getNameAsString()); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4395 | |
| 4396 | std::vector<llvm::Constant*> Values(6); |
| 4397 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 4398 | // meta-class entry symbol |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4399 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4400 | Values[1] = ClassGV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4401 | std::vector<llvm::Constant*> Methods; |
| 4402 | std::string MethodListName(Prefix); |
| 4403 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
| 4404 | "_$_" + OCD->getNameAsString(); |
| 4405 | |
| 4406 | for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(), |
| 4407 | e = OCD->instmeth_end(); i != e; ++i) { |
| 4408 | // Instance methods should always be defined. |
| 4409 | Methods.push_back(GetMethodConstant(*i)); |
| 4410 | } |
| 4411 | |
| 4412 | Values[2] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4413 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4414 | Methods); |
| 4415 | |
| 4416 | MethodListName = Prefix; |
| 4417 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 4418 | OCD->getNameAsString(); |
| 4419 | Methods.clear(); |
| 4420 | for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(), |
| 4421 | e = OCD->classmeth_end(); i != e; ++i) { |
| 4422 | // Class methods should always be defined. |
| 4423 | Methods.push_back(GetMethodConstant(*i)); |
| 4424 | } |
| 4425 | |
| 4426 | Values[3] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4427 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4428 | Methods); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4429 | const ObjCCategoryDecl *Category = |
| 4430 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4431 | if (Category) { |
| 4432 | std::string ExtName(Interface->getNameAsString() + "_$_" + |
| 4433 | OCD->getNameAsString()); |
| 4434 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
| 4435 | + Interface->getNameAsString() + "_$_" |
| 4436 | + Category->getNameAsString(), |
| 4437 | Category->protocol_begin(), |
| 4438 | Category->protocol_end()); |
| 4439 | Values[5] = |
| 4440 | EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
| 4441 | OCD, Category, ObjCTypes); |
| 4442 | } |
| 4443 | else { |
| 4444 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4445 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4446 | } |
| 4447 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4448 | llvm::Constant *Init = |
| 4449 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
| 4450 | Values); |
| 4451 | llvm::GlobalVariable *GCATV |
| 4452 | = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy, |
| 4453 | false, |
| 4454 | llvm::GlobalValue::InternalLinkage, |
| 4455 | Init, |
| 4456 | ExtCatName, |
| 4457 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4458 | GCATV->setAlignment( |
| 4459 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4460 | GCATV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4461 | UsedGlobals.push_back(GCATV); |
| 4462 | DefinedCategories.push_back(GCATV); |
| 4463 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4464 | |
| 4465 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 4466 | /// given method if it has been defined. The result is null if the |
| 4467 | /// method has not been defined. The return value has type MethodPtrTy. |
| 4468 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
| 4469 | const ObjCMethodDecl *MD) { |
| 4470 | // FIXME: Use DenseMap::lookup |
| 4471 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 4472 | if (!Fn) |
| 4473 | return 0; |
| 4474 | |
| 4475 | std::vector<llvm::Constant*> Method(3); |
| 4476 | Method[0] = |
| 4477 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4478 | ObjCTypes.SelectorPtrTy); |
| 4479 | Method[1] = GetMethodVarType(MD); |
| 4480 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 4481 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 4482 | } |
| 4483 | |
| 4484 | /// EmitMethodList - Build meta-data for method declarations |
| 4485 | /// struct _method_list_t { |
| 4486 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 4487 | /// uint32_t method_count; |
| 4488 | /// struct _objc_method method_list[method_count]; |
| 4489 | /// } |
| 4490 | /// |
| 4491 | llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList( |
| 4492 | const std::string &Name, |
| 4493 | const char *Section, |
| 4494 | const ConstantVector &Methods) { |
| 4495 | // Return null for empty list. |
| 4496 | if (Methods.empty()) |
| 4497 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
| 4498 | |
| 4499 | std::vector<llvm::Constant*> Values(3); |
| 4500 | // sizeof(struct _objc_method) |
| 4501 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy); |
| 4502 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4503 | // method_count |
| 4504 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 4505 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 4506 | Methods.size()); |
| 4507 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 4508 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4509 | |
| 4510 | llvm::GlobalVariable *GV = |
| 4511 | new llvm::GlobalVariable(Init->getType(), false, |
| 4512 | llvm::GlobalValue::InternalLinkage, |
| 4513 | Init, |
| 4514 | Name, |
| 4515 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4516 | GV->setAlignment( |
| 4517 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4518 | GV->setSection(Section); |
| 4519 | UsedGlobals.push_back(GV); |
| 4520 | return llvm::ConstantExpr::getBitCast(GV, |
| 4521 | ObjCTypes.MethodListnfABIPtrTy); |
| 4522 | } |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4523 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4524 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 4525 | /// the given ivar. |
| 4526 | /// |
| 4527 | llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 4528 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4529 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 4530 | std::string Name = "OBJC_IVAR_$_" + |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4531 | getInterfaceDeclForIvar(ID, Ivar, CGM.getContext())->getNameAsString() + |
| 4532 | '.' + Ivar->getNameAsString(); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4533 | llvm::GlobalVariable *IvarOffsetGV = |
| 4534 | CGM.getModule().getGlobalVariable(Name); |
| 4535 | if (!IvarOffsetGV) |
| 4536 | IvarOffsetGV = |
| 4537 | new llvm::GlobalVariable(ObjCTypes.LongTy, |
| 4538 | false, |
| 4539 | llvm::GlobalValue::ExternalLinkage, |
| 4540 | 0, |
| 4541 | Name, |
| 4542 | &CGM.getModule()); |
| 4543 | return IvarOffsetGV; |
| 4544 | } |
| 4545 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4546 | llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar( |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4547 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4548 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4549 | unsigned long int Offset) { |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4550 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
| 4551 | IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy, |
| 4552 | Offset)); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4553 | IvarOffsetGV->setAlignment( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4554 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy)); |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4555 | |
| 4556 | // FIXME: This matches gcc, but shouldn't the visibility be set on |
| 4557 | // the use as well (i.e., in ObjCIvarOffsetVariable). |
| 4558 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 4559 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
| 4560 | CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden) |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4561 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4562 | else |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4563 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4564 | IvarOffsetGV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4565 | return IvarOffsetGV; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4566 | } |
| 4567 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4568 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 4569 | /// implementation. The return value has type |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4570 | /// IvarListnfABIPtrTy. |
| 4571 | /// struct _ivar_t { |
| 4572 | /// unsigned long int *offset; // pointer to ivar offset location |
| 4573 | /// char *name; |
| 4574 | /// char *type; |
| 4575 | /// uint32_t alignment; |
| 4576 | /// uint32_t size; |
| 4577 | /// } |
| 4578 | /// struct _ivar_list_t { |
| 4579 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 4580 | /// uint32 count; |
| 4581 | /// struct _iver_t list[count]; |
| 4582 | /// } |
| 4583 | /// |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame^] | 4584 | |
| 4585 | void CGObjCCommonMac::GetNamedIvarList(const ObjCInterfaceDecl *OID, |
| 4586 | llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const { |
| 4587 | for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), |
| 4588 | E = OID->ivar_end(); I != E; ++I) { |
| 4589 | // Ignore unnamed bit-fields. |
| 4590 | if (!(*I)->getDeclName()) |
| 4591 | continue; |
| 4592 | |
| 4593 | Res.push_back(*I); |
| 4594 | } |
| 4595 | |
| 4596 | for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(CGM.getContext()), |
| 4597 | E = OID->prop_end(CGM.getContext()); I != E; ++I) |
| 4598 | if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl()) |
| 4599 | Res.push_back(IV); |
| 4600 | } |
| 4601 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4602 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
| 4603 | const ObjCImplementationDecl *ID) { |
| 4604 | |
| 4605 | std::vector<llvm::Constant*> Ivars, Ivar(5); |
| 4606 | |
| 4607 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4608 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
| 4609 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4610 | // FIXME. Consolidate this with similar code in GenerateClass. |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 4611 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 4612 | |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4613 | // Collect declared and synthesized ivars in a small vector. |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4614 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame^] | 4615 | GetNamedIvarList(OID, OIvars); |
Fariborz Jahanian | 99eee36 | 2009-04-01 19:37:34 +0000 | [diff] [blame] | 4616 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame^] | 4617 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 4618 | ObjCIvarDecl *IVD = OIvars[i]; |
| 4619 | const FieldDecl *Field = OID->lookupFieldDeclForIvar(CGM.getContext(), IVD); |
Daniel Dunbar | 3eec8aa | 2009-04-20 05:53:40 +0000 | [diff] [blame] | 4620 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD, |
Daniel Dunbar | 48fa064 | 2009-04-19 02:03:42 +0000 | [diff] [blame] | 4621 | GetIvarBaseOffset(Layout, Field)); |
Daniel Dunbar | 3eec8aa | 2009-04-20 05:53:40 +0000 | [diff] [blame] | 4622 | Ivar[1] = GetMethodVarName(Field->getIdentifier()); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4623 | Ivar[2] = GetMethodVarType(Field); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4624 | const llvm::Type *FieldTy = |
| 4625 | CGM.getTypes().ConvertTypeForMem(Field->getType()); |
| 4626 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
| 4627 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
| 4628 | Field->getType().getTypePtr()) >> 3; |
| 4629 | Align = llvm::Log2_32(Align); |
| 4630 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4631 | // NOTE. Size of a bitfield does not match gcc's, because of the |
| 4632 | // way bitfields are treated special in each. But I am told that |
| 4633 | // 'size' for bitfield ivars is ignored by the runtime so it does |
| 4634 | // not matter. If it matters, there is enough info to get the |
| 4635 | // bitfield right! |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4636 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4637 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
| 4638 | } |
| 4639 | // Return null for empty list. |
| 4640 | if (Ivars.empty()) |
| 4641 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4642 | std::vector<llvm::Constant*> Values(3); |
| 4643 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy); |
| 4644 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4645 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 4646 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
| 4647 | Ivars.size()); |
| 4648 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
| 4649 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4650 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 4651 | llvm::GlobalVariable *GV = |
| 4652 | new llvm::GlobalVariable(Init->getType(), false, |
| 4653 | llvm::GlobalValue::InternalLinkage, |
| 4654 | Init, |
| 4655 | Prefix + OID->getNameAsString(), |
| 4656 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4657 | GV->setAlignment( |
| 4658 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4659 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4660 | |
| 4661 | UsedGlobals.push_back(GV); |
| 4662 | return llvm::ConstantExpr::getBitCast(GV, |
| 4663 | ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4664 | } |
| 4665 | |
| 4666 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
| 4667 | const ObjCProtocolDecl *PD) { |
| 4668 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4669 | |
| 4670 | if (!Entry) { |
| 4671 | // We use the initializer as a marker of whether this is a forward |
| 4672 | // reference or not. At module finalization we add the empty |
| 4673 | // contents for protocols which were referenced but never defined. |
| 4674 | Entry = |
| 4675 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
| 4676 | llvm::GlobalValue::ExternalLinkage, |
| 4677 | 0, |
| 4678 | "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(), |
| 4679 | &CGM.getModule()); |
| 4680 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 4681 | UsedGlobals.push_back(Entry); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4682 | } |
| 4683 | |
| 4684 | return Entry; |
| 4685 | } |
| 4686 | |
| 4687 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 4688 | /// @code |
| 4689 | /// struct _protocol_t { |
| 4690 | /// id isa; // NULL |
| 4691 | /// const char * const protocol_name; |
| 4692 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 4693 | /// const struct method_list_t * const instance_methods; |
| 4694 | /// const struct method_list_t * const class_methods; |
| 4695 | /// const struct method_list_t *optionalInstanceMethods; |
| 4696 | /// const struct method_list_t *optionalClassMethods; |
| 4697 | /// const struct _prop_list_t * properties; |
| 4698 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 4699 | /// const uint32_t flags; // = 0 |
| 4700 | /// } |
| 4701 | /// @endcode |
| 4702 | /// |
| 4703 | |
| 4704 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
| 4705 | const ObjCProtocolDecl *PD) { |
| 4706 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4707 | |
| 4708 | // Early exit if a defining object has already been generated. |
| 4709 | if (Entry && Entry->hasInitializer()) |
| 4710 | return Entry; |
| 4711 | |
| 4712 | const char *ProtocolName = PD->getNameAsCString(); |
| 4713 | |
| 4714 | // Construct method lists. |
| 4715 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 4716 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4717 | for (ObjCProtocolDecl::instmeth_iterator |
| 4718 | i = PD->instmeth_begin(CGM.getContext()), |
| 4719 | e = PD->instmeth_end(CGM.getContext()); |
| 4720 | i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4721 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4722 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4723 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4724 | OptInstanceMethods.push_back(C); |
| 4725 | } else { |
| 4726 | InstanceMethods.push_back(C); |
| 4727 | } |
| 4728 | } |
| 4729 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4730 | for (ObjCProtocolDecl::classmeth_iterator |
| 4731 | i = PD->classmeth_begin(CGM.getContext()), |
| 4732 | e = PD->classmeth_end(CGM.getContext()); |
| 4733 | i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4734 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4735 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4736 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4737 | OptClassMethods.push_back(C); |
| 4738 | } else { |
| 4739 | ClassMethods.push_back(C); |
| 4740 | } |
| 4741 | } |
| 4742 | |
| 4743 | std::vector<llvm::Constant*> Values(10); |
| 4744 | // isa is NULL |
| 4745 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
| 4746 | Values[1] = GetClassName(PD->getIdentifier()); |
| 4747 | Values[2] = EmitProtocolList( |
| 4748 | "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(), |
| 4749 | PD->protocol_begin(), |
| 4750 | PD->protocol_end()); |
| 4751 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4752 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4753 | + PD->getNameAsString(), |
| 4754 | "__DATA, __objc_const", |
| 4755 | InstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4756 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4757 | + PD->getNameAsString(), |
| 4758 | "__DATA, __objc_const", |
| 4759 | ClassMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4760 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4761 | + PD->getNameAsString(), |
| 4762 | "__DATA, __objc_const", |
| 4763 | OptInstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4764 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4765 | + PD->getNameAsString(), |
| 4766 | "__DATA, __objc_const", |
| 4767 | OptClassMethods); |
| 4768 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(), |
| 4769 | 0, PD, ObjCTypes); |
| 4770 | uint32_t Size = |
| 4771 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy); |
| 4772 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4773 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
| 4774 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
| 4775 | Values); |
| 4776 | |
| 4777 | if (Entry) { |
| 4778 | // Already created, fix the linkage and update the initializer. |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4779 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4780 | Entry->setInitializer(Init); |
| 4781 | } else { |
| 4782 | Entry = |
| 4783 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4784 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4785 | Init, |
| 4786 | std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName, |
| 4787 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4788 | Entry->setAlignment( |
| 4789 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4790 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4791 | } |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4792 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4793 | |
| 4794 | // Use this protocol meta-data to build protocol list table in section |
| 4795 | // __DATA, __objc_protolist |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4796 | llvm::GlobalVariable *PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4797 | ObjCTypes.ProtocolnfABIPtrTy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4798 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4799 | Entry, |
| 4800 | std::string("\01l_OBJC_LABEL_PROTOCOL_$_") |
| 4801 | +ProtocolName, |
| 4802 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4803 | PTGV->setAlignment( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4804 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 4805 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4806 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4807 | UsedGlobals.push_back(PTGV); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4808 | return Entry; |
| 4809 | } |
| 4810 | |
| 4811 | /// EmitProtocolList - Generate protocol list meta-data: |
| 4812 | /// @code |
| 4813 | /// struct _protocol_list_t { |
| 4814 | /// long protocol_count; // Note, this is 32/64 bit |
| 4815 | /// struct _protocol_t[protocol_count]; |
| 4816 | /// } |
| 4817 | /// @endcode |
| 4818 | /// |
| 4819 | llvm::Constant * |
| 4820 | CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name, |
| 4821 | ObjCProtocolDecl::protocol_iterator begin, |
| 4822 | ObjCProtocolDecl::protocol_iterator end) { |
| 4823 | std::vector<llvm::Constant*> ProtocolRefs; |
| 4824 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4825 | // Just return null for empty protocol lists |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4826 | if (begin == end) |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4827 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4828 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4829 | // FIXME: We shouldn't need to do this lookup here, should we? |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4830 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
| 4831 | if (GV) |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4832 | return llvm::ConstantExpr::getBitCast(GV, |
| 4833 | ObjCTypes.ProtocolListnfABIPtrTy); |
| 4834 | |
| 4835 | for (; begin != end; ++begin) |
| 4836 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 4837 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4838 | // This list is null terminated. |
| 4839 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4840 | ObjCTypes.ProtocolnfABIPtrTy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4841 | |
| 4842 | std::vector<llvm::Constant*> Values(2); |
| 4843 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 4844 | Values[1] = |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4845 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4846 | ProtocolRefs.size()), |
| 4847 | ProtocolRefs); |
| 4848 | |
| 4849 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4850 | GV = new llvm::GlobalVariable(Init->getType(), false, |
| 4851 | llvm::GlobalValue::InternalLinkage, |
| 4852 | Init, |
| 4853 | Name, |
| 4854 | &CGM.getModule()); |
| 4855 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4856 | GV->setAlignment( |
| 4857 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4858 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4859 | return llvm::ConstantExpr::getBitCast(GV, |
| 4860 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4861 | } |
| 4862 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4863 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 4864 | /// struct _objc_method { |
| 4865 | /// SEL _cmd; |
| 4866 | /// char *method_type; |
| 4867 | /// char *_imp; |
| 4868 | /// } |
| 4869 | |
| 4870 | llvm::Constant * |
| 4871 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 4872 | std::vector<llvm::Constant*> Desc(3); |
| 4873 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4874 | ObjCTypes.SelectorPtrTy); |
| 4875 | Desc[1] = GetMethodVarType(MD); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4876 | // Protocol methods have no implementation. So, this entry is always NULL. |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4877 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 4878 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
| 4879 | } |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4880 | |
| 4881 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 4882 | /// This code gen. amounts to generating code for: |
| 4883 | /// @code |
| 4884 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 4885 | /// @encode |
| 4886 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4887 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4888 | CodeGen::CodeGenFunction &CGF, |
| 4889 | QualType ObjectTy, |
| 4890 | llvm::Value *BaseValue, |
| 4891 | const ObjCIvarDecl *Ivar, |
| 4892 | const FieldDecl *Field, |
| 4893 | unsigned CVRQualifiers) { |
| 4894 | assert(ObjectTy->isObjCInterfaceType() && |
| 4895 | "CGObjCNonFragileABIMac::EmitObjCValueForIvar"); |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 4896 | ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 4897 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4898 | |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4899 | // (char *) BaseValue |
Chris Lattner | 51123fe | 2009-04-17 17:46:19 +0000 | [diff] [blame] | 4900 | llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4901 | llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV); |
| 4902 | // (char*)BaseValue + Offset_symbol |
| 4903 | V = CGF.Builder.CreateGEP(V, Offset, "add.ptr"); |
| 4904 | // (type *)((char*)BaseValue + Offset_symbol) |
| 4905 | const llvm::Type *IvarTy = |
Chris Lattner | 51123fe | 2009-04-17 17:46:19 +0000 | [diff] [blame] | 4906 | CGM.getTypes().ConvertTypeForMem(Ivar->getType()); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4907 | llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy); |
| 4908 | V = CGF.Builder.CreateBitCast(V, ptrIvarTy); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4909 | |
Fariborz Jahanian | a6681ae | 2009-03-09 20:44:22 +0000 | [diff] [blame] | 4910 | if (Ivar->isBitField()) { |
Chris Lattner | 51123fe | 2009-04-17 17:46:19 +0000 | [diff] [blame] | 4911 | QualType FieldTy = Field->getType(); |
Fariborz Jahanian | a6681ae | 2009-03-09 20:44:22 +0000 | [diff] [blame] | 4912 | CodeGenTypes::BitFieldInfo bitFieldInfo = |
| 4913 | CGM.getTypes().getBitFieldInfo(Field); |
| 4914 | return LValue::MakeBitfield(V, bitFieldInfo.Begin, bitFieldInfo.Size, |
Chris Lattner | 51123fe | 2009-04-17 17:46:19 +0000 | [diff] [blame] | 4915 | FieldTy->isSignedIntegerType(), |
| 4916 | FieldTy.getCVRQualifiers()|CVRQualifiers); |
Fariborz Jahanian | a6681ae | 2009-03-09 20:44:22 +0000 | [diff] [blame] | 4917 | } |
| 4918 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4919 | LValue LV = LValue::MakeAddr(V, |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 4920 | Ivar->getType().getCVRQualifiers()|CVRQualifiers, |
| 4921 | CGM.getContext().getObjCGCAttrKind(Ivar->getType())); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4922 | LValue::SetObjCIvar(LV, true); |
| 4923 | return LV; |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4924 | } |
| 4925 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4926 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
| 4927 | CodeGen::CodeGenFunction &CGF, |
| 4928 | ObjCInterfaceDecl *Interface, |
| 4929 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 4930 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar), |
| 4931 | false, "ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4932 | } |
| 4933 | |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4934 | CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend( |
| 4935 | CodeGen::CodeGenFunction &CGF, |
| 4936 | QualType ResultType, |
| 4937 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4938 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4939 | QualType Arg0Ty, |
| 4940 | bool IsSuper, |
| 4941 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4942 | // FIXME. Even though IsSuper is passes. This function doese not |
| 4943 | // handle calls to 'super' receivers. |
| 4944 | CodeGenTypes &Types = CGM.getTypes(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4945 | llvm::Value *Arg0 = Receiver; |
| 4946 | if (!IsSuper) |
| 4947 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4948 | |
| 4949 | // Find the message function name. |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 4950 | // FIXME. This is too much work to get the ABI-specific result type |
| 4951 | // needed to find the message name. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4952 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, |
| 4953 | llvm::SmallVector<QualType, 16>()); |
| 4954 | llvm::Constant *Fn; |
| 4955 | std::string Name("\01l_"); |
| 4956 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4957 | #if 0 |
| 4958 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4959 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
| 4960 | Fn = ObjCTypes.MessageSendIdStretFixupFn; |
| 4961 | // FIXME. Is there a better way of getting these names. |
| 4962 | // They are available in RuntimeFunctions vector pair. |
| 4963 | Name += "objc_msgSendId_stret_fixup"; |
| 4964 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4965 | else |
| 4966 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4967 | if (IsSuper) { |
| 4968 | Fn = ObjCTypes.MessageSendSuper2StretFixupFn; |
| 4969 | Name += "objc_msgSendSuper2_stret_fixup"; |
| 4970 | } |
| 4971 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4972 | { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4973 | Fn = ObjCTypes.MessageSendStretFixupFn; |
| 4974 | Name += "objc_msgSend_stret_fixup"; |
| 4975 | } |
| 4976 | } |
Fariborz Jahanian | 1a6b368 | 2009-02-05 19:35:43 +0000 | [diff] [blame] | 4977 | else if (ResultType->isFloatingType() && |
| 4978 | // Selection of frret API only happens in 32bit nonfragile ABI. |
| 4979 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4980 | Fn = ObjCTypes.MessageSendFpretFixupFn; |
| 4981 | Name += "objc_msgSend_fpret_fixup"; |
| 4982 | } |
| 4983 | else { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4984 | #if 0 |
| 4985 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4986 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
| 4987 | Fn = ObjCTypes.MessageSendIdFixupFn; |
| 4988 | Name += "objc_msgSendId_fixup"; |
| 4989 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4990 | else |
| 4991 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4992 | if (IsSuper) { |
| 4993 | Fn = ObjCTypes.MessageSendSuper2FixupFn; |
| 4994 | Name += "objc_msgSendSuper2_fixup"; |
| 4995 | } |
| 4996 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4997 | { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4998 | Fn = ObjCTypes.MessageSendFixupFn; |
| 4999 | Name += "objc_msgSend_fixup"; |
| 5000 | } |
| 5001 | } |
| 5002 | Name += '_'; |
| 5003 | std::string SelName(Sel.getAsString()); |
| 5004 | // Replace all ':' in selector name with '_' ouch! |
| 5005 | for(unsigned i = 0; i < SelName.size(); i++) |
| 5006 | if (SelName[i] == ':') |
| 5007 | SelName[i] = '_'; |
| 5008 | Name += SelName; |
| 5009 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5010 | if (!GV) { |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5011 | // Build message ref table entry. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5012 | std::vector<llvm::Constant*> Values(2); |
| 5013 | Values[0] = Fn; |
| 5014 | Values[1] = GetMethodVarName(Sel); |
| 5015 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 5016 | GV = new llvm::GlobalVariable(Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 5017 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5018 | Init, |
| 5019 | Name, |
| 5020 | &CGM.getModule()); |
| 5021 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | f59c1a6 | 2009-04-15 19:04:46 +0000 | [diff] [blame] | 5022 | GV->setAlignment(16); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5023 | GV->setSection("__DATA, __objc_msgrefs, coalesced"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5024 | } |
| 5025 | llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5026 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5027 | CallArgList ActualArgs; |
| 5028 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 5029 | ActualArgs.push_back(std::make_pair(RValue::get(Arg1), |
| 5030 | ObjCTypes.MessageRefCPtrTy)); |
| 5031 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5032 | const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs); |
| 5033 | llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0); |
| 5034 | Callee = CGF.Builder.CreateLoad(Callee); |
Fariborz Jahanian | 3ab75bd | 2009-02-14 21:25:36 +0000 | [diff] [blame] | 5035 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5036 | Callee = CGF.Builder.CreateBitCast(Callee, |
| 5037 | llvm::PointerType::getUnqual(FTy)); |
| 5038 | return CGF.EmitCall(FnInfo1, Callee, ActualArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5039 | } |
| 5040 | |
| 5041 | /// Generate code for a message send expression in the nonfragile abi. |
| 5042 | CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend( |
| 5043 | CodeGen::CodeGenFunction &CGF, |
| 5044 | QualType ResultType, |
| 5045 | Selector Sel, |
| 5046 | llvm::Value *Receiver, |
| 5047 | bool IsClassMessage, |
| 5048 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5049 | return EmitMessageSend(CGF, ResultType, Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5050 | Receiver, CGF.getContext().getObjCIdType(), |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5051 | false, CallArgs); |
| 5052 | } |
| 5053 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5054 | llvm::GlobalVariable * |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5055 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5056 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5057 | |
Daniel Dunbar | dfff230 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 5058 | if (!GV) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5059 | GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 5060 | llvm::GlobalValue::ExternalLinkage, |
| 5061 | 0, Name, &CGM.getModule()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5062 | } |
| 5063 | |
| 5064 | return GV; |
| 5065 | } |
| 5066 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5067 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5068 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5069 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 5070 | |
| 5071 | if (!Entry) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5072 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5073 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5074 | Entry = |
| 5075 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5076 | llvm::GlobalValue::InternalLinkage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5077 | ClassGV, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5078 | "\01L_OBJC_CLASSLIST_REFERENCES_$_", |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5079 | &CGM.getModule()); |
| 5080 | Entry->setAlignment( |
| 5081 | CGM.getTargetData().getPrefTypeAlignment( |
| 5082 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5083 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
| 5084 | UsedGlobals.push_back(Entry); |
| 5085 | } |
| 5086 | |
| 5087 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5088 | } |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5089 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5090 | llvm::Value * |
| 5091 | CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder, |
| 5092 | const ObjCInterfaceDecl *ID) { |
| 5093 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
| 5094 | |
| 5095 | if (!Entry) { |
| 5096 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5097 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
| 5098 | Entry = |
| 5099 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5100 | llvm::GlobalValue::InternalLinkage, |
| 5101 | ClassGV, |
| 5102 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 5103 | &CGM.getModule()); |
| 5104 | Entry->setAlignment( |
| 5105 | CGM.getTargetData().getPrefTypeAlignment( |
| 5106 | ObjCTypes.ClassnfABIPtrTy)); |
| 5107 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5108 | UsedGlobals.push_back(Entry); |
| 5109 | } |
| 5110 | |
| 5111 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5112 | } |
| 5113 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5114 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 5115 | /// meta-data |
| 5116 | /// |
| 5117 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 5118 | const ObjCInterfaceDecl *ID) { |
| 5119 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 5120 | if (Entry) |
| 5121 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5122 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5123 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5124 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5125 | Entry = |
| 5126 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5127 | llvm::GlobalValue::InternalLinkage, |
| 5128 | MetaClassGV, |
| 5129 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 5130 | &CGM.getModule()); |
| 5131 | Entry->setAlignment( |
| 5132 | CGM.getTargetData().getPrefTypeAlignment( |
| 5133 | ObjCTypes.ClassnfABIPtrTy)); |
| 5134 | |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5135 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5136 | UsedGlobals.push_back(Entry); |
| 5137 | |
| 5138 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5139 | } |
| 5140 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5141 | /// GetClass - Return a reference to the class for the given interface |
| 5142 | /// decl. |
| 5143 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 5144 | const ObjCInterfaceDecl *ID) { |
| 5145 | return EmitClassRef(Builder, ID); |
| 5146 | } |
| 5147 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5148 | /// Generates a message send where the super is the receiver. This is |
| 5149 | /// a message send to self with special delivery semantics indicating |
| 5150 | /// which class's method should be called. |
| 5151 | CodeGen::RValue |
| 5152 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 5153 | QualType ResultType, |
| 5154 | Selector Sel, |
| 5155 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5156 | bool isCategoryImpl, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5157 | llvm::Value *Receiver, |
| 5158 | bool IsClassMessage, |
| 5159 | const CodeGen::CallArgList &CallArgs) { |
| 5160 | // ... |
| 5161 | // Create and init a super structure; this is a (receiver, class) |
| 5162 | // pair we will pass to objc_msgSendSuper. |
| 5163 | llvm::Value *ObjCSuper = |
| 5164 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 5165 | |
| 5166 | llvm::Value *ReceiverAsObject = |
| 5167 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 5168 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 5169 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
| 5170 | |
| 5171 | // 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] | 5172 | llvm::Value *Target; |
| 5173 | if (IsClassMessage) { |
| 5174 | if (isCategoryImpl) { |
| 5175 | // Message sent to "super' in a class method defined in |
| 5176 | // a category implementation. |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5177 | Target = EmitClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5178 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 5179 | Target = CGF.Builder.CreateLoad(Target); |
| 5180 | } |
| 5181 | else |
| 5182 | Target = EmitMetaClassRef(CGF.Builder, Class); |
| 5183 | } |
| 5184 | else |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5185 | Target = EmitSuperClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5186 | |
| 5187 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 5188 | // and ObjCTypes types. |
| 5189 | const llvm::Type *ClassTy = |
| 5190 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 5191 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 5192 | CGF.Builder.CreateStore(Target, |
| 5193 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 5194 | |
| 5195 | return EmitMessageSend(CGF, ResultType, Sel, |
| 5196 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 5197 | true, CallArgs); |
| 5198 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5199 | |
| 5200 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
| 5201 | Selector Sel) { |
| 5202 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 5203 | |
| 5204 | if (!Entry) { |
| 5205 | llvm::Constant *Casted = |
| 5206 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 5207 | ObjCTypes.SelectorPtrTy); |
| 5208 | Entry = |
| 5209 | new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false, |
| 5210 | llvm::GlobalValue::InternalLinkage, |
| 5211 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_", |
| 5212 | &CGM.getModule()); |
| 5213 | Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip"); |
| 5214 | UsedGlobals.push_back(Entry); |
| 5215 | } |
| 5216 | |
| 5217 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5218 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5219 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 5220 | /// objc_assign_ivar (id src, id *dst) |
| 5221 | /// |
| 5222 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 5223 | llvm::Value *src, llvm::Value *dst) |
| 5224 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5225 | const llvm::Type * SrcTy = src->getType(); |
| 5226 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5227 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5228 | assert(Size <= 8 && "does not support size > 8"); |
| 5229 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5230 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5231 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5232 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5233 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5234 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 5235 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn, |
| 5236 | src, dst, "assignivar"); |
| 5237 | return; |
| 5238 | } |
| 5239 | |
| 5240 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 5241 | /// objc_assign_strongCast (id src, id *dst) |
| 5242 | /// |
| 5243 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
| 5244 | CodeGen::CodeGenFunction &CGF, |
| 5245 | llvm::Value *src, llvm::Value *dst) |
| 5246 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5247 | const llvm::Type * SrcTy = src->getType(); |
| 5248 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5249 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5250 | assert(Size <= 8 && "does not support size > 8"); |
| 5251 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5252 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5253 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5254 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5255 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5256 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 5257 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn, |
| 5258 | src, dst, "weakassign"); |
| 5259 | return; |
| 5260 | } |
| 5261 | |
| 5262 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 5263 | /// object: objc_read_weak (id *src) |
| 5264 | /// |
| 5265 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
| 5266 | CodeGen::CodeGenFunction &CGF, |
| 5267 | llvm::Value *AddrWeakObj) |
| 5268 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5269 | const llvm::Type* DestTy = |
| 5270 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5271 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
| 5272 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn, |
| 5273 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5274 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5275 | return read_weak; |
| 5276 | } |
| 5277 | |
| 5278 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 5279 | /// objc_assign_weak (id src, id *dst) |
| 5280 | /// |
| 5281 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 5282 | llvm::Value *src, llvm::Value *dst) |
| 5283 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5284 | const llvm::Type * SrcTy = src->getType(); |
| 5285 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5286 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5287 | assert(Size <= 8 && "does not support size > 8"); |
| 5288 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5289 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5290 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5291 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5292 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5293 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 5294 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5295 | src, dst, "weakassign"); |
| 5296 | return; |
| 5297 | } |
| 5298 | |
| 5299 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 5300 | /// objc_assign_global (id src, id *dst) |
| 5301 | /// |
| 5302 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 5303 | llvm::Value *src, llvm::Value *dst) |
| 5304 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5305 | const llvm::Type * SrcTy = src->getType(); |
| 5306 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5307 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5308 | assert(Size <= 8 && "does not support size > 8"); |
| 5309 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5310 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5311 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5312 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5313 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5314 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 5315 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn, |
| 5316 | src, dst, "globalassign"); |
| 5317 | return; |
| 5318 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5319 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5320 | void |
| 5321 | CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 5322 | const Stmt &S) { |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5323 | bool isTry = isa<ObjCAtTryStmt>(S); |
| 5324 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 5325 | llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5326 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5327 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5328 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5329 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
| 5330 | |
| 5331 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 5332 | // evaluation of the expression must occur before we enter the |
| 5333 | // @synchronized. We can safely avoid a temp here because jumps into |
| 5334 | // @synchronized are illegal & this will dominate uses. |
| 5335 | llvm::Value *SyncArg = 0; |
| 5336 | if (!isTry) { |
| 5337 | SyncArg = |
| 5338 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 5339 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5340 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5341 | } |
| 5342 | |
| 5343 | // Push an EH context entry, used for handling rethrows and jumps |
| 5344 | // through finally. |
| 5345 | CGF.PushCleanupBlock(FinallyBlock); |
| 5346 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5347 | CGF.setInvokeDest(TryHandler); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5348 | |
| 5349 | CGF.EmitBlock(TryBlock); |
| 5350 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 5351 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
| 5352 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5353 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5354 | // Emit the exception handler. |
| 5355 | |
| 5356 | CGF.EmitBlock(TryHandler); |
| 5357 | |
| 5358 | llvm::Value *llvm_eh_exception = |
| 5359 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
| 5360 | llvm::Value *llvm_eh_selector_i64 = |
| 5361 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64); |
| 5362 | llvm::Value *llvm_eh_typeid_for_i64 = |
| 5363 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64); |
| 5364 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5365 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow"); |
| 5366 | |
| 5367 | llvm::SmallVector<llvm::Value*, 8> SelectorArgs; |
| 5368 | SelectorArgs.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5369 | SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5370 | |
| 5371 | // Construct the lists of (type, catch body) to handle. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5372 | llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5373 | bool HasCatchAll = false; |
| 5374 | if (isTry) { |
| 5375 | if (const ObjCAtCatchStmt* CatchStmt = |
| 5376 | cast<ObjCAtTryStmt>(S).getCatchStmts()) { |
| 5377 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5378 | const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5379 | Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody())); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5380 | |
| 5381 | // catch(...) always matches. |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5382 | if (!CatchDecl) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5383 | // Use i8* null here to signal this is a catch all, not a cleanup. |
| 5384 | llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 5385 | SelectorArgs.push_back(Null); |
| 5386 | HasCatchAll = true; |
| 5387 | break; |
| 5388 | } |
| 5389 | |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5390 | if (CGF.getContext().isObjCIdType(CatchDecl->getType()) || |
| 5391 | CatchDecl->getType()->isObjCQualifiedIdType()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5392 | llvm::Value *IDEHType = |
| 5393 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 5394 | if (!IDEHType) |
| 5395 | IDEHType = |
| 5396 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5397 | llvm::GlobalValue::ExternalLinkage, |
| 5398 | 0, "OBJC_EHTYPE_id", &CGM.getModule()); |
| 5399 | SelectorArgs.push_back(IDEHType); |
| 5400 | HasCatchAll = true; |
| 5401 | break; |
| 5402 | } |
| 5403 | |
| 5404 | // All other types should be Objective-C interface pointer types. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5405 | const PointerType *PT = CatchDecl->getType()->getAsPointerType(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5406 | assert(PT && "Invalid @catch type."); |
| 5407 | const ObjCInterfaceType *IT = |
| 5408 | PT->getPointeeType()->getAsObjCInterfaceType(); |
| 5409 | assert(IT && "Invalid @catch type."); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5410 | llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5411 | SelectorArgs.push_back(EHType); |
| 5412 | } |
| 5413 | } |
| 5414 | } |
| 5415 | |
| 5416 | // We use a cleanup unless there was already a catch all. |
| 5417 | if (!HasCatchAll) { |
| 5418 | SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5419 | Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5420 | } |
| 5421 | |
| 5422 | llvm::Value *Selector = |
| 5423 | CGF.Builder.CreateCall(llvm_eh_selector_i64, |
| 5424 | SelectorArgs.begin(), SelectorArgs.end(), |
| 5425 | "selector"); |
| 5426 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5427 | const ParmVarDecl *CatchParam = Handlers[i].first; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5428 | const Stmt *CatchBody = Handlers[i].second; |
| 5429 | |
| 5430 | llvm::BasicBlock *Next = 0; |
| 5431 | |
| 5432 | // The last handler always matches. |
| 5433 | if (i + 1 != e) { |
| 5434 | assert(CatchParam && "Only last handler can be a catch all."); |
| 5435 | |
| 5436 | llvm::BasicBlock *Match = CGF.createBasicBlock("match"); |
| 5437 | Next = CGF.createBasicBlock("catch.next"); |
| 5438 | llvm::Value *Id = |
| 5439 | CGF.Builder.CreateCall(llvm_eh_typeid_for_i64, |
| 5440 | CGF.Builder.CreateBitCast(SelectorArgs[i+2], |
| 5441 | ObjCTypes.Int8PtrTy)); |
| 5442 | CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id), |
| 5443 | Match, Next); |
| 5444 | |
| 5445 | CGF.EmitBlock(Match); |
| 5446 | } |
| 5447 | |
| 5448 | if (CatchBody) { |
| 5449 | llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end"); |
| 5450 | llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler"); |
| 5451 | |
| 5452 | // Cleanups must call objc_end_catch. |
| 5453 | // |
| 5454 | // FIXME: It seems incorrect for objc_begin_catch to be inside |
| 5455 | // this context, but this matches gcc. |
| 5456 | CGF.PushCleanupBlock(MatchEnd); |
| 5457 | CGF.setInvokeDest(MatchHandler); |
| 5458 | |
| 5459 | llvm::Value *ExcObject = |
| 5460 | CGF.Builder.CreateCall(ObjCTypes.ObjCBeginCatchFn, Exc); |
| 5461 | |
| 5462 | // Bind the catch parameter if it exists. |
| 5463 | if (CatchParam) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5464 | ExcObject = |
| 5465 | CGF.Builder.CreateBitCast(ExcObject, |
| 5466 | CGF.ConvertType(CatchParam->getType())); |
| 5467 | // CatchParam is a ParmVarDecl because of the grammar |
| 5468 | // construction used to handle this, but for codegen purposes |
| 5469 | // we treat this as a local decl. |
| 5470 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
| 5471 | CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5472 | } |
| 5473 | |
| 5474 | CGF.ObjCEHValueStack.push_back(ExcObject); |
| 5475 | CGF.EmitStmt(CatchBody); |
| 5476 | CGF.ObjCEHValueStack.pop_back(); |
| 5477 | |
| 5478 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5479 | |
| 5480 | CGF.EmitBlock(MatchHandler); |
| 5481 | |
| 5482 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5483 | // We are required to emit this call to satisfy LLVM, even |
| 5484 | // though we don't use the result. |
| 5485 | llvm::SmallVector<llvm::Value*, 8> Args; |
| 5486 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5487 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5488 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5489 | 0)); |
| 5490 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5491 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5492 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5493 | |
| 5494 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5495 | |
| 5496 | CGF.EmitBlock(MatchEnd); |
| 5497 | |
| 5498 | // Unfortunately, we also have to generate another EH frame here |
| 5499 | // in case this throws. |
| 5500 | llvm::BasicBlock *MatchEndHandler = |
| 5501 | CGF.createBasicBlock("match.end.handler"); |
| 5502 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
| 5503 | CGF.Builder.CreateInvoke(ObjCTypes.ObjCEndCatchFn, |
| 5504 | Cont, MatchEndHandler, |
| 5505 | Args.begin(), Args.begin()); |
| 5506 | |
| 5507 | CGF.EmitBlock(Cont); |
| 5508 | if (Info.SwitchBlock) |
| 5509 | CGF.EmitBlock(Info.SwitchBlock); |
| 5510 | if (Info.EndBlock) |
| 5511 | CGF.EmitBlock(Info.EndBlock); |
| 5512 | |
| 5513 | CGF.EmitBlock(MatchEndHandler); |
| 5514 | Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5515 | // We are required to emit this call to satisfy LLVM, even |
| 5516 | // though we don't use the result. |
| 5517 | Args.clear(); |
| 5518 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5519 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5520 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5521 | 0)); |
| 5522 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5523 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5524 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5525 | |
| 5526 | if (Next) |
| 5527 | CGF.EmitBlock(Next); |
| 5528 | } else { |
| 5529 | assert(!Next && "catchup should be last handler."); |
| 5530 | |
| 5531 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5532 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5533 | } |
| 5534 | } |
| 5535 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5536 | // Pop the cleanup entry, the @finally is outside this cleanup |
| 5537 | // scope. |
| 5538 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5539 | CGF.setInvokeDest(PrevLandingPad); |
| 5540 | |
| 5541 | CGF.EmitBlock(FinallyBlock); |
| 5542 | |
| 5543 | if (isTry) { |
| 5544 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 5545 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 5546 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 5547 | } else { |
| 5548 | // Emit 'objc_sync_exit(expr)' as finally's sole statement for |
| 5549 | // @synchronized. |
| 5550 | CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5551 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5552 | |
| 5553 | if (Info.SwitchBlock) |
| 5554 | CGF.EmitBlock(Info.SwitchBlock); |
| 5555 | if (Info.EndBlock) |
| 5556 | CGF.EmitBlock(Info.EndBlock); |
| 5557 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5558 | // Branch around the rethrow code. |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5559 | CGF.EmitBranch(FinallyEnd); |
| 5560 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5561 | CGF.EmitBlock(FinallyRethrow); |
| 5562 | CGF.Builder.CreateCall(ObjCTypes.UnwindResumeOrRethrowFn, |
| 5563 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5564 | CGF.Builder.CreateUnreachable(); |
| 5565 | |
| 5566 | CGF.EmitBlock(FinallyEnd); |
| 5567 | } |
| 5568 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5569 | /// EmitThrowStmt - Generate code for a throw statement. |
| 5570 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 5571 | const ObjCAtThrowStmt &S) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5572 | llvm::Value *Exception; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5573 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5574 | Exception = CGF.EmitScalarExpr(ThrowExpr); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5575 | } else { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5576 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
| 5577 | "Unexpected rethrow outside @catch block."); |
| 5578 | Exception = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5579 | } |
| 5580 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5581 | llvm::Value *ExceptionAsObject = |
| 5582 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 5583 | llvm::BasicBlock *InvokeDest = CGF.getInvokeDest(); |
| 5584 | if (InvokeDest) { |
| 5585 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
| 5586 | CGF.Builder.CreateInvoke(ObjCTypes.ExceptionThrowFn, |
| 5587 | Cont, InvokeDest, |
| 5588 | &ExceptionAsObject, &ExceptionAsObject + 1); |
| 5589 | CGF.EmitBlock(Cont); |
| 5590 | } else |
| 5591 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject); |
| 5592 | CGF.Builder.CreateUnreachable(); |
| 5593 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5594 | // Clear the insertion point to indicate we are in unreachable code. |
| 5595 | CGF.Builder.ClearInsertionPoint(); |
| 5596 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5597 | |
| 5598 | llvm::Value * |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5599 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 5600 | bool ForDefinition) { |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5601 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5602 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5603 | // If we don't need a definition, return the entry if found or check |
| 5604 | // if we use an external reference. |
| 5605 | if (!ForDefinition) { |
| 5606 | if (Entry) |
| 5607 | return Entry; |
Daniel Dunbar | 7e075cb | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 5608 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5609 | // If this type (or a super class) has the __objc_exception__ |
| 5610 | // attribute, emit an external reference. |
| 5611 | if (hasObjCExceptionAttribute(ID)) |
| 5612 | return Entry = |
| 5613 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5614 | llvm::GlobalValue::ExternalLinkage, |
| 5615 | 0, |
| 5616 | (std::string("OBJC_EHTYPE_$_") + |
| 5617 | ID->getIdentifier()->getName()), |
| 5618 | &CGM.getModule()); |
| 5619 | } |
| 5620 | |
| 5621 | // Otherwise we need to either make a new entry or fill in the |
| 5622 | // initializer. |
| 5623 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5624 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5625 | std::string VTableName = "objc_ehtype_vtable"; |
| 5626 | llvm::GlobalVariable *VTableGV = |
| 5627 | CGM.getModule().getGlobalVariable(VTableName); |
| 5628 | if (!VTableGV) |
| 5629 | VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false, |
| 5630 | llvm::GlobalValue::ExternalLinkage, |
| 5631 | 0, VTableName, &CGM.getModule()); |
| 5632 | |
| 5633 | llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2); |
| 5634 | |
| 5635 | std::vector<llvm::Constant*> Values(3); |
| 5636 | Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1); |
| 5637 | Values[1] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5638 | Values[2] = GetClassGlobal(ClassName); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5639 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
| 5640 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5641 | if (Entry) { |
| 5642 | Entry->setInitializer(Init); |
| 5643 | } else { |
| 5644 | Entry = new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5645 | llvm::GlobalValue::WeakAnyLinkage, |
| 5646 | Init, |
| 5647 | (std::string("OBJC_EHTYPE_$_") + |
| 5648 | ID->getIdentifier()->getName()), |
| 5649 | &CGM.getModule()); |
| 5650 | } |
| 5651 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 5652 | if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden) |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5653 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5654 | Entry->setAlignment(8); |
| 5655 | |
| 5656 | if (ForDefinition) { |
| 5657 | Entry->setSection("__DATA,__objc_const"); |
| 5658 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 5659 | } else { |
| 5660 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 5661 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5662 | |
| 5663 | return Entry; |
| 5664 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5665 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5666 | /* *** */ |
| 5667 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5668 | CodeGen::CGObjCRuntime * |
| 5669 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 5670 | return new CGObjCMac(CGM); |
| 5671 | } |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5672 | |
| 5673 | CodeGen::CGObjCRuntime * |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5674 | CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5675 | return new CGObjCNonFragileABIMac(CGM); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5676 | } |