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