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 | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 23 | #include "llvm/Module.h" |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/DenseSet.h" |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetData.h" |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 26 | #include <sstream> |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 29 | using namespace CodeGen; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 30 | |
| 31 | namespace { |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 32 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 33 | typedef std::vector<llvm::Constant*> ConstantVector; |
| 34 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 35 | // FIXME: We should find a nicer way to make the labels for |
| 36 | // metadata, string concatenation is lame. |
| 37 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 38 | class ObjCCommonTypesHelper { |
| 39 | protected: |
| 40 | CodeGen::CodeGenModule &CGM; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 41 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 42 | public: |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 43 | const llvm::Type *ShortTy, *IntTy, *LongTy; |
| 44 | const llvm::Type *Int8PtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 45 | |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 46 | /// ObjectPtrTy - LLVM type for object handles (typeof(id)) |
| 47 | const llvm::Type *ObjectPtrTy; |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 48 | |
| 49 | /// PtrObjectPtrTy - LLVM type for id * |
| 50 | const llvm::Type *PtrObjectPtrTy; |
| 51 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 52 | /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL)) |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 53 | const llvm::Type *SelectorPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 54 | /// ProtocolPtrTy - LLVM type for external protocol handles |
| 55 | /// (typeof(Protocol)) |
| 56 | const llvm::Type *ExternalProtocolPtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 57 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 58 | // SuperCTy - clang type for struct objc_super. |
| 59 | QualType SuperCTy; |
| 60 | // SuperPtrCTy - clang type for struct objc_super *. |
| 61 | QualType SuperPtrCTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 62 | |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 63 | /// SuperTy - LLVM type for struct objc_super. |
| 64 | const llvm::StructType *SuperTy; |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 65 | /// SuperPtrTy - LLVM type for struct objc_super *. |
| 66 | const llvm::Type *SuperPtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 67 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 68 | /// PropertyTy - LLVM type for struct objc_property (struct _prop_t |
| 69 | /// in GCC parlance). |
| 70 | const llvm::StructType *PropertyTy; |
| 71 | |
| 72 | /// PropertyListTy - LLVM type for struct objc_property_list |
| 73 | /// (_prop_list_t in GCC parlance). |
| 74 | const llvm::StructType *PropertyListTy; |
| 75 | /// PropertyListPtrTy - LLVM type for struct objc_property_list*. |
| 76 | const llvm::Type *PropertyListPtrTy; |
| 77 | |
| 78 | // MethodTy - LLVM type for struct objc_method. |
| 79 | const llvm::StructType *MethodTy; |
| 80 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 81 | /// CacheTy - LLVM type for struct objc_cache. |
| 82 | const llvm::Type *CacheTy; |
| 83 | /// CachePtrTy - LLVM type for struct objc_cache *. |
| 84 | const llvm::Type *CachePtrTy; |
| 85 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 86 | llvm::Function *GetPropertyFn, *SetPropertyFn; |
| 87 | |
| 88 | llvm::Function *EnumerationMutationFn; |
| 89 | |
| 90 | /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. |
| 91 | llvm::Function *GcReadWeakFn; |
| 92 | |
| 93 | /// GcAssignWeakFn -- LLVM objc_assign_weak function. |
| 94 | llvm::Function *GcAssignWeakFn; |
| 95 | |
| 96 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
| 97 | llvm::Function *GcAssignGlobalFn; |
| 98 | |
| 99 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
| 100 | llvm::Function *GcAssignIvarFn; |
| 101 | |
| 102 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
| 103 | llvm::Function *GcAssignStrongCastFn; |
| 104 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 105 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
| 106 | ~ObjCCommonTypesHelper(){} |
| 107 | }; |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 108 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 109 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 110 | /// construction of varies types used during ObjC generation. |
| 111 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
| 112 | private: |
| 113 | |
| 114 | llvm::Function *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn; |
| 115 | llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn, |
| 116 | *MessageSendSuperFpretFn; |
| 117 | |
| 118 | public: |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 119 | /// SymtabTy - LLVM type for struct objc_symtab. |
| 120 | const llvm::StructType *SymtabTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 121 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
| 122 | const llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 123 | /// ModuleTy - LLVM type for struct objc_module. |
| 124 | const llvm::StructType *ModuleTy; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 125 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 126 | /// ProtocolTy - LLVM type for struct objc_protocol. |
| 127 | const llvm::StructType *ProtocolTy; |
| 128 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
| 129 | const llvm::Type *ProtocolPtrTy; |
| 130 | /// ProtocolExtensionTy - LLVM type for struct |
| 131 | /// objc_protocol_extension. |
| 132 | const llvm::StructType *ProtocolExtensionTy; |
| 133 | /// ProtocolExtensionTy - LLVM type for struct |
| 134 | /// objc_protocol_extension *. |
| 135 | const llvm::Type *ProtocolExtensionPtrTy; |
| 136 | /// MethodDescriptionTy - LLVM type for struct |
| 137 | /// objc_method_description. |
| 138 | const llvm::StructType *MethodDescriptionTy; |
| 139 | /// MethodDescriptionListTy - LLVM type for struct |
| 140 | /// objc_method_description_list. |
| 141 | const llvm::StructType *MethodDescriptionListTy; |
| 142 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 143 | /// objc_method_description_list *. |
| 144 | const llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 145 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
| 146 | const llvm::Type *ProtocolListTy; |
| 147 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
| 148 | const llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 149 | /// CategoryTy - LLVM type for struct objc_category. |
| 150 | const llvm::StructType *CategoryTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 151 | /// ClassTy - LLVM type for struct objc_class. |
| 152 | const llvm::StructType *ClassTy; |
| 153 | /// ClassPtrTy - LLVM type for struct objc_class *. |
| 154 | const llvm::Type *ClassPtrTy; |
| 155 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
| 156 | const llvm::StructType *ClassExtensionTy; |
| 157 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
| 158 | const llvm::Type *ClassExtensionPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 159 | // IvarTy - LLVM type for struct objc_ivar. |
| 160 | const llvm::StructType *IvarTy; |
| 161 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
| 162 | const llvm::Type *IvarListTy; |
| 163 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
| 164 | const llvm::Type *IvarListPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 165 | /// MethodListTy - LLVM type for struct objc_method_list. |
| 166 | const llvm::Type *MethodListTy; |
| 167 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
| 168 | const llvm::Type *MethodListPtrTy; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 169 | |
| 170 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
| 171 | const llvm::Type *ExceptionDataTy; |
| 172 | |
| 173 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
| 174 | llvm::Function *ExceptionThrowFn; |
| 175 | |
| 176 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
| 177 | llvm::Function *ExceptionTryEnterFn; |
| 178 | |
| 179 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
| 180 | llvm::Function *ExceptionTryExitFn; |
| 181 | |
| 182 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
| 183 | llvm::Function *ExceptionExtractFn; |
| 184 | |
| 185 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
| 186 | llvm::Function *ExceptionMatchFn; |
| 187 | |
| 188 | /// SetJmpFn - LLVM _setjmp function. |
| 189 | llvm::Function *SetJmpFn; |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 190 | |
| 191 | /// SyncEnterFn - LLVM object_sync_enter function. |
| 192 | llvm::Function *SyncEnterFn; |
| 193 | |
| 194 | /// SyncExitFn - LLVM object_sync_exit function. |
| 195 | llvm::Function *SyncExitFn; |
| 196 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 197 | public: |
| 198 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 199 | ~ObjCTypesHelper() {} |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 200 | |
| 201 | |
| 202 | llvm::Function *getSendFn(bool IsSuper) { |
| 203 | return IsSuper ? MessageSendSuperFn : MessageSendFn; |
| 204 | } |
| 205 | |
| 206 | llvm::Function *getSendStretFn(bool IsSuper) { |
| 207 | return IsSuper ? MessageSendSuperStretFn : MessageSendStretFn; |
| 208 | } |
| 209 | |
| 210 | llvm::Function *getSendFpretFn(bool IsSuper) { |
| 211 | return IsSuper ? MessageSendSuperFpretFn : MessageSendFpretFn; |
| 212 | } |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 213 | }; |
| 214 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 215 | /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 216 | /// modern abi |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 217 | class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 218 | private: |
| 219 | llvm::Function *MessageSendFixupFn, *MessageSendFpretFixupFn, |
| 220 | *MessageSendStretFixupFn, *MessageSendIdFixupFn, |
| 221 | *MessageSendIdStretFixupFn, *MessageSendSuper2FixupFn, |
| 222 | *MessageSendSuper2StretFixupFn; |
| 223 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 224 | public: |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 225 | // MethodListnfABITy - LLVM for struct _method_list_t |
| 226 | const llvm::StructType *MethodListnfABITy; |
| 227 | |
| 228 | // MethodListnfABIPtrTy - LLVM for struct _method_list_t* |
| 229 | const llvm::Type *MethodListnfABIPtrTy; |
| 230 | |
| 231 | // ProtocolnfABITy = LLVM for struct _protocol_t |
| 232 | const llvm::StructType *ProtocolnfABITy; |
| 233 | |
| 234 | // ProtocolListnfABITy - LLVM for struct _objc_protocol_list |
| 235 | const llvm::StructType *ProtocolListnfABITy; |
| 236 | |
| 237 | // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list* |
| 238 | const llvm::Type *ProtocolListnfABIPtrTy; |
| 239 | |
| 240 | // ClassnfABITy - LLVM for struct _class_t |
| 241 | const llvm::StructType *ClassnfABITy; |
| 242 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 243 | // ClassnfABIPtrTy - LLVM for struct _class_t* |
| 244 | const llvm::Type *ClassnfABIPtrTy; |
| 245 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 246 | // IvarnfABITy - LLVM for struct _ivar_t |
| 247 | const llvm::StructType *IvarnfABITy; |
| 248 | |
| 249 | // IvarListnfABITy - LLVM for struct _ivar_list_t |
| 250 | const llvm::StructType *IvarListnfABITy; |
| 251 | |
| 252 | // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t* |
| 253 | const llvm::Type *IvarListnfABIPtrTy; |
| 254 | |
| 255 | // ClassRonfABITy - LLVM for struct _class_ro_t |
| 256 | const llvm::StructType *ClassRonfABITy; |
| 257 | |
| 258 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 259 | const llvm::Type *ImpnfABITy; |
| 260 | |
| 261 | // CategorynfABITy - LLVM for struct _category_t |
| 262 | const llvm::StructType *CategorynfABITy; |
| 263 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 264 | // New types for nonfragile abi messaging. |
| 265 | |
| 266 | // MessageRefTy - LLVM for: |
| 267 | // struct _message_ref_t { |
| 268 | // IMP messenger; |
| 269 | // SEL name; |
| 270 | // }; |
| 271 | const llvm::StructType *MessageRefTy; |
| 272 | |
| 273 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 274 | const llvm::Type *MessageRefPtrTy; |
| 275 | |
| 276 | // SuperMessageRefTy - LLVM for: |
| 277 | // struct _super_message_ref_t { |
| 278 | // SUPER_IMP messenger; |
| 279 | // SEL name; |
| 280 | // }; |
| 281 | const llvm::StructType *SuperMessageRefTy; |
| 282 | |
| 283 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 284 | const llvm::Type *SuperMessageRefPtrTy; |
| 285 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 286 | ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); |
| 287 | ~ObjCNonFragileABITypesHelper(){} |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 288 | }; |
| 289 | |
| 290 | class CGObjCCommonMac : public CodeGen::CGObjCRuntime { |
| 291 | protected: |
| 292 | CodeGen::CodeGenModule &CGM; |
| 293 | // FIXME! May not be needing this after all. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 294 | unsigned ObjCABI; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 295 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 296 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 297 | /// DefinedSymbols and FinishModule(). |
| 298 | std::set<IdentifierInfo*> LazySymbols; |
| 299 | |
| 300 | /// DefinedSymbols - External symbols which are defined by this |
| 301 | /// module. The symbols in this list and LazySymbols are used to add |
| 302 | /// special linker symbols which ensure that Objective-C modules are |
| 303 | /// linked properly. |
| 304 | std::set<IdentifierInfo*> DefinedSymbols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 305 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 306 | /// ClassNames - uniqued class names. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 307 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 308 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 309 | /// MethodVarNames - uniqued method variable names. |
| 310 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 311 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 312 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 313 | /// a StringMap here because have no other unique reference. |
| 314 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 315 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 316 | /// MethodDefinitions - map of methods which have been defined in |
| 317 | /// this translation unit. |
| 318 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 319 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 320 | /// PropertyNames - uniqued method variable names. |
| 321 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 322 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 323 | /// ClassReferences - uniqued class references. |
| 324 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 325 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 326 | /// SelectorReferences - uniqued selector references. |
| 327 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 328 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 329 | /// Protocols - Protocols for which an objc_protocol structure has |
| 330 | /// been emitted. Forward declarations are handled by creating an |
| 331 | /// empty structure whose initializer is filled in when/if defined. |
| 332 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 333 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 334 | /// DefinedProtocols - Protocols which have actually been |
| 335 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 336 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 337 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 338 | /// DefinedClasses - List of defined classes. |
| 339 | std::vector<llvm::GlobalValue*> DefinedClasses; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 340 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 341 | /// DefinedCategories - List of defined categories. |
| 342 | std::vector<llvm::GlobalValue*> DefinedCategories; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 343 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 344 | /// UsedGlobals - List of globals to pack into the llvm.used metadata |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 345 | /// to prevent them from being clobbered. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 346 | std::vector<llvm::GlobalVariable*> UsedGlobals; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 347 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 348 | /// GetNameForMethod - Return a name for the given method. |
| 349 | /// \param[out] NameOut - The return value. |
| 350 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 351 | const ObjCContainerDecl *CD, |
| 352 | std::string &NameOut); |
| 353 | |
| 354 | /// GetMethodVarName - Return a unique constant for the given |
| 355 | /// selector's name. The return value has type char *. |
| 356 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 357 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
| 358 | llvm::Constant *GetMethodVarName(const std::string &Name); |
| 359 | |
| 360 | /// GetMethodVarType - Return a unique constant for the given |
| 361 | /// selector's name. The return value has type char *. |
| 362 | |
| 363 | // FIXME: This is a horrible name. |
| 364 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D); |
| 365 | llvm::Constant *GetMethodVarType(const std::string &Name); |
| 366 | |
| 367 | /// GetPropertyName - Return a unique constant for the given |
| 368 | /// name. The return value has type char *. |
| 369 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
| 370 | |
| 371 | // FIXME: This can be dropped once string functions are unified. |
| 372 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 373 | const Decl *Container); |
| 374 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 375 | /// GetClassName - Return a unique constant for the given selector's |
| 376 | /// name. The return value has type char *. |
| 377 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
| 378 | |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 379 | const RecordDecl *GetFirstIvarInRecord(const ObjCInterfaceDecl *OID, |
| 380 | RecordDecl::field_iterator &FIV, |
| 381 | RecordDecl::field_iterator &PIV); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 382 | /// EmitPropertyList - Emit the given property list. The return |
| 383 | /// value has type PropertyListPtrTy. |
| 384 | llvm::Constant *EmitPropertyList(const std::string &Name, |
| 385 | const Decl *Container, |
| 386 | const ObjCContainerDecl *OCD, |
| 387 | const ObjCCommonTypesHelper &ObjCTypes); |
| 388 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 389 | /// GetProtocolRef - Return a reference to the internal protocol |
| 390 | /// description, creating an empty one if it has not been |
| 391 | /// defined. The return value has type ProtocolPtrTy. |
| 392 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
| 393 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 394 | public: |
| 395 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm) |
| 396 | { } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 397 | |
| 398 | virtual llvm::Constant *GenerateConstantString(const std::string &String); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 399 | |
| 400 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 401 | const ObjCContainerDecl *CD=0); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 402 | |
| 403 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
| 404 | |
| 405 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 406 | /// declaration, emitting it if necessary. The return value has type |
| 407 | /// ProtocolPtrTy. |
| 408 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
| 409 | |
| 410 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 411 | /// object for the given declaration, emitting it if needed. These |
| 412 | /// forward references will be filled in with empty bodies if no |
| 413 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 414 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 415 | }; |
| 416 | |
| 417 | class CGObjCMac : public CGObjCCommonMac { |
| 418 | private: |
| 419 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 420 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 421 | /// level information. |
| 422 | void EmitImageInfo(); |
| 423 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 424 | /// EmitModuleInfo - Another marker encoding module level |
| 425 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 426 | void EmitModuleInfo(); |
| 427 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 428 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 429 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 430 | llvm::Constant *EmitModuleSymbols(); |
| 431 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 432 | /// FinishModule - Write out global data structures at the end of |
| 433 | /// processing a translation unit. |
| 434 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 435 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 436 | /// EmitClassExtension - Generate the class extension structure used |
| 437 | /// to store the weak ivar layout and properties. The return value |
| 438 | /// has type ClassExtensionPtrTy. |
| 439 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 440 | |
| 441 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 442 | /// for the given class. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 443 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 444 | const ObjCInterfaceDecl *ID); |
| 445 | |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 446 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 447 | QualType ResultType, |
| 448 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 449 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 450 | QualType Arg0Ty, |
| 451 | bool IsSuper, |
| 452 | const CallArgList &CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 453 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 454 | /// EmitIvarList - Emit the ivar list for the given |
| 455 | /// implementation. If ForClass is true the list of class ivars |
| 456 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 457 | /// interface ivars will be emitted. The return value has type |
| 458 | /// IvarListPtrTy. |
| 459 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 460 | bool ForClass); |
| 461 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 462 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 463 | /// for the metaclass of the given interface. The return value has |
| 464 | /// type ClassPtrTy. |
| 465 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 466 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 467 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 468 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 469 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 470 | llvm::Constant *Protocols, |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 471 | const llvm::Type *InterfaceTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 472 | const ConstantVector &Methods); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 473 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 474 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 475 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 476 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 477 | |
| 478 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 479 | /// implementation. The return value has type MethodListPtrTy. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 480 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 481 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 482 | const ConstantVector &Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 483 | |
| 484 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 485 | /// method declarations. |
| 486 | /// - TypeName: The name for the type containing the methods. |
| 487 | /// - IsProtocol: True iff these methods are for a protocol. |
| 488 | /// - ClassMethds: True iff these are class methods. |
| 489 | /// - Required: When true, only "required" methods are |
| 490 | /// listed. Similarly, when false only "optional" methods are |
| 491 | /// listed. For classes this should always be true. |
| 492 | /// - begin, end: The method list to output. |
| 493 | /// |
| 494 | /// The return value has type MethodDescriptionListPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 495 | llvm::Constant *EmitMethodDescList(const std::string &Name, |
| 496 | const char *Section, |
| 497 | const ConstantVector &Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 498 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 499 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 500 | /// declaration, emitting it if necessary. The return value has type |
| 501 | /// ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 502 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 503 | |
| 504 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 505 | /// object for the given declaration, emitting it if needed. These |
| 506 | /// forward references will be filled in with empty bodies if no |
| 507 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 508 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 509 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 510 | /// EmitProtocolExtension - Generate the protocol extension |
| 511 | /// structure used to store optional instance and class methods, and |
| 512 | /// protocol properties. The return value has type |
| 513 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 514 | llvm::Constant * |
| 515 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 516 | const ConstantVector &OptInstanceMethods, |
| 517 | const ConstantVector &OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 518 | |
| 519 | /// EmitProtocolList - Generate the list of referenced |
| 520 | /// protocols. The return value has type ProtocolListPtrTy. |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 521 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 522 | ObjCProtocolDecl::protocol_iterator begin, |
| 523 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 524 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 525 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 526 | /// for the given selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 527 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 528 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 529 | public: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 530 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 531 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 532 | virtual llvm::Function *ModuleInitFunction(); |
| 533 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 534 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 535 | QualType ResultType, |
| 536 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 537 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 538 | bool IsClassMessage, |
| 539 | const CallArgList &CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 540 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 541 | virtual CodeGen::RValue |
| 542 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 543 | QualType ResultType, |
| 544 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 545 | const ObjCInterfaceDecl *Class, |
| 546 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 547 | bool IsClassMessage, |
| 548 | const CallArgList &CallArgs); |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 549 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 550 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 551 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 552 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 553 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 554 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 555 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 556 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 557 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 558 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 559 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 560 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 561 | |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 562 | virtual llvm::Function *GetPropertyGetFunction(); |
| 563 | virtual llvm::Function *GetPropertySetFunction(); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 564 | virtual llvm::Function *EnumerationMutationFunction(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 565 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 566 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 567 | const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 568 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 569 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 570 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 571 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 572 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 573 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 574 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 575 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 576 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 577 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 578 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 579 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 580 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 581 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 582 | QualType ObjectTy, |
| 583 | llvm::Value *BaseValue, |
| 584 | const ObjCIvarDecl *Ivar, |
| 585 | const FieldDecl *Field, |
| 586 | unsigned CVRQualifiers); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 587 | }; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 588 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 589 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 590 | private: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 591 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 592 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 593 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
| 594 | |
| 595 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 596 | /// processing a translation unit. |
| 597 | void FinishNonFragileABIModule(); |
| 598 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 599 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 600 | unsigned InstanceStart, |
| 601 | unsigned InstanceSize, |
| 602 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 603 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
| 604 | llvm::Constant *IsAGV, |
| 605 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 606 | llvm::Constant *ClassRoGV, |
| 607 | bool HiddenVisibility); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 608 | |
| 609 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
| 610 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 611 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
| 612 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 613 | /// EmitMethodList - Emit the method list for the given |
| 614 | /// implementation. The return value has type MethodListnfABITy. |
| 615 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 616 | const char *Section, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 617 | const ConstantVector &Methods); |
| 618 | /// EmitIvarList - Emit the ivar list for the given |
| 619 | /// implementation. If ForClass is true the list of class ivars |
| 620 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 621 | /// interface ivars will be emitted. The return value has type |
| 622 | /// IvarListnfABIPtrTy. |
| 623 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 624 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 625 | llvm::Constant *EmitIvarOffsetVar(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 626 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 627 | unsigned long int offset); |
| 628 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 629 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 630 | /// declaration, emitting it if necessary. The return value has type |
| 631 | /// ProtocolPtrTy. |
| 632 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
| 633 | |
| 634 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 635 | /// object for the given declaration, emitting it if needed. These |
| 636 | /// forward references will be filled in with empty bodies if no |
| 637 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 638 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
| 639 | |
| 640 | /// EmitProtocolList - Generate the list of referenced |
| 641 | /// protocols. The return value has type ProtocolListPtrTy. |
| 642 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 643 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame^] | 644 | ObjCProtocolDecl::protocol_iterator end); |
| 645 | |
| 646 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 647 | QualType ResultType, |
| 648 | Selector Sel, |
| 649 | llvm::Value *Arg0, |
| 650 | QualType Arg0Ty, |
| 651 | bool IsSuper, |
| 652 | const CallArgList &CallArgs); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 653 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 654 | public: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 655 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 656 | // FIXME. All stubs for now! |
| 657 | virtual llvm::Function *ModuleInitFunction(); |
| 658 | |
| 659 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
| 660 | QualType ResultType, |
| 661 | Selector Sel, |
| 662 | llvm::Value *Receiver, |
| 663 | bool IsClassMessage, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame^] | 664 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 665 | |
| 666 | virtual CodeGen::RValue |
| 667 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 668 | QualType ResultType, |
| 669 | Selector Sel, |
| 670 | const ObjCInterfaceDecl *Class, |
| 671 | llvm::Value *Receiver, |
| 672 | bool IsClassMessage, |
| 673 | const CallArgList &CallArgs){ return RValue::get(0);} |
| 674 | |
| 675 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
| 676 | const ObjCInterfaceDecl *ID){ return 0; } |
| 677 | |
| 678 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel) |
| 679 | { return 0; } |
| 680 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 681 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 682 | |
| 683 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 684 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 685 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 686 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 687 | virtual llvm::Function *GetPropertyGetFunction(){ return 0; } |
| 688 | virtual llvm::Function *GetPropertySetFunction() |
| 689 | { return 0; } |
| 690 | virtual llvm::Function *EnumerationMutationFunction() |
| 691 | { return 0; } |
| 692 | |
| 693 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 694 | const Stmt &S) |
| 695 | { return; } |
| 696 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 697 | const ObjCAtThrowStmt &S) |
| 698 | { return; } |
| 699 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
| 700 | llvm::Value *AddrWeakObj) |
| 701 | { return 0; } |
| 702 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 703 | llvm::Value *src, llvm::Value *dst) |
| 704 | { return; } |
| 705 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 706 | llvm::Value *src, llvm::Value *dest) |
| 707 | { return; } |
| 708 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 709 | llvm::Value *src, llvm::Value *dest) |
| 710 | { return; } |
| 711 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 712 | llvm::Value *src, llvm::Value *dest) |
| 713 | { return; } |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 714 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 715 | QualType ObjectTy, |
| 716 | llvm::Value *BaseValue, |
| 717 | const ObjCIvarDecl *Ivar, |
| 718 | const FieldDecl *Field, |
| 719 | unsigned CVRQualifiers); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 720 | }; |
| 721 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 722 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 723 | |
| 724 | /* *** Helper Functions *** */ |
| 725 | |
| 726 | /// getConstantGEP() - Help routine to construct simple GEPs. |
| 727 | static llvm::Constant *getConstantGEP(llvm::Constant *C, |
| 728 | unsigned idx0, |
| 729 | unsigned idx1) { |
| 730 | llvm::Value *Idxs[] = { |
| 731 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0), |
| 732 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1) |
| 733 | }; |
| 734 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2); |
| 735 | } |
| 736 | |
| 737 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 738 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 739 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
| 740 | ObjCTypes(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 741 | { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 742 | ObjCABI = 1; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 743 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 746 | /// GetClass - Return a reference to the class for the given interface |
| 747 | /// decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 748 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 749 | const ObjCInterfaceDecl *ID) { |
| 750 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 754 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 755 | return EmitSelector(Builder, Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 758 | /// Generate a constant CFString object. |
| 759 | /* |
| 760 | struct __builtin_CFString { |
| 761 | const int *isa; // point to __CFConstantStringClassReference |
| 762 | int flags; |
| 763 | const char *str; |
| 764 | long length; |
| 765 | }; |
| 766 | */ |
| 767 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 768 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
| 769 | const std::string &String) { |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 770 | return CGM.GetAddrOfConstantCFString(String); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | /// Generates a message send where the super is the receiver. This is |
| 774 | /// a message send to self with special delivery semantics indicating |
| 775 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 776 | CodeGen::RValue |
| 777 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 778 | QualType ResultType, |
| 779 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 780 | const ObjCInterfaceDecl *Class, |
| 781 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 782 | bool IsClassMessage, |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 783 | const CodeGen::CallArgList &CallArgs) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 784 | // Create and init a super structure; this is a (receiver, class) |
| 785 | // pair we will pass to objc_msgSendSuper. |
| 786 | llvm::Value *ObjCSuper = |
| 787 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 788 | llvm::Value *ReceiverAsObject = |
| 789 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 790 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 791 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 792 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 793 | // If this is a class message the metaclass is passed as the target. |
| 794 | llvm::Value *Target; |
| 795 | if (IsClassMessage) { |
| 796 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 797 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 798 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 799 | Target = Super; |
| 800 | } else { |
| 801 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 802 | } |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 803 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 804 | // and ObjCTypes types. |
| 805 | const llvm::Type *ClassTy = |
| 806 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 807 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 808 | CGF.Builder.CreateStore(Target, |
| 809 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 810 | |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 811 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 812 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 813 | true, CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 814 | } |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 815 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 816 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 817 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 818 | QualType ResultType, |
| 819 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 820 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 821 | bool IsClassMessage, |
| 822 | const CallArgList &CallArgs) { |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 823 | llvm::Value *Arg0 = |
| 824 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 825 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 826 | Arg0, CGF.getContext().getObjCIdType(), |
| 827 | false, CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 831 | QualType ResultType, |
| 832 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 833 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 834 | QualType Arg0Ty, |
| 835 | bool IsSuper, |
| 836 | const CallArgList &CallArgs) { |
| 837 | CallArgList ActualArgs; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 838 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 839 | ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder, |
| 840 | Sel)), |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 841 | CGF.getContext().getObjCSelType())); |
| 842 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 843 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 844 | CodeGenTypes &Types = CGM.getTypes(); |
| 845 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); |
| 846 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 847 | |
| 848 | llvm::Constant *Fn; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 849 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 850 | Fn = ObjCTypes.getSendStretFn(IsSuper); |
| 851 | } else if (ResultType->isFloatingType()) { |
| 852 | // FIXME: Sadly, this is wrong. This actually depends on the |
| 853 | // architecture. This happens to be right for x86-32 though. |
| 854 | Fn = ObjCTypes.getSendFpretFn(IsSuper); |
| 855 | } else { |
| 856 | Fn = ObjCTypes.getSendFn(IsSuper); |
| 857 | } |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 858 | Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy)); |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 859 | return CGF.EmitCall(FnInfo, Fn, ActualArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 862 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 863 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 864 | // FIXME: I don't understand why gcc generates this, or where it is |
| 865 | // resolved. Investigate. Its also wasteful to look this up over and |
| 866 | // over. |
| 867 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 868 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 869 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
| 870 | ObjCTypes.ExternalProtocolPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 873 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 874 | // FIXME: We shouldn't need this, the protocol decl should contain |
| 875 | // enough information to tell us whether this was a declaration or a |
| 876 | // definition. |
| 877 | DefinedProtocols.insert(PD->getIdentifier()); |
| 878 | |
| 879 | // If we have generated a forward reference to this protocol, emit |
| 880 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 881 | // emitted. |
| 882 | if (Protocols.count(PD->getIdentifier())) |
| 883 | GetOrEmitProtocol(PD); |
| 884 | } |
| 885 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 886 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 887 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 888 | return GetOrEmitProtocol(PD); |
| 889 | return GetOrEmitProtocolRef(PD); |
| 890 | } |
| 891 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 892 | /* |
| 893 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 894 | struct _objc_protocol { |
| 895 | struct _objc_protocol_extension *isa; |
| 896 | char *protocol_name; |
| 897 | struct _objc_protocol_list *protocol_list; |
| 898 | struct _objc__method_prototype_list *instance_methods; |
| 899 | struct _objc__method_prototype_list *class_methods |
| 900 | }; |
| 901 | |
| 902 | See EmitProtocolExtension(). |
| 903 | */ |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 904 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
| 905 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 906 | |
| 907 | // Early exit if a defining object has already been generated. |
| 908 | if (Entry && Entry->hasInitializer()) |
| 909 | return Entry; |
| 910 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 911 | // FIXME: I don't understand why gcc generates this, or where it is |
| 912 | // resolved. Investigate. Its also wasteful to look this up over and |
| 913 | // over. |
| 914 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 915 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 916 | const char *ProtocolName = PD->getNameAsCString(); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 917 | |
| 918 | // Construct method lists. |
| 919 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 920 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
| 921 | for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(), |
| 922 | e = PD->instmeth_end(); i != e; ++i) { |
| 923 | ObjCMethodDecl *MD = *i; |
| 924 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 925 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 926 | OptInstanceMethods.push_back(C); |
| 927 | } else { |
| 928 | InstanceMethods.push_back(C); |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(), |
| 933 | e = PD->classmeth_end(); i != e; ++i) { |
| 934 | ObjCMethodDecl *MD = *i; |
| 935 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 936 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 937 | OptClassMethods.push_back(C); |
| 938 | } else { |
| 939 | ClassMethods.push_back(C); |
| 940 | } |
| 941 | } |
| 942 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 943 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 944 | Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 945 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 946 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 947 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 948 | PD->protocol_begin(), |
| 949 | PD->protocol_end()); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 950 | Values[3] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 951 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" |
| 952 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 953 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 954 | InstanceMethods); |
| 955 | Values[4] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 956 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" |
| 957 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 958 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 959 | ClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 960 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 961 | Values); |
| 962 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 963 | if (Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 964 | // Already created, fix the linkage and update the initializer. |
| 965 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 966 | Entry->setInitializer(Init); |
| 967 | } else { |
| 968 | Entry = |
| 969 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
| 970 | llvm::GlobalValue::InternalLinkage, |
| 971 | Init, |
| 972 | std::string("\01L_OBJC_PROTOCOL_")+ProtocolName, |
| 973 | &CGM.getModule()); |
| 974 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
| 975 | UsedGlobals.push_back(Entry); |
| 976 | // FIXME: Is this necessary? Why only for protocol? |
| 977 | Entry->setAlignment(4); |
| 978 | } |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 979 | |
| 980 | return Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 983 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 984 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 985 | |
| 986 | if (!Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 987 | // We use the initializer as a marker of whether this is a forward |
| 988 | // reference or not. At module finalization we add the empty |
| 989 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 990 | Entry = |
| 991 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 992 | llvm::GlobalValue::ExternalLinkage, |
| 993 | 0, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 994 | "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 995 | &CGM.getModule()); |
| 996 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
| 997 | UsedGlobals.push_back(Entry); |
| 998 | // FIXME: Is this necessary? Why only for protocol? |
| 999 | Entry->setAlignment(4); |
| 1000 | } |
| 1001 | |
| 1002 | return Entry; |
| 1003 | } |
| 1004 | |
| 1005 | /* |
| 1006 | struct _objc_protocol_extension { |
| 1007 | uint32_t size; |
| 1008 | struct objc_method_description_list *optional_instance_methods; |
| 1009 | struct objc_method_description_list *optional_class_methods; |
| 1010 | struct objc_property_list *instance_properties; |
| 1011 | }; |
| 1012 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1013 | llvm::Constant * |
| 1014 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1015 | const ConstantVector &OptInstanceMethods, |
| 1016 | const ConstantVector &OptClassMethods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1017 | uint64_t Size = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1018 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1019 | std::vector<llvm::Constant*> Values(4); |
| 1020 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1021 | Values[1] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1022 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
| 1023 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1024 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1025 | OptInstanceMethods); |
| 1026 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1027 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" |
| 1028 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1029 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1030 | OptClassMethods); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1031 | Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + |
| 1032 | PD->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1033 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1034 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1035 | // Return null if no extension bits are used. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1036 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
| 1037 | Values[3]->isNullValue()) |
| 1038 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 1039 | |
| 1040 | llvm::Constant *Init = |
| 1041 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
| 1042 | llvm::GlobalVariable *GV = |
| 1043 | new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false, |
| 1044 | llvm::GlobalValue::InternalLinkage, |
| 1045 | Init, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1046 | "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1047 | &CGM.getModule()); |
| 1048 | // No special section, but goes in llvm.used |
| 1049 | UsedGlobals.push_back(GV); |
| 1050 | |
| 1051 | return GV; |
| 1052 | } |
| 1053 | |
| 1054 | /* |
| 1055 | struct objc_protocol_list { |
| 1056 | struct objc_protocol_list *next; |
| 1057 | long count; |
| 1058 | Protocol *list[]; |
| 1059 | }; |
| 1060 | */ |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1061 | llvm::Constant * |
| 1062 | CGObjCMac::EmitProtocolList(const std::string &Name, |
| 1063 | ObjCProtocolDecl::protocol_iterator begin, |
| 1064 | ObjCProtocolDecl::protocol_iterator end) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1065 | std::vector<llvm::Constant*> ProtocolRefs; |
| 1066 | |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1067 | for (; begin != end; ++begin) |
| 1068 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1069 | |
| 1070 | // Just return null for empty protocol lists |
| 1071 | if (ProtocolRefs.empty()) |
| 1072 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1073 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1074 | // This list is null terminated. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1075 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
| 1076 | |
| 1077 | std::vector<llvm::Constant*> Values(3); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1078 | // This field is only used by the runtime. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1079 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1080 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 1081 | Values[2] = |
| 1082 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 1083 | ProtocolRefs.size()), |
| 1084 | ProtocolRefs); |
| 1085 | |
| 1086 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1087 | llvm::GlobalVariable *GV = |
| 1088 | new llvm::GlobalVariable(Init->getType(), false, |
| 1089 | llvm::GlobalValue::InternalLinkage, |
| 1090 | Init, |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1091 | Name, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1092 | &CGM.getModule()); |
| 1093 | GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip"); |
| 1094 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
| 1095 | } |
| 1096 | |
| 1097 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1098 | struct _objc_property { |
| 1099 | const char * const name; |
| 1100 | const char * const attributes; |
| 1101 | }; |
| 1102 | |
| 1103 | struct _objc_property_list { |
| 1104 | uint32_t entsize; // sizeof (struct _objc_property) |
| 1105 | uint32_t prop_count; |
| 1106 | struct _objc_property[prop_count]; |
| 1107 | }; |
| 1108 | */ |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1109 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name, |
| 1110 | const Decl *Container, |
| 1111 | const ObjCContainerDecl *OCD, |
| 1112 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1113 | std::vector<llvm::Constant*> Properties, Prop(2); |
Steve Naroff | 93983f8 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 1114 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(), |
| 1115 | E = OCD->prop_end(); I != E; ++I) { |
| 1116 | const ObjCPropertyDecl *PD = *I; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1117 | Prop[0] = GetPropertyName(PD->getIdentifier()); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1118 | Prop[1] = GetPropertyTypeString(PD, Container); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1119 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
| 1120 | Prop)); |
| 1121 | } |
| 1122 | |
| 1123 | // Return null for empty list. |
| 1124 | if (Properties.empty()) |
| 1125 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1126 | |
| 1127 | unsigned PropertySize = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1128 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1129 | std::vector<llvm::Constant*> Values(3); |
| 1130 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 1131 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
| 1132 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
| 1133 | Properties.size()); |
| 1134 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
| 1135 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1136 | |
| 1137 | llvm::GlobalVariable *GV = |
| 1138 | new llvm::GlobalVariable(Init->getType(), false, |
| 1139 | llvm::GlobalValue::InternalLinkage, |
| 1140 | Init, |
| 1141 | Name, |
| 1142 | &CGM.getModule()); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1143 | if (ObjCABI == 2) |
| 1144 | GV->setSection("__DATA, __objc_const"); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1145 | // No special section on property lists? |
| 1146 | UsedGlobals.push_back(GV); |
| 1147 | return llvm::ConstantExpr::getBitCast(GV, |
| 1148 | ObjCTypes.PropertyListPtrTy); |
| 1149 | |
| 1150 | } |
| 1151 | |
| 1152 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1153 | struct objc_method_description_list { |
| 1154 | int count; |
| 1155 | struct objc_method_description list[]; |
| 1156 | }; |
| 1157 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1158 | llvm::Constant * |
| 1159 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 1160 | std::vector<llvm::Constant*> Desc(2); |
| 1161 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1162 | ObjCTypes.SelectorPtrTy); |
| 1163 | Desc[1] = GetMethodVarType(MD); |
| 1164 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
| 1165 | Desc); |
| 1166 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1167 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1168 | llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name, |
| 1169 | const char *Section, |
| 1170 | const ConstantVector &Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1171 | // Return null for empty list. |
| 1172 | if (Methods.empty()) |
| 1173 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 1174 | |
| 1175 | std::vector<llvm::Constant*> Values(2); |
| 1176 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 1177 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
| 1178 | Methods.size()); |
| 1179 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
| 1180 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1181 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1182 | llvm::GlobalVariable *GV = |
| 1183 | new llvm::GlobalVariable(Init->getType(), false, |
| 1184 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1185 | Init, Name, &CGM.getModule()); |
| 1186 | GV->setSection(Section); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1187 | UsedGlobals.push_back(GV); |
| 1188 | return llvm::ConstantExpr::getBitCast(GV, |
| 1189 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1192 | /* |
| 1193 | struct _objc_category { |
| 1194 | char *category_name; |
| 1195 | char *class_name; |
| 1196 | struct _objc_method_list *instance_methods; |
| 1197 | struct _objc_method_list *class_methods; |
| 1198 | struct _objc_protocol_list *protocols; |
| 1199 | uint32_t size; // <rdar://4585769> |
| 1200 | struct _objc_property_list *instance_properties; |
| 1201 | }; |
| 1202 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1203 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1204 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1205 | |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1206 | // FIXME: This is poor design, the OCD should have a pointer to the |
| 1207 | // category decl. Additionally, note that Category can be null for |
| 1208 | // the @implementation w/o an @interface case. Sema should just |
| 1209 | // create one for us as it does for @implementation so everyone else |
| 1210 | // can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1211 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1212 | const ObjCCategoryDecl *Category = |
| 1213 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1214 | std::string ExtName(Interface->getNameAsString() + "_" + |
| 1215 | OCD->getNameAsString()); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1216 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1217 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1218 | for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(), |
| 1219 | e = OCD->instmeth_end(); i != e; ++i) { |
| 1220 | // Instance methods should always be defined. |
| 1221 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1222 | } |
| 1223 | for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(), |
| 1224 | e = OCD->classmeth_end(); i != e; ++i) { |
| 1225 | // Class methods should always be defined. |
| 1226 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1227 | } |
| 1228 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1229 | std::vector<llvm::Constant*> Values(7); |
| 1230 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 1231 | Values[1] = GetClassName(Interface->getIdentifier()); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1232 | Values[2] = |
| 1233 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") + |
| 1234 | ExtName, |
| 1235 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1236 | InstanceMethods); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1237 | Values[3] = |
| 1238 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName, |
| 1239 | "__OBJC,__cat_class_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1240 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1241 | if (Category) { |
| 1242 | Values[4] = |
| 1243 | EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName, |
| 1244 | Category->protocol_begin(), |
| 1245 | Category->protocol_end()); |
| 1246 | } else { |
| 1247 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1248 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1249 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1250 | |
| 1251 | // If there is no category @interface then there can be no properties. |
| 1252 | if (Category) { |
| 1253 | Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName, |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1254 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1255 | } else { |
| 1256 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1257 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1258 | |
| 1259 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
| 1260 | Values); |
| 1261 | |
| 1262 | llvm::GlobalVariable *GV = |
| 1263 | new llvm::GlobalVariable(ObjCTypes.CategoryTy, false, |
| 1264 | llvm::GlobalValue::InternalLinkage, |
| 1265 | Init, |
| 1266 | std::string("\01L_OBJC_CATEGORY_")+ExtName, |
| 1267 | &CGM.getModule()); |
| 1268 | GV->setSection("__OBJC,__category,regular,no_dead_strip"); |
| 1269 | UsedGlobals.push_back(GV); |
| 1270 | DefinedCategories.push_back(GV); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1273 | // FIXME: Get from somewhere? |
| 1274 | enum ClassFlags { |
| 1275 | eClassFlags_Factory = 0x00001, |
| 1276 | eClassFlags_Meta = 0x00002, |
| 1277 | // <rdr://5142207> |
| 1278 | eClassFlags_HasCXXStructors = 0x02000, |
| 1279 | eClassFlags_Hidden = 0x20000, |
| 1280 | eClassFlags_ABI2_Hidden = 0x00010, |
| 1281 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 1282 | }; |
| 1283 | |
| 1284 | // <rdr://5142207&4705298&4843145> |
| 1285 | static bool IsClassHidden(const ObjCInterfaceDecl *ID) { |
| 1286 | if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) { |
| 1287 | // FIXME: Support -fvisibility |
| 1288 | switch (attr->getVisibility()) { |
| 1289 | default: |
| 1290 | assert(0 && "Unknown visibility"); |
| 1291 | return false; |
| 1292 | case VisibilityAttr::DefaultVisibility: |
| 1293 | case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here? |
| 1294 | return false; |
| 1295 | case VisibilityAttr::HiddenVisibility: |
| 1296 | return true; |
| 1297 | } |
| 1298 | } else { |
| 1299 | return false; // FIXME: Support -fvisibility |
| 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | /* |
| 1304 | struct _objc_class { |
| 1305 | Class isa; |
| 1306 | Class super_class; |
| 1307 | const char *name; |
| 1308 | long version; |
| 1309 | long info; |
| 1310 | long instance_size; |
| 1311 | struct _objc_ivar_list *ivars; |
| 1312 | struct _objc_method_list *methods; |
| 1313 | struct _objc_cache *cache; |
| 1314 | struct _objc_protocol_list *protocols; |
| 1315 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 1316 | const char *ivar_layout; |
| 1317 | struct _objc_class_ext *ext; |
| 1318 | }; |
| 1319 | |
| 1320 | See EmitClassExtension(); |
| 1321 | */ |
| 1322 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1323 | DefinedSymbols.insert(ID->getIdentifier()); |
| 1324 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1325 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1326 | // FIXME: Gross |
| 1327 | ObjCInterfaceDecl *Interface = |
| 1328 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1329 | llvm::Constant *Protocols = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1330 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1331 | Interface->protocol_begin(), |
| 1332 | Interface->protocol_end()); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1333 | const llvm::Type *InterfaceTy = |
| 1334 | CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface)); |
| 1335 | unsigned Flags = eClassFlags_Factory; |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1336 | unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1337 | |
| 1338 | // FIXME: Set CXX-structors flag. |
| 1339 | if (IsClassHidden(ID->getClassInterface())) |
| 1340 | Flags |= eClassFlags_Hidden; |
| 1341 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1342 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1343 | for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(), |
| 1344 | e = ID->instmeth_end(); i != e; ++i) { |
| 1345 | // Instance methods should always be defined. |
| 1346 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1347 | } |
| 1348 | for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(), |
| 1349 | e = ID->classmeth_end(); i != e; ++i) { |
| 1350 | // Class methods should always be defined. |
| 1351 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1352 | } |
| 1353 | |
| 1354 | for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(), |
| 1355 | e = ID->propimpl_end(); i != e; ++i) { |
| 1356 | ObjCPropertyImplDecl *PID = *i; |
| 1357 | |
| 1358 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 1359 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 1360 | |
| 1361 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 1362 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1363 | InstanceMethods.push_back(C); |
| 1364 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 1365 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1366 | InstanceMethods.push_back(C); |
| 1367 | } |
| 1368 | } |
| 1369 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1370 | std::vector<llvm::Constant*> Values(12); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1371 | Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1372 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1373 | // Record a reference to the super class. |
| 1374 | LazySymbols.insert(Super->getIdentifier()); |
| 1375 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1376 | Values[ 1] = |
| 1377 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1378 | ObjCTypes.ClassPtrTy); |
| 1379 | } else { |
| 1380 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1381 | } |
| 1382 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1383 | // Version is always 0. |
| 1384 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1385 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1386 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1387 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1388 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1389 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1390 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1391 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1392 | // cache is always NULL. |
| 1393 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1394 | Values[ 9] = Protocols; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1395 | // FIXME: Set ivar_layout |
| 1396 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1397 | Values[11] = EmitClassExtension(ID); |
| 1398 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1399 | Values); |
| 1400 | |
| 1401 | llvm::GlobalVariable *GV = |
| 1402 | new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1403 | llvm::GlobalValue::InternalLinkage, |
| 1404 | Init, |
| 1405 | std::string("\01L_OBJC_CLASS_")+ClassName, |
| 1406 | &CGM.getModule()); |
| 1407 | GV->setSection("__OBJC,__class,regular,no_dead_strip"); |
| 1408 | UsedGlobals.push_back(GV); |
| 1409 | // FIXME: Why? |
| 1410 | GV->setAlignment(32); |
| 1411 | DefinedClasses.push_back(GV); |
| 1412 | } |
| 1413 | |
| 1414 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1415 | llvm::Constant *Protocols, |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1416 | const llvm::Type *InterfaceTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1417 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1418 | unsigned Flags = eClassFlags_Meta; |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1419 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1420 | |
| 1421 | if (IsClassHidden(ID->getClassInterface())) |
| 1422 | Flags |= eClassFlags_Hidden; |
| 1423 | |
| 1424 | std::vector<llvm::Constant*> Values(12); |
| 1425 | // The isa for the metaclass is the root of the hierarchy. |
| 1426 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 1427 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 1428 | Root = Super; |
| 1429 | Values[ 0] = |
| 1430 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
| 1431 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1432 | // The super class for the metaclass is emitted as the name of the |
| 1433 | // super class. The runtime fixes this up to point to the |
| 1434 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1435 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
| 1436 | Values[ 1] = |
| 1437 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1438 | ObjCTypes.ClassPtrTy); |
| 1439 | } else { |
| 1440 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1441 | } |
| 1442 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1443 | // Version is always 0. |
| 1444 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1445 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1446 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1447 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1448 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1449 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1450 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1451 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1452 | // cache is always NULL. |
| 1453 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1454 | Values[ 9] = Protocols; |
| 1455 | // ivar_layout for metaclass is always NULL. |
| 1456 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 1457 | // The class extension is always unused for metaclasses. |
| 1458 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 1459 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1460 | Values); |
| 1461 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1462 | std::string Name("\01L_OBJC_METACLASS_"); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1463 | Name += ID->getNameAsCString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1464 | |
| 1465 | // Check for a forward reference. |
| 1466 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 1467 | if (GV) { |
| 1468 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 1469 | "Forward metaclass reference has incorrect type."); |
| 1470 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 1471 | GV->setInitializer(Init); |
| 1472 | } else { |
| 1473 | GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1474 | llvm::GlobalValue::InternalLinkage, |
| 1475 | Init, Name, |
| 1476 | &CGM.getModule()); |
| 1477 | } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1478 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
| 1479 | UsedGlobals.push_back(GV); |
| 1480 | // FIXME: Why? |
| 1481 | GV->setAlignment(32); |
| 1482 | |
| 1483 | return GV; |
| 1484 | } |
| 1485 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1486 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1487 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1488 | |
| 1489 | // FIXME: Should we look these up somewhere other than the |
| 1490 | // module. Its a bit silly since we only generate these while |
| 1491 | // processing an implementation, so exactly one pointer would work |
| 1492 | // if know when we entered/exitted an implementation block. |
| 1493 | |
| 1494 | // Check for an existing forward reference. |
Fariborz Jahanian | b0d2794 | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 1495 | // Previously, metaclass with internal linkage may have been defined. |
| 1496 | // pass 'true' as 2nd argument so it is returned. |
| 1497 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) { |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1498 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 1499 | "Forward metaclass reference has incorrect type."); |
| 1500 | return GV; |
| 1501 | } else { |
| 1502 | // Generate as an external reference to keep a consistent |
| 1503 | // module. This will be patched up when we emit the metaclass. |
| 1504 | return new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1505 | llvm::GlobalValue::ExternalLinkage, |
| 1506 | 0, |
| 1507 | Name, |
| 1508 | &CGM.getModule()); |
| 1509 | } |
| 1510 | } |
| 1511 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1512 | /* |
| 1513 | struct objc_class_ext { |
| 1514 | uint32_t size; |
| 1515 | const char *weak_ivar_layout; |
| 1516 | struct _objc_property_list *properties; |
| 1517 | }; |
| 1518 | */ |
| 1519 | llvm::Constant * |
| 1520 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
| 1521 | uint64_t Size = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1522 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1523 | |
| 1524 | std::vector<llvm::Constant*> Values(3); |
| 1525 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1526 | // FIXME: Output weak_ivar_layout string. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1527 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1528 | Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1529 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1530 | |
| 1531 | // Return null if no extension bits are used. |
| 1532 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
| 1533 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 1534 | |
| 1535 | llvm::Constant *Init = |
| 1536 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
| 1537 | llvm::GlobalVariable *GV = |
| 1538 | new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false, |
| 1539 | llvm::GlobalValue::InternalLinkage, |
| 1540 | Init, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1541 | "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1542 | &CGM.getModule()); |
| 1543 | // No special section, but goes in llvm.used |
| 1544 | UsedGlobals.push_back(GV); |
| 1545 | |
| 1546 | return GV; |
| 1547 | } |
| 1548 | |
Fariborz Jahanian | f54b194 | 2009-01-17 19:36:33 +0000 | [diff] [blame] | 1549 | /// countInheritedIvars - count number of ivars in class and its super class(s) |
| 1550 | /// |
| 1551 | static int countInheritedIvars(const ObjCInterfaceDecl *OI) { |
| 1552 | int count = 0; |
| 1553 | if (!OI) |
| 1554 | return 0; |
| 1555 | const ObjCInterfaceDecl *SuperClass = OI->getSuperClass(); |
| 1556 | if (SuperClass) |
| 1557 | count += countInheritedIvars(SuperClass); |
| 1558 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1559 | E = OI->ivar_end(); I != E; ++I) |
| 1560 | ++count; |
| 1561 | return count; |
| 1562 | } |
| 1563 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1564 | /* |
| 1565 | struct objc_ivar { |
| 1566 | char *ivar_name; |
| 1567 | char *ivar_type; |
| 1568 | int ivar_offset; |
| 1569 | }; |
| 1570 | |
| 1571 | struct objc_ivar_list { |
| 1572 | int ivar_count; |
| 1573 | struct objc_ivar list[count]; |
| 1574 | }; |
| 1575 | */ |
| 1576 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1577 | bool ForClass) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1578 | std::vector<llvm::Constant*> Ivars, Ivar(3); |
| 1579 | |
| 1580 | // When emitting the root class GCC emits ivar entries for the |
| 1581 | // actual class structure. It is not clear if we need to follow this |
| 1582 | // behavior; for now lets try and get away with not doing it. If so, |
| 1583 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 1584 | // for the class. |
| 1585 | if (ForClass) |
| 1586 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1587 | |
| 1588 | ObjCInterfaceDecl *OID = |
| 1589 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
| 1590 | const llvm::Type *InterfaceTy = |
| 1591 | CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1592 | const llvm::StructLayout *Layout = |
| 1593 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy)); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1594 | |
| 1595 | RecordDecl::field_iterator ifield, pfield; |
| 1596 | const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield); |
Fariborz Jahanian | f54b194 | 2009-01-17 19:36:33 +0000 | [diff] [blame] | 1597 | for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) { |
| 1598 | FieldDecl *Field = *ifield; |
| 1599 | unsigned Offset = Layout->getElementOffset(CGM.getTypes(). |
| 1600 | getLLVMFieldNo(Field)); |
| 1601 | if (Field->getIdentifier()) |
| 1602 | Ivar[0] = GetMethodVarName(Field->getIdentifier()); |
| 1603 | else |
| 1604 | Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1605 | std::string TypeStr; |
Fariborz Jahanian | f54b194 | 2009-01-17 19:36:33 +0000 | [diff] [blame] | 1606 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1607 | Ivar[1] = GetMethodVarType(TypeStr); |
| 1608 | Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1609 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | // Return null for empty list. |
| 1613 | if (Ivars.empty()) |
| 1614 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
| 1615 | |
| 1616 | std::vector<llvm::Constant*> Values(2); |
| 1617 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 1618 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
| 1619 | Ivars.size()); |
| 1620 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
| 1621 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1622 | |
| 1623 | const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" : |
| 1624 | "\01L_OBJC_INSTANCE_VARIABLES_"); |
| 1625 | llvm::GlobalVariable *GV = |
| 1626 | new llvm::GlobalVariable(Init->getType(), false, |
| 1627 | llvm::GlobalValue::InternalLinkage, |
| 1628 | Init, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1629 | Prefix + ID->getNameAsString(), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1630 | &CGM.getModule()); |
| 1631 | if (ForClass) { |
| 1632 | GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip"); |
| 1633 | // FIXME: Why is this only here? |
| 1634 | GV->setAlignment(32); |
| 1635 | } else { |
| 1636 | GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip"); |
| 1637 | } |
| 1638 | UsedGlobals.push_back(GV); |
| 1639 | return llvm::ConstantExpr::getBitCast(GV, |
| 1640 | ObjCTypes.IvarListPtrTy); |
| 1641 | } |
| 1642 | |
| 1643 | /* |
| 1644 | struct objc_method { |
| 1645 | SEL method_name; |
| 1646 | char *method_types; |
| 1647 | void *method; |
| 1648 | }; |
| 1649 | |
| 1650 | struct objc_method_list { |
| 1651 | struct objc_method_list *obsolete; |
| 1652 | int count; |
| 1653 | struct objc_method methods_list[count]; |
| 1654 | }; |
| 1655 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1656 | |
| 1657 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 1658 | /// given method if it has been defined. The result is null if the |
| 1659 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1660 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1661 | // FIXME: Use DenseMap::lookup |
| 1662 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 1663 | if (!Fn) |
| 1664 | return 0; |
| 1665 | |
| 1666 | std::vector<llvm::Constant*> Method(3); |
| 1667 | Method[0] = |
| 1668 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1669 | ObjCTypes.SelectorPtrTy); |
| 1670 | Method[1] = GetMethodVarType(MD); |
| 1671 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 1672 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 1673 | } |
| 1674 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1675 | llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name, |
| 1676 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1677 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1678 | // Return null for empty list. |
| 1679 | if (Methods.empty()) |
| 1680 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
| 1681 | |
| 1682 | std::vector<llvm::Constant*> Values(3); |
| 1683 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 1684 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 1685 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 1686 | Methods.size()); |
| 1687 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 1688 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1689 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1690 | llvm::GlobalVariable *GV = |
| 1691 | new llvm::GlobalVariable(Init->getType(), false, |
| 1692 | llvm::GlobalValue::InternalLinkage, |
| 1693 | Init, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1694 | Name, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1695 | &CGM.getModule()); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1696 | GV->setSection(Section); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1697 | UsedGlobals.push_back(GV); |
| 1698 | return llvm::ConstantExpr::getBitCast(GV, |
| 1699 | ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1702 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 1703 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1704 | std::string Name; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 1705 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1706 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1707 | CodeGenTypes &Types = CGM.getTypes(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1708 | const llvm::FunctionType *MethodTy = |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1709 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1710 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1711 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1712 | llvm::GlobalValue::InternalLinkage, |
| 1713 | Name, |
| 1714 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1715 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1716 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1717 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
| 1720 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1721 | // Abuse this interface function as a place to finalize. |
| 1722 | FinishModule(); |
| 1723 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1724 | return NULL; |
| 1725 | } |
| 1726 | |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 1727 | llvm::Function *CGObjCMac::GetPropertyGetFunction() { |
| 1728 | return ObjCTypes.GetPropertyFn; |
| 1729 | } |
| 1730 | |
| 1731 | llvm::Function *CGObjCMac::GetPropertySetFunction() { |
| 1732 | return ObjCTypes.SetPropertyFn; |
| 1733 | } |
| 1734 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1735 | llvm::Function *CGObjCMac::EnumerationMutationFunction() |
| 1736 | { |
| 1737 | return ObjCTypes.EnumerationMutationFn; |
| 1738 | } |
| 1739 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1740 | /* |
| 1741 | |
| 1742 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 1743 | -- |
| 1744 | |
| 1745 | The basic framework for a @try-catch-finally is as follows: |
| 1746 | { |
| 1747 | objc_exception_data d; |
| 1748 | id _rethrow = null; |
| 1749 | |
| 1750 | objc_exception_try_enter(&d); |
| 1751 | if (!setjmp(d.jmp_buf)) { |
| 1752 | ... try body ... |
| 1753 | } else { |
| 1754 | // exception path |
| 1755 | id _caught = objc_exception_extract(&d); |
| 1756 | |
| 1757 | // enter new try scope for handlers |
| 1758 | if (!setjmp(d.jmp_buf)) { |
| 1759 | ... match exception and execute catch blocks ... |
| 1760 | |
| 1761 | // fell off end, rethrow. |
| 1762 | _rethrow = _caught; |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1763 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1764 | } else { |
| 1765 | // exception in catch block |
| 1766 | _rethrow = objc_exception_extract(&d); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1767 | ... jump-through-finally_no_exit to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1768 | } |
| 1769 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1770 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1771 | |
| 1772 | finally: |
| 1773 | // match either the initial try_enter or the catch try_enter, |
| 1774 | // depending on the path followed. |
| 1775 | objc_exception_try_exit(&d); |
| 1776 | finally_no_exit: |
| 1777 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1778 | ... dispatch to finally destination ... |
| 1779 | |
| 1780 | finally_rethrow: |
| 1781 | objc_exception_throw(_rethrow); |
| 1782 | |
| 1783 | finally_end: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | This framework differs slightly from the one gcc uses, in that gcc |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1787 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 1788 | and if the object should be rethrown. This breaks in the face of |
| 1789 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1790 | |
| 1791 | We specialize this framework for a few particular circumstances: |
| 1792 | |
| 1793 | - If there are no catch blocks, then we avoid emitting the second |
| 1794 | exception handling context. |
| 1795 | |
| 1796 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 1797 | e)) we avoid emitting the code to rethrow an uncaught exception. |
| 1798 | |
| 1799 | - FIXME: If there is no @finally block we can do a few more |
| 1800 | simplifications. |
| 1801 | |
| 1802 | Rethrows and Jumps-Through-Finally |
| 1803 | -- |
| 1804 | |
| 1805 | Support for implicit rethrows and jumping through the finally block is |
| 1806 | handled by storing the current exception-handling context in |
| 1807 | ObjCEHStack. |
| 1808 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1809 | In order to implement proper @finally semantics, we support one basic |
| 1810 | mechanism for jumping through the finally block to an arbitrary |
| 1811 | destination. Constructs which generate exits from a @try or @catch |
| 1812 | block use this mechanism to implement the proper semantics by chaining |
| 1813 | jumps, as necessary. |
| 1814 | |
| 1815 | This mechanism works like the one used for indirect goto: we |
| 1816 | arbitrarily assign an ID to each destination and store the ID for the |
| 1817 | destination in a variable prior to entering the finally block. At the |
| 1818 | end of the finally block we simply create a switch to the proper |
| 1819 | destination. |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 1820 | |
| 1821 | Code gen for @synchronized(expr) stmt; |
| 1822 | Effectively generating code for: |
| 1823 | objc_sync_enter(expr); |
| 1824 | @try stmt @finally { objc_sync_exit(expr); } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1825 | */ |
| 1826 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 1827 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1828 | const Stmt &S) { |
| 1829 | bool isTry = isa<ObjCAtTryStmt>(S); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1830 | // Create various blocks we refer to for handling @finally. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 1831 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
| 1832 | llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit"); |
| 1833 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
| 1834 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1835 | llvm::Value *DestCode = |
| 1836 | CGF.CreateTempAlloca(llvm::Type::Int32Ty, "finally.dst"); |
| 1837 | |
| 1838 | // Generate jump code. Done here so we can directly add things to |
| 1839 | // the switch instruction. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 1840 | llvm::BasicBlock *FinallyJump = CGF.createBasicBlock("finally.jump"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1841 | llvm::SwitchInst *FinallySwitch = |
| 1842 | llvm::SwitchInst::Create(new llvm::LoadInst(DestCode, "", FinallyJump), |
| 1843 | FinallyEnd, 10, FinallyJump); |
| 1844 | |
| 1845 | // Push an EH context entry, used for handling rethrows and jumps |
| 1846 | // through finally. |
| 1847 | CodeGenFunction::ObjCEHEntry EHEntry(FinallyBlock, FinallyNoExit, |
| 1848 | FinallySwitch, DestCode); |
| 1849 | CGF.ObjCEHStack.push_back(&EHEntry); |
| 1850 | |
| 1851 | // Allocate memory for the exception data and rethrow pointer. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1852 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 1853 | "exceptiondata.ptr"); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1854 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy, |
| 1855 | "_rethrow"); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 1856 | if (!isTry) { |
| 1857 | // For @synchronized, call objc_sync_enter(sync.expr) |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 1858 | llvm::Value *Arg = CGF.EmitScalarExpr( |
| 1859 | cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 1860 | Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy); |
| 1861 | CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 1862 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1863 | |
| 1864 | // Enter a new try block and call setjmp. |
| 1865 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData); |
| 1866 | llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0, |
| 1867 | "jmpbufarray"); |
| 1868 | JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp"); |
| 1869 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn, |
| 1870 | JmpBufPtr, "result"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1871 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 1872 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 1873 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 1874 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 1875 | TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1876 | |
| 1877 | // Emit the @try block. |
| 1878 | CGF.EmitBlock(TryBlock); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 1879 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 1880 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1881 | CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1882 | |
| 1883 | // Emit the "exception in @try" block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 1884 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 1885 | |
| 1886 | // Retrieve the exception object. We may emit multiple blocks but |
| 1887 | // nothing can cross this so the value is already in SSA form. |
| 1888 | llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn, |
| 1889 | ExceptionData, |
| 1890 | "caught"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1891 | EHEntry.Exception = Caught; |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 1892 | if (!isTry) |
| 1893 | { |
| 1894 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
| 1895 | CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false); |
| 1896 | } |
| 1897 | else if (const ObjCAtCatchStmt* CatchStmt = |
| 1898 | cast<ObjCAtTryStmt>(S).getCatchStmts()) |
| 1899 | { |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 1900 | // Enter a new exception try block (in case a @catch block throws |
| 1901 | // an exception). |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1902 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 1903 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1904 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn, |
| 1905 | JmpBufPtr, "result"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 1906 | llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1907 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 1908 | llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch"); |
| 1909 | llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler"); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 1910 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1911 | |
| 1912 | CGF.EmitBlock(CatchBlock); |
| 1913 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 1914 | // Handle catch list. As a special case we check if everything is |
| 1915 | // matched and avoid generating code for falling off the end if |
| 1916 | // so. |
| 1917 | bool AllMatched = false; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1918 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 1919 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1920 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 1921 | const DeclStmt *CatchParam = |
| 1922 | cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt()); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 1923 | const VarDecl *VD = 0; |
| 1924 | const PointerType *PT = 0; |
| 1925 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1926 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 1927 | if (!CatchParam) { |
| 1928 | AllMatched = true; |
| 1929 | } else { |
Ted Kremenek | de3b8fb | 2008-10-06 20:58:56 +0000 | [diff] [blame] | 1930 | VD = cast<VarDecl>(CatchParam->getSolitaryDecl()); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 1931 | PT = VD->getType()->getAsPointerType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1932 | |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 1933 | // catch(id e) always matches. |
| 1934 | // FIXME: For the time being we also match id<X>; this should |
| 1935 | // be rejected by Sema instead. |
| 1936 | if ((PT && CGF.getContext().isObjCIdType(PT->getPointeeType())) || |
| 1937 | VD->getType()->isObjCQualifiedIdType()) |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 1938 | AllMatched = true; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1939 | } |
| 1940 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 1941 | if (AllMatched) { |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 1942 | if (CatchParam) { |
| 1943 | CGF.EmitStmt(CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 1944 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 1945 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 1946 | } |
Anders Carlsson | 1452f55 | 2008-09-11 08:21:54 +0000 | [diff] [blame] | 1947 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 1948 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1949 | CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1950 | break; |
| 1951 | } |
| 1952 | |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 1953 | assert(PT && "Unexpected non-pointer type in @catch"); |
| 1954 | QualType T = PT->getPointeeType(); |
Anders Carlsson | 4b7ff6e | 2008-09-11 06:35:14 +0000 | [diff] [blame] | 1955 | const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1956 | assert(ObjCType && "Catch parameter must have Objective-C type!"); |
| 1957 | |
| 1958 | // Check if the @catch block matches the exception object. |
| 1959 | llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl()); |
| 1960 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1961 | llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn, |
| 1962 | Class, Caught, "match"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1963 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 1964 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1965 | |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 1966 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 1967 | MatchedBlock, NextCatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1968 | |
| 1969 | // Emit the @catch block. |
| 1970 | CGF.EmitBlock(MatchedBlock); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1971 | CGF.EmitStmt(CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 1972 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1973 | |
| 1974 | llvm::Value *Tmp = |
| 1975 | CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()), |
| 1976 | "tmp"); |
| 1977 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 1978 | |
| 1979 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1980 | CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1981 | |
| 1982 | CGF.EmitBlock(NextCatchBlock); |
| 1983 | } |
| 1984 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 1985 | if (!AllMatched) { |
| 1986 | // None of the handlers caught the exception, so store it to be |
| 1987 | // rethrown at the end of the @finally block. |
| 1988 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1989 | CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | // Emit the exception handler for the @catch blocks. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 1993 | CGF.EmitBlock(CatchHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 1994 | CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn, |
| 1995 | ExceptionData), |
| 1996 | RethrowPtr); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1997 | CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 1998 | } else { |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1999 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2000 | CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2003 | // Pop the exception-handling stack entry. It is important to do |
| 2004 | // this now, because the code in the @finally block is not in this |
| 2005 | // context. |
| 2006 | CGF.ObjCEHStack.pop_back(); |
| 2007 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2008 | // Emit the @finally block. |
| 2009 | CGF.EmitBlock(FinallyBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2010 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2011 | |
| 2012 | CGF.EmitBlock(FinallyNoExit); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2013 | if (isTry) { |
| 2014 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 2015 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 2016 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 2017 | } |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 2018 | else { |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2019 | // For @synchronized objc_sync_exit(expr); As finally's sole statement. |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 2020 | // For @synchronized, call objc_sync_enter(sync.expr) |
| 2021 | llvm::Value *Arg = CGF.EmitScalarExpr( |
| 2022 | cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 2023 | Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy); |
| 2024 | CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg); |
| 2025 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2026 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2027 | CGF.EmitBlock(FinallyJump); |
| 2028 | |
| 2029 | CGF.EmitBlock(FinallyRethrow); |
| 2030 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, |
| 2031 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2032 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2033 | |
| 2034 | CGF.EmitBlock(FinallyEnd); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2035 | } |
| 2036 | |
| 2037 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2038 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2039 | llvm::Value *ExceptionAsObject; |
| 2040 | |
| 2041 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 2042 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
| 2043 | ExceptionAsObject = |
| 2044 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 2045 | } else { |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2046 | assert((!CGF.ObjCEHStack.empty() && CGF.ObjCEHStack.back()->Exception) && |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2047 | "Unexpected rethrow outside @catch block."); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2048 | ExceptionAsObject = CGF.ObjCEHStack.back()->Exception; |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
| 2051 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2052 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2053 | |
| 2054 | // Clear the insertion point to indicate we are in unreachable code. |
| 2055 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2058 | void CodeGenFunction::EmitJumpThroughFinally(ObjCEHEntry *E, |
| 2059 | llvm::BasicBlock *Dst, |
| 2060 | bool ExecuteTryExit) { |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2061 | if (!HaveInsertPoint()) |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2062 | return; |
| 2063 | |
| 2064 | // Find the destination code for this block. We always use 0 for the |
| 2065 | // fallthrough block (default destination). |
| 2066 | llvm::SwitchInst *SI = E->FinallySwitch; |
| 2067 | llvm::ConstantInt *ID; |
| 2068 | if (Dst == SI->getDefaultDest()) { |
| 2069 | ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 2070 | } else { |
| 2071 | ID = SI->findCaseDest(Dst); |
| 2072 | if (!ID) { |
| 2073 | // No code found, get a new unique one by just using the number |
| 2074 | // of switch successors. |
| 2075 | ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, SI->getNumSuccessors()); |
| 2076 | SI->addCase(ID, Dst); |
| 2077 | } |
| 2078 | } |
| 2079 | |
| 2080 | // Set the destination code and branch. |
| 2081 | Builder.CreateStore(ID, E->DestCode); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2082 | EmitBranch(ExecuteTryExit ? E->FinallyBlock : E->FinallyNoExit); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2083 | } |
| 2084 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2085 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2086 | /// object: objc_read_weak (id *src) |
| 2087 | /// |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2088 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2089 | llvm::Value *AddrWeakObj) |
| 2090 | { |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2091 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2092 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn, |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2093 | AddrWeakObj, "weakread"); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2094 | return read_weak; |
| 2095 | } |
| 2096 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2097 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 2098 | /// objc_assign_weak (id src, id *dst) |
| 2099 | /// |
| 2100 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 2101 | llvm::Value *src, llvm::Value *dst) |
| 2102 | { |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2103 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2104 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2105 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn, |
| 2106 | src, dst, "weakassign"); |
| 2107 | return; |
| 2108 | } |
| 2109 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2110 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 2111 | /// objc_assign_global (id src, id *dst) |
| 2112 | /// |
| 2113 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 2114 | llvm::Value *src, llvm::Value *dst) |
| 2115 | { |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2116 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2117 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2118 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn, |
| 2119 | src, dst, "globalassign"); |
| 2120 | return; |
| 2121 | } |
| 2122 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2123 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 2124 | /// objc_assign_ivar (id src, id *dst) |
| 2125 | /// |
| 2126 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 2127 | llvm::Value *src, llvm::Value *dst) |
| 2128 | { |
| 2129 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2130 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 2131 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn, |
| 2132 | src, dst, "assignivar"); |
| 2133 | return; |
| 2134 | } |
| 2135 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2136 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 2137 | /// objc_assign_strongCast (id src, id *dst) |
| 2138 | /// |
| 2139 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 2140 | llvm::Value *src, llvm::Value *dst) |
| 2141 | { |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2142 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2143 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2144 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn, |
| 2145 | src, dst, "weakassign"); |
| 2146 | return; |
| 2147 | } |
| 2148 | |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2149 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 2150 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2151 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 2152 | QualType ObjectTy, |
| 2153 | llvm::Value *BaseValue, |
| 2154 | const ObjCIvarDecl *Ivar, |
| 2155 | const FieldDecl *Field, |
| 2156 | unsigned CVRQualifiers) { |
| 2157 | if (Ivar->isBitField()) |
| 2158 | return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field), |
| 2159 | CVRQualifiers); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2160 | // TODO: Add a special case for isa (index 0) |
| 2161 | unsigned Index = CGM.getTypes().getLLVMFieldNo(Field); |
| 2162 | llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp"); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2163 | LValue LV = LValue::MakeAddr(V, |
| 2164 | Ivar->getType().getCVRQualifiers()|CVRQualifiers); |
| 2165 | LValue::SetObjCIvar(LV, true); |
| 2166 | return LV; |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2169 | /* *** Private Interface *** */ |
| 2170 | |
| 2171 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 2172 | /// level information. |
| 2173 | /// |
| 2174 | /// See: <rdr://4810609&4810587&4810587> |
| 2175 | /// struct IMAGE_INFO { |
| 2176 | /// unsigned version; |
| 2177 | /// unsigned flags; |
| 2178 | /// }; |
| 2179 | enum ImageInfoFlags { |
| 2180 | eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies |
| 2181 | eImageInfo_GarbageCollected = (1 << 1), |
| 2182 | eImageInfo_GCOnly = (1 << 2) |
| 2183 | }; |
| 2184 | |
| 2185 | void CGObjCMac::EmitImageInfo() { |
| 2186 | unsigned version = 0; // Version is unused? |
| 2187 | unsigned flags = 0; |
| 2188 | |
| 2189 | // FIXME: Fix and continue? |
| 2190 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 2191 | flags |= eImageInfo_GarbageCollected; |
| 2192 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 2193 | flags |= eImageInfo_GCOnly; |
| 2194 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2195 | // Emitted as int[2]; |
| 2196 | llvm::Constant *values[2] = { |
| 2197 | llvm::ConstantInt::get(llvm::Type::Int32Ty, version), |
| 2198 | llvm::ConstantInt::get(llvm::Type::Int32Ty, flags) |
| 2199 | }; |
| 2200 | llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2201 | llvm::GlobalVariable *GV = |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2202 | new llvm::GlobalVariable(AT, true, |
| 2203 | llvm::GlobalValue::InternalLinkage, |
| 2204 | llvm::ConstantArray::get(AT, values, 2), |
| 2205 | "\01L_OBJC_IMAGE_INFO", |
| 2206 | &CGM.getModule()); |
| 2207 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 2208 | if (ObjCABI == 1) { |
| 2209 | GV->setSection("__OBJC, __image_info,regular"); |
| 2210 | } else { |
| 2211 | GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); |
| 2212 | } |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2213 | |
| 2214 | UsedGlobals.push_back(GV); |
| 2215 | } |
| 2216 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2217 | |
| 2218 | // struct objc_module { |
| 2219 | // unsigned long version; |
| 2220 | // unsigned long size; |
| 2221 | // const char *name; |
| 2222 | // Symtab symtab; |
| 2223 | // }; |
| 2224 | |
| 2225 | // FIXME: Get from somewhere |
| 2226 | static const int ModuleVersion = 7; |
| 2227 | |
| 2228 | void CGObjCMac::EmitModuleInfo() { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 2229 | uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2230 | |
| 2231 | std::vector<llvm::Constant*> Values(4); |
| 2232 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion); |
| 2233 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2234 | // This used to be the filename, now it is unused. <rdr://4327263> |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2235 | Values[2] = GetClassName(&CGM.getContext().Idents.get("")); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2236 | Values[3] = EmitModuleSymbols(); |
| 2237 | |
| 2238 | llvm::GlobalVariable *GV = |
| 2239 | new llvm::GlobalVariable(ObjCTypes.ModuleTy, false, |
| 2240 | llvm::GlobalValue::InternalLinkage, |
| 2241 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, |
| 2242 | Values), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2243 | "\01L_OBJC_MODULES", |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2244 | &CGM.getModule()); |
| 2245 | GV->setSection("__OBJC,__module_info,regular,no_dead_strip"); |
| 2246 | UsedGlobals.push_back(GV); |
| 2247 | } |
| 2248 | |
| 2249 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2250 | unsigned NumClasses = DefinedClasses.size(); |
| 2251 | unsigned NumCategories = DefinedCategories.size(); |
| 2252 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2253 | // Return null if no symbols were defined. |
| 2254 | if (!NumClasses && !NumCategories) |
| 2255 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
| 2256 | |
| 2257 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2258 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2259 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
| 2260 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 2261 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
| 2262 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2263 | // The runtime expects exactly the list of defined classes followed |
| 2264 | // by the list of defined categories, in a single array. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2265 | std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2266 | for (unsigned i=0; i<NumClasses; i++) |
| 2267 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 2268 | ObjCTypes.Int8PtrTy); |
| 2269 | for (unsigned i=0; i<NumCategories; i++) |
| 2270 | Symbols[NumClasses + i] = |
| 2271 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 2272 | ObjCTypes.Int8PtrTy); |
| 2273 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2274 | Values[4] = |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2275 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2276 | NumClasses + NumCategories), |
| 2277 | Symbols); |
| 2278 | |
| 2279 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2280 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2281 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2282 | new llvm::GlobalVariable(Init->getType(), false, |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2283 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2284 | Init, |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2285 | "\01L_OBJC_SYMBOLS", |
| 2286 | &CGM.getModule()); |
| 2287 | GV->setSection("__OBJC,__symbols,regular,no_dead_strip"); |
| 2288 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2289 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
| 2290 | } |
| 2291 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2292 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2293 | const ObjCInterfaceDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2294 | LazySymbols.insert(ID->getIdentifier()); |
| 2295 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2296 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 2297 | |
| 2298 | if (!Entry) { |
| 2299 | llvm::Constant *Casted = |
| 2300 | llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()), |
| 2301 | ObjCTypes.ClassPtrTy); |
| 2302 | Entry = |
| 2303 | new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false, |
| 2304 | llvm::GlobalValue::InternalLinkage, |
| 2305 | Casted, "\01L_OBJC_CLASS_REFERENCES_", |
| 2306 | &CGM.getModule()); |
| 2307 | Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip"); |
| 2308 | UsedGlobals.push_back(Entry); |
| 2309 | } |
| 2310 | |
| 2311 | return Builder.CreateLoad(Entry, false, "tmp"); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2312 | } |
| 2313 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2314 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2315 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 2316 | |
| 2317 | if (!Entry) { |
| 2318 | llvm::Constant *Casted = |
| 2319 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 2320 | ObjCTypes.SelectorPtrTy); |
| 2321 | Entry = |
| 2322 | new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false, |
| 2323 | llvm::GlobalValue::InternalLinkage, |
| 2324 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_", |
| 2325 | &CGM.getModule()); |
| 2326 | Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip"); |
| 2327 | UsedGlobals.push_back(Entry); |
| 2328 | } |
| 2329 | |
| 2330 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 2331 | } |
| 2332 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 2333 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2334 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2335 | |
| 2336 | if (!Entry) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2337 | llvm::Constant *C = llvm::ConstantArray::get(Ident->getName()); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2338 | Entry = |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2339 | new llvm::GlobalVariable(C->getType(), false, |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2340 | llvm::GlobalValue::InternalLinkage, |
| 2341 | C, "\01L_OBJC_CLASS_NAME_", |
| 2342 | &CGM.getModule()); |
| 2343 | Entry->setSection("__TEXT,__cstring,cstring_literals"); |
| 2344 | UsedGlobals.push_back(Entry); |
| 2345 | } |
| 2346 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2347 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2348 | } |
| 2349 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 2350 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2351 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 2352 | |
| 2353 | if (!Entry) { |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2354 | // FIXME: Avoid std::string copying. |
| 2355 | llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString()); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2356 | Entry = |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2357 | new llvm::GlobalVariable(C->getType(), false, |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2358 | llvm::GlobalValue::InternalLinkage, |
| 2359 | C, "\01L_OBJC_METH_VAR_NAME_", |
| 2360 | &CGM.getModule()); |
| 2361 | Entry->setSection("__TEXT,__cstring,cstring_literals"); |
| 2362 | UsedGlobals.push_back(Entry); |
| 2363 | } |
| 2364 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2365 | return getConstantGEP(Entry, 0, 0); |
| 2366 | } |
| 2367 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2368 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 2369 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2370 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 2371 | } |
| 2372 | |
| 2373 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 2374 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2375 | return GetMethodVarName(&CGM.getContext().Idents.get(Name)); |
| 2376 | } |
| 2377 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 2378 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2379 | llvm::GlobalVariable *&Entry = MethodVarTypes[Name]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2380 | |
| 2381 | if (!Entry) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2382 | llvm::Constant *C = llvm::ConstantArray::get(Name); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2383 | Entry = |
| 2384 | new llvm::GlobalVariable(C->getType(), false, |
| 2385 | llvm::GlobalValue::InternalLinkage, |
| 2386 | C, "\01L_OBJC_METH_VAR_TYPE_", |
| 2387 | &CGM.getModule()); |
| 2388 | Entry->setSection("__TEXT,__cstring,cstring_literals"); |
| 2389 | UsedGlobals.push_back(Entry); |
| 2390 | } |
| 2391 | |
| 2392 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2393 | } |
| 2394 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2395 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 2396 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2397 | std::string TypeStr; |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2398 | CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D), |
| 2399 | TypeStr); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2400 | return GetMethodVarType(TypeStr); |
| 2401 | } |
| 2402 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2403 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 2404 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2405 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
| 2406 | |
| 2407 | if (!Entry) { |
| 2408 | llvm::Constant *C = llvm::ConstantArray::get(Ident->getName()); |
| 2409 | Entry = |
| 2410 | new llvm::GlobalVariable(C->getType(), false, |
| 2411 | llvm::GlobalValue::InternalLinkage, |
| 2412 | C, "\01L_OBJC_PROP_NAME_ATTR_", |
| 2413 | &CGM.getModule()); |
| 2414 | Entry->setSection("__TEXT,__cstring,cstring_literals"); |
| 2415 | UsedGlobals.push_back(Entry); |
| 2416 | } |
| 2417 | |
| 2418 | return getConstantGEP(Entry, 0, 0); |
| 2419 | } |
| 2420 | |
| 2421 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2422 | // FIXME: This Decl should be more precise. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 2423 | llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2424 | const Decl *Container) { |
| 2425 | std::string TypeStr; |
| 2426 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2427 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 2428 | } |
| 2429 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 2430 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
| 2431 | const ObjCContainerDecl *CD, |
| 2432 | std::string &NameOut) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2433 | // FIXME: Find the mangling GCC uses. |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 2434 | NameOut = (D->isInstanceMethod() ? "-" : "+"); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2435 | NameOut += '['; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 2436 | assert (CD && "Missing container decl in GetNameForMethod"); |
| 2437 | NameOut += CD->getNameAsString(); |
Fariborz Jahanian | 5284733 | 2009-01-26 23:49:05 +0000 | [diff] [blame] | 2438 | // FIXME. For a method in a category, (CAT_NAME) is inserted here. |
| 2439 | // Right now! there is not enough info. to do this. |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2440 | NameOut += ' '; |
| 2441 | NameOut += D->getSelector().getAsString(); |
| 2442 | NameOut += ']'; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2443 | } |
| 2444 | |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2445 | /// GetFirstIvarInRecord - This routine returns the record for the |
| 2446 | /// implementation of the fiven class OID. It also returns field |
| 2447 | /// corresponding to the first ivar in the class in FIV. It also |
| 2448 | /// returns the one before the first ivar. |
| 2449 | /// |
| 2450 | const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord( |
| 2451 | const ObjCInterfaceDecl *OID, |
| 2452 | RecordDecl::field_iterator &FIV, |
| 2453 | RecordDecl::field_iterator &PIV) { |
| 2454 | int countSuperClassIvars = countInheritedIvars(OID->getSuperClass()); |
| 2455 | const RecordDecl *RD = CGM.getContext().addRecordToClass(OID); |
| 2456 | RecordDecl::field_iterator ifield = RD->field_begin(); |
| 2457 | RecordDecl::field_iterator pfield = RD->field_end(); |
| 2458 | while (countSuperClassIvars-- > 0) { |
| 2459 | pfield = ifield; |
| 2460 | ++ifield; |
| 2461 | } |
| 2462 | FIV = ifield; |
| 2463 | PIV = pfield; |
| 2464 | return RD; |
| 2465 | } |
| 2466 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2467 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2468 | EmitModuleInfo(); |
| 2469 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2470 | // Emit the dummy bodies for any protocols which were referenced but |
| 2471 | // never defined. |
| 2472 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
| 2473 | i = Protocols.begin(), e = Protocols.end(); i != e; ++i) { |
| 2474 | if (i->second->hasInitializer()) |
| 2475 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2476 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2477 | std::vector<llvm::Constant*> Values(5); |
| 2478 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 2479 | Values[1] = GetClassName(i->first); |
| 2480 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 2481 | Values[3] = Values[4] = |
| 2482 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 2483 | i->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 2484 | i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 2485 | Values)); |
| 2486 | } |
| 2487 | |
| 2488 | std::vector<llvm::Constant*> Used; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2489 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2490 | e = UsedGlobals.end(); i != e; ++i) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2491 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2492 | } |
| 2493 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2494 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2495 | llvm::GlobalValue *GV = |
| 2496 | new llvm::GlobalVariable(AT, false, |
| 2497 | llvm::GlobalValue::AppendingLinkage, |
| 2498 | llvm::ConstantArray::get(AT, Used), |
| 2499 | "llvm.used", |
| 2500 | &CGM.getModule()); |
| 2501 | |
| 2502 | GV->setSection("llvm.metadata"); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2503 | |
| 2504 | // Add assembler directives to add lazy undefined symbol references |
| 2505 | // for classes which are referenced but not defined. This is |
| 2506 | // important for correct linker interaction. |
| 2507 | |
| 2508 | // FIXME: Uh, this isn't particularly portable. |
| 2509 | std::stringstream s; |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 2510 | |
| 2511 | if (!CGM.getModule().getModuleInlineAsm().empty()) |
| 2512 | s << "\n"; |
| 2513 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2514 | for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(), |
| 2515 | e = LazySymbols.end(); i != e; ++i) { |
| 2516 | s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n"; |
| 2517 | } |
| 2518 | for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(), |
| 2519 | e = DefinedSymbols.end(); i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2520 | s << "\t.objc_class_name_" << (*i)->getName() << "=0\n" |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2521 | << "\t.globl .objc_class_name_" << (*i)->getName() << "\n"; |
| 2522 | } |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 2523 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2524 | CGM.getModule().appendModuleInlineAsm(s.str()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2525 | } |
| 2526 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 2527 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 2528 | : CGObjCCommonMac(cgm), |
| 2529 | ObjCTypes(cgm) |
| 2530 | { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 2531 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 2532 | ObjCABI = 2; |
| 2533 | } |
| 2534 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2535 | /* *** */ |
| 2536 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 2537 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
| 2538 | : CGM(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 2539 | { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2540 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 2541 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 2542 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2543 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2544 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2545 | LongTy = Types.ConvertType(Ctx.LongTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2546 | Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 2547 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2548 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 2549 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2550 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2551 | |
| 2552 | // FIXME: It would be nice to unify this with the opaque type, so |
| 2553 | // that the IR comes out a bit cleaner. |
| 2554 | const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 2555 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 2556 | |
| 2557 | // I'm not sure I like this. The implicit coordination is a bit |
| 2558 | // gross. We should solve this in a reasonable fashion because this |
| 2559 | // is a pretty common task (match some runtime data structure with |
| 2560 | // an LLVM data structure). |
| 2561 | |
| 2562 | // FIXME: This is leaked. |
| 2563 | // FIXME: Merge with rewriter code? |
| 2564 | |
| 2565 | // struct _objc_super { |
| 2566 | // id self; |
| 2567 | // Class cls; |
| 2568 | // } |
| 2569 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 2570 | SourceLocation(), |
| 2571 | &Ctx.Idents.get("_objc_super")); |
| 2572 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 2573 | Ctx.getObjCIdType(), 0, false)); |
| 2574 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 2575 | Ctx.getObjCClassType(), 0, false)); |
| 2576 | RD->completeDefinition(Ctx); |
| 2577 | |
| 2578 | SuperCTy = Ctx.getTagDeclType(RD); |
| 2579 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
| 2580 | |
| 2581 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 2582 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 2583 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 2584 | // struct _prop_t { |
| 2585 | // char *name; |
| 2586 | // char *attributes; |
| 2587 | // } |
| 2588 | PropertyTy = llvm::StructType::get(Int8PtrTy, |
| 2589 | Int8PtrTy, |
| 2590 | NULL); |
| 2591 | CGM.getModule().addTypeName("struct._prop_t", |
| 2592 | PropertyTy); |
| 2593 | |
| 2594 | // struct _prop_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 2595 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 2596 | // uint32_t count_of_properties; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 2597 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 2598 | // } |
| 2599 | PropertyListTy = llvm::StructType::get(IntTy, |
| 2600 | IntTy, |
| 2601 | llvm::ArrayType::get(PropertyTy, 0), |
| 2602 | NULL); |
| 2603 | CGM.getModule().addTypeName("struct._prop_list_t", |
| 2604 | PropertyListTy); |
| 2605 | // struct _prop_list_t * |
| 2606 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
| 2607 | |
| 2608 | // struct _objc_method { |
| 2609 | // SEL _cmd; |
| 2610 | // char *method_type; |
| 2611 | // char *_imp; |
| 2612 | // } |
| 2613 | MethodTy = llvm::StructType::get(SelectorPtrTy, |
| 2614 | Int8PtrTy, |
| 2615 | Int8PtrTy, |
| 2616 | NULL); |
| 2617 | CGM.getModule().addTypeName("struct._objc_method", MethodTy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 2618 | |
| 2619 | // struct _objc_cache * |
| 2620 | CacheTy = llvm::OpaqueType::get(); |
| 2621 | CGM.getModule().addTypeName("struct._objc_cache", CacheTy); |
| 2622 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 2623 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 2624 | // Property manipulation functions. |
| 2625 | |
| 2626 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
| 2627 | std::vector<const llvm::Type*> Params; |
| 2628 | Params.push_back(ObjectPtrTy); |
| 2629 | Params.push_back(SelectorPtrTy); |
| 2630 | Params.push_back(LongTy); |
| 2631 | Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy)); |
| 2632 | GetPropertyFn = |
| 2633 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 2634 | Params, |
| 2635 | false), |
| 2636 | "objc_getProperty"); |
| 2637 | |
| 2638 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
| 2639 | Params.clear(); |
| 2640 | Params.push_back(ObjectPtrTy); |
| 2641 | Params.push_back(SelectorPtrTy); |
| 2642 | Params.push_back(LongTy); |
| 2643 | Params.push_back(ObjectPtrTy); |
| 2644 | Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy)); |
| 2645 | Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy)); |
| 2646 | SetPropertyFn = |
| 2647 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 2648 | Params, |
| 2649 | false), |
| 2650 | "objc_setProperty"); |
| 2651 | // Enumeration mutation. |
| 2652 | |
| 2653 | Params.clear(); |
| 2654 | Params.push_back(ObjectPtrTy); |
| 2655 | EnumerationMutationFn = |
| 2656 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 2657 | Params, |
| 2658 | false), |
| 2659 | "objc_enumerationMutation"); |
| 2660 | |
| 2661 | // gc's API |
| 2662 | // id objc_read_weak (id *) |
| 2663 | Params.clear(); |
| 2664 | Params.push_back(PtrObjectPtrTy); |
| 2665 | GcReadWeakFn = |
| 2666 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 2667 | Params, |
| 2668 | false), |
| 2669 | "objc_read_weak"); |
| 2670 | // id objc_assign_weak (id, id *) |
| 2671 | Params.clear(); |
| 2672 | Params.push_back(ObjectPtrTy); |
| 2673 | Params.push_back(PtrObjectPtrTy); |
| 2674 | GcAssignWeakFn = |
| 2675 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 2676 | Params, |
| 2677 | false), |
| 2678 | "objc_assign_weak"); |
| 2679 | GcAssignGlobalFn = |
| 2680 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 2681 | Params, |
| 2682 | false), |
| 2683 | "objc_assign_global"); |
| 2684 | GcAssignIvarFn = |
| 2685 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 2686 | Params, |
| 2687 | false), |
| 2688 | "objc_assign_ivar"); |
| 2689 | GcAssignStrongCastFn = |
| 2690 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 2691 | Params, |
| 2692 | false), |
| 2693 | "objc_assign_strongCast"); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 2694 | } |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2695 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 2696 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
| 2697 | : ObjCCommonTypesHelper(cgm) |
| 2698 | { |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2699 | // struct _objc_method_description { |
| 2700 | // SEL name; |
| 2701 | // char *types; |
| 2702 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2703 | MethodDescriptionTy = |
| 2704 | llvm::StructType::get(SelectorPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2705 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2706 | NULL); |
| 2707 | CGM.getModule().addTypeName("struct._objc_method_description", |
| 2708 | MethodDescriptionTy); |
| 2709 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2710 | // struct _objc_method_description_list { |
| 2711 | // int count; |
| 2712 | // struct _objc_method_description[1]; |
| 2713 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2714 | MethodDescriptionListTy = |
| 2715 | llvm::StructType::get(IntTy, |
| 2716 | llvm::ArrayType::get(MethodDescriptionTy, 0), |
| 2717 | NULL); |
| 2718 | CGM.getModule().addTypeName("struct._objc_method_description_list", |
| 2719 | MethodDescriptionListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2720 | |
| 2721 | // struct _objc_method_description_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2722 | MethodDescriptionListPtrTy = |
| 2723 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
| 2724 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2725 | // Protocol description structures |
| 2726 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2727 | // struct _objc_protocol_extension { |
| 2728 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 2729 | // struct _objc_method_description_list *optional_instance_methods; |
| 2730 | // struct _objc_method_description_list *optional_class_methods; |
| 2731 | // struct _objc_property_list *instance_properties; |
| 2732 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2733 | ProtocolExtensionTy = |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2734 | llvm::StructType::get(IntTy, |
| 2735 | MethodDescriptionListPtrTy, |
| 2736 | MethodDescriptionListPtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2737 | PropertyListPtrTy, |
| 2738 | NULL); |
| 2739 | CGM.getModule().addTypeName("struct._objc_protocol_extension", |
| 2740 | ProtocolExtensionTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2741 | |
| 2742 | // struct _objc_protocol_extension * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2743 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
| 2744 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2745 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2746 | |
| 2747 | llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(); |
| 2748 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 2749 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 2750 | const llvm::Type *T = |
| 2751 | llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 2752 | LongTy, |
| 2753 | llvm::ArrayType::get(ProtocolTyHolder, 0), |
| 2754 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2755 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T); |
| 2756 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2757 | // struct _objc_protocol { |
| 2758 | // struct _objc_protocol_extension *isa; |
| 2759 | // char *protocol_name; |
| 2760 | // struct _objc_protocol **_objc_protocol_list; |
| 2761 | // struct _objc_method_description_list *instance_methods; |
| 2762 | // struct _objc_method_description_list *class_methods; |
| 2763 | // } |
| 2764 | T = llvm::StructType::get(ProtocolExtensionPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2765 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2766 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 2767 | MethodDescriptionListPtrTy, |
| 2768 | MethodDescriptionListPtrTy, |
| 2769 | NULL); |
| 2770 | cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T); |
| 2771 | |
| 2772 | ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get()); |
| 2773 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 2774 | ProtocolListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2775 | // struct _objc_protocol_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2776 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
| 2777 | |
| 2778 | ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get()); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2779 | CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2780 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2781 | |
| 2782 | // Class description structures |
| 2783 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2784 | // struct _objc_ivar { |
| 2785 | // char *ivar_name; |
| 2786 | // char *ivar_type; |
| 2787 | // int ivar_offset; |
| 2788 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2789 | IvarTy = llvm::StructType::get(Int8PtrTy, |
| 2790 | Int8PtrTy, |
| 2791 | IntTy, |
| 2792 | NULL); |
| 2793 | CGM.getModule().addTypeName("struct._objc_ivar", IvarTy); |
| 2794 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2795 | // struct _objc_ivar_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2796 | IvarListTy = llvm::OpaqueType::get(); |
| 2797 | CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy); |
| 2798 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
| 2799 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2800 | // struct _objc_method_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2801 | MethodListTy = llvm::OpaqueType::get(); |
| 2802 | CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy); |
| 2803 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
| 2804 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2805 | // struct _objc_class_extension * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2806 | ClassExtensionTy = |
| 2807 | llvm::StructType::get(IntTy, |
| 2808 | Int8PtrTy, |
| 2809 | PropertyListPtrTy, |
| 2810 | NULL); |
| 2811 | CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy); |
| 2812 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
| 2813 | |
| 2814 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 2815 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2816 | // struct _objc_class { |
| 2817 | // Class isa; |
| 2818 | // Class super_class; |
| 2819 | // char *name; |
| 2820 | // long version; |
| 2821 | // long info; |
| 2822 | // long instance_size; |
| 2823 | // struct _objc_ivar_list *ivars; |
| 2824 | // struct _objc_method_list *methods; |
| 2825 | // struct _objc_cache *cache; |
| 2826 | // struct _objc_protocol_list *protocols; |
| 2827 | // char *ivar_layout; |
| 2828 | // struct _objc_class_ext *ext; |
| 2829 | // }; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2830 | T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 2831 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 2832 | Int8PtrTy, |
| 2833 | LongTy, |
| 2834 | LongTy, |
| 2835 | LongTy, |
| 2836 | IvarListPtrTy, |
| 2837 | MethodListPtrTy, |
| 2838 | CachePtrTy, |
| 2839 | ProtocolListPtrTy, |
| 2840 | Int8PtrTy, |
| 2841 | ClassExtensionPtrTy, |
| 2842 | NULL); |
| 2843 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T); |
| 2844 | |
| 2845 | ClassTy = cast<llvm::StructType>(ClassTyHolder.get()); |
| 2846 | CGM.getModule().addTypeName("struct._objc_class", ClassTy); |
| 2847 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
| 2848 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2849 | // struct _objc_category { |
| 2850 | // char *category_name; |
| 2851 | // char *class_name; |
| 2852 | // struct _objc_method_list *instance_method; |
| 2853 | // struct _objc_method_list *class_method; |
| 2854 | // uint32_t size; // sizeof(struct _objc_category) |
| 2855 | // struct _objc_property_list *instance_properties;// category's @property |
| 2856 | // } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2857 | CategoryTy = llvm::StructType::get(Int8PtrTy, |
| 2858 | Int8PtrTy, |
| 2859 | MethodListPtrTy, |
| 2860 | MethodListPtrTy, |
| 2861 | ProtocolListPtrTy, |
| 2862 | IntTy, |
| 2863 | PropertyListPtrTy, |
| 2864 | NULL); |
| 2865 | CGM.getModule().addTypeName("struct._objc_category", CategoryTy); |
| 2866 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2867 | // Global metadata structures |
| 2868 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2869 | // struct _objc_symtab { |
| 2870 | // long sel_ref_cnt; |
| 2871 | // SEL *refs; |
| 2872 | // short cls_def_cnt; |
| 2873 | // short cat_def_cnt; |
| 2874 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 2875 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2876 | SymtabTy = llvm::StructType::get(LongTy, |
| 2877 | SelectorPtrTy, |
| 2878 | ShortTy, |
| 2879 | ShortTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2880 | llvm::ArrayType::get(Int8PtrTy, 0), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2881 | NULL); |
| 2882 | CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy); |
| 2883 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
| 2884 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 2885 | // struct _objc_module { |
| 2886 | // long version; |
| 2887 | // long size; // sizeof(struct _objc_module) |
| 2888 | // char *name; |
| 2889 | // struct _objc_symtab* symtab; |
| 2890 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2891 | ModuleTy = |
| 2892 | llvm::StructType::get(LongTy, |
| 2893 | LongTy, |
| 2894 | Int8PtrTy, |
| 2895 | SymtabPtrTy, |
| 2896 | NULL); |
| 2897 | CGM.getModule().addTypeName("struct._objc_module", ModuleTy); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 2898 | |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2899 | // Message send functions. |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 2900 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 2901 | // id objc_msgSend (id, SEL, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 2902 | std::vector<const llvm::Type*> Params; |
| 2903 | Params.push_back(ObjectPtrTy); |
| 2904 | Params.push_back(SelectorPtrTy); |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 2905 | MessageSendFn = |
| 2906 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 2907 | Params, |
| 2908 | true), |
| 2909 | "objc_msgSend"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 2910 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 2911 | // id objc_msgSend_stret (id, SEL, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 2912 | Params.clear(); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 2913 | Params.push_back(ObjectPtrTy); |
| 2914 | Params.push_back(SelectorPtrTy); |
| 2915 | MessageSendStretFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 2916 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 2917 | Params, |
| 2918 | true), |
| 2919 | "objc_msgSend_stret"); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 2920 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 2921 | // |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 2922 | Params.clear(); |
| 2923 | Params.push_back(ObjectPtrTy); |
| 2924 | Params.push_back(SelectorPtrTy); |
| 2925 | // FIXME: This should be long double on x86_64? |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 2926 | // [double | long double] objc_msgSend_fpret(id self, SEL op, ...) |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 2927 | MessageSendFpretFn = |
| 2928 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy, |
| 2929 | Params, |
| 2930 | true), |
| 2931 | "objc_msgSend_fpret"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 2932 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 2933 | // id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 2934 | Params.clear(); |
| 2935 | Params.push_back(SuperPtrTy); |
| 2936 | Params.push_back(SelectorPtrTy); |
| 2937 | MessageSendSuperFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 2938 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 2939 | Params, |
| 2940 | true), |
| 2941 | "objc_msgSendSuper"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 2942 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 2943 | // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super, |
| 2944 | // SEL op, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 2945 | Params.clear(); |
| 2946 | Params.push_back(Int8PtrTy); |
| 2947 | Params.push_back(SuperPtrTy); |
| 2948 | Params.push_back(SelectorPtrTy); |
| 2949 | MessageSendSuperStretFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 2950 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 2951 | Params, |
| 2952 | true), |
| 2953 | "objc_msgSendSuper_stret"); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 2954 | |
| 2955 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 2956 | MessageSendSuperFpretFn = MessageSendSuperFn; |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2957 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 2958 | // FIXME: This is the size of the setjmp buffer and should be |
| 2959 | // target specific. 18 is what's used on 32-bit X86. |
| 2960 | uint64_t SetJmpBufferSize = 18; |
| 2961 | |
| 2962 | // Exceptions |
| 2963 | const llvm::Type *StackPtrTy = |
Daniel Dunbar | 1000491 | 2008-09-27 06:32:25 +0000 | [diff] [blame] | 2964 | llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 2965 | |
| 2966 | ExceptionDataTy = |
| 2967 | llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty, |
| 2968 | SetJmpBufferSize), |
| 2969 | StackPtrTy, NULL); |
| 2970 | CGM.getModule().addTypeName("struct._objc_exception_data", |
| 2971 | ExceptionDataTy); |
| 2972 | |
| 2973 | Params.clear(); |
| 2974 | Params.push_back(ObjectPtrTy); |
| 2975 | ExceptionThrowFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 2976 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 2977 | Params, |
| 2978 | false), |
| 2979 | "objc_exception_throw"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 2980 | |
| 2981 | Params.clear(); |
| 2982 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 2983 | ExceptionTryEnterFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 2984 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 2985 | Params, |
| 2986 | false), |
| 2987 | "objc_exception_try_enter"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 2988 | ExceptionTryExitFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 2989 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 2990 | Params, |
| 2991 | false), |
| 2992 | "objc_exception_try_exit"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 2993 | ExceptionExtractFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 2994 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 2995 | Params, |
| 2996 | false), |
| 2997 | "objc_exception_extract"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 2998 | |
| 2999 | Params.clear(); |
| 3000 | Params.push_back(ClassPtrTy); |
| 3001 | Params.push_back(ObjectPtrTy); |
| 3002 | ExceptionMatchFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3003 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 3004 | Params, |
| 3005 | false), |
| 3006 | "objc_exception_match"); |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 3007 | |
| 3008 | // synchronized APIs |
| 3009 | // void objc_sync_enter (id) |
| 3010 | Params.clear(); |
| 3011 | Params.push_back(ObjectPtrTy); |
| 3012 | SyncEnterFn = |
| 3013 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3014 | Params, |
| 3015 | false), |
| 3016 | "objc_sync_enter"); |
| 3017 | // void objc_sync_exit (id) |
| 3018 | SyncExitFn = |
| 3019 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3020 | Params, |
| 3021 | false), |
| 3022 | "objc_sync_exit"); |
| 3023 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3024 | |
| 3025 | Params.clear(); |
| 3026 | Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty)); |
| 3027 | SetJmpFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3028 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 3029 | Params, |
| 3030 | false), |
| 3031 | "_setjmp"); |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 3032 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3033 | } |
| 3034 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3035 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3036 | : ObjCCommonTypesHelper(cgm) |
| 3037 | { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3038 | // struct _method_list_t { |
| 3039 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 3040 | // uint32_t method_count; |
| 3041 | // struct _objc_method method_list[method_count]; |
| 3042 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3043 | MethodListnfABITy = llvm::StructType::get(IntTy, |
| 3044 | IntTy, |
| 3045 | llvm::ArrayType::get(MethodTy, 0), |
| 3046 | NULL); |
| 3047 | CGM.getModule().addTypeName("struct.__method_list_t", |
| 3048 | MethodListnfABITy); |
| 3049 | // struct method_list_t * |
| 3050 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3051 | |
| 3052 | // struct _protocol_t { |
| 3053 | // id isa; // NULL |
| 3054 | // const char * const protocol_name; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3055 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3056 | // const struct method_list_t * const instance_methods; |
| 3057 | // const struct method_list_t * const class_methods; |
| 3058 | // const struct method_list_t *optionalInstanceMethods; |
| 3059 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3060 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3061 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 3062 | // const uint32_t flags; // = 0 |
| 3063 | // } |
| 3064 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3065 | // Holder for struct _protocol_list_t * |
| 3066 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3067 | |
| 3068 | ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy, |
| 3069 | Int8PtrTy, |
| 3070 | llvm::PointerType::getUnqual( |
| 3071 | ProtocolListTyHolder), |
| 3072 | MethodListnfABIPtrTy, |
| 3073 | MethodListnfABIPtrTy, |
| 3074 | MethodListnfABIPtrTy, |
| 3075 | MethodListnfABIPtrTy, |
| 3076 | PropertyListPtrTy, |
| 3077 | IntTy, |
| 3078 | IntTy, |
| 3079 | NULL); |
| 3080 | CGM.getModule().addTypeName("struct._protocol_t", |
| 3081 | ProtocolnfABITy); |
| 3082 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3083 | // struct _protocol_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3084 | // long protocol_count; // Note, this is 32/64 bit |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3085 | // struct _protocol_t[protocol_count]; |
| 3086 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3087 | ProtocolListnfABITy = llvm::StructType::get(LongTy, |
| 3088 | llvm::ArrayType::get( |
| 3089 | ProtocolnfABITy, 0), |
| 3090 | NULL); |
| 3091 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3092 | ProtocolListnfABITy); |
| 3093 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3094 | // struct _objc_protocol_list* |
| 3095 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3096 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3097 | // FIXME! Is this doing the right thing? |
| 3098 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo( |
| 3099 | ProtocolListnfABIPtrTy); |
| 3100 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3101 | // struct _ivar_t { |
| 3102 | // unsigned long int *offset; // pointer to ivar offset location |
| 3103 | // char *name; |
| 3104 | // char *type; |
| 3105 | // uint32_t alignment; |
| 3106 | // uint32_t size; |
| 3107 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3108 | IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy), |
| 3109 | Int8PtrTy, |
| 3110 | Int8PtrTy, |
| 3111 | IntTy, |
| 3112 | IntTy, |
| 3113 | NULL); |
| 3114 | CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy); |
| 3115 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3116 | // struct _ivar_list_t { |
| 3117 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 3118 | // uint32 count; |
| 3119 | // struct _iver_t list[count]; |
| 3120 | // } |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3121 | IvarListnfABITy = llvm::StructType::get(IntTy, |
| 3122 | IntTy, |
| 3123 | llvm::ArrayType::get( |
| 3124 | IvarnfABITy, 0), |
| 3125 | NULL); |
| 3126 | CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy); |
| 3127 | |
| 3128 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3129 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3130 | // struct _class_ro_t { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3131 | // uint32_t const flags; |
| 3132 | // uint32_t const instanceStart; |
| 3133 | // uint32_t const instanceSize; |
| 3134 | // uint32_t const reserved; // only when building for 64bit targets |
| 3135 | // const uint8_t * const ivarLayout; |
| 3136 | // const char *const name; |
| 3137 | // const struct _method_list_t * const baseMethods; |
| 3138 | // const struct _objc_protocol_list *const baseProtocols; |
| 3139 | // const struct _ivar_list_t *const ivars; |
| 3140 | // const uint8_t * const weakIvarLayout; |
| 3141 | // const struct _prop_list_t * const properties; |
| 3142 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3143 | |
| 3144 | // FIXME. Add 'reserved' field in 64bit abi mode! |
| 3145 | ClassRonfABITy = llvm::StructType::get(IntTy, |
| 3146 | IntTy, |
| 3147 | IntTy, |
| 3148 | Int8PtrTy, |
| 3149 | Int8PtrTy, |
| 3150 | MethodListnfABIPtrTy, |
| 3151 | ProtocolListnfABIPtrTy, |
| 3152 | IvarListnfABIPtrTy, |
| 3153 | Int8PtrTy, |
| 3154 | PropertyListPtrTy, |
| 3155 | NULL); |
| 3156 | CGM.getModule().addTypeName("struct._class_ro_t", |
| 3157 | ClassRonfABITy); |
| 3158 | |
| 3159 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 3160 | std::vector<const llvm::Type*> Params; |
| 3161 | Params.push_back(ObjectPtrTy); |
| 3162 | Params.push_back(SelectorPtrTy); |
| 3163 | ImpnfABITy = llvm::PointerType::getUnqual( |
| 3164 | llvm::FunctionType::get(ObjectPtrTy, Params, false)); |
| 3165 | |
| 3166 | // struct _class_t { |
| 3167 | // struct _class_t *isa; |
| 3168 | // struct _class_t * const superclass; |
| 3169 | // void *cache; |
| 3170 | // IMP *vtable; |
| 3171 | // struct class_ro_t *ro; |
| 3172 | // } |
| 3173 | |
| 3174 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3175 | ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3176 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3177 | CachePtrTy, |
| 3178 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 3179 | llvm::PointerType::getUnqual( |
| 3180 | ClassRonfABITy), |
| 3181 | NULL); |
| 3182 | CGM.getModule().addTypeName("struct._class_t", ClassnfABITy); |
| 3183 | |
| 3184 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo( |
| 3185 | ClassnfABITy); |
| 3186 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3187 | // LLVM for struct _class_t * |
| 3188 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
| 3189 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3190 | // struct _category_t { |
| 3191 | // const char * const name; |
| 3192 | // struct _class_t *const cls; |
| 3193 | // const struct _method_list_t * const instance_methods; |
| 3194 | // const struct _method_list_t * const class_methods; |
| 3195 | // const struct _protocol_list_t * const protocols; |
| 3196 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3197 | // } |
| 3198 | CategorynfABITy = llvm::StructType::get(Int8PtrTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3199 | ClassnfABIPtrTy, |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3200 | MethodListnfABIPtrTy, |
| 3201 | MethodListnfABIPtrTy, |
| 3202 | ProtocolListnfABIPtrTy, |
| 3203 | PropertyListPtrTy, |
| 3204 | NULL); |
| 3205 | CGM.getModule().addTypeName("struct._category_t", CategorynfABITy); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3206 | |
| 3207 | // New types for nonfragile abi messaging. |
| 3208 | |
| 3209 | // MessageRefTy - LLVM for: |
| 3210 | // struct _message_ref_t { |
| 3211 | // IMP messenger; |
| 3212 | // SEL name; |
| 3213 | // }; |
| 3214 | MessageRefTy = llvm::StructType::get(ImpnfABITy, |
| 3215 | SelectorPtrTy, |
| 3216 | NULL); |
| 3217 | CGM.getModule().addTypeName("struct._message_ref_t", MessageRefTy); |
| 3218 | |
| 3219 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 3220 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
| 3221 | |
| 3222 | // SuperMessageRefTy - LLVM for: |
| 3223 | // struct _super_message_ref_t { |
| 3224 | // SUPER_IMP messenger; |
| 3225 | // SEL name; |
| 3226 | // }; |
| 3227 | SuperMessageRefTy = llvm::StructType::get(ImpnfABITy, |
| 3228 | SelectorPtrTy, |
| 3229 | NULL); |
| 3230 | CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy); |
| 3231 | |
| 3232 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 3233 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
| 3234 | |
| 3235 | // id objc_msgSend_fixup (id, struct message_ref_t*, ...) |
| 3236 | Params.clear(); |
| 3237 | Params.push_back(ObjectPtrTy); |
| 3238 | Params.push_back(MessageRefPtrTy); |
| 3239 | MessageSendFixupFn = |
| 3240 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3241 | Params, |
| 3242 | true), |
| 3243 | "objc_msgSend_fixup"); |
| 3244 | |
| 3245 | // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...) |
| 3246 | MessageSendFpretFixupFn = |
| 3247 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3248 | Params, |
| 3249 | true), |
| 3250 | "objc_msgSend_fpret_fixup"); |
| 3251 | |
| 3252 | // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...) |
| 3253 | MessageSendStretFixupFn = |
| 3254 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3255 | Params, |
| 3256 | true), |
| 3257 | "objc_msgSend_stret_fixup"); |
| 3258 | |
| 3259 | // id objc_msgSendId_fixup (id, struct message_ref_t*, ...) |
| 3260 | MessageSendIdFixupFn = |
| 3261 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3262 | Params, |
| 3263 | true), |
| 3264 | "objc_msgSendId_fixup"); |
| 3265 | |
| 3266 | |
| 3267 | // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...) |
| 3268 | MessageSendIdStretFixupFn = |
| 3269 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3270 | Params, |
| 3271 | true), |
| 3272 | "MessageSendIdStretFixupFn"); |
| 3273 | |
| 3274 | // id objc_msgSendSuper2_fixup (struct objc_super *, |
| 3275 | // struct _super_message_ref_t*, ...) |
| 3276 | Params.clear(); |
| 3277 | Params.push_back(SuperPtrTy); |
| 3278 | Params.push_back(SuperMessageRefPtrTy); |
| 3279 | MessageSendSuper2FixupFn = |
| 3280 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3281 | Params, |
| 3282 | true), |
| 3283 | "objc_msgSendSuper2_fixup"); |
| 3284 | |
| 3285 | |
| 3286 | // id objc_msgSendSuper2_stret_fixup (struct objc_super *, |
| 3287 | // struct _super_message_ref_t*, ...) |
| 3288 | MessageSendSuper2StretFixupFn = |
| 3289 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3290 | Params, |
| 3291 | true), |
| 3292 | "objc_msgSendSuper2_stret_fixup"); |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3293 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3294 | } |
| 3295 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3296 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
| 3297 | FinishNonFragileABIModule(); |
| 3298 | |
| 3299 | return NULL; |
| 3300 | } |
| 3301 | |
| 3302 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 3303 | // nonfragile abi has no module definition. |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 3304 | |
| 3305 | // Build list of all implemented classe addresses in array |
| 3306 | // L_OBJC_LABEL_CLASS_$. |
| 3307 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$ |
| 3308 | // list of 'nonlazy' implementations (defined as those with a +load{} |
| 3309 | // method!!). |
| 3310 | unsigned NumClasses = DefinedClasses.size(); |
| 3311 | if (NumClasses) { |
| 3312 | std::vector<llvm::Constant*> Symbols(NumClasses); |
| 3313 | for (unsigned i=0; i<NumClasses; i++) |
| 3314 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 3315 | ObjCTypes.Int8PtrTy); |
| 3316 | llvm::Constant* Init = |
| 3317 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 3318 | NumClasses), |
| 3319 | Symbols); |
| 3320 | |
| 3321 | llvm::GlobalVariable *GV = |
| 3322 | new llvm::GlobalVariable(Init->getType(), false, |
| 3323 | llvm::GlobalValue::InternalLinkage, |
| 3324 | Init, |
| 3325 | "\01L_OBJC_LABEL_CLASS_$", |
| 3326 | &CGM.getModule()); |
| 3327 | GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip"); |
| 3328 | UsedGlobals.push_back(GV); |
| 3329 | } |
| 3330 | |
| 3331 | // Build list of all implemented category addresses in array |
| 3332 | // L_OBJC_LABEL_CATEGORY_$. |
| 3333 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$ |
| 3334 | // list of 'nonlazy' category implementations (defined as those with a +load{} |
| 3335 | // method!!). |
| 3336 | unsigned NumCategory = DefinedCategories.size(); |
| 3337 | if (NumCategory) { |
| 3338 | std::vector<llvm::Constant*> Symbols(NumCategory); |
| 3339 | for (unsigned i=0; i<NumCategory; i++) |
| 3340 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 3341 | ObjCTypes.Int8PtrTy); |
| 3342 | llvm::Constant* Init = |
| 3343 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 3344 | NumCategory), |
| 3345 | Symbols); |
| 3346 | |
| 3347 | llvm::GlobalVariable *GV = |
| 3348 | new llvm::GlobalVariable(Init->getType(), false, |
| 3349 | llvm::GlobalValue::InternalLinkage, |
| 3350 | Init, |
| 3351 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 3352 | &CGM.getModule()); |
| 3353 | GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip"); |
| 3354 | UsedGlobals.push_back(GV); |
| 3355 | } |
| 3356 | |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 3357 | // static int L_OBJC_IMAGE_INFO[2] = { 0, flags }; |
| 3358 | // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0 |
| 3359 | std::vector<llvm::Constant*> Values(2); |
| 3360 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0); |
| 3361 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0); |
| 3362 | llvm::Constant* Init = llvm::ConstantArray::get( |
| 3363 | llvm::ArrayType::get(ObjCTypes.IntTy, 2), |
| 3364 | Values); |
| 3365 | llvm::GlobalVariable *IMGV = |
| 3366 | new llvm::GlobalVariable(Init->getType(), false, |
| 3367 | llvm::GlobalValue::InternalLinkage, |
| 3368 | Init, |
| 3369 | "\01L_OBJC_IMAGE_INFO", |
| 3370 | &CGM.getModule()); |
| 3371 | IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); |
| 3372 | UsedGlobals.push_back(IMGV); |
| 3373 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3374 | std::vector<llvm::Constant*> Used; |
| 3375 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
| 3376 | e = UsedGlobals.end(); i != e; ++i) { |
| 3377 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
| 3378 | } |
| 3379 | |
| 3380 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
| 3381 | llvm::GlobalValue *GV = |
| 3382 | new llvm::GlobalVariable(AT, false, |
| 3383 | llvm::GlobalValue::AppendingLinkage, |
| 3384 | llvm::ConstantArray::get(AT, Used), |
| 3385 | "llvm.used", |
| 3386 | &CGM.getModule()); |
| 3387 | |
| 3388 | GV->setSection("llvm.metadata"); |
| 3389 | |
| 3390 | } |
| 3391 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3392 | // Metadata flags |
| 3393 | enum MetaDataDlags { |
| 3394 | CLS = 0x0, |
| 3395 | CLS_META = 0x1, |
| 3396 | CLS_ROOT = 0x2, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 3397 | OBJC2_CLS_HIDDEN = 0x10, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3398 | CLS_EXCEPTION = 0x20 |
| 3399 | }; |
| 3400 | /// BuildClassRoTInitializer - generate meta-data for: |
| 3401 | /// struct _class_ro_t { |
| 3402 | /// uint32_t const flags; |
| 3403 | /// uint32_t const instanceStart; |
| 3404 | /// uint32_t const instanceSize; |
| 3405 | /// uint32_t const reserved; // only when building for 64bit targets |
| 3406 | /// const uint8_t * const ivarLayout; |
| 3407 | /// const char *const name; |
| 3408 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3409 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3410 | /// const struct _ivar_list_t *const ivars; |
| 3411 | /// const uint8_t * const weakIvarLayout; |
| 3412 | /// const struct _prop_list_t * const properties; |
| 3413 | /// } |
| 3414 | /// |
| 3415 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
| 3416 | unsigned flags, |
| 3417 | unsigned InstanceStart, |
| 3418 | unsigned InstanceSize, |
| 3419 | const ObjCImplementationDecl *ID) { |
| 3420 | std::string ClassName = ID->getNameAsString(); |
| 3421 | std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets! |
| 3422 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 3423 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 3424 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
| 3425 | // FIXME. For 64bit targets add 0 here. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3426 | // FIXME. ivarLayout is currently null! |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3427 | Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 3428 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 3429 | // const struct _method_list_t * const baseMethods; |
| 3430 | std::vector<llvm::Constant*> Methods; |
| 3431 | std::string MethodListName("\01l_OBJC_$_"); |
| 3432 | if (flags & CLS_META) { |
| 3433 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
| 3434 | for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(), |
| 3435 | e = ID->classmeth_end(); i != e; ++i) { |
| 3436 | // Class methods should always be defined. |
| 3437 | Methods.push_back(GetMethodConstant(*i)); |
| 3438 | } |
| 3439 | } else { |
| 3440 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
| 3441 | for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(), |
| 3442 | e = ID->instmeth_end(); i != e; ++i) { |
| 3443 | // Instance methods should always be defined. |
| 3444 | Methods.push_back(GetMethodConstant(*i)); |
| 3445 | } |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 3446 | for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(), |
| 3447 | e = ID->propimpl_end(); i != e; ++i) { |
| 3448 | ObjCPropertyImplDecl *PID = *i; |
| 3449 | |
| 3450 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 3451 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 3452 | |
| 3453 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 3454 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 3455 | Methods.push_back(C); |
| 3456 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 3457 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 3458 | Methods.push_back(C); |
| 3459 | } |
| 3460 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 3461 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 3462 | Values[ 5] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 3463 | "__DATA, __objc_const", Methods); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3464 | |
| 3465 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 3466 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
| 3467 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
| 3468 | + OID->getNameAsString(), |
| 3469 | OID->protocol_begin(), |
| 3470 | OID->protocol_end()); |
| 3471 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 3472 | if (flags & CLS_META) |
| 3473 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 3474 | else |
| 3475 | Values[ 7] = EmitIvarList(ID); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3476 | // FIXME. weakIvarLayout is currently null. |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3477 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 3478 | if (flags & CLS_META) |
| 3479 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 3480 | else |
| 3481 | Values[ 9] = |
| 3482 | EmitPropertyList( |
| 3483 | "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
| 3484 | ID, ID->getClassInterface(), ObjCTypes); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3485 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
| 3486 | Values); |
| 3487 | llvm::GlobalVariable *CLASS_RO_GV = |
| 3488 | new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false, |
| 3489 | llvm::GlobalValue::InternalLinkage, |
| 3490 | Init, |
| 3491 | (flags & CLS_META) ? |
| 3492 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 3493 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName, |
| 3494 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 3495 | CLASS_RO_GV->setAlignment( |
| 3496 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 3497 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3498 | UsedGlobals.push_back(CLASS_RO_GV); |
| 3499 | return CLASS_RO_GV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 3500 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3501 | } |
| 3502 | |
| 3503 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 3504 | /// for the given ClassName for: |
| 3505 | /// struct _class_t { |
| 3506 | /// struct _class_t *isa; |
| 3507 | /// struct _class_t * const superclass; |
| 3508 | /// void *cache; |
| 3509 | /// IMP *vtable; |
| 3510 | /// struct class_ro_t *ro; |
| 3511 | /// } |
| 3512 | /// |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 3513 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
| 3514 | std::string &ClassName, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3515 | llvm::Constant *IsAGV, |
| 3516 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 3517 | llvm::Constant *ClassRoGV, |
| 3518 | bool HiddenVisibility) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3519 | std::vector<llvm::Constant*> Values(5); |
| 3520 | Values[0] = IsAGV; |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 3521 | Values[1] = SuperClassGV |
| 3522 | ? SuperClassGV |
| 3523 | : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3524 | Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar |
| 3525 | Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar |
| 3526 | Values[4] = ClassRoGV; // &CLASS_RO_GV |
| 3527 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
| 3528 | Values); |
| 3529 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName); |
| 3530 | if (GV) |
| 3531 | GV->setInitializer(Init); |
| 3532 | else |
| 3533 | GV = |
| 3534 | new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 3535 | llvm::GlobalValue::ExternalLinkage, |
| 3536 | Init, |
| 3537 | ClassName, |
| 3538 | &CGM.getModule()); |
Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 3539 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 3540 | GV->setAlignment( |
| 3541 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 3542 | if (HiddenVisibility) |
| 3543 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3544 | UsedGlobals.push_back(GV); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 3545 | return GV; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3546 | } |
| 3547 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3548 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 3549 | std::string ClassName = ID->getNameAsString(); |
| 3550 | if (!ObjCEmptyCacheVar) { |
| 3551 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
| 3552 | ObjCTypes.CachePtrTy, |
| 3553 | false, |
| 3554 | llvm::GlobalValue::ExternalLinkage, |
| 3555 | 0, |
Fariborz Jahanian | 07da367 | 2009-02-03 17:34:34 +0000 | [diff] [blame] | 3556 | "\01__objc_empty_cache", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3557 | &CGM.getModule()); |
| 3558 | UsedGlobals.push_back(ObjCEmptyCacheVar); |
| 3559 | |
| 3560 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
| 3561 | llvm::PointerType::getUnqual( |
| 3562 | ObjCTypes.ImpnfABITy), |
| 3563 | false, |
| 3564 | llvm::GlobalValue::ExternalLinkage, |
| 3565 | 0, |
Fariborz Jahanian | 07da367 | 2009-02-03 17:34:34 +0000 | [diff] [blame] | 3566 | "\01__objc_empty_vtable", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3567 | &CGM.getModule()); |
| 3568 | UsedGlobals.push_back(ObjCEmptyVtableVar); |
| 3569 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 3570 | assert(ID->getClassInterface() && |
| 3571 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3572 | uint32_t InstanceStart = |
| 3573 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy); |
| 3574 | uint32_t InstanceSize = InstanceStart; |
| 3575 | uint32_t flags = CLS_META; |
| 3576 | std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_"); |
| 3577 | std::string ObjCClassName("\01_OBJC_CLASS_$_"); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3578 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3579 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3580 | |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 3581 | bool classIsHidden = IsClassHidden(ID->getClassInterface()); |
| 3582 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 3583 | flags |= OBJC2_CLS_HIDDEN; |
| 3584 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3585 | // class is root |
| 3586 | flags |= CLS_ROOT; |
| 3587 | std::string SuperClassName = ObjCClassName + ClassName; |
| 3588 | SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName); |
| 3589 | if (!SuperClassGV) |
| 3590 | SuperClassGV = |
| 3591 | new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 3592 | llvm::GlobalValue::ExternalLinkage, |
| 3593 | 0, |
| 3594 | SuperClassName, |
| 3595 | &CGM.getModule()); |
| 3596 | UsedGlobals.push_back(SuperClassGV); |
| 3597 | std::string IsAClassName = ObjCMetaClassName + ClassName; |
| 3598 | IsAGV = CGM.getModule().getGlobalVariable(IsAClassName); |
| 3599 | if (!IsAGV) |
| 3600 | IsAGV = |
| 3601 | new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 3602 | llvm::GlobalValue::ExternalLinkage, |
| 3603 | 0, |
| 3604 | IsAClassName, |
| 3605 | &CGM.getModule()); |
| 3606 | UsedGlobals.push_back(IsAGV); |
| 3607 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 3608 | // Has a root. Current class is not a root. |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3609 | std::string RootClassName = |
| 3610 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
| 3611 | std::string SuperClassName = ObjCMetaClassName + RootClassName; |
| 3612 | SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName); |
| 3613 | if (!SuperClassGV) |
| 3614 | SuperClassGV = |
| 3615 | new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 3616 | llvm::GlobalValue::ExternalLinkage, |
| 3617 | 0, |
| 3618 | SuperClassName, |
| 3619 | &CGM.getModule()); |
| 3620 | UsedGlobals.push_back(SuperClassGV); |
| 3621 | IsAGV = SuperClassGV; |
| 3622 | } |
| 3623 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 3624 | InstanceStart, |
| 3625 | InstanceSize,ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 3626 | std::string TClassName = ObjCMetaClassName + ClassName; |
| 3627 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 3628 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 3629 | classIsHidden); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 3630 | |
| 3631 | // Metadata for the class |
| 3632 | flags = CLS; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 3633 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 3634 | flags |= OBJC2_CLS_HIDDEN; |
| 3635 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 3636 | flags |= CLS_ROOT; |
| 3637 | SuperClassGV = 0; |
| 3638 | } |
| 3639 | else { |
| 3640 | // Has a root. Current class is not a root. |
| 3641 | std::string RootClassName = |
| 3642 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
| 3643 | std::string SuperClassName = ObjCClassName + RootClassName; |
| 3644 | SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName); |
| 3645 | if (!SuperClassGV) |
| 3646 | SuperClassGV = |
| 3647 | new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 3648 | llvm::GlobalValue::ExternalLinkage, |
| 3649 | 0, |
| 3650 | SuperClassName, |
| 3651 | &CGM.getModule()); |
| 3652 | UsedGlobals.push_back(SuperClassGV); |
| 3653 | |
| 3654 | } |
| 3655 | |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 3656 | InstanceStart = InstanceSize = 0; |
| 3657 | if (ObjCInterfaceDecl *OID = |
| 3658 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) { |
| 3659 | // FIXME. Share this with the one in EmitIvarList. |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 3660 | const llvm::Type *InterfaceTy = |
| 3661 | CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID)); |
| 3662 | const llvm::StructLayout *Layout = |
| 3663 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy)); |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 3664 | |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 3665 | RecordDecl::field_iterator firstField, lastField; |
| 3666 | const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField); |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 3667 | |
| 3668 | for (RecordDecl::field_iterator e = RD->field_end(), |
| 3669 | ifield = firstField; ifield != e; ++ifield) |
| 3670 | lastField = ifield; |
| 3671 | |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 3672 | if (lastField != RD->field_end()) { |
| 3673 | FieldDecl *Field = *lastField; |
| 3674 | const llvm::Type *FieldTy = |
| 3675 | CGM.getTypes().ConvertTypeForMem(Field->getType()); |
| 3676 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
| 3677 | InstanceSize = Layout->getElementOffset( |
| 3678 | CGM.getTypes().getLLVMFieldNo(Field)) + |
| 3679 | Size; |
| 3680 | if (firstField == RD->field_end()) |
| 3681 | InstanceStart = InstanceSize; |
| 3682 | else |
| 3683 | InstanceStart = Layout->getElementOffset(CGM.getTypes(). |
| 3684 | getLLVMFieldNo(*firstField)); |
| 3685 | } |
| 3686 | } |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 3687 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 3688 | InstanceStart, |
| 3689 | InstanceSize, |
| 3690 | ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 3691 | |
| 3692 | TClassName = ObjCClassName + ClassName; |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 3693 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 3694 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 3695 | classIsHidden); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 3696 | DefinedClasses.push_back(ClassMD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3697 | } |
| 3698 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 3699 | /// GenerateProtocolRef - This routine is called to generate code for |
| 3700 | /// a protocol reference expression; as in: |
| 3701 | /// @code |
| 3702 | /// @protocol(Proto1); |
| 3703 | /// @endcode |
| 3704 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 3705 | /// which will hold address of the protocol meta-data. |
| 3706 | /// |
| 3707 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
| 3708 | const ObjCProtocolDecl *PD) { |
| 3709 | |
| 3710 | llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
| 3711 | ObjCTypes.ExternalProtocolPtrTy); |
| 3712 | |
| 3713 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
| 3714 | ProtocolName += PD->getNameAsCString(); |
| 3715 | |
| 3716 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 3717 | if (PTGV) |
| 3718 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 3719 | PTGV = new llvm::GlobalVariable( |
| 3720 | Init->getType(), false, |
| 3721 | llvm::GlobalValue::WeakLinkage, |
| 3722 | Init, |
| 3723 | ProtocolName, |
| 3724 | &CGM.getModule()); |
| 3725 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 3726 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 3727 | UsedGlobals.push_back(PTGV); |
| 3728 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 3729 | } |
| 3730 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 3731 | /// GenerateCategory - Build metadata for a category implementation. |
| 3732 | /// struct _category_t { |
| 3733 | /// const char * const name; |
| 3734 | /// struct _class_t *const cls; |
| 3735 | /// const struct _method_list_t * const instance_methods; |
| 3736 | /// const struct _method_list_t * const class_methods; |
| 3737 | /// const struct _protocol_list_t * const protocols; |
| 3738 | /// const struct _prop_list_t * const properties; |
| 3739 | /// } |
| 3740 | /// |
| 3741 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) |
| 3742 | { |
| 3743 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 3744 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
| 3745 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 3746 | "_$_" + OCD->getNameAsString()); |
| 3747 | std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString()); |
| 3748 | |
| 3749 | std::vector<llvm::Constant*> Values(6); |
| 3750 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 3751 | // meta-class entry symbol |
| 3752 | llvm::GlobalVariable *ClassGV = |
| 3753 | CGM.getModule().getGlobalVariable(ExtClassName); |
| 3754 | if (!ClassGV) |
| 3755 | ClassGV = |
| 3756 | new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 3757 | llvm::GlobalValue::ExternalLinkage, |
| 3758 | 0, |
| 3759 | ExtClassName, |
| 3760 | &CGM.getModule()); |
| 3761 | UsedGlobals.push_back(ClassGV); |
| 3762 | Values[1] = ClassGV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 3763 | std::vector<llvm::Constant*> Methods; |
| 3764 | std::string MethodListName(Prefix); |
| 3765 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
| 3766 | "_$_" + OCD->getNameAsString(); |
| 3767 | |
| 3768 | for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(), |
| 3769 | e = OCD->instmeth_end(); i != e; ++i) { |
| 3770 | // Instance methods should always be defined. |
| 3771 | Methods.push_back(GetMethodConstant(*i)); |
| 3772 | } |
| 3773 | |
| 3774 | Values[2] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 3775 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 3776 | Methods); |
| 3777 | |
| 3778 | MethodListName = Prefix; |
| 3779 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 3780 | OCD->getNameAsString(); |
| 3781 | Methods.clear(); |
| 3782 | for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(), |
| 3783 | e = OCD->classmeth_end(); i != e; ++i) { |
| 3784 | // Class methods should always be defined. |
| 3785 | Methods.push_back(GetMethodConstant(*i)); |
| 3786 | } |
| 3787 | |
| 3788 | Values[3] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 3789 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 3790 | Methods); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 3791 | const ObjCCategoryDecl *Category = |
| 3792 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | eb73213 | 2009-01-29 23:23:06 +0000 | [diff] [blame] | 3793 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
| 3794 | + Interface->getNameAsString() + "_$_" |
| 3795 | + Category->getNameAsString(), |
| 3796 | Category->protocol_begin(), |
| 3797 | Category->protocol_end()); |
| 3798 | |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 3799 | std::string ExtName(Interface->getNameAsString() + "_$_" + |
| 3800 | OCD->getNameAsString()); |
| 3801 | Values[5] = |
| 3802 | EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
| 3803 | OCD, Category, ObjCTypes); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 3804 | llvm::Constant *Init = |
| 3805 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
| 3806 | Values); |
| 3807 | llvm::GlobalVariable *GCATV |
| 3808 | = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy, |
| 3809 | false, |
| 3810 | llvm::GlobalValue::InternalLinkage, |
| 3811 | Init, |
| 3812 | ExtCatName, |
| 3813 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 3814 | GCATV->setAlignment( |
| 3815 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 3816 | GCATV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 3817 | UsedGlobals.push_back(GCATV); |
| 3818 | DefinedCategories.push_back(GCATV); |
| 3819 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 3820 | |
| 3821 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 3822 | /// given method if it has been defined. The result is null if the |
| 3823 | /// method has not been defined. The return value has type MethodPtrTy. |
| 3824 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
| 3825 | const ObjCMethodDecl *MD) { |
| 3826 | // FIXME: Use DenseMap::lookup |
| 3827 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 3828 | if (!Fn) |
| 3829 | return 0; |
| 3830 | |
| 3831 | std::vector<llvm::Constant*> Method(3); |
| 3832 | Method[0] = |
| 3833 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 3834 | ObjCTypes.SelectorPtrTy); |
| 3835 | Method[1] = GetMethodVarType(MD); |
| 3836 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 3837 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 3838 | } |
| 3839 | |
| 3840 | /// EmitMethodList - Build meta-data for method declarations |
| 3841 | /// struct _method_list_t { |
| 3842 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 3843 | /// uint32_t method_count; |
| 3844 | /// struct _objc_method method_list[method_count]; |
| 3845 | /// } |
| 3846 | /// |
| 3847 | llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList( |
| 3848 | const std::string &Name, |
| 3849 | const char *Section, |
| 3850 | const ConstantVector &Methods) { |
| 3851 | // Return null for empty list. |
| 3852 | if (Methods.empty()) |
| 3853 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
| 3854 | |
| 3855 | std::vector<llvm::Constant*> Values(3); |
| 3856 | // sizeof(struct _objc_method) |
| 3857 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy); |
| 3858 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 3859 | // method_count |
| 3860 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 3861 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 3862 | Methods.size()); |
| 3863 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 3864 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 3865 | |
| 3866 | llvm::GlobalVariable *GV = |
| 3867 | new llvm::GlobalVariable(Init->getType(), false, |
| 3868 | llvm::GlobalValue::InternalLinkage, |
| 3869 | Init, |
| 3870 | Name, |
| 3871 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 3872 | GV->setAlignment( |
| 3873 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 3874 | GV->setSection(Section); |
| 3875 | UsedGlobals.push_back(GV); |
| 3876 | return llvm::ConstantExpr::getBitCast(GV, |
| 3877 | ObjCTypes.MethodListnfABIPtrTy); |
| 3878 | } |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 3879 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 3880 | llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar( |
| 3881 | const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 3882 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 3883 | unsigned long int Offset) { |
| 3884 | |
| 3885 | std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.' |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 3886 | + Ivar->getNameAsString()); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 3887 | llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.LongTy, Offset); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 3888 | |
| 3889 | llvm::GlobalVariable *IvarOffsetGV = |
| 3890 | CGM.getModule().getGlobalVariable(ExternalName); |
| 3891 | if (IvarOffsetGV) { |
| 3892 | // ivar offset symbol already built due to user code referencing it. |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 3893 | IvarOffsetGV->setAlignment( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 3894 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 3895 | IvarOffsetGV->setInitializer(Init); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 3896 | IvarOffsetGV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 3897 | UsedGlobals.push_back(IvarOffsetGV); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 3898 | return IvarOffsetGV; |
| 3899 | } |
| 3900 | |
| 3901 | IvarOffsetGV = |
| 3902 | new llvm::GlobalVariable(Init->getType(), |
| 3903 | false, |
| 3904 | llvm::GlobalValue::ExternalLinkage, |
| 3905 | Init, |
| 3906 | ExternalName, |
| 3907 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 3908 | IvarOffsetGV->setAlignment( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 3909 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy)); |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 3910 | // @private and @package have hidden visibility. |
| 3911 | bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public || |
| 3912 | Ivar->getAccessControl() == ObjCIvarDecl::Protected); |
| 3913 | if (!globalVisibility) |
| 3914 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 3915 | else |
| 3916 | if (const ObjCInterfaceDecl *OID = ID->getClassInterface()) |
| 3917 | if (IsClassHidden(OID)) |
| 3918 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 3919 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 3920 | IvarOffsetGV->setSection("__DATA, __objc_const"); |
| 3921 | UsedGlobals.push_back(IvarOffsetGV); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 3922 | return IvarOffsetGV; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 3923 | } |
| 3924 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 3925 | /// EmitIvarList - Emit the ivar list for the given |
| 3926 | /// implementation. If ForClass is true the list of class ivars |
| 3927 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 3928 | /// interface ivars will be emitted. The return value has type |
| 3929 | /// IvarListnfABIPtrTy. |
| 3930 | /// struct _ivar_t { |
| 3931 | /// unsigned long int *offset; // pointer to ivar offset location |
| 3932 | /// char *name; |
| 3933 | /// char *type; |
| 3934 | /// uint32_t alignment; |
| 3935 | /// uint32_t size; |
| 3936 | /// } |
| 3937 | /// struct _ivar_list_t { |
| 3938 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 3939 | /// uint32 count; |
| 3940 | /// struct _iver_t list[count]; |
| 3941 | /// } |
| 3942 | /// |
| 3943 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
| 3944 | const ObjCImplementationDecl *ID) { |
| 3945 | |
| 3946 | std::vector<llvm::Constant*> Ivars, Ivar(5); |
| 3947 | |
| 3948 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 3949 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
| 3950 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 3951 | // FIXME. Consolidate this with similar code in GenerateClass. |
| 3952 | const llvm::Type *InterfaceTy = |
| 3953 | CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType( |
| 3954 | const_cast<ObjCInterfaceDecl*>(OID))); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 3955 | const llvm::StructLayout *Layout = |
| 3956 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy)); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 3957 | |
| 3958 | RecordDecl::field_iterator i,p; |
| 3959 | const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p); |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 3960 | ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(); |
| 3961 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 3962 | for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) { |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 3963 | FieldDecl *Field = *i; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 3964 | unsigned long offset = Layout->getElementOffset(CGM.getTypes(). |
| 3965 | getLLVMFieldNo(Field)); |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 3966 | const ObjCIvarDecl *ivarDecl = *I++; |
| 3967 | Ivar[0] = EmitIvarOffsetVar(ID, ivarDecl, offset); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 3968 | if (Field->getIdentifier()) |
| 3969 | Ivar[1] = GetMethodVarName(Field->getIdentifier()); |
| 3970 | else |
| 3971 | Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 3972 | std::string TypeStr; |
| 3973 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 3974 | Ivar[2] = GetMethodVarType(TypeStr); |
| 3975 | const llvm::Type *FieldTy = |
| 3976 | CGM.getTypes().ConvertTypeForMem(Field->getType()); |
| 3977 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
| 3978 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
| 3979 | Field->getType().getTypePtr()) >> 3; |
| 3980 | Align = llvm::Log2_32(Align); |
| 3981 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Fariborz Jahanian | 07236ba | 2009-01-27 22:27:56 +0000 | [diff] [blame] | 3982 | // NOTE. Size of a bitfield does not match gcc's, because of the way |
| 3983 | // bitfields are treated special in each. But I am told that 'size' |
| 3984 | // for bitfield ivars is ignored by the runtime so it does not matter. |
| 3985 | // (even if it matters, some day, there is enough info. to get the bitfield |
| 3986 | // right! |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 3987 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 3988 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
| 3989 | } |
| 3990 | // Return null for empty list. |
| 3991 | if (Ivars.empty()) |
| 3992 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 3993 | std::vector<llvm::Constant*> Values(3); |
| 3994 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy); |
| 3995 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 3996 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 3997 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
| 3998 | Ivars.size()); |
| 3999 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
| 4000 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4001 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 4002 | llvm::GlobalVariable *GV = |
| 4003 | new llvm::GlobalVariable(Init->getType(), false, |
| 4004 | llvm::GlobalValue::InternalLinkage, |
| 4005 | Init, |
| 4006 | Prefix + OID->getNameAsString(), |
| 4007 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4008 | GV->setAlignment( |
| 4009 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4010 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4011 | |
| 4012 | UsedGlobals.push_back(GV); |
| 4013 | return llvm::ConstantExpr::getBitCast(GV, |
| 4014 | ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4015 | } |
| 4016 | |
| 4017 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
| 4018 | const ObjCProtocolDecl *PD) { |
| 4019 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4020 | |
| 4021 | if (!Entry) { |
| 4022 | // We use the initializer as a marker of whether this is a forward |
| 4023 | // reference or not. At module finalization we add the empty |
| 4024 | // contents for protocols which were referenced but never defined. |
| 4025 | Entry = |
| 4026 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
| 4027 | llvm::GlobalValue::ExternalLinkage, |
| 4028 | 0, |
| 4029 | "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(), |
| 4030 | &CGM.getModule()); |
| 4031 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 4032 | UsedGlobals.push_back(Entry); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4033 | } |
| 4034 | |
| 4035 | return Entry; |
| 4036 | } |
| 4037 | |
| 4038 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 4039 | /// @code |
| 4040 | /// struct _protocol_t { |
| 4041 | /// id isa; // NULL |
| 4042 | /// const char * const protocol_name; |
| 4043 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 4044 | /// const struct method_list_t * const instance_methods; |
| 4045 | /// const struct method_list_t * const class_methods; |
| 4046 | /// const struct method_list_t *optionalInstanceMethods; |
| 4047 | /// const struct method_list_t *optionalClassMethods; |
| 4048 | /// const struct _prop_list_t * properties; |
| 4049 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 4050 | /// const uint32_t flags; // = 0 |
| 4051 | /// } |
| 4052 | /// @endcode |
| 4053 | /// |
| 4054 | |
| 4055 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
| 4056 | const ObjCProtocolDecl *PD) { |
| 4057 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4058 | |
| 4059 | // Early exit if a defining object has already been generated. |
| 4060 | if (Entry && Entry->hasInitializer()) |
| 4061 | return Entry; |
| 4062 | |
| 4063 | const char *ProtocolName = PD->getNameAsCString(); |
| 4064 | |
| 4065 | // Construct method lists. |
| 4066 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 4067 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
| 4068 | for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(), |
| 4069 | e = PD->instmeth_end(); i != e; ++i) { |
| 4070 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4071 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4072 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4073 | OptInstanceMethods.push_back(C); |
| 4074 | } else { |
| 4075 | InstanceMethods.push_back(C); |
| 4076 | } |
| 4077 | } |
| 4078 | |
| 4079 | for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(), |
| 4080 | e = PD->classmeth_end(); i != e; ++i) { |
| 4081 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4082 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4083 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4084 | OptClassMethods.push_back(C); |
| 4085 | } else { |
| 4086 | ClassMethods.push_back(C); |
| 4087 | } |
| 4088 | } |
| 4089 | |
| 4090 | std::vector<llvm::Constant*> Values(10); |
| 4091 | // isa is NULL |
| 4092 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
| 4093 | Values[1] = GetClassName(PD->getIdentifier()); |
| 4094 | Values[2] = EmitProtocolList( |
| 4095 | "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(), |
| 4096 | PD->protocol_begin(), |
| 4097 | PD->protocol_end()); |
| 4098 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4099 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4100 | + PD->getNameAsString(), |
| 4101 | "__DATA, __objc_const", |
| 4102 | InstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4103 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4104 | + PD->getNameAsString(), |
| 4105 | "__DATA, __objc_const", |
| 4106 | ClassMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4107 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4108 | + PD->getNameAsString(), |
| 4109 | "__DATA, __objc_const", |
| 4110 | OptInstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4111 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4112 | + PD->getNameAsString(), |
| 4113 | "__DATA, __objc_const", |
| 4114 | OptClassMethods); |
| 4115 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(), |
| 4116 | 0, PD, ObjCTypes); |
| 4117 | uint32_t Size = |
| 4118 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy); |
| 4119 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4120 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
| 4121 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
| 4122 | Values); |
| 4123 | |
| 4124 | if (Entry) { |
| 4125 | // Already created, fix the linkage and update the initializer. |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4126 | Entry->setLinkage(llvm::GlobalValue::WeakLinkage); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4127 | Entry->setInitializer(Init); |
| 4128 | } else { |
| 4129 | Entry = |
| 4130 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4131 | llvm::GlobalValue::WeakLinkage, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4132 | Init, |
| 4133 | std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName, |
| 4134 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4135 | Entry->setAlignment( |
| 4136 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4137 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4138 | } |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4139 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4140 | |
| 4141 | // Use this protocol meta-data to build protocol list table in section |
| 4142 | // __DATA, __objc_protolist |
| 4143 | llvm::Type *ptype = llvm::PointerType::getUnqual(ObjCTypes.ProtocolnfABITy); |
| 4144 | llvm::GlobalVariable *PTGV = new llvm::GlobalVariable( |
| 4145 | ptype, false, |
| 4146 | llvm::GlobalValue::WeakLinkage, |
| 4147 | Entry, |
| 4148 | std::string("\01l_OBJC_LABEL_PROTOCOL_$_") |
| 4149 | +ProtocolName, |
| 4150 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4151 | PTGV->setAlignment( |
| 4152 | CGM.getTargetData().getPrefTypeAlignment(ptype)); |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4153 | PTGV->setSection("__DATA, __objc_protolist"); |
| 4154 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4155 | UsedGlobals.push_back(PTGV); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4156 | return Entry; |
| 4157 | } |
| 4158 | |
| 4159 | /// EmitProtocolList - Generate protocol list meta-data: |
| 4160 | /// @code |
| 4161 | /// struct _protocol_list_t { |
| 4162 | /// long protocol_count; // Note, this is 32/64 bit |
| 4163 | /// struct _protocol_t[protocol_count]; |
| 4164 | /// } |
| 4165 | /// @endcode |
| 4166 | /// |
| 4167 | llvm::Constant * |
| 4168 | CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name, |
| 4169 | ObjCProtocolDecl::protocol_iterator begin, |
| 4170 | ObjCProtocolDecl::protocol_iterator end) { |
| 4171 | std::vector<llvm::Constant*> ProtocolRefs; |
| 4172 | |
| 4173 | for (; begin != end; ++begin) |
| 4174 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 4175 | |
| 4176 | // Just return null for empty protocol lists |
| 4177 | if (ProtocolRefs.empty()) |
| 4178 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4179 | |
| 4180 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
| 4181 | if (GV) |
| 4182 | return GV; |
| 4183 | // This list is null terminated. |
| 4184 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
| 4185 | ObjCTypes.ProtocolListnfABIPtrTy)); |
| 4186 | |
| 4187 | std::vector<llvm::Constant*> Values(2); |
| 4188 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 4189 | Values[1] = |
| 4190 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolListnfABIPtrTy, |
| 4191 | ProtocolRefs.size()), |
| 4192 | ProtocolRefs); |
| 4193 | |
| 4194 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4195 | GV = new llvm::GlobalVariable(Init->getType(), false, |
| 4196 | llvm::GlobalValue::InternalLinkage, |
| 4197 | Init, |
| 4198 | Name, |
| 4199 | &CGM.getModule()); |
| 4200 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4201 | GV->setAlignment( |
| 4202 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4203 | UsedGlobals.push_back(GV); |
| 4204 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy); |
| 4205 | } |
| 4206 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4207 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 4208 | /// struct _objc_method { |
| 4209 | /// SEL _cmd; |
| 4210 | /// char *method_type; |
| 4211 | /// char *_imp; |
| 4212 | /// } |
| 4213 | |
| 4214 | llvm::Constant * |
| 4215 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 4216 | std::vector<llvm::Constant*> Desc(3); |
| 4217 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4218 | ObjCTypes.SelectorPtrTy); |
| 4219 | Desc[1] = GetMethodVarType(MD); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4220 | // Protocol methods have no implementation. So, this entry is always NULL. |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4221 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 4222 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
| 4223 | } |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4224 | |
| 4225 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 4226 | /// This code gen. amounts to generating code for: |
| 4227 | /// @code |
| 4228 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 4229 | /// @encode |
| 4230 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4231 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4232 | CodeGen::CodeGenFunction &CGF, |
| 4233 | QualType ObjectTy, |
| 4234 | llvm::Value *BaseValue, |
| 4235 | const ObjCIvarDecl *Ivar, |
| 4236 | const FieldDecl *Field, |
| 4237 | unsigned CVRQualifiers) { |
| 4238 | assert(ObjectTy->isObjCInterfaceType() && |
| 4239 | "CGObjCNonFragileABIMac::EmitObjCValueForIvar"); |
| 4240 | NamedDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
| 4241 | // NOTE. This name must match one in EmitIvarOffsetVar. |
| 4242 | // FIXME. Consolidate into one naming routine. |
| 4243 | std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.' |
| 4244 | + Ivar->getNameAsString()); |
| 4245 | |
| 4246 | llvm::GlobalVariable *IvarOffsetGV = |
| 4247 | CGM.getModule().getGlobalVariable(ExternalName); |
| 4248 | if (!IvarOffsetGV) |
| 4249 | IvarOffsetGV = |
| 4250 | new llvm::GlobalVariable(ObjCTypes.LongTy, |
| 4251 | false, |
| 4252 | llvm::GlobalValue::ExternalLinkage, |
| 4253 | 0, |
| 4254 | ExternalName, |
| 4255 | &CGM.getModule()); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4256 | |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4257 | // (char *) BaseValue |
| 4258 | llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, |
| 4259 | ObjCTypes.Int8PtrTy); |
| 4260 | llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV); |
| 4261 | // (char*)BaseValue + Offset_symbol |
| 4262 | V = CGF.Builder.CreateGEP(V, Offset, "add.ptr"); |
| 4263 | // (type *)((char*)BaseValue + Offset_symbol) |
| 4264 | const llvm::Type *IvarTy = |
| 4265 | CGM.getTypes().ConvertType(Ivar->getType()); |
| 4266 | llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy); |
| 4267 | V = CGF.Builder.CreateBitCast(V, ptrIvarTy); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4268 | |
| 4269 | if (Ivar->isBitField()) |
| 4270 | return CGF.EmitLValueForBitfield(V, const_cast<FieldDecl *>(Field), |
| 4271 | CVRQualifiers); |
| 4272 | |
| 4273 | LValue LV = LValue::MakeAddr(V, |
| 4274 | Ivar->getType().getCVRQualifiers()|CVRQualifiers); |
| 4275 | LValue::SetObjCIvar(LV, true); |
| 4276 | return LV; |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4277 | } |
| 4278 | |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame^] | 4279 | CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend( |
| 4280 | CodeGen::CodeGenFunction &CGF, |
| 4281 | QualType ResultType, |
| 4282 | Selector Sel, |
| 4283 | llvm::Value *Arg0, |
| 4284 | QualType Arg0Ty, |
| 4285 | bool IsSuper, |
| 4286 | const CallArgList &CallArgs) { |
| 4287 | return RValue::get(0); |
| 4288 | } |
| 4289 | |
| 4290 | /// Generate code for a message send expression in the nonfragile abi. |
| 4291 | CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend( |
| 4292 | CodeGen::CodeGenFunction &CGF, |
| 4293 | QualType ResultType, |
| 4294 | Selector Sel, |
| 4295 | llvm::Value *Receiver, |
| 4296 | bool IsClassMessage, |
| 4297 | const CallArgList &CallArgs) { |
| 4298 | llvm::Value *Arg0 = |
| 4299 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp"); |
| 4300 | return EmitMessageSend(CGF, ResultType, Sel, |
| 4301 | Arg0, CGF.getContext().getObjCIdType(), |
| 4302 | false, CallArgs); |
| 4303 | } |
| 4304 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4305 | /* *** */ |
| 4306 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4307 | CodeGen::CGObjCRuntime * |
| 4308 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 4309 | return new CGObjCMac(CGM); |
| 4310 | } |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4311 | |
| 4312 | CodeGen::CGObjCRuntime * |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4313 | CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4314 | return new CGObjCNonFragileABIMac(CGM); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4315 | } |