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