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