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