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