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