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