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: |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 44 | const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 87 | llvm::Constant *GetPropertyFn, *SetPropertyFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 89 | llvm::Constant *EnumerationMutationFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 90 | |
| 91 | /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 92 | llvm::Constant *GcReadWeakFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 93 | |
| 94 | /// GcAssignWeakFn -- LLVM objc_assign_weak function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 95 | llvm::Constant *GcAssignWeakFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 96 | |
| 97 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 98 | llvm::Constant *GcAssignGlobalFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 99 | |
| 100 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 101 | llvm::Constant *GcAssignIvarFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 102 | |
| 103 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 104 | llvm::Constant *GcAssignStrongCastFn; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 105 | |
| 106 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 107 | llvm::Constant *ExceptionThrowFn; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 108 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 109 | /// SyncEnterFn - LLVM object_sync_enter function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 110 | llvm::Constant *getSyncEnterFn() { |
| 111 | // void objc_sync_enter (id) |
| 112 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 113 | llvm::FunctionType *FTy = |
| 114 | llvm::FunctionType::get(llvm::Type::VoidTy, Args, false); |
| 115 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 116 | } |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 117 | |
| 118 | /// SyncExitFn - LLVM object_sync_exit function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 119 | llvm::Constant *SyncExitFn; |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 120 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 121 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
| 122 | ~ObjCCommonTypesHelper(){} |
| 123 | }; |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 124 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 125 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 126 | /// construction of varies types used during ObjC generation. |
| 127 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
| 128 | private: |
| 129 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 130 | llvm::Constant *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn; |
| 131 | llvm::Constant *MessageSendSuperFn, *MessageSendSuperStretFn, |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 132 | *MessageSendSuperFpretFn; |
| 133 | |
| 134 | public: |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 135 | /// SymtabTy - LLVM type for struct objc_symtab. |
| 136 | const llvm::StructType *SymtabTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 137 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
| 138 | const llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 139 | /// ModuleTy - LLVM type for struct objc_module. |
| 140 | const llvm::StructType *ModuleTy; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 141 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 142 | /// ProtocolTy - LLVM type for struct objc_protocol. |
| 143 | const llvm::StructType *ProtocolTy; |
| 144 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
| 145 | const llvm::Type *ProtocolPtrTy; |
| 146 | /// ProtocolExtensionTy - LLVM type for struct |
| 147 | /// objc_protocol_extension. |
| 148 | const llvm::StructType *ProtocolExtensionTy; |
| 149 | /// ProtocolExtensionTy - LLVM type for struct |
| 150 | /// objc_protocol_extension *. |
| 151 | const llvm::Type *ProtocolExtensionPtrTy; |
| 152 | /// MethodDescriptionTy - LLVM type for struct |
| 153 | /// objc_method_description. |
| 154 | const llvm::StructType *MethodDescriptionTy; |
| 155 | /// MethodDescriptionListTy - LLVM type for struct |
| 156 | /// objc_method_description_list. |
| 157 | const llvm::StructType *MethodDescriptionListTy; |
| 158 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 159 | /// objc_method_description_list *. |
| 160 | const llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 161 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
| 162 | const llvm::Type *ProtocolListTy; |
| 163 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
| 164 | const llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 165 | /// CategoryTy - LLVM type for struct objc_category. |
| 166 | const llvm::StructType *CategoryTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 167 | /// ClassTy - LLVM type for struct objc_class. |
| 168 | const llvm::StructType *ClassTy; |
| 169 | /// ClassPtrTy - LLVM type for struct objc_class *. |
| 170 | const llvm::Type *ClassPtrTy; |
| 171 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
| 172 | const llvm::StructType *ClassExtensionTy; |
| 173 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
| 174 | const llvm::Type *ClassExtensionPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 175 | // IvarTy - LLVM type for struct objc_ivar. |
| 176 | const llvm::StructType *IvarTy; |
| 177 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
| 178 | const llvm::Type *IvarListTy; |
| 179 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
| 180 | const llvm::Type *IvarListPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 181 | /// MethodListTy - LLVM type for struct objc_method_list. |
| 182 | const llvm::Type *MethodListTy; |
| 183 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
| 184 | const llvm::Type *MethodListPtrTy; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 185 | |
| 186 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
| 187 | const llvm::Type *ExceptionDataTy; |
| 188 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 189 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 190 | llvm::Constant *ExceptionTryEnterFn; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 191 | |
| 192 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 193 | llvm::Constant *ExceptionTryExitFn; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 194 | |
| 195 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 196 | llvm::Constant *ExceptionExtractFn; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 197 | |
| 198 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 199 | llvm::Constant *ExceptionMatchFn; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 200 | |
| 201 | /// SetJmpFn - LLVM _setjmp function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 202 | llvm::Constant *SetJmpFn; |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 203 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 204 | public: |
| 205 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 206 | ~ObjCTypesHelper() {} |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 207 | |
| 208 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 209 | llvm::Constant *getSendFn(bool IsSuper) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 210 | return IsSuper ? MessageSendSuperFn : MessageSendFn; |
| 211 | } |
| 212 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 213 | llvm::Constant *getSendStretFn(bool IsSuper) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 214 | return IsSuper ? MessageSendSuperStretFn : MessageSendStretFn; |
| 215 | } |
| 216 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 217 | llvm::Constant *getSendFpretFn(bool IsSuper) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 218 | return IsSuper ? MessageSendSuperFpretFn : MessageSendFpretFn; |
| 219 | } |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 220 | }; |
| 221 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 222 | /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 223 | /// modern abi |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 224 | class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 225 | public: |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 226 | llvm::Constant *MessageSendFixupFn, *MessageSendFpretFixupFn, |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 227 | *MessageSendStretFixupFn, *MessageSendIdFixupFn, |
| 228 | *MessageSendIdStretFixupFn, *MessageSendSuper2FixupFn, |
| 229 | *MessageSendSuper2StretFixupFn; |
| 230 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 231 | // MethodListnfABITy - LLVM for struct _method_list_t |
| 232 | const llvm::StructType *MethodListnfABITy; |
| 233 | |
| 234 | // MethodListnfABIPtrTy - LLVM for struct _method_list_t* |
| 235 | const llvm::Type *MethodListnfABIPtrTy; |
| 236 | |
| 237 | // ProtocolnfABITy = LLVM for struct _protocol_t |
| 238 | const llvm::StructType *ProtocolnfABITy; |
| 239 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 240 | // ProtocolnfABIPtrTy = LLVM for struct _protocol_t* |
| 241 | const llvm::Type *ProtocolnfABIPtrTy; |
| 242 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 243 | // ProtocolListnfABITy - LLVM for struct _objc_protocol_list |
| 244 | const llvm::StructType *ProtocolListnfABITy; |
| 245 | |
| 246 | // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list* |
| 247 | const llvm::Type *ProtocolListnfABIPtrTy; |
| 248 | |
| 249 | // ClassnfABITy - LLVM for struct _class_t |
| 250 | const llvm::StructType *ClassnfABITy; |
| 251 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 252 | // ClassnfABIPtrTy - LLVM for struct _class_t* |
| 253 | const llvm::Type *ClassnfABIPtrTy; |
| 254 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 255 | // IvarnfABITy - LLVM for struct _ivar_t |
| 256 | const llvm::StructType *IvarnfABITy; |
| 257 | |
| 258 | // IvarListnfABITy - LLVM for struct _ivar_list_t |
| 259 | const llvm::StructType *IvarListnfABITy; |
| 260 | |
| 261 | // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t* |
| 262 | const llvm::Type *IvarListnfABIPtrTy; |
| 263 | |
| 264 | // ClassRonfABITy - LLVM for struct _class_ro_t |
| 265 | const llvm::StructType *ClassRonfABITy; |
| 266 | |
| 267 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 268 | const llvm::Type *ImpnfABITy; |
| 269 | |
| 270 | // CategorynfABITy - LLVM for struct _category_t |
| 271 | const llvm::StructType *CategorynfABITy; |
| 272 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 273 | // New types for nonfragile abi messaging. |
| 274 | |
| 275 | // MessageRefTy - LLVM for: |
| 276 | // struct _message_ref_t { |
| 277 | // IMP messenger; |
| 278 | // SEL name; |
| 279 | // }; |
| 280 | const llvm::StructType *MessageRefTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 281 | // MessageRefCTy - clang type for struct _message_ref_t |
| 282 | QualType MessageRefCTy; |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 283 | |
| 284 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 285 | const llvm::Type *MessageRefPtrTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 286 | // MessageRefCPtrTy - clang type for struct _message_ref_t* |
| 287 | QualType MessageRefCPtrTy; |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 288 | |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 289 | // MessengerTy - Type of the messenger (shown as IMP above) |
| 290 | const llvm::FunctionType *MessengerTy; |
| 291 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 292 | // SuperMessageRefTy - LLVM for: |
| 293 | // struct _super_message_ref_t { |
| 294 | // SUPER_IMP messenger; |
| 295 | // SEL name; |
| 296 | // }; |
| 297 | const llvm::StructType *SuperMessageRefTy; |
| 298 | |
| 299 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 300 | const llvm::Type *SuperMessageRefPtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 301 | |
| 302 | /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C |
| 303 | /// exception personality function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 304 | llvm::Value *getEHPersonalityPtr() { |
| 305 | llvm::Constant *Personality = |
| 306 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 307 | std::vector<const llvm::Type*>(), |
| 308 | true), |
| 309 | "__objc_personality_v0"); |
| 310 | return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy); |
| 311 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 312 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 313 | llvm::Constant *UnwindResumeOrRethrowFn, *ObjCBeginCatchFn, *ObjCEndCatchFn; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 314 | |
| 315 | const llvm::StructType *EHTypeTy; |
| 316 | const llvm::Type *EHTypePtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 317 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 318 | ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); |
| 319 | ~ObjCNonFragileABITypesHelper(){} |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 320 | }; |
| 321 | |
| 322 | class CGObjCCommonMac : public CodeGen::CGObjCRuntime { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 323 | public: |
| 324 | // FIXME - accessibility |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 325 | class GC_IVAR { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 326 | public: |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 327 | unsigned int ivar_bytepos; |
| 328 | unsigned int ivar_size; |
| 329 | GC_IVAR() : ivar_bytepos(0), ivar_size(0) {} |
| 330 | }; |
| 331 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 332 | class SKIP_SCAN { |
| 333 | public: |
| 334 | unsigned int skip; |
| 335 | unsigned int scan; |
| 336 | SKIP_SCAN() : skip(0), scan(0) {} |
| 337 | }; |
| 338 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 339 | protected: |
| 340 | CodeGen::CodeGenModule &CGM; |
| 341 | // FIXME! May not be needing this after all. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 342 | unsigned ObjCABI; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 343 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 344 | // gc ivar layout bitmap calculation helper caches. |
| 345 | llvm::SmallVector<GC_IVAR, 16> SkipIvars; |
| 346 | llvm::SmallVector<GC_IVAR, 16> IvarsInfo; |
| 347 | llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 348 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 349 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 350 | /// DefinedSymbols and FinishModule(). |
| 351 | std::set<IdentifierInfo*> LazySymbols; |
| 352 | |
| 353 | /// DefinedSymbols - External symbols which are defined by this |
| 354 | /// module. The symbols in this list and LazySymbols are used to add |
| 355 | /// special linker symbols which ensure that Objective-C modules are |
| 356 | /// linked properly. |
| 357 | std::set<IdentifierInfo*> DefinedSymbols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 358 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 359 | /// ClassNames - uniqued class names. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 360 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 361 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 362 | /// MethodVarNames - uniqued method variable names. |
| 363 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 364 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 365 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 366 | /// a StringMap here because have no other unique reference. |
| 367 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 368 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 369 | /// MethodDefinitions - map of methods which have been defined in |
| 370 | /// this translation unit. |
| 371 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 372 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 373 | /// PropertyNames - uniqued method variable names. |
| 374 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 375 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 376 | /// ClassReferences - uniqued class references. |
| 377 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 378 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 379 | /// SelectorReferences - uniqued selector references. |
| 380 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 381 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 382 | /// Protocols - Protocols for which an objc_protocol structure has |
| 383 | /// been emitted. Forward declarations are handled by creating an |
| 384 | /// empty structure whose initializer is filled in when/if defined. |
| 385 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 386 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 387 | /// DefinedProtocols - Protocols which have actually been |
| 388 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 389 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 390 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 391 | /// DefinedClasses - List of defined classes. |
| 392 | std::vector<llvm::GlobalValue*> DefinedClasses; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 393 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 394 | /// DefinedCategories - List of defined categories. |
| 395 | std::vector<llvm::GlobalValue*> DefinedCategories; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 396 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 397 | /// UsedGlobals - List of globals to pack into the llvm.used metadata |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 398 | /// to prevent them from being clobbered. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 399 | std::vector<llvm::GlobalVariable*> UsedGlobals; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 400 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 401 | /// GetNameForMethod - Return a name for the given method. |
| 402 | /// \param[out] NameOut - The return value. |
| 403 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 404 | const ObjCContainerDecl *CD, |
| 405 | std::string &NameOut); |
| 406 | |
| 407 | /// GetMethodVarName - Return a unique constant for the given |
| 408 | /// selector's name. The return value has type char *. |
| 409 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 410 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
| 411 | llvm::Constant *GetMethodVarName(const std::string &Name); |
| 412 | |
| 413 | /// GetMethodVarType - Return a unique constant for the given |
| 414 | /// selector's name. The return value has type char *. |
| 415 | |
| 416 | // FIXME: This is a horrible name. |
| 417 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 418 | llvm::Constant *GetMethodVarType(FieldDecl *D); |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 419 | |
| 420 | /// GetPropertyName - Return a unique constant for the given |
| 421 | /// name. The return value has type char *. |
| 422 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
| 423 | |
| 424 | // FIXME: This can be dropped once string functions are unified. |
| 425 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 426 | const Decl *Container); |
| 427 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 428 | /// GetClassName - Return a unique constant for the given selector's |
| 429 | /// name. The return value has type char *. |
| 430 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
| 431 | |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 432 | /// GetInterfaceDeclStructLayout - Get layout for ivars of given |
| 433 | /// interface declaration. |
| 434 | const llvm::StructLayout *GetInterfaceDeclStructLayout( |
| 435 | const ObjCInterfaceDecl *ID) const; |
| 436 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 437 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 438 | /// implementation for the __strong or __weak case. |
| 439 | /// |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 440 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
| 441 | bool ForStrongLayout); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 442 | |
Fariborz Jahanian | cf71dd4 | 2009-04-07 20:26:30 +0000 | [diff] [blame] | 443 | bool IsClassHidden(const ObjCInterfaceDecl *ID); |
| 444 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 445 | void BuildAggrIvarLayout(const ObjCInterfaceDecl *OI, |
| 446 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 447 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 448 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 449 | unsigned int BytePos, bool ForStrongLayout, |
| 450 | int &Index, int &SkIndex, bool &HasUnion); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 451 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 452 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 453 | /// ivar layout bitmap. |
| 454 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 455 | const ObjCCommonTypesHelper &ObjCTypes); |
| 456 | |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 457 | const RecordDecl *GetFirstIvarInRecord(const ObjCInterfaceDecl *OID, |
| 458 | RecordDecl::field_iterator &FIV, |
| 459 | RecordDecl::field_iterator &PIV); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 460 | /// EmitPropertyList - Emit the given property list. The return |
| 461 | /// value has type PropertyListPtrTy. |
| 462 | llvm::Constant *EmitPropertyList(const std::string &Name, |
| 463 | const Decl *Container, |
| 464 | const ObjCContainerDecl *OCD, |
| 465 | const ObjCCommonTypesHelper &ObjCTypes); |
| 466 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 467 | /// GetProtocolRef - Return a reference to the internal protocol |
| 468 | /// description, creating an empty one if it has not been |
| 469 | /// defined. The return value has type ProtocolPtrTy. |
| 470 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 471 | |
| 472 | /// GetIvarBaseOffset - returns ivars byte offset. |
| 473 | uint64_t GetIvarBaseOffset(const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 474 | const FieldDecl *Field); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 475 | |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 476 | /// GetFieldBaseOffset - return's field byte offset. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 477 | uint64_t GetFieldBaseOffset(const ObjCInterfaceDecl *OI, |
| 478 | const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 479 | const FieldDecl *Field); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 480 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 481 | /// CreateMetadataVar - Create a global variable with internal |
| 482 | /// linkage for use by the Objective-C runtime. |
| 483 | /// |
| 484 | /// This is a convenience wrapper which not only creates the |
| 485 | /// variable, but also sets the section and alignment and adds the |
| 486 | /// global to the UsedGlobals list. |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 487 | /// |
| 488 | /// \param Name - The variable name. |
| 489 | /// \param Init - The variable initializer; this is also used to |
| 490 | /// define the type of the variable. |
| 491 | /// \param Section - The section the variable should go into, or 0. |
| 492 | /// \param Align - The alignment for the variable, or 0. |
| 493 | /// \param AddToUsed - Whether the variable should be added to |
| 494 | /// llvm. |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 495 | llvm::GlobalVariable *CreateMetadataVar(const std::string &Name, |
| 496 | llvm::Constant *Init, |
| 497 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 498 | unsigned Align, |
| 499 | bool AddToUsed); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 500 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 501 | public: |
| 502 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm) |
| 503 | { } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 504 | |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 505 | virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 506 | |
| 507 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 508 | const ObjCContainerDecl *CD=0); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 509 | |
| 510 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
| 511 | |
| 512 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 513 | /// declaration, emitting it if necessary. The return value has type |
| 514 | /// ProtocolPtrTy. |
| 515 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
| 516 | |
| 517 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 518 | /// object for the given declaration, emitting it if needed. These |
| 519 | /// forward references will be filled in with empty bodies if no |
| 520 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 521 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 522 | }; |
| 523 | |
| 524 | class CGObjCMac : public CGObjCCommonMac { |
| 525 | private: |
| 526 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 527 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 528 | /// level information. |
| 529 | void EmitImageInfo(); |
| 530 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 531 | /// EmitModuleInfo - Another marker encoding module level |
| 532 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 533 | void EmitModuleInfo(); |
| 534 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 535 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 536 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 537 | llvm::Constant *EmitModuleSymbols(); |
| 538 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 539 | /// FinishModule - Write out global data structures at the end of |
| 540 | /// processing a translation unit. |
| 541 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 542 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 543 | /// EmitClassExtension - Generate the class extension structure used |
| 544 | /// to store the weak ivar layout and properties. The return value |
| 545 | /// has type ClassExtensionPtrTy. |
| 546 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 547 | |
| 548 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 549 | /// for the given class. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 550 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 551 | const ObjCInterfaceDecl *ID); |
| 552 | |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 553 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 554 | QualType ResultType, |
| 555 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 556 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 557 | QualType Arg0Ty, |
| 558 | bool IsSuper, |
| 559 | const CallArgList &CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 560 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 561 | /// EmitIvarList - Emit the ivar list for the given |
| 562 | /// implementation. If ForClass is true the list of class ivars |
| 563 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 564 | /// interface ivars will be emitted. The return value has type |
| 565 | /// IvarListPtrTy. |
| 566 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 567 | bool ForClass); |
| 568 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 569 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 570 | /// for the metaclass of the given interface. The return value has |
| 571 | /// type ClassPtrTy. |
| 572 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 573 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 574 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 575 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 576 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 577 | llvm::Constant *Protocols, |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 578 | const llvm::Type *InterfaceTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 579 | const ConstantVector &Methods); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 580 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 581 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 582 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 583 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 584 | |
| 585 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 586 | /// implementation. The return value has type MethodListPtrTy. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 587 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 588 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 589 | const ConstantVector &Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 590 | |
| 591 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 592 | /// method declarations. |
| 593 | /// - TypeName: The name for the type containing the methods. |
| 594 | /// - IsProtocol: True iff these methods are for a protocol. |
| 595 | /// - ClassMethds: True iff these are class methods. |
| 596 | /// - Required: When true, only "required" methods are |
| 597 | /// listed. Similarly, when false only "optional" methods are |
| 598 | /// listed. For classes this should always be true. |
| 599 | /// - begin, end: The method list to output. |
| 600 | /// |
| 601 | /// The return value has type MethodDescriptionListPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 602 | llvm::Constant *EmitMethodDescList(const std::string &Name, |
| 603 | const char *Section, |
| 604 | const ConstantVector &Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 605 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 606 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 607 | /// declaration, emitting it if necessary. The return value has type |
| 608 | /// ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 609 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 610 | |
| 611 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 612 | /// object for the given declaration, emitting it if needed. These |
| 613 | /// forward references will be filled in with empty bodies if no |
| 614 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 615 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 616 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 617 | /// EmitProtocolExtension - Generate the protocol extension |
| 618 | /// structure used to store optional instance and class methods, and |
| 619 | /// protocol properties. The return value has type |
| 620 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 621 | llvm::Constant * |
| 622 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 623 | const ConstantVector &OptInstanceMethods, |
| 624 | const ConstantVector &OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 625 | |
| 626 | /// EmitProtocolList - Generate the list of referenced |
| 627 | /// protocols. The return value has type ProtocolListPtrTy. |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 628 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 629 | ObjCProtocolDecl::protocol_iterator begin, |
| 630 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 631 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 632 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 633 | /// for the given selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 634 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 635 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 636 | public: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 637 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 638 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 639 | virtual llvm::Function *ModuleInitFunction(); |
| 640 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 641 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 642 | QualType ResultType, |
| 643 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 644 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 645 | bool IsClassMessage, |
| 646 | const CallArgList &CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 647 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 648 | virtual CodeGen::RValue |
| 649 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 650 | QualType ResultType, |
| 651 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 652 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 653 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 654 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 655 | bool IsClassMessage, |
| 656 | const CallArgList &CallArgs); |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 657 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 658 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 659 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 660 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 661 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 662 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 663 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 664 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 665 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 666 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 667 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 668 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 669 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 670 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 671 | virtual llvm::Constant *GetPropertySetFunction(); |
| 672 | virtual llvm::Constant *EnumerationMutationFunction(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 673 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 674 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 675 | const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 676 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 677 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 678 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 679 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 680 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 681 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 682 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 683 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 684 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 685 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 686 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 687 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 688 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 689 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 690 | QualType ObjectTy, |
| 691 | llvm::Value *BaseValue, |
| 692 | const ObjCIvarDecl *Ivar, |
| 693 | const FieldDecl *Field, |
| 694 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 695 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
| 696 | ObjCInterfaceDecl *Interface, |
| 697 | const ObjCIvarDecl *Ivar); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 698 | }; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 699 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 700 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 701 | private: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 702 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 703 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 704 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 705 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 706 | /// MetaClassReferences - uniqued meta class references. |
| 707 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 708 | |
| 709 | /// EHTypeReferences - uniqued class ehtype references. |
| 710 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 711 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 712 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 713 | /// processing a translation unit. |
| 714 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame^] | 715 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 716 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 717 | unsigned InstanceStart, |
| 718 | unsigned InstanceSize, |
| 719 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 720 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
| 721 | llvm::Constant *IsAGV, |
| 722 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 723 | llvm::Constant *ClassRoGV, |
| 724 | bool HiddenVisibility); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 725 | |
| 726 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
| 727 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 728 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
| 729 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 730 | /// EmitMethodList - Emit the method list for the given |
| 731 | /// implementation. The return value has type MethodListnfABITy. |
| 732 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 733 | const char *Section, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 734 | const ConstantVector &Methods); |
| 735 | /// EmitIvarList - Emit the ivar list for the given |
| 736 | /// implementation. If ForClass is true the list of class ivars |
| 737 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 738 | /// interface ivars will be emitted. The return value has type |
| 739 | /// IvarListnfABIPtrTy. |
| 740 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 741 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 742 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 743 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 744 | unsigned long int offset); |
| 745 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 746 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 747 | /// declaration, emitting it if necessary. The return value has type |
| 748 | /// ProtocolPtrTy. |
| 749 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
| 750 | |
| 751 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 752 | /// object for the given declaration, emitting it if needed. These |
| 753 | /// forward references will be filled in with empty bodies if no |
| 754 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 755 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
| 756 | |
| 757 | /// EmitProtocolList - Generate the list of referenced |
| 758 | /// protocols. The return value has type ProtocolListPtrTy. |
| 759 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 760 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 761 | ObjCProtocolDecl::protocol_iterator end); |
| 762 | |
| 763 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 764 | QualType ResultType, |
| 765 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 766 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 767 | QualType Arg0Ty, |
| 768 | bool IsSuper, |
| 769 | const CallArgList &CallArgs); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 770 | |
| 771 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 772 | /// class of the given name. |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 773 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name, |
| 774 | bool AddToUsed = true); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 775 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 776 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 777 | /// for the given class. |
| 778 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 779 | const ObjCInterfaceDecl *ID, |
| 780 | bool IsSuper = false); |
| 781 | |
| 782 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 783 | /// meta-data |
| 784 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
| 785 | const ObjCInterfaceDecl *ID); |
| 786 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 787 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 788 | /// the given ivar. |
| 789 | /// |
| 790 | llvm::GlobalVariable * ObjCIvarOffsetVariable(std::string &Name, |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 791 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 792 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 793 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 794 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 795 | /// for the given selector. |
| 796 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 797 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame^] | 798 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 799 | /// interface. The return value has type EHTypePtrTy. |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame^] | 800 | llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 801 | bool ForDefinition); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 802 | |
| 803 | const char *getMetaclassSymbolPrefix() const { |
| 804 | return "OBJC_METACLASS_$_"; |
| 805 | } |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 806 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 807 | const char *getClassSymbolPrefix() const { |
| 808 | return "OBJC_CLASS_$_"; |
| 809 | } |
| 810 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 811 | public: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 812 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 813 | // FIXME. All stubs for now! |
| 814 | virtual llvm::Function *ModuleInitFunction(); |
| 815 | |
| 816 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
| 817 | QualType ResultType, |
| 818 | Selector Sel, |
| 819 | llvm::Value *Receiver, |
| 820 | bool IsClassMessage, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 821 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 822 | |
| 823 | virtual CodeGen::RValue |
| 824 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 825 | QualType ResultType, |
| 826 | Selector Sel, |
| 827 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 828 | bool isCategoryImpl, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 829 | llvm::Value *Receiver, |
| 830 | bool IsClassMessage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 831 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 832 | |
| 833 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 834 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 835 | |
| 836 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel) |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 837 | { return EmitSelector(Builder, Sel); } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 838 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 839 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 840 | |
| 841 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 842 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 843 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 844 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 845 | virtual llvm::Constant *GetPropertyGetFunction() { |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 846 | return ObjCTypes.GetPropertyFn; |
| 847 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 848 | virtual llvm::Constant *GetPropertySetFunction() { |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 849 | return ObjCTypes.SetPropertyFn; |
| 850 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 851 | virtual llvm::Constant *EnumerationMutationFunction() { |
Daniel Dunbar | 28ed084 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 852 | return ObjCTypes.EnumerationMutationFn; |
| 853 | } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 854 | |
| 855 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 856 | const Stmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 857 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 858 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 859 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 860 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 861 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 862 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 863 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 864 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 865 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 866 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 867 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 868 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 869 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 870 | QualType ObjectTy, |
| 871 | llvm::Value *BaseValue, |
| 872 | const ObjCIvarDecl *Ivar, |
| 873 | const FieldDecl *Field, |
| 874 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 875 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
| 876 | ObjCInterfaceDecl *Interface, |
| 877 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 878 | }; |
| 879 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 880 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 881 | |
| 882 | /* *** Helper Functions *** */ |
| 883 | |
| 884 | /// getConstantGEP() - Help routine to construct simple GEPs. |
| 885 | static llvm::Constant *getConstantGEP(llvm::Constant *C, |
| 886 | unsigned idx0, |
| 887 | unsigned idx1) { |
| 888 | llvm::Value *Idxs[] = { |
| 889 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0), |
| 890 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1) |
| 891 | }; |
| 892 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2); |
| 893 | } |
| 894 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame^] | 895 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 896 | /// class has the __objc_exception__ attribute. |
| 897 | static bool hasObjCExceptionAttribute(const ObjCInterfaceDecl *OID) { |
| 898 | if (OID->getAttr<ObjCExceptionAttr>()) |
| 899 | return true; |
| 900 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
| 901 | return hasObjCExceptionAttribute(Super); |
| 902 | return false; |
| 903 | } |
| 904 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 905 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 906 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 907 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
| 908 | ObjCTypes(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 909 | { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 910 | ObjCABI = 1; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 911 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 914 | /// GetClass - Return a reference to the class for the given interface |
| 915 | /// decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 916 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 917 | const ObjCInterfaceDecl *ID) { |
| 918 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 922 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 923 | return EmitSelector(Builder, Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 926 | /// Generate a constant CFString object. |
| 927 | /* |
| 928 | struct __builtin_CFString { |
| 929 | const int *isa; // point to __CFConstantStringClassReference |
| 930 | int flags; |
| 931 | const char *str; |
| 932 | long length; |
| 933 | }; |
| 934 | */ |
| 935 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 936 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 937 | const ObjCStringLiteral *SL) { |
Steve Naroff | 8d4141f | 2009-04-01 13:55:36 +0000 | [diff] [blame] | 938 | return CGM.GetAddrOfConstantCFString(SL->getString()); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | /// Generates a message send where the super is the receiver. This is |
| 942 | /// a message send to self with special delivery semantics indicating |
| 943 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 944 | CodeGen::RValue |
| 945 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 946 | QualType ResultType, |
| 947 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 948 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 949 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 950 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 951 | bool IsClassMessage, |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 952 | const CodeGen::CallArgList &CallArgs) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 953 | // Create and init a super structure; this is a (receiver, class) |
| 954 | // pair we will pass to objc_msgSendSuper. |
| 955 | llvm::Value *ObjCSuper = |
| 956 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 957 | llvm::Value *ReceiverAsObject = |
| 958 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 959 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 960 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 961 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 962 | // If this is a class message the metaclass is passed as the target. |
| 963 | llvm::Value *Target; |
| 964 | if (IsClassMessage) { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 965 | if (isCategoryImpl) { |
| 966 | // Message sent to 'super' in a class method defined in a category |
| 967 | // implementation requires an odd treatment. |
| 968 | // If we are in a class method, we must retrieve the |
| 969 | // _metaclass_ for the current class, pointed at by |
| 970 | // the class's "isa" pointer. The following assumes that |
| 971 | // isa" is the first ivar in a class (which it must be). |
| 972 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 973 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 974 | Target = CGF.Builder.CreateLoad(Target); |
| 975 | } |
| 976 | else { |
| 977 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 978 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 979 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 980 | Target = Super; |
| 981 | } |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 982 | } else { |
| 983 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 984 | } |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 985 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 986 | // and ObjCTypes types. |
| 987 | const llvm::Type *ClassTy = |
| 988 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 989 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 990 | CGF.Builder.CreateStore(Target, |
| 991 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 992 | |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 993 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 994 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 995 | true, CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 996 | } |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 997 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 998 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 999 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1000 | QualType ResultType, |
| 1001 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1002 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1003 | bool IsClassMessage, |
| 1004 | const CallArgList &CallArgs) { |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1005 | llvm::Value *Arg0 = |
| 1006 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1007 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1008 | Arg0, CGF.getContext().getObjCIdType(), |
| 1009 | false, CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1013 | QualType ResultType, |
| 1014 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1015 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1016 | QualType Arg0Ty, |
| 1017 | bool IsSuper, |
| 1018 | const CallArgList &CallArgs) { |
| 1019 | CallArgList ActualArgs; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1020 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 1021 | ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder, |
| 1022 | Sel)), |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1023 | CGF.getContext().getObjCSelType())); |
| 1024 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1025 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1026 | CodeGenTypes &Types = CGM.getTypes(); |
| 1027 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); |
| 1028 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1029 | |
| 1030 | llvm::Constant *Fn; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1031 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1032 | Fn = ObjCTypes.getSendStretFn(IsSuper); |
| 1033 | } else if (ResultType->isFloatingType()) { |
| 1034 | // FIXME: Sadly, this is wrong. This actually depends on the |
| 1035 | // architecture. This happens to be right for x86-32 though. |
| 1036 | Fn = ObjCTypes.getSendFpretFn(IsSuper); |
| 1037 | } else { |
| 1038 | Fn = ObjCTypes.getSendFn(IsSuper); |
| 1039 | } |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 1040 | Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy)); |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1041 | return CGF.EmitCall(FnInfo, Fn, ActualArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1044 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1045 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1046 | // FIXME: I don't understand why gcc generates this, or where it is |
| 1047 | // resolved. Investigate. Its also wasteful to look this up over and |
| 1048 | // over. |
| 1049 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1050 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1051 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
| 1052 | ObjCTypes.ExternalProtocolPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1055 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1056 | // FIXME: We shouldn't need this, the protocol decl should contain |
| 1057 | // enough information to tell us whether this was a declaration or a |
| 1058 | // definition. |
| 1059 | DefinedProtocols.insert(PD->getIdentifier()); |
| 1060 | |
| 1061 | // If we have generated a forward reference to this protocol, emit |
| 1062 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 1063 | // emitted. |
| 1064 | if (Protocols.count(PD->getIdentifier())) |
| 1065 | GetOrEmitProtocol(PD); |
| 1066 | } |
| 1067 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1068 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1069 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 1070 | return GetOrEmitProtocol(PD); |
| 1071 | return GetOrEmitProtocolRef(PD); |
| 1072 | } |
| 1073 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1074 | /* |
| 1075 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 1076 | struct _objc_protocol { |
| 1077 | struct _objc_protocol_extension *isa; |
| 1078 | char *protocol_name; |
| 1079 | struct _objc_protocol_list *protocol_list; |
| 1080 | struct _objc__method_prototype_list *instance_methods; |
| 1081 | struct _objc__method_prototype_list *class_methods |
| 1082 | }; |
| 1083 | |
| 1084 | See EmitProtocolExtension(). |
| 1085 | */ |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1086 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
| 1087 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1088 | |
| 1089 | // Early exit if a defining object has already been generated. |
| 1090 | if (Entry && Entry->hasInitializer()) |
| 1091 | return Entry; |
| 1092 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1093 | // FIXME: I don't understand why gcc generates this, or where it is |
| 1094 | // resolved. Investigate. Its also wasteful to look this up over and |
| 1095 | // over. |
| 1096 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1097 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1098 | const char *ProtocolName = PD->getNameAsCString(); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1099 | |
| 1100 | // Construct method lists. |
| 1101 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1102 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
| 1103 | for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(), |
| 1104 | e = PD->instmeth_end(); i != e; ++i) { |
| 1105 | ObjCMethodDecl *MD = *i; |
| 1106 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1107 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1108 | OptInstanceMethods.push_back(C); |
| 1109 | } else { |
| 1110 | InstanceMethods.push_back(C); |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(), |
| 1115 | e = PD->classmeth_end(); i != e; ++i) { |
| 1116 | ObjCMethodDecl *MD = *i; |
| 1117 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1118 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1119 | OptClassMethods.push_back(C); |
| 1120 | } else { |
| 1121 | ClassMethods.push_back(C); |
| 1122 | } |
| 1123 | } |
| 1124 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1125 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1126 | Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1127 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1128 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1129 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1130 | PD->protocol_begin(), |
| 1131 | PD->protocol_end()); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1132 | Values[3] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1133 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" |
| 1134 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1135 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1136 | InstanceMethods); |
| 1137 | Values[4] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1138 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" |
| 1139 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1140 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1141 | ClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1142 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 1143 | Values); |
| 1144 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1145 | if (Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1146 | // Already created, fix the linkage and update the initializer. |
| 1147 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1148 | Entry->setInitializer(Init); |
| 1149 | } else { |
| 1150 | Entry = |
| 1151 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
| 1152 | llvm::GlobalValue::InternalLinkage, |
| 1153 | Init, |
| 1154 | std::string("\01L_OBJC_PROTOCOL_")+ProtocolName, |
| 1155 | &CGM.getModule()); |
| 1156 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1157 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1158 | UsedGlobals.push_back(Entry); |
| 1159 | // FIXME: Is this necessary? Why only for protocol? |
| 1160 | Entry->setAlignment(4); |
| 1161 | } |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1162 | |
| 1163 | return Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1166 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1167 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1168 | |
| 1169 | if (!Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1170 | // We use the initializer as a marker of whether this is a forward |
| 1171 | // reference or not. At module finalization we add the empty |
| 1172 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1173 | Entry = |
| 1174 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1175 | llvm::GlobalValue::ExternalLinkage, |
| 1176 | 0, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1177 | "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1178 | &CGM.getModule()); |
| 1179 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1180 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1181 | UsedGlobals.push_back(Entry); |
| 1182 | // FIXME: Is this necessary? Why only for protocol? |
| 1183 | Entry->setAlignment(4); |
| 1184 | } |
| 1185 | |
| 1186 | return Entry; |
| 1187 | } |
| 1188 | |
| 1189 | /* |
| 1190 | struct _objc_protocol_extension { |
| 1191 | uint32_t size; |
| 1192 | struct objc_method_description_list *optional_instance_methods; |
| 1193 | struct objc_method_description_list *optional_class_methods; |
| 1194 | struct objc_property_list *instance_properties; |
| 1195 | }; |
| 1196 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1197 | llvm::Constant * |
| 1198 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1199 | const ConstantVector &OptInstanceMethods, |
| 1200 | const ConstantVector &OptClassMethods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1201 | uint64_t Size = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1202 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1203 | std::vector<llvm::Constant*> Values(4); |
| 1204 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1205 | Values[1] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1206 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
| 1207 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1208 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1209 | OptInstanceMethods); |
| 1210 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1211 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" |
| 1212 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1213 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1214 | OptClassMethods); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1215 | Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + |
| 1216 | PD->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1217 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1218 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1219 | // Return null if no extension bits are used. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1220 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
| 1221 | Values[3]->isNullValue()) |
| 1222 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 1223 | |
| 1224 | llvm::Constant *Init = |
| 1225 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1226 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1227 | // No special section, but goes in llvm.used |
| 1228 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(), |
| 1229 | Init, |
| 1230 | 0, 0, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | /* |
| 1234 | struct objc_protocol_list { |
| 1235 | struct objc_protocol_list *next; |
| 1236 | long count; |
| 1237 | Protocol *list[]; |
| 1238 | }; |
| 1239 | */ |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1240 | llvm::Constant * |
| 1241 | CGObjCMac::EmitProtocolList(const std::string &Name, |
| 1242 | ObjCProtocolDecl::protocol_iterator begin, |
| 1243 | ObjCProtocolDecl::protocol_iterator end) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1244 | std::vector<llvm::Constant*> ProtocolRefs; |
| 1245 | |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1246 | for (; begin != end; ++begin) |
| 1247 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1248 | |
| 1249 | // Just return null for empty protocol lists |
| 1250 | if (ProtocolRefs.empty()) |
| 1251 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1252 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1253 | // This list is null terminated. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1254 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
| 1255 | |
| 1256 | std::vector<llvm::Constant*> Values(3); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1257 | // This field is only used by the runtime. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1258 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1259 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 1260 | Values[2] = |
| 1261 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 1262 | ProtocolRefs.size()), |
| 1263 | ProtocolRefs); |
| 1264 | |
| 1265 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1266 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1267 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1268 | 4, false); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1269 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
| 1270 | } |
| 1271 | |
| 1272 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1273 | struct _objc_property { |
| 1274 | const char * const name; |
| 1275 | const char * const attributes; |
| 1276 | }; |
| 1277 | |
| 1278 | struct _objc_property_list { |
| 1279 | uint32_t entsize; // sizeof (struct _objc_property) |
| 1280 | uint32_t prop_count; |
| 1281 | struct _objc_property[prop_count]; |
| 1282 | }; |
| 1283 | */ |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1284 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name, |
| 1285 | const Decl *Container, |
| 1286 | const ObjCContainerDecl *OCD, |
| 1287 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1288 | std::vector<llvm::Constant*> Properties, Prop(2); |
Steve Naroff | 93983f8 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 1289 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(), |
| 1290 | E = OCD->prop_end(); I != E; ++I) { |
| 1291 | const ObjCPropertyDecl *PD = *I; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1292 | Prop[0] = GetPropertyName(PD->getIdentifier()); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1293 | Prop[1] = GetPropertyTypeString(PD, Container); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1294 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
| 1295 | Prop)); |
| 1296 | } |
| 1297 | |
| 1298 | // Return null for empty list. |
| 1299 | if (Properties.empty()) |
| 1300 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1301 | |
| 1302 | unsigned PropertySize = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1303 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1304 | std::vector<llvm::Constant*> Values(3); |
| 1305 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 1306 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
| 1307 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
| 1308 | Properties.size()); |
| 1309 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
| 1310 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1311 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1312 | // No special section on property lists? |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1313 | llvm::GlobalVariable *GV = |
| 1314 | CreateMetadataVar(Name, Init, (ObjCABI == 2) ? "__DATA, __objc_const" : 0, |
| 1315 | 0, true); |
| 1316 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1320 | struct objc_method_description_list { |
| 1321 | int count; |
| 1322 | struct objc_method_description list[]; |
| 1323 | }; |
| 1324 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1325 | llvm::Constant * |
| 1326 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 1327 | std::vector<llvm::Constant*> Desc(2); |
| 1328 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1329 | ObjCTypes.SelectorPtrTy); |
| 1330 | Desc[1] = GetMethodVarType(MD); |
| 1331 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
| 1332 | Desc); |
| 1333 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1334 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1335 | llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name, |
| 1336 | const char *Section, |
| 1337 | const ConstantVector &Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1338 | // Return null for empty list. |
| 1339 | if (Methods.empty()) |
| 1340 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 1341 | |
| 1342 | std::vector<llvm::Constant*> Values(2); |
| 1343 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 1344 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
| 1345 | Methods.size()); |
| 1346 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
| 1347 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1348 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1349 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 0, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1350 | return llvm::ConstantExpr::getBitCast(GV, |
| 1351 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1354 | /* |
| 1355 | struct _objc_category { |
| 1356 | char *category_name; |
| 1357 | char *class_name; |
| 1358 | struct _objc_method_list *instance_methods; |
| 1359 | struct _objc_method_list *class_methods; |
| 1360 | struct _objc_protocol_list *protocols; |
| 1361 | uint32_t size; // <rdar://4585769> |
| 1362 | struct _objc_property_list *instance_properties; |
| 1363 | }; |
| 1364 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1365 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1366 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1367 | |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1368 | // FIXME: This is poor design, the OCD should have a pointer to the |
| 1369 | // category decl. Additionally, note that Category can be null for |
| 1370 | // the @implementation w/o an @interface case. Sema should just |
| 1371 | // create one for us as it does for @implementation so everyone else |
| 1372 | // can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1373 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1374 | const ObjCCategoryDecl *Category = |
| 1375 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1376 | std::string ExtName(Interface->getNameAsString() + "_" + |
| 1377 | OCD->getNameAsString()); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1378 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1379 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1380 | for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(), |
| 1381 | e = OCD->instmeth_end(); i != e; ++i) { |
| 1382 | // Instance methods should always be defined. |
| 1383 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1384 | } |
| 1385 | for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(), |
| 1386 | e = OCD->classmeth_end(); i != e; ++i) { |
| 1387 | // Class methods should always be defined. |
| 1388 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1389 | } |
| 1390 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1391 | std::vector<llvm::Constant*> Values(7); |
| 1392 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 1393 | Values[1] = GetClassName(Interface->getIdentifier()); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1394 | Values[2] = |
| 1395 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") + |
| 1396 | ExtName, |
| 1397 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1398 | InstanceMethods); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1399 | Values[3] = |
| 1400 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName, |
| 1401 | "__OBJC,__cat_class_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1402 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1403 | if (Category) { |
| 1404 | Values[4] = |
| 1405 | EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName, |
| 1406 | Category->protocol_begin(), |
| 1407 | Category->protocol_end()); |
| 1408 | } else { |
| 1409 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1410 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1411 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1412 | |
| 1413 | // If there is no category @interface then there can be no properties. |
| 1414 | if (Category) { |
| 1415 | Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName, |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1416 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1417 | } else { |
| 1418 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1419 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1420 | |
| 1421 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
| 1422 | Values); |
| 1423 | |
| 1424 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1425 | CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init, |
| 1426 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1427 | 4, true); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1428 | DefinedCategories.push_back(GV); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1431 | // FIXME: Get from somewhere? |
| 1432 | enum ClassFlags { |
| 1433 | eClassFlags_Factory = 0x00001, |
| 1434 | eClassFlags_Meta = 0x00002, |
| 1435 | // <rdr://5142207> |
| 1436 | eClassFlags_HasCXXStructors = 0x02000, |
| 1437 | eClassFlags_Hidden = 0x20000, |
| 1438 | eClassFlags_ABI2_Hidden = 0x00010, |
| 1439 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 1440 | }; |
| 1441 | |
Fariborz Jahanian | cf71dd4 | 2009-04-07 20:26:30 +0000 | [diff] [blame] | 1442 | bool CGObjCCommonMac::IsClassHidden(const ObjCInterfaceDecl *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1443 | if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1444 | switch (attr->getVisibility()) { |
| 1445 | default: |
| 1446 | assert(0 && "Unknown visibility"); |
| 1447 | return false; |
| 1448 | case VisibilityAttr::DefaultVisibility: |
| 1449 | case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here? |
| 1450 | return false; |
| 1451 | case VisibilityAttr::HiddenVisibility: |
| 1452 | return true; |
| 1453 | } |
Fariborz Jahanian | cf71dd4 | 2009-04-07 20:26:30 +0000 | [diff] [blame] | 1454 | } else |
| 1455 | return (CGM.getLangOptions().getVisibilityMode() == |
| 1456 | LangOptions::HiddenVisibility); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | /* |
| 1460 | struct _objc_class { |
| 1461 | Class isa; |
| 1462 | Class super_class; |
| 1463 | const char *name; |
| 1464 | long version; |
| 1465 | long info; |
| 1466 | long instance_size; |
| 1467 | struct _objc_ivar_list *ivars; |
| 1468 | struct _objc_method_list *methods; |
| 1469 | struct _objc_cache *cache; |
| 1470 | struct _objc_protocol_list *protocols; |
| 1471 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 1472 | const char *ivar_layout; |
| 1473 | struct _objc_class_ext *ext; |
| 1474 | }; |
| 1475 | |
| 1476 | See EmitClassExtension(); |
| 1477 | */ |
| 1478 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1479 | DefinedSymbols.insert(ID->getIdentifier()); |
| 1480 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1481 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1482 | // FIXME: Gross |
| 1483 | ObjCInterfaceDecl *Interface = |
| 1484 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1485 | llvm::Constant *Protocols = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1486 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1487 | Interface->protocol_begin(), |
| 1488 | Interface->protocol_end()); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1489 | const llvm::Type *InterfaceTy = |
Chris Lattner | 03d9f34 | 2009-04-01 06:23:52 +0000 | [diff] [blame] | 1490 | CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1491 | unsigned Flags = eClassFlags_Factory; |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1492 | unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1493 | |
| 1494 | // FIXME: Set CXX-structors flag. |
| 1495 | if (IsClassHidden(ID->getClassInterface())) |
| 1496 | Flags |= eClassFlags_Hidden; |
| 1497 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1498 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1499 | for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(), |
| 1500 | e = ID->instmeth_end(); i != e; ++i) { |
| 1501 | // Instance methods should always be defined. |
| 1502 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1503 | } |
| 1504 | for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(), |
| 1505 | e = ID->classmeth_end(); i != e; ++i) { |
| 1506 | // Class methods should always be defined. |
| 1507 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1508 | } |
| 1509 | |
| 1510 | for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(), |
| 1511 | e = ID->propimpl_end(); i != e; ++i) { |
| 1512 | ObjCPropertyImplDecl *PID = *i; |
| 1513 | |
| 1514 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 1515 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 1516 | |
| 1517 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 1518 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1519 | InstanceMethods.push_back(C); |
| 1520 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 1521 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1522 | InstanceMethods.push_back(C); |
| 1523 | } |
| 1524 | } |
| 1525 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1526 | std::vector<llvm::Constant*> Values(12); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1527 | Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1528 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1529 | // Record a reference to the super class. |
| 1530 | LazySymbols.insert(Super->getIdentifier()); |
| 1531 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1532 | Values[ 1] = |
| 1533 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1534 | ObjCTypes.ClassPtrTy); |
| 1535 | } else { |
| 1536 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1537 | } |
| 1538 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1539 | // Version is always 0. |
| 1540 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1541 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1542 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1543 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1544 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1545 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1546 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1547 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1548 | // cache is always NULL. |
| 1549 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1550 | Values[ 9] = Protocols; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1551 | // FIXME: Set ivar_layout |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 1552 | // Values[10] = BuildIvarLayout(ID, true); |
| 1553 | Values[10] = GetIvarLayoutName(0, ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1554 | Values[11] = EmitClassExtension(ID); |
| 1555 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1556 | Values); |
| 1557 | |
| 1558 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1559 | CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init, |
| 1560 | "__OBJC,__class,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1561 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1562 | DefinedClasses.push_back(GV); |
| 1563 | } |
| 1564 | |
| 1565 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1566 | llvm::Constant *Protocols, |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1567 | const llvm::Type *InterfaceTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1568 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1569 | unsigned Flags = eClassFlags_Meta; |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1570 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1571 | |
| 1572 | if (IsClassHidden(ID->getClassInterface())) |
| 1573 | Flags |= eClassFlags_Hidden; |
| 1574 | |
| 1575 | std::vector<llvm::Constant*> Values(12); |
| 1576 | // The isa for the metaclass is the root of the hierarchy. |
| 1577 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 1578 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 1579 | Root = Super; |
| 1580 | Values[ 0] = |
| 1581 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
| 1582 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1583 | // The super class for the metaclass is emitted as the name of the |
| 1584 | // super class. The runtime fixes this up to point to the |
| 1585 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1586 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
| 1587 | Values[ 1] = |
| 1588 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1589 | ObjCTypes.ClassPtrTy); |
| 1590 | } else { |
| 1591 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1592 | } |
| 1593 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1594 | // Version is always 0. |
| 1595 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1596 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1597 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1598 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1599 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1600 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1601 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1602 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1603 | // cache is always NULL. |
| 1604 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1605 | Values[ 9] = Protocols; |
| 1606 | // ivar_layout for metaclass is always NULL. |
| 1607 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 1608 | // The class extension is always unused for metaclasses. |
| 1609 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 1610 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1611 | Values); |
| 1612 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1613 | std::string Name("\01L_OBJC_METACLASS_"); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1614 | Name += ID->getNameAsCString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1615 | |
| 1616 | // Check for a forward reference. |
| 1617 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 1618 | if (GV) { |
| 1619 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 1620 | "Forward metaclass reference has incorrect type."); |
| 1621 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 1622 | GV->setInitializer(Init); |
| 1623 | } else { |
| 1624 | GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1625 | llvm::GlobalValue::InternalLinkage, |
| 1626 | Init, Name, |
| 1627 | &CGM.getModule()); |
| 1628 | } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1629 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1630 | GV->setAlignment(4); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1631 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1632 | |
| 1633 | return GV; |
| 1634 | } |
| 1635 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1636 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1637 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1638 | |
| 1639 | // FIXME: Should we look these up somewhere other than the |
| 1640 | // module. Its a bit silly since we only generate these while |
| 1641 | // processing an implementation, so exactly one pointer would work |
| 1642 | // if know when we entered/exitted an implementation block. |
| 1643 | |
| 1644 | // Check for an existing forward reference. |
Fariborz Jahanian | b0d2794 | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 1645 | // Previously, metaclass with internal linkage may have been defined. |
| 1646 | // pass 'true' as 2nd argument so it is returned. |
| 1647 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) { |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1648 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 1649 | "Forward metaclass reference has incorrect type."); |
| 1650 | return GV; |
| 1651 | } else { |
| 1652 | // Generate as an external reference to keep a consistent |
| 1653 | // module. This will be patched up when we emit the metaclass. |
| 1654 | return new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1655 | llvm::GlobalValue::ExternalLinkage, |
| 1656 | 0, |
| 1657 | Name, |
| 1658 | &CGM.getModule()); |
| 1659 | } |
| 1660 | } |
| 1661 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1662 | /* |
| 1663 | struct objc_class_ext { |
| 1664 | uint32_t size; |
| 1665 | const char *weak_ivar_layout; |
| 1666 | struct _objc_property_list *properties; |
| 1667 | }; |
| 1668 | */ |
| 1669 | llvm::Constant * |
| 1670 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
| 1671 | uint64_t Size = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1672 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1673 | |
| 1674 | std::vector<llvm::Constant*> Values(3); |
| 1675 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1676 | // FIXME: Output weak_ivar_layout string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 1677 | // Values[1] = BuildIvarLayout(ID, false); |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 1678 | Values[1] = GetIvarLayoutName(0, ObjCTypes); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1679 | Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1680 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1681 | |
| 1682 | // Return null if no extension bits are used. |
| 1683 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
| 1684 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 1685 | |
| 1686 | llvm::Constant *Init = |
| 1687 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1688 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(), |
| 1689 | Init, 0, 0, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
Fariborz Jahanian | f54b194 | 2009-01-17 19:36:33 +0000 | [diff] [blame] | 1692 | /// countInheritedIvars - count number of ivars in class and its super class(s) |
| 1693 | /// |
| 1694 | static int countInheritedIvars(const ObjCInterfaceDecl *OI) { |
| 1695 | int count = 0; |
| 1696 | if (!OI) |
| 1697 | return 0; |
| 1698 | const ObjCInterfaceDecl *SuperClass = OI->getSuperClass(); |
| 1699 | if (SuperClass) |
| 1700 | count += countInheritedIvars(SuperClass); |
| 1701 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1702 | E = OI->ivar_end(); I != E; ++I) |
| 1703 | ++count; |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 1704 | // look into properties. |
| 1705 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(), |
| 1706 | E = OI->prop_end(); I != E; ++I) { |
| 1707 | if ((*I)->getPropertyIvarDecl()) |
| 1708 | ++count; |
| 1709 | } |
Fariborz Jahanian | f54b194 | 2009-01-17 19:36:33 +0000 | [diff] [blame] | 1710 | return count; |
| 1711 | } |
| 1712 | |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1713 | /// getInterfaceDeclForIvar - Get the interface declaration node where |
| 1714 | /// this ivar is declared in. |
| 1715 | /// FIXME. Ideally, this info should be in the ivar node. But currently |
| 1716 | /// it is not and prevailing wisdom is that ASTs should not have more |
| 1717 | /// info than is absolutely needed, even though this info reflects the |
| 1718 | /// source language. |
| 1719 | /// |
| 1720 | static const ObjCInterfaceDecl *getInterfaceDeclForIvar( |
| 1721 | const ObjCInterfaceDecl *OI, |
| 1722 | const ObjCIvarDecl *IVD) { |
| 1723 | if (!OI) |
| 1724 | return 0; |
| 1725 | assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface"); |
| 1726 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1727 | E = OI->ivar_end(); I != E; ++I) |
| 1728 | if ((*I)->getIdentifier() == IVD->getIdentifier()) |
| 1729 | return OI; |
Fariborz Jahanian | 5a4b453 | 2009-03-31 17:00:52 +0000 | [diff] [blame] | 1730 | // look into properties. |
| 1731 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(), |
| 1732 | E = OI->prop_end(); I != E; ++I) { |
| 1733 | ObjCPropertyDecl *PDecl = (*I); |
| 1734 | if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl()) |
| 1735 | if (IV->getIdentifier() == IVD->getIdentifier()) |
| 1736 | return OI; |
| 1737 | } |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1738 | return getInterfaceDeclForIvar(OI->getSuperClass(), IVD); |
| 1739 | } |
| 1740 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1741 | /* |
| 1742 | struct objc_ivar { |
| 1743 | char *ivar_name; |
| 1744 | char *ivar_type; |
| 1745 | int ivar_offset; |
| 1746 | }; |
| 1747 | |
| 1748 | struct objc_ivar_list { |
| 1749 | int ivar_count; |
| 1750 | struct objc_ivar list[count]; |
| 1751 | }; |
| 1752 | */ |
| 1753 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1754 | bool ForClass) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1755 | std::vector<llvm::Constant*> Ivars, Ivar(3); |
| 1756 | |
| 1757 | // When emitting the root class GCC emits ivar entries for the |
| 1758 | // actual class structure. It is not clear if we need to follow this |
| 1759 | // behavior; for now lets try and get away with not doing it. If so, |
| 1760 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 1761 | // for the class. |
| 1762 | if (ForClass) |
| 1763 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1764 | |
| 1765 | ObjCInterfaceDecl *OID = |
| 1766 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 1767 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1768 | |
| 1769 | RecordDecl::field_iterator ifield, pfield; |
| 1770 | const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield); |
Fariborz Jahanian | f54b194 | 2009-01-17 19:36:33 +0000 | [diff] [blame] | 1771 | for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) { |
| 1772 | FieldDecl *Field = *ifield; |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 1773 | uint64_t Offset = GetIvarBaseOffset(Layout, Field); |
Fariborz Jahanian | f54b194 | 2009-01-17 19:36:33 +0000 | [diff] [blame] | 1774 | if (Field->getIdentifier()) |
| 1775 | Ivar[0] = GetMethodVarName(Field->getIdentifier()); |
| 1776 | else |
| 1777 | Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 1778 | Ivar[1] = GetMethodVarType(Field); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1779 | Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1780 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1781 | } |
| 1782 | |
| 1783 | // Return null for empty list. |
| 1784 | if (Ivars.empty()) |
| 1785 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
| 1786 | |
| 1787 | std::vector<llvm::Constant*> Values(2); |
| 1788 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 1789 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
| 1790 | Ivars.size()); |
| 1791 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
| 1792 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1793 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1794 | llvm::GlobalVariable *GV; |
| 1795 | if (ForClass) |
| 1796 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(), |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1797 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
| 1798 | 4, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1799 | else |
| 1800 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" |
| 1801 | + ID->getNameAsString(), |
| 1802 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
| 1803 | 0, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1804 | return llvm::ConstantExpr::getBitCast(GV, |
| 1805 | ObjCTypes.IvarListPtrTy); |
| 1806 | } |
| 1807 | |
| 1808 | /* |
| 1809 | struct objc_method { |
| 1810 | SEL method_name; |
| 1811 | char *method_types; |
| 1812 | void *method; |
| 1813 | }; |
| 1814 | |
| 1815 | struct objc_method_list { |
| 1816 | struct objc_method_list *obsolete; |
| 1817 | int count; |
| 1818 | struct objc_method methods_list[count]; |
| 1819 | }; |
| 1820 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1821 | |
| 1822 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 1823 | /// given method if it has been defined. The result is null if the |
| 1824 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1825 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1826 | // FIXME: Use DenseMap::lookup |
| 1827 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 1828 | if (!Fn) |
| 1829 | return 0; |
| 1830 | |
| 1831 | std::vector<llvm::Constant*> Method(3); |
| 1832 | Method[0] = |
| 1833 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1834 | ObjCTypes.SelectorPtrTy); |
| 1835 | Method[1] = GetMethodVarType(MD); |
| 1836 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 1837 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 1838 | } |
| 1839 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1840 | llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name, |
| 1841 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1842 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1843 | // Return null for empty list. |
| 1844 | if (Methods.empty()) |
| 1845 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
| 1846 | |
| 1847 | std::vector<llvm::Constant*> Values(3); |
| 1848 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 1849 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 1850 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 1851 | Methods.size()); |
| 1852 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 1853 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1854 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1855 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 0, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1856 | return llvm::ConstantExpr::getBitCast(GV, |
| 1857 | ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1860 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 1861 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1862 | std::string Name; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 1863 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1864 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1865 | CodeGenTypes &Types = CGM.getTypes(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1866 | const llvm::FunctionType *MethodTy = |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1867 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1868 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1869 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1870 | llvm::GlobalValue::InternalLinkage, |
| 1871 | Name, |
| 1872 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1873 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1874 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1875 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 1878 | uint64_t CGObjCCommonMac::GetIvarBaseOffset(const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 1879 | const FieldDecl *Field) { |
Fariborz Jahanian | a6681ae | 2009-03-09 20:44:22 +0000 | [diff] [blame] | 1880 | if (!Field->isBitField()) |
| 1881 | return Layout->getElementOffset( |
| 1882 | CGM.getTypes().getLLVMFieldNo(Field)); |
| 1883 | // FIXME. Must be a better way of getting a bitfield base offset. |
| 1884 | uint64_t offset = CGM.getTypes().getLLVMFieldNo(Field); |
| 1885 | const llvm::Type *Ty = CGM.getTypes().ConvertTypeForMemRecursive(Field->getType()); |
| 1886 | uint64_t size = CGM.getTypes().getTargetData().getTypePaddedSizeInBits(Ty); |
| 1887 | offset = (offset*size)/8; |
| 1888 | return offset; |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 1891 | /// GetFieldBaseOffset - return's field byt offset. |
| 1892 | uint64_t CGObjCCommonMac::GetFieldBaseOffset(const ObjCInterfaceDecl *OI, |
| 1893 | const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 1894 | const FieldDecl *Field) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 1895 | const ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field); |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 1896 | const FieldDecl *FD = OI->lookupFieldDeclForIvar(CGM.getContext(), Ivar); |
| 1897 | return GetIvarBaseOffset(Layout, FD); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1900 | llvm::GlobalVariable * |
| 1901 | CGObjCCommonMac::CreateMetadataVar(const std::string &Name, |
| 1902 | llvm::Constant *Init, |
| 1903 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 1904 | unsigned Align, |
| 1905 | bool AddToUsed) { |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1906 | const llvm::Type *Ty = Init->getType(); |
| 1907 | llvm::GlobalVariable *GV = |
| 1908 | new llvm::GlobalVariable(Ty, false, |
| 1909 | llvm::GlobalValue::InternalLinkage, |
| 1910 | Init, |
| 1911 | Name, |
| 1912 | &CGM.getModule()); |
| 1913 | if (Section) |
| 1914 | GV->setSection(Section); |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 1915 | if (Align) |
| 1916 | GV->setAlignment(Align); |
| 1917 | if (AddToUsed) |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1918 | UsedGlobals.push_back(GV); |
| 1919 | return GV; |
| 1920 | } |
| 1921 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1922 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1923 | // Abuse this interface function as a place to finalize. |
| 1924 | FinishModule(); |
| 1925 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1926 | return NULL; |
| 1927 | } |
| 1928 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1929 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 1930 | return ObjCTypes.GetPropertyFn; |
| 1931 | } |
| 1932 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1933 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 1934 | return ObjCTypes.SetPropertyFn; |
| 1935 | } |
| 1936 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1937 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1938 | return ObjCTypes.EnumerationMutationFn; |
| 1939 | } |
| 1940 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1941 | /* |
| 1942 | |
| 1943 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 1944 | -- |
| 1945 | |
| 1946 | The basic framework for a @try-catch-finally is as follows: |
| 1947 | { |
| 1948 | objc_exception_data d; |
| 1949 | id _rethrow = null; |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 1950 | bool _call_try_exit = true; |
| 1951 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1952 | objc_exception_try_enter(&d); |
| 1953 | if (!setjmp(d.jmp_buf)) { |
| 1954 | ... try body ... |
| 1955 | } else { |
| 1956 | // exception path |
| 1957 | id _caught = objc_exception_extract(&d); |
| 1958 | |
| 1959 | // enter new try scope for handlers |
| 1960 | if (!setjmp(d.jmp_buf)) { |
| 1961 | ... match exception and execute catch blocks ... |
| 1962 | |
| 1963 | // fell off end, rethrow. |
| 1964 | _rethrow = _caught; |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1965 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1966 | } else { |
| 1967 | // exception in catch block |
| 1968 | _rethrow = objc_exception_extract(&d); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 1969 | _call_try_exit = false; |
| 1970 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1971 | } |
| 1972 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1973 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1974 | |
| 1975 | finally: |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 1976 | if (_call_try_exit) |
| 1977 | objc_exception_try_exit(&d); |
| 1978 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1979 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1980 | ... dispatch to finally destination ... |
| 1981 | |
| 1982 | finally_rethrow: |
| 1983 | objc_exception_throw(_rethrow); |
| 1984 | |
| 1985 | finally_end: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | This framework differs slightly from the one gcc uses, in that gcc |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1989 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 1990 | and if the object should be rethrown. This breaks in the face of |
| 1991 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1992 | |
| 1993 | We specialize this framework for a few particular circumstances: |
| 1994 | |
| 1995 | - If there are no catch blocks, then we avoid emitting the second |
| 1996 | exception handling context. |
| 1997 | |
| 1998 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 1999 | e)) we avoid emitting the code to rethrow an uncaught exception. |
| 2000 | |
| 2001 | - FIXME: If there is no @finally block we can do a few more |
| 2002 | simplifications. |
| 2003 | |
| 2004 | Rethrows and Jumps-Through-Finally |
| 2005 | -- |
| 2006 | |
| 2007 | Support for implicit rethrows and jumping through the finally block is |
| 2008 | handled by storing the current exception-handling context in |
| 2009 | ObjCEHStack. |
| 2010 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2011 | In order to implement proper @finally semantics, we support one basic |
| 2012 | mechanism for jumping through the finally block to an arbitrary |
| 2013 | destination. Constructs which generate exits from a @try or @catch |
| 2014 | block use this mechanism to implement the proper semantics by chaining |
| 2015 | jumps, as necessary. |
| 2016 | |
| 2017 | This mechanism works like the one used for indirect goto: we |
| 2018 | arbitrarily assign an ID to each destination and store the ID for the |
| 2019 | destination in a variable prior to entering the finally block. At the |
| 2020 | end of the finally block we simply create a switch to the proper |
| 2021 | destination. |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2022 | |
| 2023 | Code gen for @synchronized(expr) stmt; |
| 2024 | Effectively generating code for: |
| 2025 | objc_sync_enter(expr); |
| 2026 | @try stmt @finally { objc_sync_exit(expr); } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2027 | */ |
| 2028 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2029 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 2030 | const Stmt &S) { |
| 2031 | bool isTry = isa<ObjCAtTryStmt>(S); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2032 | // Create various blocks we refer to for handling @finally. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2033 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2034 | llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit"); |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2035 | llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit"); |
| 2036 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
| 2037 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2038 | |
| 2039 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 2040 | // evaluation of the expression must occur before we enter the |
| 2041 | // @synchronized. We can safely avoid a temp here because jumps into |
| 2042 | // @synchronized are illegal & this will dominate uses. |
| 2043 | llvm::Value *SyncArg = 0; |
| 2044 | if (!isTry) { |
| 2045 | SyncArg = |
| 2046 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 2047 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 2048 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2049 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2050 | |
| 2051 | // Push an EH context entry, used for handling rethrows and jumps |
| 2052 | // through finally. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2053 | CGF.PushCleanupBlock(FinallyBlock); |
| 2054 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2055 | CGF.ObjCEHValueStack.push_back(0); |
| 2056 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2057 | // Allocate memory for the exception data and rethrow pointer. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2058 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 2059 | "exceptiondata.ptr"); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2060 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy, |
| 2061 | "_rethrow"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2062 | llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty, |
| 2063 | "_call_try_exit"); |
| 2064 | CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr); |
| 2065 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2066 | // Enter a new try block and call setjmp. |
| 2067 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData); |
| 2068 | llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0, |
| 2069 | "jmpbufarray"); |
| 2070 | JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp"); |
| 2071 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn, |
| 2072 | JmpBufPtr, "result"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2073 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2074 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 2075 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2076 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2077 | TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2078 | |
| 2079 | // Emit the @try block. |
| 2080 | CGF.EmitBlock(TryBlock); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2081 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 2082 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2083 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2084 | |
| 2085 | // Emit the "exception in @try" block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2086 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2087 | |
| 2088 | // Retrieve the exception object. We may emit multiple blocks but |
| 2089 | // nothing can cross this so the value is already in SSA form. |
| 2090 | llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn, |
| 2091 | ExceptionData, |
| 2092 | "caught"); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2093 | CGF.ObjCEHValueStack.back() = Caught; |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2094 | if (!isTry) |
| 2095 | { |
| 2096 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2097 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2098 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2099 | } |
| 2100 | else if (const ObjCAtCatchStmt* CatchStmt = |
| 2101 | cast<ObjCAtTryStmt>(S).getCatchStmts()) |
| 2102 | { |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2103 | // Enter a new exception try block (in case a @catch block throws |
| 2104 | // an exception). |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2105 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2106 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2107 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn, |
| 2108 | JmpBufPtr, "result"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2109 | llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2110 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2111 | llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch"); |
| 2112 | llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler"); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2113 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2114 | |
| 2115 | CGF.EmitBlock(CatchBlock); |
| 2116 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2117 | // Handle catch list. As a special case we check if everything is |
| 2118 | // matched and avoid generating code for falling off the end if |
| 2119 | // so. |
| 2120 | bool AllMatched = false; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2121 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2122 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2123 | |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2124 | const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2125 | const PointerType *PT = 0; |
| 2126 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2127 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2128 | if (!CatchParam) { |
| 2129 | AllMatched = true; |
| 2130 | } else { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2131 | PT = CatchParam->getType()->getAsPointerType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2132 | |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 2133 | // catch(id e) always matches. |
| 2134 | // FIXME: For the time being we also match id<X>; this should |
| 2135 | // be rejected by Sema instead. |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2136 | if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) || |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2137 | CatchParam->getType()->isObjCQualifiedIdType()) |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2138 | AllMatched = true; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2141 | if (AllMatched) { |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2142 | if (CatchParam) { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2143 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2144 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2145 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2146 | } |
Anders Carlsson | 1452f55 | 2008-09-11 08:21:54 +0000 | [diff] [blame] | 2147 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2148 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2149 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2150 | break; |
| 2151 | } |
| 2152 | |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2153 | assert(PT && "Unexpected non-pointer type in @catch"); |
| 2154 | QualType T = PT->getPointeeType(); |
Anders Carlsson | 4b7ff6e | 2008-09-11 06:35:14 +0000 | [diff] [blame] | 2155 | const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2156 | assert(ObjCType && "Catch parameter must have Objective-C type!"); |
| 2157 | |
| 2158 | // Check if the @catch block matches the exception object. |
| 2159 | llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl()); |
| 2160 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2161 | llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn, |
| 2162 | Class, Caught, "match"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2163 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2164 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2165 | |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2166 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2167 | MatchedBlock, NextCatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2168 | |
| 2169 | // Emit the @catch block. |
| 2170 | CGF.EmitBlock(MatchedBlock); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2171 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2172 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2173 | |
| 2174 | llvm::Value *Tmp = |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2175 | CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()), |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2176 | "tmp"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2177 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2178 | |
| 2179 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2180 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2181 | |
| 2182 | CGF.EmitBlock(NextCatchBlock); |
| 2183 | } |
| 2184 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2185 | if (!AllMatched) { |
| 2186 | // None of the handlers caught the exception, so store it to be |
| 2187 | // rethrown at the end of the @finally block. |
| 2188 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2189 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2190 | } |
| 2191 | |
| 2192 | // Emit the exception handler for the @catch blocks. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2193 | CGF.EmitBlock(CatchHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2194 | CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn, |
| 2195 | ExceptionData), |
| 2196 | RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2197 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2198 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2199 | } else { |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2200 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2201 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2202 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2203 | } |
| 2204 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2205 | // Pop the exception-handling stack entry. It is important to do |
| 2206 | // this now, because the code in the @finally block is not in this |
| 2207 | // context. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2208 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 2209 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2210 | CGF.ObjCEHValueStack.pop_back(); |
| 2211 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2212 | // Emit the @finally block. |
| 2213 | CGF.EmitBlock(FinallyBlock); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2214 | llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp"); |
| 2215 | |
| 2216 | CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit); |
| 2217 | |
| 2218 | CGF.EmitBlock(FinallyExit); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2219 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2220 | |
| 2221 | CGF.EmitBlock(FinallyNoExit); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2222 | if (isTry) { |
| 2223 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 2224 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 2225 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2226 | } else { |
| 2227 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 2228 | // @synchronized. |
| 2229 | CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg); |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 2230 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2231 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2232 | // Emit the switch block |
| 2233 | if (Info.SwitchBlock) |
| 2234 | CGF.EmitBlock(Info.SwitchBlock); |
| 2235 | if (Info.EndBlock) |
| 2236 | CGF.EmitBlock(Info.EndBlock); |
| 2237 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2238 | CGF.EmitBlock(FinallyRethrow); |
| 2239 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, |
| 2240 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2241 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2242 | |
| 2243 | CGF.EmitBlock(FinallyEnd); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2247 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2248 | llvm::Value *ExceptionAsObject; |
| 2249 | |
| 2250 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 2251 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
| 2252 | ExceptionAsObject = |
| 2253 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 2254 | } else { |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2255 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2256 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2257 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2258 | } |
| 2259 | |
| 2260 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2261 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2262 | |
| 2263 | // Clear the insertion point to indicate we are in unreachable code. |
| 2264 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2267 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2268 | /// object: objc_read_weak (id *src) |
| 2269 | /// |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2270 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2271 | llvm::Value *AddrWeakObj) |
| 2272 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2273 | const llvm::Type* DestTy = |
| 2274 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2275 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2276 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn, |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2277 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2278 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2279 | return read_weak; |
| 2280 | } |
| 2281 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2282 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 2283 | /// objc_assign_weak (id src, id *dst) |
| 2284 | /// |
| 2285 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 2286 | llvm::Value *src, llvm::Value *dst) |
| 2287 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2288 | const llvm::Type * SrcTy = src->getType(); |
| 2289 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2290 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2291 | assert(Size <= 8 && "does not support size > 8"); |
| 2292 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2293 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2294 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2295 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2296 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2297 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2298 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn, |
| 2299 | src, dst, "weakassign"); |
| 2300 | return; |
| 2301 | } |
| 2302 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2303 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 2304 | /// objc_assign_global (id src, id *dst) |
| 2305 | /// |
| 2306 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 2307 | llvm::Value *src, llvm::Value *dst) |
| 2308 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2309 | const llvm::Type * SrcTy = src->getType(); |
| 2310 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2311 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2312 | assert(Size <= 8 && "does not support size > 8"); |
| 2313 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2314 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2315 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2316 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2317 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2318 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2319 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn, |
| 2320 | src, dst, "globalassign"); |
| 2321 | return; |
| 2322 | } |
| 2323 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2324 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 2325 | /// objc_assign_ivar (id src, id *dst) |
| 2326 | /// |
| 2327 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 2328 | llvm::Value *src, llvm::Value *dst) |
| 2329 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2330 | const llvm::Type * SrcTy = src->getType(); |
| 2331 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2332 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2333 | assert(Size <= 8 && "does not support size > 8"); |
| 2334 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2335 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2336 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2337 | } |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2338 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2339 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 2340 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn, |
| 2341 | src, dst, "assignivar"); |
| 2342 | return; |
| 2343 | } |
| 2344 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2345 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 2346 | /// objc_assign_strongCast (id src, id *dst) |
| 2347 | /// |
| 2348 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 2349 | llvm::Value *src, llvm::Value *dst) |
| 2350 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2351 | const llvm::Type * SrcTy = src->getType(); |
| 2352 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2353 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2354 | assert(Size <= 8 && "does not support size > 8"); |
| 2355 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2356 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2357 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2358 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2359 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2360 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2361 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn, |
| 2362 | src, dst, "weakassign"); |
| 2363 | return; |
| 2364 | } |
| 2365 | |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2366 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 2367 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2368 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 2369 | QualType ObjectTy, |
| 2370 | llvm::Value *BaseValue, |
| 2371 | const ObjCIvarDecl *Ivar, |
| 2372 | const FieldDecl *Field, |
| 2373 | unsigned CVRQualifiers) { |
| 2374 | if (Ivar->isBitField()) |
| 2375 | return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field), |
| 2376 | CVRQualifiers); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2377 | // TODO: Add a special case for isa (index 0) |
| 2378 | unsigned Index = CGM.getTypes().getLLVMFieldNo(Field); |
| 2379 | llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp"); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2380 | LValue LV = LValue::MakeAddr(V, |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 2381 | Ivar->getType().getCVRQualifiers()|CVRQualifiers, |
| 2382 | CGM.getContext().getObjCGCAttrKind(Ivar->getType())); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2383 | LValue::SetObjCIvar(LV, true); |
| 2384 | return LV; |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2385 | } |
| 2386 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2387 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
| 2388 | ObjCInterfaceDecl *Interface, |
| 2389 | const ObjCIvarDecl *Ivar) { |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 2390 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(Interface); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2391 | FieldDecl *Field = Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 2392 | uint64_t Offset = GetIvarBaseOffset(Layout, Field); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2393 | return llvm::ConstantInt::get( |
| 2394 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 2395 | Offset); |
| 2396 | } |
| 2397 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2398 | /* *** Private Interface *** */ |
| 2399 | |
| 2400 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 2401 | /// level information. |
| 2402 | /// |
| 2403 | /// See: <rdr://4810609&4810587&4810587> |
| 2404 | /// struct IMAGE_INFO { |
| 2405 | /// unsigned version; |
| 2406 | /// unsigned flags; |
| 2407 | /// }; |
| 2408 | enum ImageInfoFlags { |
| 2409 | eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies |
| 2410 | eImageInfo_GarbageCollected = (1 << 1), |
| 2411 | eImageInfo_GCOnly = (1 << 2) |
| 2412 | }; |
| 2413 | |
| 2414 | void CGObjCMac::EmitImageInfo() { |
| 2415 | unsigned version = 0; // Version is unused? |
| 2416 | unsigned flags = 0; |
| 2417 | |
| 2418 | // FIXME: Fix and continue? |
| 2419 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 2420 | flags |= eImageInfo_GarbageCollected; |
| 2421 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 2422 | flags |= eImageInfo_GCOnly; |
| 2423 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2424 | // Emitted as int[2]; |
| 2425 | llvm::Constant *values[2] = { |
| 2426 | llvm::ConstantInt::get(llvm::Type::Int32Ty, version), |
| 2427 | llvm::ConstantInt::get(llvm::Type::Int32Ty, flags) |
| 2428 | }; |
| 2429 | llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2430 | |
| 2431 | const char *Section; |
| 2432 | if (ObjCABI == 1) |
| 2433 | Section = "__OBJC, __image_info,regular"; |
| 2434 | else |
| 2435 | Section = "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2436 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2437 | CreateMetadataVar("\01L_OBJC_IMAGE_INFO", |
| 2438 | llvm::ConstantArray::get(AT, values, 2), |
| 2439 | Section, |
| 2440 | 0, |
| 2441 | true); |
| 2442 | GV->setConstant(true); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2443 | } |
| 2444 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2445 | |
| 2446 | // struct objc_module { |
| 2447 | // unsigned long version; |
| 2448 | // unsigned long size; |
| 2449 | // const char *name; |
| 2450 | // Symtab symtab; |
| 2451 | // }; |
| 2452 | |
| 2453 | // FIXME: Get from somewhere |
| 2454 | static const int ModuleVersion = 7; |
| 2455 | |
| 2456 | void CGObjCMac::EmitModuleInfo() { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 2457 | uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2458 | |
| 2459 | std::vector<llvm::Constant*> Values(4); |
| 2460 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion); |
| 2461 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2462 | // This used to be the filename, now it is unused. <rdr://4327263> |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2463 | Values[2] = GetClassName(&CGM.getContext().Idents.get("")); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2464 | Values[3] = EmitModuleSymbols(); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2465 | CreateMetadataVar("\01L_OBJC_MODULES", |
| 2466 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
| 2467 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2468 | 4, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2469 | } |
| 2470 | |
| 2471 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2472 | unsigned NumClasses = DefinedClasses.size(); |
| 2473 | unsigned NumCategories = DefinedCategories.size(); |
| 2474 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2475 | // Return null if no symbols were defined. |
| 2476 | if (!NumClasses && !NumCategories) |
| 2477 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
| 2478 | |
| 2479 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2480 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2481 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
| 2482 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 2483 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
| 2484 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2485 | // The runtime expects exactly the list of defined classes followed |
| 2486 | // by the list of defined categories, in a single array. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2487 | std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2488 | for (unsigned i=0; i<NumClasses; i++) |
| 2489 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 2490 | ObjCTypes.Int8PtrTy); |
| 2491 | for (unsigned i=0; i<NumCategories; i++) |
| 2492 | Symbols[NumClasses + i] = |
| 2493 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 2494 | ObjCTypes.Int8PtrTy); |
| 2495 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2496 | Values[4] = |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2497 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2498 | NumClasses + NumCategories), |
| 2499 | Symbols); |
| 2500 | |
| 2501 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2502 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2503 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2504 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 2505 | "__OBJC,__symbols,regular,no_dead_strip", |
| 2506 | 0, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2507 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
| 2508 | } |
| 2509 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2510 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2511 | const ObjCInterfaceDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2512 | LazySymbols.insert(ID->getIdentifier()); |
| 2513 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2514 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 2515 | |
| 2516 | if (!Entry) { |
| 2517 | llvm::Constant *Casted = |
| 2518 | llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()), |
| 2519 | ObjCTypes.ClassPtrTy); |
| 2520 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2521 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 2522 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
| 2523 | 0, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
| 2526 | return Builder.CreateLoad(Entry, false, "tmp"); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2527 | } |
| 2528 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2529 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2530 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 2531 | |
| 2532 | if (!Entry) { |
| 2533 | llvm::Constant *Casted = |
| 2534 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 2535 | ObjCTypes.SelectorPtrTy); |
| 2536 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2537 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 2538 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
| 2539 | 0, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2540 | } |
| 2541 | |
| 2542 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 2543 | } |
| 2544 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 2545 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2546 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2547 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2548 | if (!Entry) |
| 2549 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 2550 | llvm::ConstantArray::get(Ident->getName()), |
| 2551 | "__TEXT,__cstring,cstring_literals", |
| 2552 | 0, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2553 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2554 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 2557 | /// GetInterfaceDeclStructLayout - Get layout for ivars of given |
| 2558 | /// interface declaration. |
| 2559 | const llvm::StructLayout *CGObjCCommonMac::GetInterfaceDeclStructLayout( |
| 2560 | const ObjCInterfaceDecl *OID) const { |
| 2561 | const llvm::Type *InterfaceTy = |
| 2562 | CGM.getTypes().ConvertType( |
| 2563 | CGM.getContext().getObjCInterfaceType( |
| 2564 | const_cast<ObjCInterfaceDecl*>(OID))); |
| 2565 | const llvm::StructLayout *Layout = |
| 2566 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy)); |
| 2567 | return Layout; |
| 2568 | } |
| 2569 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 2570 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 2571 | /// ivar layout bitmap. |
| 2572 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
| 2573 | const ObjCCommonTypesHelper &ObjCTypes) { |
| 2574 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 2575 | } |
| 2576 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2577 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCInterfaceDecl *OI, |
| 2578 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2579 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2580 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2581 | unsigned int BytePos, bool ForStrongLayout, |
| 2582 | int &Index, int &SkIndex, bool &HasUnion) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2583 | bool IsUnion = (RD && RD->isUnion()); |
| 2584 | uint64_t MaxUnionIvarSize = 0; |
| 2585 | uint64_t MaxSkippedUnionIvarSize = 0; |
| 2586 | FieldDecl *MaxField = 0; |
| 2587 | FieldDecl *MaxSkippedField = 0; |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2588 | unsigned base = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2589 | if (RecFields.empty()) |
| 2590 | return; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2591 | if (IsUnion) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2592 | base = BytePos + GetFieldBaseOffset(OI, Layout, RecFields[0]); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2593 | unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0); |
| 2594 | unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth(); |
| 2595 | |
| 2596 | llvm::SmallVector<FieldDecl*, 16> TmpRecFields; |
| 2597 | |
| 2598 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2599 | FieldDecl *Field = RecFields[i]; |
| 2600 | // Skip over unnamed or bitfields |
| 2601 | if (!Field->getIdentifier() || Field->isBitField()) |
| 2602 | continue; |
| 2603 | QualType FQT = Field->getType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2604 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2605 | if (FQT->isUnionType()) |
| 2606 | HasUnion = true; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2607 | else |
| 2608 | assert(FQT->isRecordType() && |
| 2609 | "only union/record is supported for ivar layout bitmap"); |
| 2610 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2611 | const RecordType *RT = FQT->getAsRecordType(); |
| 2612 | const RecordDecl *RD = RT->getDecl(); |
| 2613 | // FIXME - Find a more efficiant way of passing records down. |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2614 | TmpRecFields.append(RD->field_begin(), RD->field_end()); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2615 | // FIXME - Is Layout correct? |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2616 | BuildAggrIvarLayout(OI, Layout, RD, TmpRecFields, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2617 | BytePos + GetFieldBaseOffset(OI, Layout, Field), |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2618 | ForStrongLayout, Index, SkIndex, |
| 2619 | HasUnion); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2620 | TmpRecFields.clear(); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2621 | continue; |
| 2622 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2623 | |
| 2624 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2625 | const ConstantArrayType *CArray = |
| 2626 | dyn_cast_or_null<ConstantArrayType>(Array); |
| 2627 | assert(CArray && "only array with know element size is supported"); |
| 2628 | FQT = CArray->getElementType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2629 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 2630 | const ConstantArrayType *CArray = |
| 2631 | dyn_cast_or_null<ConstantArrayType>(Array); |
| 2632 | FQT = CArray->getElementType(); |
| 2633 | } |
| 2634 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2635 | assert(!FQT->isUnionType() && |
| 2636 | "layout for array of unions not supported"); |
| 2637 | if (FQT->isRecordType()) { |
| 2638 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
| 2639 | int OldIndex = Index; |
| 2640 | int OldSkIndex = SkIndex; |
| 2641 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2642 | // FIXME - Use a common routine with the above! |
| 2643 | const RecordType *RT = FQT->getAsRecordType(); |
| 2644 | const RecordDecl *RD = RT->getDecl(); |
| 2645 | // FIXME - Find a more efficiant way of passing records down. |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2646 | TmpRecFields.append(RD->field_begin(), RD->field_end()); |
| 2647 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2648 | BuildAggrIvarLayout(OI, Layout, RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2649 | TmpRecFields, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2650 | BytePos + GetFieldBaseOffset(OI, Layout, Field), |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2651 | ForStrongLayout, Index, SkIndex, |
| 2652 | HasUnion); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2653 | TmpRecFields.clear(); |
| 2654 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2655 | // Replicate layout information for each array element. Note that |
| 2656 | // one element is already done. |
| 2657 | uint64_t ElIx = 1; |
| 2658 | for (int FirstIndex = Index, FirstSkIndex = SkIndex; |
| 2659 | ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2660 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2661 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 2662 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2663 | GC_IVAR gcivar; |
| 2664 | gcivar.ivar_bytepos = IvarsInfo[i].ivar_bytepos + Size*ElIx; |
| 2665 | gcivar.ivar_size = IvarsInfo[i].ivar_size; |
| 2666 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2667 | } |
| 2668 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2669 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2670 | GC_IVAR skivar; |
| 2671 | skivar.ivar_bytepos = SkipIvars[i].ivar_bytepos + Size*ElIx; |
| 2672 | skivar.ivar_size = SkipIvars[i].ivar_size; |
| 2673 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2674 | } |
| 2675 | } |
| 2676 | continue; |
| 2677 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2678 | } |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2679 | // At this point, we are done with Record/Union and array there of. |
| 2680 | // For other arrays we are down to its element type. |
| 2681 | QualType::GCAttrTypes GCAttr = QualType::GCNone; |
| 2682 | do { |
| 2683 | if (FQT.isObjCGCStrong() || FQT.isObjCGCWeak()) { |
| 2684 | GCAttr = FQT.isObjCGCStrong() ? QualType::Strong : QualType::Weak; |
| 2685 | break; |
| 2686 | } |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2687 | else if (CGM.getContext().isObjCObjectPointerType(FQT)) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2688 | GCAttr = QualType::Strong; |
| 2689 | break; |
| 2690 | } |
| 2691 | else if (const PointerType *PT = FQT->getAsPointerType()) { |
| 2692 | FQT = PT->getPointeeType(); |
| 2693 | } |
| 2694 | else { |
| 2695 | break; |
| 2696 | } |
| 2697 | } while (true); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2698 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2699 | if ((ForStrongLayout && GCAttr == QualType::Strong) |
| 2700 | || (!ForStrongLayout && GCAttr == QualType::Weak)) { |
| 2701 | if (IsUnion) |
| 2702 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2703 | uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType()) |
| 2704 | / WordSizeInBits; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2705 | if (UnionIvarSize > MaxUnionIvarSize) |
| 2706 | { |
| 2707 | MaxUnionIvarSize = UnionIvarSize; |
| 2708 | MaxField = Field; |
| 2709 | } |
| 2710 | } |
| 2711 | else |
| 2712 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2713 | GC_IVAR gcivar; |
| 2714 | gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field); |
| 2715 | gcivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) / |
| 2716 | WordSizeInBits; |
| 2717 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2718 | } |
| 2719 | } |
| 2720 | else if ((ForStrongLayout && |
| 2721 | (GCAttr == QualType::GCNone || GCAttr == QualType::Weak)) |
| 2722 | || (!ForStrongLayout && GCAttr != QualType::Weak)) { |
| 2723 | if (IsUnion) |
| 2724 | { |
| 2725 | uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType()); |
| 2726 | if (UnionIvarSize > MaxSkippedUnionIvarSize) |
| 2727 | { |
| 2728 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 2729 | MaxSkippedField = Field; |
| 2730 | } |
| 2731 | } |
| 2732 | else |
| 2733 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2734 | GC_IVAR skivar; |
| 2735 | skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field); |
| 2736 | skivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) / |
| 2737 | WordSizeInBits; |
| 2738 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2739 | } |
| 2740 | } |
| 2741 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2742 | if (MaxField) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2743 | GC_IVAR gcivar; |
| 2744 | gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, MaxField); |
| 2745 | gcivar.ivar_size = MaxUnionIvarSize; |
| 2746 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2747 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2748 | |
| 2749 | if (MaxSkippedField) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2750 | GC_IVAR skivar; |
| 2751 | skivar.ivar_bytepos = BytePos + |
| 2752 | GetFieldBaseOffset(OI, Layout, MaxSkippedField); |
| 2753 | skivar.ivar_size = MaxSkippedUnionIvarSize; |
| 2754 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2755 | } |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2756 | } |
| 2757 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2758 | static int |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2759 | IvarBytePosCompare(const void *a, const void *b) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2760 | { |
| 2761 | unsigned int sa = ((CGObjCCommonMac::GC_IVAR *)a)->ivar_bytepos; |
| 2762 | unsigned int sb = ((CGObjCCommonMac::GC_IVAR *)b)->ivar_bytepos; |
| 2763 | |
| 2764 | if (sa < sb) |
| 2765 | return -1; |
| 2766 | if (sa > sb) |
| 2767 | return 1; |
| 2768 | return 0; |
| 2769 | } |
| 2770 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2771 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 2772 | /// implementation for the __strong or __weak case. |
| 2773 | /// The layout map displays which words in ivar list must be skipped |
| 2774 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 2775 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
| 2776 | /// of words to skip and right nibble is count of words to scan. So, each |
| 2777 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
| 2778 | /// represented by a 0x00 byte which also ends the string. |
| 2779 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 2780 | /// - id, Class |
| 2781 | /// - object * |
| 2782 | /// - __strong anything |
| 2783 | /// |
| 2784 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 2785 | /// - __weak anything |
| 2786 | /// |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2787 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2788 | const ObjCImplementationDecl *OMD, |
| 2789 | bool ForStrongLayout) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2790 | int Index = -1; |
| 2791 | int SkIndex = -1; |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2792 | bool hasUnion = false; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2793 | int SkipScan; |
| 2794 | unsigned int WordsToScan, WordsToSkip; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2795 | const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 2796 | if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) |
| 2797 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2798 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2799 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2800 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2801 | CGM.getContext().CollectObjCIvars(OI, RecFields); |
| 2802 | if (RecFields.empty()) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2803 | return llvm::Constant::getNullValue(PtrTy); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2804 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2805 | SkipIvars.clear(); |
| 2806 | IvarsInfo.clear(); |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 2807 | |
| 2808 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OI); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2809 | BuildAggrIvarLayout(OI, Layout, 0, RecFields, 0, ForStrongLayout, |
| 2810 | Index, SkIndex, hasUnion); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2811 | if (Index == -1) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2812 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2813 | |
| 2814 | // Sort on byte position in case we encounterred a union nested in |
| 2815 | // the ivar list. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2816 | if (hasUnion && !IvarsInfo.empty()) |
| 2817 | qsort(&IvarsInfo[0], Index+1, sizeof(GC_IVAR), IvarBytePosCompare); |
| 2818 | if (hasUnion && !SkipIvars.empty()) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2819 | qsort(&SkipIvars[0], Index+1, sizeof(GC_IVAR), IvarBytePosCompare); |
| 2820 | |
| 2821 | // Build the string of skip/scan nibbles |
| 2822 | SkipScan = -1; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2823 | SkipScanIvars.clear(); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2824 | unsigned int WordSize = |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2825 | CGM.getTypes().getTargetData().getTypePaddedSize(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2826 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 2827 | WordsToSkip = 0; |
| 2828 | WordsToScan = IvarsInfo[0].ivar_size; |
| 2829 | } |
| 2830 | else { |
| 2831 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 2832 | WordsToScan = IvarsInfo[0].ivar_size; |
| 2833 | } |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2834 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2835 | { |
| 2836 | unsigned int TailPrevGCObjC = |
| 2837 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
| 2838 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) |
| 2839 | { |
| 2840 | // consecutive 'scanned' object pointers. |
| 2841 | WordsToScan += IvarsInfo[i].ivar_size; |
| 2842 | } |
| 2843 | else |
| 2844 | { |
| 2845 | // Skip over 'gc'able object pointer which lay over each other. |
| 2846 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 2847 | continue; |
| 2848 | // Must skip over 1 or more words. We save current skip/scan values |
| 2849 | // and start a new pair. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2850 | SKIP_SCAN SkScan; |
| 2851 | SkScan.skip = WordsToSkip; |
| 2852 | SkScan.scan = WordsToScan; |
| 2853 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
| 2854 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2855 | // Skip the hole. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2856 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 2857 | SkScan.scan = 0; |
| 2858 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2859 | WordsToSkip = 0; |
| 2860 | WordsToScan = IvarsInfo[i].ivar_size; |
| 2861 | } |
| 2862 | } |
| 2863 | if (WordsToScan > 0) |
| 2864 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2865 | SKIP_SCAN SkScan; |
| 2866 | SkScan.skip = WordsToSkip; |
| 2867 | SkScan.scan = WordsToScan; |
| 2868 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2869 | } |
| 2870 | |
| 2871 | bool BytesSkipped = false; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2872 | if (!SkipIvars.empty()) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2873 | { |
| 2874 | int LastByteSkipped = |
| 2875 | SkipIvars[SkIndex].ivar_bytepos + SkipIvars[SkIndex].ivar_size; |
| 2876 | int LastByteScanned = |
| 2877 | IvarsInfo[Index].ivar_bytepos + IvarsInfo[Index].ivar_size * WordSize; |
| 2878 | BytesSkipped = (LastByteSkipped > LastByteScanned); |
| 2879 | // Compute number of bytes to skip at the tail end of the last ivar scanned. |
| 2880 | if (BytesSkipped) |
| 2881 | { |
| 2882 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2883 | SKIP_SCAN SkScan; |
| 2884 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
| 2885 | SkScan.scan = 0; |
| 2886 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2887 | } |
| 2888 | } |
| 2889 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 2890 | // as 0xMN. |
| 2891 | for (int i = 0; i <= SkipScan; i++) |
| 2892 | { |
| 2893 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 2894 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 2895 | // 0xM0 followed by 0x0N detected. |
| 2896 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 2897 | for (int j = i+1; j < SkipScan; j++) |
| 2898 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 2899 | --SkipScan; |
| 2900 | } |
| 2901 | } |
| 2902 | |
| 2903 | // Generate the string. |
| 2904 | std::string BitMap; |
| 2905 | for (int i = 0; i <= SkipScan; i++) |
| 2906 | { |
| 2907 | unsigned char byte; |
| 2908 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 2909 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 2910 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 2911 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
| 2912 | |
| 2913 | if (skip_small > 0 || skip_big > 0) |
| 2914 | BytesSkipped = true; |
| 2915 | // first skip big. |
| 2916 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 2917 | BitMap += (unsigned char)(0xf0); |
| 2918 | |
| 2919 | // next (skip small, scan) |
| 2920 | if (skip_small) |
| 2921 | { |
| 2922 | byte = skip_small << 4; |
| 2923 | if (scan_big > 0) |
| 2924 | { |
| 2925 | byte |= 0xf; |
| 2926 | --scan_big; |
| 2927 | } |
| 2928 | else if (scan_small) |
| 2929 | { |
| 2930 | byte |= scan_small; |
| 2931 | scan_small = 0; |
| 2932 | } |
| 2933 | BitMap += byte; |
| 2934 | } |
| 2935 | // next scan big |
| 2936 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 2937 | BitMap += (unsigned char)(0x0f); |
| 2938 | // last scan small |
| 2939 | if (scan_small) |
| 2940 | { |
| 2941 | byte = scan_small; |
| 2942 | BitMap += byte; |
| 2943 | } |
| 2944 | } |
| 2945 | // null terminate string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2946 | unsigned char zero = 0; |
| 2947 | BitMap += zero; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2948 | // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as |
| 2949 | // final layout. |
| 2950 | if (ForStrongLayout && !BytesSkipped) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2951 | return llvm::Constant::getNullValue(PtrTy); |
| 2952 | llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 2953 | llvm::ConstantArray::get(BitMap.c_str()), |
| 2954 | "__TEXT,__cstring,cstring_literals", |
| 2955 | 0, true); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2956 | // FIXME. Need a commomand-line option for this eventually. |
| 2957 | if (ForStrongLayout) |
| 2958 | printf("\nstrong ivar layout: "); |
| 2959 | else |
| 2960 | printf("\nweak ivar layout: "); |
| 2961 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
| 2962 | for (unsigned i = 0; i < BitMap.size(); i++) |
Fariborz Jahanian | dbf15cb | 2009-03-26 19:10:36 +0000 | [diff] [blame] | 2963 | if (!(s[i] & 0xf0)) |
| 2964 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 2965 | else |
| 2966 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2967 | printf("\n"); |
| 2968 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2969 | return getConstantGEP(Entry, 0, 0); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2970 | } |
| 2971 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 2972 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2973 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 2974 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2975 | // FIXME: Avoid std::string copying. |
| 2976 | if (!Entry) |
| 2977 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
| 2978 | llvm::ConstantArray::get(Sel.getAsString()), |
| 2979 | "__TEXT,__cstring,cstring_literals", |
| 2980 | 0, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2981 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2982 | return getConstantGEP(Entry, 0, 0); |
| 2983 | } |
| 2984 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2985 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 2986 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2987 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 2988 | } |
| 2989 | |
| 2990 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 2991 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2992 | return GetMethodVarName(&CGM.getContext().Idents.get(Name)); |
| 2993 | } |
| 2994 | |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 2995 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(FieldDecl *Field) { |
| 2996 | std::string TypeStr; |
| 2997 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 2998 | |
| 2999 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3000 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3001 | if (!Entry) |
| 3002 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3003 | llvm::ConstantArray::get(TypeStr), |
| 3004 | "__TEXT,__cstring,cstring_literals", |
| 3005 | 0, true); |
| 3006 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3007 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3010 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3011 | std::string TypeStr; |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3012 | CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D), |
| 3013 | TypeStr); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3014 | |
| 3015 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 3016 | |
| 3017 | if (!Entry) { |
| 3018 | llvm::Constant *C = llvm::ConstantArray::get(TypeStr); |
| 3019 | Entry = |
| 3020 | new llvm::GlobalVariable(C->getType(), false, |
| 3021 | llvm::GlobalValue::InternalLinkage, |
| 3022 | C, "\01L_OBJC_METH_VAR_TYPE_", |
| 3023 | &CGM.getModule()); |
| 3024 | Entry->setSection("__TEXT,__cstring,cstring_literals"); |
| 3025 | UsedGlobals.push_back(Entry); |
| 3026 | } |
| 3027 | |
| 3028 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3029 | } |
| 3030 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3031 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3032 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3033 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
| 3034 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3035 | if (!Entry) |
| 3036 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
| 3037 | llvm::ConstantArray::get(Ident->getName()), |
| 3038 | "__TEXT,__cstring,cstring_literals", |
| 3039 | 0, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3040 | |
| 3041 | return getConstantGEP(Entry, 0, 0); |
| 3042 | } |
| 3043 | |
| 3044 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3045 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3046 | llvm::Constant * |
| 3047 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 3048 | const Decl *Container) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3049 | std::string TypeStr; |
| 3050 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3051 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 3052 | } |
| 3053 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3054 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
| 3055 | const ObjCContainerDecl *CD, |
| 3056 | std::string &NameOut) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 3057 | NameOut = '\01'; |
| 3058 | NameOut += (D->isInstanceMethod() ? '-' : '+'); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3059 | NameOut += '['; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 3060 | assert (CD && "Missing container decl in GetNameForMethod"); |
| 3061 | NameOut += CD->getNameAsString(); |
Fariborz Jahanian | 5284733 | 2009-01-26 23:49:05 +0000 | [diff] [blame] | 3062 | // FIXME. For a method in a category, (CAT_NAME) is inserted here. |
| 3063 | // Right now! there is not enough info. to do this. |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3064 | NameOut += ' '; |
| 3065 | NameOut += D->getSelector().getAsString(); |
| 3066 | NameOut += ']'; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3067 | } |
| 3068 | |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 3069 | /// GetFirstIvarInRecord - This routine returns the record for the |
| 3070 | /// implementation of the fiven class OID. It also returns field |
| 3071 | /// corresponding to the first ivar in the class in FIV. It also |
| 3072 | /// returns the one before the first ivar. |
| 3073 | /// |
| 3074 | const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord( |
| 3075 | const ObjCInterfaceDecl *OID, |
| 3076 | RecordDecl::field_iterator &FIV, |
| 3077 | RecordDecl::field_iterator &PIV) { |
| 3078 | int countSuperClassIvars = countInheritedIvars(OID->getSuperClass()); |
| 3079 | const RecordDecl *RD = CGM.getContext().addRecordToClass(OID); |
| 3080 | RecordDecl::field_iterator ifield = RD->field_begin(); |
| 3081 | RecordDecl::field_iterator pfield = RD->field_end(); |
| 3082 | while (countSuperClassIvars-- > 0) { |
| 3083 | pfield = ifield; |
| 3084 | ++ifield; |
| 3085 | } |
| 3086 | FIV = ifield; |
| 3087 | PIV = pfield; |
| 3088 | return RD; |
| 3089 | } |
| 3090 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3091 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3092 | EmitModuleInfo(); |
| 3093 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3094 | // Emit the dummy bodies for any protocols which were referenced but |
| 3095 | // never defined. |
| 3096 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
| 3097 | i = Protocols.begin(), e = Protocols.end(); i != e; ++i) { |
| 3098 | if (i->second->hasInitializer()) |
| 3099 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3100 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3101 | std::vector<llvm::Constant*> Values(5); |
| 3102 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 3103 | Values[1] = GetClassName(i->first); |
| 3104 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 3105 | Values[3] = Values[4] = |
| 3106 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 3107 | i->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 3108 | i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 3109 | Values)); |
| 3110 | } |
| 3111 | |
| 3112 | std::vector<llvm::Constant*> Used; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3113 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3114 | e = UsedGlobals.end(); i != e; ++i) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3115 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3116 | } |
| 3117 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3118 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3119 | llvm::GlobalValue *GV = |
| 3120 | new llvm::GlobalVariable(AT, false, |
| 3121 | llvm::GlobalValue::AppendingLinkage, |
| 3122 | llvm::ConstantArray::get(AT, Used), |
| 3123 | "llvm.used", |
| 3124 | &CGM.getModule()); |
| 3125 | |
| 3126 | GV->setSection("llvm.metadata"); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3127 | |
| 3128 | // Add assembler directives to add lazy undefined symbol references |
| 3129 | // for classes which are referenced but not defined. This is |
| 3130 | // important for correct linker interaction. |
| 3131 | |
| 3132 | // FIXME: Uh, this isn't particularly portable. |
| 3133 | std::stringstream s; |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3134 | |
| 3135 | if (!CGM.getModule().getModuleInlineAsm().empty()) |
| 3136 | s << "\n"; |
| 3137 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3138 | for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(), |
| 3139 | e = LazySymbols.end(); i != e; ++i) { |
| 3140 | s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n"; |
| 3141 | } |
| 3142 | for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(), |
| 3143 | e = DefinedSymbols.end(); i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3144 | s << "\t.objc_class_name_" << (*i)->getName() << "=0\n" |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3145 | << "\t.globl .objc_class_name_" << (*i)->getName() << "\n"; |
| 3146 | } |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3147 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3148 | CGM.getModule().appendModuleInlineAsm(s.str()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3149 | } |
| 3150 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3151 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3152 | : CGObjCCommonMac(cgm), |
| 3153 | ObjCTypes(cgm) |
| 3154 | { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3155 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3156 | ObjCABI = 2; |
| 3157 | } |
| 3158 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3159 | /* *** */ |
| 3160 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3161 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3162 | : CGM(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3163 | { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3164 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3165 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3166 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3167 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3168 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3169 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3170 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3171 | Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 3172 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3173 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 3174 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3175 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3176 | |
| 3177 | // FIXME: It would be nice to unify this with the opaque type, so |
| 3178 | // that the IR comes out a bit cleaner. |
| 3179 | const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 3180 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3181 | |
| 3182 | // I'm not sure I like this. The implicit coordination is a bit |
| 3183 | // gross. We should solve this in a reasonable fashion because this |
| 3184 | // is a pretty common task (match some runtime data structure with |
| 3185 | // an LLVM data structure). |
| 3186 | |
| 3187 | // FIXME: This is leaked. |
| 3188 | // FIXME: Merge with rewriter code? |
| 3189 | |
| 3190 | // struct _objc_super { |
| 3191 | // id self; |
| 3192 | // Class cls; |
| 3193 | // } |
| 3194 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3195 | SourceLocation(), |
| 3196 | &Ctx.Idents.get("_objc_super")); |
| 3197 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3198 | Ctx.getObjCIdType(), 0, false)); |
| 3199 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3200 | Ctx.getObjCClassType(), 0, false)); |
| 3201 | RD->completeDefinition(Ctx); |
| 3202 | |
| 3203 | SuperCTy = Ctx.getTagDeclType(RD); |
| 3204 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
| 3205 | |
| 3206 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3207 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 3208 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3209 | // struct _prop_t { |
| 3210 | // char *name; |
| 3211 | // char *attributes; |
| 3212 | // } |
| 3213 | PropertyTy = llvm::StructType::get(Int8PtrTy, |
| 3214 | Int8PtrTy, |
| 3215 | NULL); |
| 3216 | CGM.getModule().addTypeName("struct._prop_t", |
| 3217 | PropertyTy); |
| 3218 | |
| 3219 | // struct _prop_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3220 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3221 | // uint32_t count_of_properties; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3222 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3223 | // } |
| 3224 | PropertyListTy = llvm::StructType::get(IntTy, |
| 3225 | IntTy, |
| 3226 | llvm::ArrayType::get(PropertyTy, 0), |
| 3227 | NULL); |
| 3228 | CGM.getModule().addTypeName("struct._prop_list_t", |
| 3229 | PropertyListTy); |
| 3230 | // struct _prop_list_t * |
| 3231 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
| 3232 | |
| 3233 | // struct _objc_method { |
| 3234 | // SEL _cmd; |
| 3235 | // char *method_type; |
| 3236 | // char *_imp; |
| 3237 | // } |
| 3238 | MethodTy = llvm::StructType::get(SelectorPtrTy, |
| 3239 | Int8PtrTy, |
| 3240 | Int8PtrTy, |
| 3241 | NULL); |
| 3242 | CGM.getModule().addTypeName("struct._objc_method", MethodTy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3243 | |
| 3244 | // struct _objc_cache * |
| 3245 | CacheTy = llvm::OpaqueType::get(); |
| 3246 | CGM.getModule().addTypeName("struct._objc_cache", CacheTy); |
| 3247 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3248 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3249 | // Property manipulation functions. |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3250 | |
| 3251 | QualType IdType = Ctx.getObjCIdType(); |
| 3252 | QualType SelType = Ctx.getObjCSelType(); |
| 3253 | llvm::SmallVector<QualType,16> Params; |
| 3254 | const llvm::FunctionType *FTy; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3255 | |
| 3256 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3257 | Params.push_back(IdType); |
| 3258 | Params.push_back(SelType); |
| 3259 | Params.push_back(Ctx.LongTy); |
| 3260 | Params.push_back(Ctx.BoolTy); |
| 3261 | FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), |
| 3262 | false); |
| 3263 | GetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_getProperty"); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3264 | |
| 3265 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
| 3266 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3267 | Params.push_back(IdType); |
| 3268 | Params.push_back(SelType); |
| 3269 | Params.push_back(Ctx.LongTy); |
| 3270 | Params.push_back(IdType); |
| 3271 | Params.push_back(Ctx.BoolTy); |
| 3272 | Params.push_back(Ctx.BoolTy); |
| 3273 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
| 3274 | SetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); |
| 3275 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3276 | // Enumeration mutation. |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3277 | |
| 3278 | // void objc_enumerationMutation (id) |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3279 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3280 | Params.push_back(IdType); |
| 3281 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
| 3282 | EnumerationMutationFn = CGM.CreateRuntimeFunction(FTy, |
| 3283 | "objc_enumerationMutation"); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3284 | |
| 3285 | // gc's API |
| 3286 | // id objc_read_weak (id *) |
| 3287 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3288 | Params.push_back(Ctx.getPointerType(IdType)); |
| 3289 | FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false); |
| 3290 | GcReadWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
| 3291 | |
| 3292 | // id objc_assign_weak (id, id *) |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3293 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3294 | Params.push_back(IdType); |
| 3295 | Params.push_back(Ctx.getPointerType(IdType)); |
| 3296 | |
| 3297 | FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false); |
| 3298 | GcAssignWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 3299 | GcAssignGlobalFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 3300 | GcAssignIvarFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 3301 | GcAssignStrongCastFn = |
| 3302 | CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 3303 | |
| 3304 | // void objc_exception_throw(id) |
| 3305 | Params.clear(); |
| 3306 | Params.push_back(IdType); |
| 3307 | |
| 3308 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 3309 | ExceptionThrowFn = |
| 3310 | CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3311 | |
| 3312 | // synchronized APIs |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3313 | // void objc_sync_exit (id) |
| 3314 | Params.clear(); |
| 3315 | Params.push_back(IdType); |
| 3316 | |
| 3317 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3318 | SyncExitFn = CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3319 | } |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3320 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3321 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3322 | : ObjCCommonTypesHelper(cgm) |
| 3323 | { |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3324 | // struct _objc_method_description { |
| 3325 | // SEL name; |
| 3326 | // char *types; |
| 3327 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3328 | MethodDescriptionTy = |
| 3329 | llvm::StructType::get(SelectorPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3330 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3331 | NULL); |
| 3332 | CGM.getModule().addTypeName("struct._objc_method_description", |
| 3333 | MethodDescriptionTy); |
| 3334 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3335 | // struct _objc_method_description_list { |
| 3336 | // int count; |
| 3337 | // struct _objc_method_description[1]; |
| 3338 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3339 | MethodDescriptionListTy = |
| 3340 | llvm::StructType::get(IntTy, |
| 3341 | llvm::ArrayType::get(MethodDescriptionTy, 0), |
| 3342 | NULL); |
| 3343 | CGM.getModule().addTypeName("struct._objc_method_description_list", |
| 3344 | MethodDescriptionListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3345 | |
| 3346 | // struct _objc_method_description_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3347 | MethodDescriptionListPtrTy = |
| 3348 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
| 3349 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3350 | // Protocol description structures |
| 3351 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3352 | // struct _objc_protocol_extension { |
| 3353 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 3354 | // struct _objc_method_description_list *optional_instance_methods; |
| 3355 | // struct _objc_method_description_list *optional_class_methods; |
| 3356 | // struct _objc_property_list *instance_properties; |
| 3357 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3358 | ProtocolExtensionTy = |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3359 | llvm::StructType::get(IntTy, |
| 3360 | MethodDescriptionListPtrTy, |
| 3361 | MethodDescriptionListPtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3362 | PropertyListPtrTy, |
| 3363 | NULL); |
| 3364 | CGM.getModule().addTypeName("struct._objc_protocol_extension", |
| 3365 | ProtocolExtensionTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3366 | |
| 3367 | // struct _objc_protocol_extension * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3368 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
| 3369 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3370 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3371 | |
| 3372 | llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(); |
| 3373 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3374 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3375 | const llvm::Type *T = |
| 3376 | llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3377 | LongTy, |
| 3378 | llvm::ArrayType::get(ProtocolTyHolder, 0), |
| 3379 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3380 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T); |
| 3381 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3382 | // struct _objc_protocol { |
| 3383 | // struct _objc_protocol_extension *isa; |
| 3384 | // char *protocol_name; |
| 3385 | // struct _objc_protocol **_objc_protocol_list; |
| 3386 | // struct _objc_method_description_list *instance_methods; |
| 3387 | // struct _objc_method_description_list *class_methods; |
| 3388 | // } |
| 3389 | T = llvm::StructType::get(ProtocolExtensionPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3390 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3391 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3392 | MethodDescriptionListPtrTy, |
| 3393 | MethodDescriptionListPtrTy, |
| 3394 | NULL); |
| 3395 | cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T); |
| 3396 | |
| 3397 | ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get()); |
| 3398 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3399 | ProtocolListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3400 | // struct _objc_protocol_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3401 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
| 3402 | |
| 3403 | ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get()); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3404 | CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3405 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3406 | |
| 3407 | // Class description structures |
| 3408 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3409 | // struct _objc_ivar { |
| 3410 | // char *ivar_name; |
| 3411 | // char *ivar_type; |
| 3412 | // int ivar_offset; |
| 3413 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3414 | IvarTy = llvm::StructType::get(Int8PtrTy, |
| 3415 | Int8PtrTy, |
| 3416 | IntTy, |
| 3417 | NULL); |
| 3418 | CGM.getModule().addTypeName("struct._objc_ivar", IvarTy); |
| 3419 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3420 | // struct _objc_ivar_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3421 | IvarListTy = llvm::OpaqueType::get(); |
| 3422 | CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy); |
| 3423 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
| 3424 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3425 | // struct _objc_method_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3426 | MethodListTy = llvm::OpaqueType::get(); |
| 3427 | CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy); |
| 3428 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
| 3429 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3430 | // struct _objc_class_extension * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3431 | ClassExtensionTy = |
| 3432 | llvm::StructType::get(IntTy, |
| 3433 | Int8PtrTy, |
| 3434 | PropertyListPtrTy, |
| 3435 | NULL); |
| 3436 | CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy); |
| 3437 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
| 3438 | |
| 3439 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3440 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3441 | // struct _objc_class { |
| 3442 | // Class isa; |
| 3443 | // Class super_class; |
| 3444 | // char *name; |
| 3445 | // long version; |
| 3446 | // long info; |
| 3447 | // long instance_size; |
| 3448 | // struct _objc_ivar_list *ivars; |
| 3449 | // struct _objc_method_list *methods; |
| 3450 | // struct _objc_cache *cache; |
| 3451 | // struct _objc_protocol_list *protocols; |
| 3452 | // char *ivar_layout; |
| 3453 | // struct _objc_class_ext *ext; |
| 3454 | // }; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3455 | T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3456 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3457 | Int8PtrTy, |
| 3458 | LongTy, |
| 3459 | LongTy, |
| 3460 | LongTy, |
| 3461 | IvarListPtrTy, |
| 3462 | MethodListPtrTy, |
| 3463 | CachePtrTy, |
| 3464 | ProtocolListPtrTy, |
| 3465 | Int8PtrTy, |
| 3466 | ClassExtensionPtrTy, |
| 3467 | NULL); |
| 3468 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T); |
| 3469 | |
| 3470 | ClassTy = cast<llvm::StructType>(ClassTyHolder.get()); |
| 3471 | CGM.getModule().addTypeName("struct._objc_class", ClassTy); |
| 3472 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
| 3473 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3474 | // struct _objc_category { |
| 3475 | // char *category_name; |
| 3476 | // char *class_name; |
| 3477 | // struct _objc_method_list *instance_method; |
| 3478 | // struct _objc_method_list *class_method; |
| 3479 | // uint32_t size; // sizeof(struct _objc_category) |
| 3480 | // struct _objc_property_list *instance_properties;// category's @property |
| 3481 | // } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3482 | CategoryTy = llvm::StructType::get(Int8PtrTy, |
| 3483 | Int8PtrTy, |
| 3484 | MethodListPtrTy, |
| 3485 | MethodListPtrTy, |
| 3486 | ProtocolListPtrTy, |
| 3487 | IntTy, |
| 3488 | PropertyListPtrTy, |
| 3489 | NULL); |
| 3490 | CGM.getModule().addTypeName("struct._objc_category", CategoryTy); |
| 3491 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3492 | // Global metadata structures |
| 3493 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3494 | // struct _objc_symtab { |
| 3495 | // long sel_ref_cnt; |
| 3496 | // SEL *refs; |
| 3497 | // short cls_def_cnt; |
| 3498 | // short cat_def_cnt; |
| 3499 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 3500 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3501 | SymtabTy = llvm::StructType::get(LongTy, |
| 3502 | SelectorPtrTy, |
| 3503 | ShortTy, |
| 3504 | ShortTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3505 | llvm::ArrayType::get(Int8PtrTy, 0), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3506 | NULL); |
| 3507 | CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy); |
| 3508 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
| 3509 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3510 | // struct _objc_module { |
| 3511 | // long version; |
| 3512 | // long size; // sizeof(struct _objc_module) |
| 3513 | // char *name; |
| 3514 | // struct _objc_symtab* symtab; |
| 3515 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3516 | ModuleTy = |
| 3517 | llvm::StructType::get(LongTy, |
| 3518 | LongTy, |
| 3519 | Int8PtrTy, |
| 3520 | SymtabPtrTy, |
| 3521 | NULL); |
| 3522 | CGM.getModule().addTypeName("struct._objc_module", ModuleTy); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3523 | |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 3524 | // Message send functions. |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3525 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3526 | // id objc_msgSend (id, SEL, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3527 | std::vector<const llvm::Type*> Params; |
| 3528 | Params.push_back(ObjectPtrTy); |
| 3529 | Params.push_back(SelectorPtrTy); |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3530 | MessageSendFn = |
| 3531 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3532 | Params, |
| 3533 | true), |
| 3534 | "objc_msgSend"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3535 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3536 | // id objc_msgSend_stret (id, SEL, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3537 | Params.clear(); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3538 | Params.push_back(ObjectPtrTy); |
| 3539 | Params.push_back(SelectorPtrTy); |
| 3540 | MessageSendStretFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3541 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3542 | Params, |
| 3543 | true), |
| 3544 | "objc_msgSend_stret"); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3545 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3546 | // |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3547 | Params.clear(); |
| 3548 | Params.push_back(ObjectPtrTy); |
| 3549 | Params.push_back(SelectorPtrTy); |
| 3550 | // FIXME: This should be long double on x86_64? |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3551 | // [double | long double] objc_msgSend_fpret(id self, SEL op, ...) |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3552 | MessageSendFpretFn = |
| 3553 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy, |
| 3554 | Params, |
| 3555 | true), |
| 3556 | "objc_msgSend_fpret"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3557 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3558 | // id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3559 | Params.clear(); |
| 3560 | Params.push_back(SuperPtrTy); |
| 3561 | Params.push_back(SelectorPtrTy); |
| 3562 | MessageSendSuperFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3563 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3564 | Params, |
| 3565 | true), |
| 3566 | "objc_msgSendSuper"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3567 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3568 | // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super, |
| 3569 | // SEL op, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3570 | Params.clear(); |
| 3571 | Params.push_back(Int8PtrTy); |
| 3572 | Params.push_back(SuperPtrTy); |
| 3573 | Params.push_back(SelectorPtrTy); |
| 3574 | MessageSendSuperStretFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3575 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3576 | Params, |
| 3577 | true), |
| 3578 | "objc_msgSendSuper_stret"); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3579 | |
| 3580 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 3581 | MessageSendSuperFpretFn = MessageSendSuperFn; |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 3582 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3583 | // FIXME: This is the size of the setjmp buffer and should be |
| 3584 | // target specific. 18 is what's used on 32-bit X86. |
| 3585 | uint64_t SetJmpBufferSize = 18; |
| 3586 | |
| 3587 | // Exceptions |
| 3588 | const llvm::Type *StackPtrTy = |
Daniel Dunbar | 1000491 | 2008-09-27 06:32:25 +0000 | [diff] [blame] | 3589 | llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3590 | |
| 3591 | ExceptionDataTy = |
| 3592 | llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty, |
| 3593 | SetJmpBufferSize), |
| 3594 | StackPtrTy, NULL); |
| 3595 | CGM.getModule().addTypeName("struct._objc_exception_data", |
| 3596 | ExceptionDataTy); |
| 3597 | |
| 3598 | Params.clear(); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3599 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 3600 | ExceptionTryEnterFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3601 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3602 | Params, |
| 3603 | false), |
| 3604 | "objc_exception_try_enter"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3605 | ExceptionTryExitFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3606 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3607 | Params, |
| 3608 | false), |
| 3609 | "objc_exception_try_exit"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3610 | ExceptionExtractFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3611 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3612 | Params, |
| 3613 | false), |
| 3614 | "objc_exception_extract"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3615 | |
| 3616 | Params.clear(); |
| 3617 | Params.push_back(ClassPtrTy); |
| 3618 | Params.push_back(ObjectPtrTy); |
| 3619 | ExceptionMatchFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3620 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 3621 | Params, |
| 3622 | false), |
| 3623 | "objc_exception_match"); |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 3624 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3625 | Params.clear(); |
| 3626 | Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty)); |
| 3627 | SetJmpFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3628 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 3629 | Params, |
| 3630 | false), |
| 3631 | "_setjmp"); |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 3632 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3633 | } |
| 3634 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3635 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3636 | : ObjCCommonTypesHelper(cgm) |
| 3637 | { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3638 | // struct _method_list_t { |
| 3639 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 3640 | // uint32_t method_count; |
| 3641 | // struct _objc_method method_list[method_count]; |
| 3642 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3643 | MethodListnfABITy = llvm::StructType::get(IntTy, |
| 3644 | IntTy, |
| 3645 | llvm::ArrayType::get(MethodTy, 0), |
| 3646 | NULL); |
| 3647 | CGM.getModule().addTypeName("struct.__method_list_t", |
| 3648 | MethodListnfABITy); |
| 3649 | // struct method_list_t * |
| 3650 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3651 | |
| 3652 | // struct _protocol_t { |
| 3653 | // id isa; // NULL |
| 3654 | // const char * const protocol_name; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3655 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3656 | // const struct method_list_t * const instance_methods; |
| 3657 | // const struct method_list_t * const class_methods; |
| 3658 | // const struct method_list_t *optionalInstanceMethods; |
| 3659 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3660 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3661 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 3662 | // const uint32_t flags; // = 0 |
| 3663 | // } |
| 3664 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3665 | // Holder for struct _protocol_list_t * |
| 3666 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3667 | |
| 3668 | ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy, |
| 3669 | Int8PtrTy, |
| 3670 | llvm::PointerType::getUnqual( |
| 3671 | ProtocolListTyHolder), |
| 3672 | MethodListnfABIPtrTy, |
| 3673 | MethodListnfABIPtrTy, |
| 3674 | MethodListnfABIPtrTy, |
| 3675 | MethodListnfABIPtrTy, |
| 3676 | PropertyListPtrTy, |
| 3677 | IntTy, |
| 3678 | IntTy, |
| 3679 | NULL); |
| 3680 | CGM.getModule().addTypeName("struct._protocol_t", |
| 3681 | ProtocolnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3682 | |
| 3683 | // struct _protocol_t* |
| 3684 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3685 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3686 | // struct _protocol_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3687 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3688 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3689 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3690 | ProtocolListnfABITy = llvm::StructType::get(LongTy, |
| 3691 | llvm::ArrayType::get( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3692 | ProtocolnfABIPtrTy, 0), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3693 | NULL); |
| 3694 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3695 | ProtocolListnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3696 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo( |
| 3697 | ProtocolListnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3698 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3699 | // struct _objc_protocol_list* |
| 3700 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3701 | |
| 3702 | // struct _ivar_t { |
| 3703 | // unsigned long int *offset; // pointer to ivar offset location |
| 3704 | // char *name; |
| 3705 | // char *type; |
| 3706 | // uint32_t alignment; |
| 3707 | // uint32_t size; |
| 3708 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3709 | IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy), |
| 3710 | Int8PtrTy, |
| 3711 | Int8PtrTy, |
| 3712 | IntTy, |
| 3713 | IntTy, |
| 3714 | NULL); |
| 3715 | CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy); |
| 3716 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3717 | // struct _ivar_list_t { |
| 3718 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 3719 | // uint32 count; |
| 3720 | // struct _iver_t list[count]; |
| 3721 | // } |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3722 | IvarListnfABITy = llvm::StructType::get(IntTy, |
| 3723 | IntTy, |
| 3724 | llvm::ArrayType::get( |
| 3725 | IvarnfABITy, 0), |
| 3726 | NULL); |
| 3727 | CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy); |
| 3728 | |
| 3729 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3730 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3731 | // struct _class_ro_t { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3732 | // uint32_t const flags; |
| 3733 | // uint32_t const instanceStart; |
| 3734 | // uint32_t const instanceSize; |
| 3735 | // uint32_t const reserved; // only when building for 64bit targets |
| 3736 | // const uint8_t * const ivarLayout; |
| 3737 | // const char *const name; |
| 3738 | // const struct _method_list_t * const baseMethods; |
| 3739 | // const struct _objc_protocol_list *const baseProtocols; |
| 3740 | // const struct _ivar_list_t *const ivars; |
| 3741 | // const uint8_t * const weakIvarLayout; |
| 3742 | // const struct _prop_list_t * const properties; |
| 3743 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3744 | |
| 3745 | // FIXME. Add 'reserved' field in 64bit abi mode! |
| 3746 | ClassRonfABITy = llvm::StructType::get(IntTy, |
| 3747 | IntTy, |
| 3748 | IntTy, |
| 3749 | Int8PtrTy, |
| 3750 | Int8PtrTy, |
| 3751 | MethodListnfABIPtrTy, |
| 3752 | ProtocolListnfABIPtrTy, |
| 3753 | IvarListnfABIPtrTy, |
| 3754 | Int8PtrTy, |
| 3755 | PropertyListPtrTy, |
| 3756 | NULL); |
| 3757 | CGM.getModule().addTypeName("struct._class_ro_t", |
| 3758 | ClassRonfABITy); |
| 3759 | |
| 3760 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 3761 | std::vector<const llvm::Type*> Params; |
| 3762 | Params.push_back(ObjectPtrTy); |
| 3763 | Params.push_back(SelectorPtrTy); |
| 3764 | ImpnfABITy = llvm::PointerType::getUnqual( |
| 3765 | llvm::FunctionType::get(ObjectPtrTy, Params, false)); |
| 3766 | |
| 3767 | // struct _class_t { |
| 3768 | // struct _class_t *isa; |
| 3769 | // struct _class_t * const superclass; |
| 3770 | // void *cache; |
| 3771 | // IMP *vtable; |
| 3772 | // struct class_ro_t *ro; |
| 3773 | // } |
| 3774 | |
| 3775 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3776 | ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3777 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3778 | CachePtrTy, |
| 3779 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 3780 | llvm::PointerType::getUnqual( |
| 3781 | ClassRonfABITy), |
| 3782 | NULL); |
| 3783 | CGM.getModule().addTypeName("struct._class_t", ClassnfABITy); |
| 3784 | |
| 3785 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo( |
| 3786 | ClassnfABITy); |
| 3787 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3788 | // LLVM for struct _class_t * |
| 3789 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
| 3790 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3791 | // struct _category_t { |
| 3792 | // const char * const name; |
| 3793 | // struct _class_t *const cls; |
| 3794 | // const struct _method_list_t * const instance_methods; |
| 3795 | // const struct _method_list_t * const class_methods; |
| 3796 | // const struct _protocol_list_t * const protocols; |
| 3797 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3798 | // } |
| 3799 | CategorynfABITy = llvm::StructType::get(Int8PtrTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3800 | ClassnfABIPtrTy, |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3801 | MethodListnfABIPtrTy, |
| 3802 | MethodListnfABIPtrTy, |
| 3803 | ProtocolListnfABIPtrTy, |
| 3804 | PropertyListPtrTy, |
| 3805 | NULL); |
| 3806 | CGM.getModule().addTypeName("struct._category_t", CategorynfABITy); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3807 | |
| 3808 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3809 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3810 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3811 | |
| 3812 | // MessageRefTy - LLVM for: |
| 3813 | // struct _message_ref_t { |
| 3814 | // IMP messenger; |
| 3815 | // SEL name; |
| 3816 | // }; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3817 | |
| 3818 | // First the clang type for struct _message_ref_t |
| 3819 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3820 | SourceLocation(), |
| 3821 | &Ctx.Idents.get("_message_ref_t")); |
| 3822 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3823 | Ctx.VoidPtrTy, 0, false)); |
| 3824 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3825 | Ctx.getObjCSelType(), 0, false)); |
| 3826 | RD->completeDefinition(Ctx); |
| 3827 | |
| 3828 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 3829 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 3830 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3831 | |
| 3832 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 3833 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
| 3834 | |
| 3835 | // SuperMessageRefTy - LLVM for: |
| 3836 | // struct _super_message_ref_t { |
| 3837 | // SUPER_IMP messenger; |
| 3838 | // SEL name; |
| 3839 | // }; |
| 3840 | SuperMessageRefTy = llvm::StructType::get(ImpnfABITy, |
| 3841 | SelectorPtrTy, |
| 3842 | NULL); |
| 3843 | CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy); |
| 3844 | |
| 3845 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 3846 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
| 3847 | |
| 3848 | // id objc_msgSend_fixup (id, struct message_ref_t*, ...) |
| 3849 | Params.clear(); |
| 3850 | Params.push_back(ObjectPtrTy); |
| 3851 | Params.push_back(MessageRefPtrTy); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 3852 | MessengerTy = llvm::FunctionType::get(ObjectPtrTy, |
| 3853 | Params, |
| 3854 | true); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3855 | MessageSendFixupFn = |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 3856 | CGM.CreateRuntimeFunction(MessengerTy, |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3857 | "objc_msgSend_fixup"); |
| 3858 | |
| 3859 | // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...) |
| 3860 | MessageSendFpretFixupFn = |
| 3861 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3862 | Params, |
| 3863 | true), |
| 3864 | "objc_msgSend_fpret_fixup"); |
| 3865 | |
| 3866 | // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...) |
| 3867 | MessageSendStretFixupFn = |
| 3868 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3869 | Params, |
| 3870 | true), |
| 3871 | "objc_msgSend_stret_fixup"); |
| 3872 | |
| 3873 | // id objc_msgSendId_fixup (id, struct message_ref_t*, ...) |
| 3874 | MessageSendIdFixupFn = |
| 3875 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3876 | Params, |
| 3877 | true), |
| 3878 | "objc_msgSendId_fixup"); |
| 3879 | |
| 3880 | |
| 3881 | // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...) |
| 3882 | MessageSendIdStretFixupFn = |
| 3883 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3884 | Params, |
| 3885 | true), |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3886 | "objc_msgSendId_stret_fixup"); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3887 | |
| 3888 | // id objc_msgSendSuper2_fixup (struct objc_super *, |
| 3889 | // struct _super_message_ref_t*, ...) |
| 3890 | Params.clear(); |
| 3891 | Params.push_back(SuperPtrTy); |
| 3892 | Params.push_back(SuperMessageRefPtrTy); |
| 3893 | MessageSendSuper2FixupFn = |
| 3894 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3895 | Params, |
| 3896 | true), |
| 3897 | "objc_msgSendSuper2_fixup"); |
| 3898 | |
| 3899 | |
| 3900 | // id objc_msgSendSuper2_stret_fixup (struct objc_super *, |
| 3901 | // struct _super_message_ref_t*, ...) |
| 3902 | MessageSendSuper2StretFixupFn = |
| 3903 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3904 | Params, |
| 3905 | true), |
| 3906 | "objc_msgSendSuper2_stret_fixup"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 3907 | |
| 3908 | Params.clear(); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 3909 | Params.push_back(Int8PtrTy); |
| 3910 | UnwindResumeOrRethrowFn = |
| 3911 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3912 | Params, |
| 3913 | false), |
| 3914 | "_Unwind_Resume_or_Rethrow"); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 3915 | ObjCBeginCatchFn = |
| 3916 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy, |
| 3917 | Params, |
| 3918 | false), |
| 3919 | "objc_begin_catch"); |
| 3920 | |
| 3921 | Params.clear(); |
| 3922 | ObjCEndCatchFn = |
| 3923 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3924 | Params, |
| 3925 | false), |
| 3926 | "objc_end_catch"); |
| 3927 | |
| 3928 | // struct objc_typeinfo { |
| 3929 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 3930 | // const char* name; // c++ typeinfo string |
| 3931 | // Class cls; |
| 3932 | // }; |
| 3933 | EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy), |
| 3934 | Int8PtrTy, |
| 3935 | ClassnfABIPtrTy, |
| 3936 | NULL); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 3937 | CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 3938 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3941 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
| 3942 | FinishNonFragileABIModule(); |
| 3943 | |
| 3944 | return NULL; |
| 3945 | } |
| 3946 | |
| 3947 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 3948 | // nonfragile abi has no module definition. |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 3949 | |
| 3950 | // Build list of all implemented classe addresses in array |
| 3951 | // L_OBJC_LABEL_CLASS_$. |
| 3952 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$ |
| 3953 | // list of 'nonlazy' implementations (defined as those with a +load{} |
| 3954 | // method!!). |
| 3955 | unsigned NumClasses = DefinedClasses.size(); |
| 3956 | if (NumClasses) { |
| 3957 | std::vector<llvm::Constant*> Symbols(NumClasses); |
| 3958 | for (unsigned i=0; i<NumClasses; i++) |
| 3959 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 3960 | ObjCTypes.Int8PtrTy); |
| 3961 | llvm::Constant* Init = |
| 3962 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 3963 | NumClasses), |
| 3964 | Symbols); |
| 3965 | |
| 3966 | llvm::GlobalVariable *GV = |
| 3967 | new llvm::GlobalVariable(Init->getType(), false, |
| 3968 | llvm::GlobalValue::InternalLinkage, |
| 3969 | Init, |
| 3970 | "\01L_OBJC_LABEL_CLASS_$", |
| 3971 | &CGM.getModule()); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 3972 | GV->setAlignment(8); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 3973 | GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip"); |
| 3974 | UsedGlobals.push_back(GV); |
| 3975 | } |
| 3976 | |
| 3977 | // Build list of all implemented category addresses in array |
| 3978 | // L_OBJC_LABEL_CATEGORY_$. |
| 3979 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$ |
| 3980 | // list of 'nonlazy' category implementations (defined as those with a +load{} |
| 3981 | // method!!). |
| 3982 | unsigned NumCategory = DefinedCategories.size(); |
| 3983 | if (NumCategory) { |
| 3984 | std::vector<llvm::Constant*> Symbols(NumCategory); |
| 3985 | for (unsigned i=0; i<NumCategory; i++) |
| 3986 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 3987 | ObjCTypes.Int8PtrTy); |
| 3988 | llvm::Constant* Init = |
| 3989 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 3990 | NumCategory), |
| 3991 | Symbols); |
| 3992 | |
| 3993 | llvm::GlobalVariable *GV = |
| 3994 | new llvm::GlobalVariable(Init->getType(), false, |
| 3995 | llvm::GlobalValue::InternalLinkage, |
| 3996 | Init, |
| 3997 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 3998 | &CGM.getModule()); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 3999 | GV->setAlignment(8); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4000 | GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip"); |
| 4001 | UsedGlobals.push_back(GV); |
| 4002 | } |
| 4003 | |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4004 | // static int L_OBJC_IMAGE_INFO[2] = { 0, flags }; |
| 4005 | // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0 |
| 4006 | std::vector<llvm::Constant*> Values(2); |
| 4007 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0); |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4008 | unsigned int flags = 0; |
Fariborz Jahanian | 66a5c2c | 2009-02-24 23:34:44 +0000 | [diff] [blame] | 4009 | // FIXME: Fix and continue? |
| 4010 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 4011 | flags |= eImageInfo_GarbageCollected; |
| 4012 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 4013 | flags |= eImageInfo_GCOnly; |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4014 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4015 | llvm::Constant* Init = llvm::ConstantArray::get( |
| 4016 | llvm::ArrayType::get(ObjCTypes.IntTy, 2), |
| 4017 | Values); |
| 4018 | llvm::GlobalVariable *IMGV = |
| 4019 | new llvm::GlobalVariable(Init->getType(), false, |
| 4020 | llvm::GlobalValue::InternalLinkage, |
| 4021 | Init, |
| 4022 | "\01L_OBJC_IMAGE_INFO", |
| 4023 | &CGM.getModule()); |
| 4024 | IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); |
| 4025 | UsedGlobals.push_back(IMGV); |
| 4026 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4027 | std::vector<llvm::Constant*> Used; |
| 4028 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
| 4029 | e = UsedGlobals.end(); i != e; ++i) { |
| 4030 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
| 4031 | } |
| 4032 | |
| 4033 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
| 4034 | llvm::GlobalValue *GV = |
| 4035 | new llvm::GlobalVariable(AT, false, |
| 4036 | llvm::GlobalValue::AppendingLinkage, |
| 4037 | llvm::ConstantArray::get(AT, Used), |
| 4038 | "llvm.used", |
| 4039 | &CGM.getModule()); |
| 4040 | |
| 4041 | GV->setSection("llvm.metadata"); |
| 4042 | |
| 4043 | } |
| 4044 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4045 | // Metadata flags |
| 4046 | enum MetaDataDlags { |
| 4047 | CLS = 0x0, |
| 4048 | CLS_META = 0x1, |
| 4049 | CLS_ROOT = 0x2, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4050 | OBJC2_CLS_HIDDEN = 0x10, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4051 | CLS_EXCEPTION = 0x20 |
| 4052 | }; |
| 4053 | /// BuildClassRoTInitializer - generate meta-data for: |
| 4054 | /// struct _class_ro_t { |
| 4055 | /// uint32_t const flags; |
| 4056 | /// uint32_t const instanceStart; |
| 4057 | /// uint32_t const instanceSize; |
| 4058 | /// uint32_t const reserved; // only when building for 64bit targets |
| 4059 | /// const uint8_t * const ivarLayout; |
| 4060 | /// const char *const name; |
| 4061 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4062 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4063 | /// const struct _ivar_list_t *const ivars; |
| 4064 | /// const uint8_t * const weakIvarLayout; |
| 4065 | /// const struct _prop_list_t * const properties; |
| 4066 | /// } |
| 4067 | /// |
| 4068 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
| 4069 | unsigned flags, |
| 4070 | unsigned InstanceStart, |
| 4071 | unsigned InstanceSize, |
| 4072 | const ObjCImplementationDecl *ID) { |
| 4073 | std::string ClassName = ID->getNameAsString(); |
| 4074 | std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets! |
| 4075 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 4076 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 4077 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
| 4078 | // FIXME. For 64bit targets add 0 here. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4079 | // FIXME. ivarLayout is currently null! |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 4080 | Values[ 3] = GetIvarLayoutName(0, ObjCTypes); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4081 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4082 | // const struct _method_list_t * const baseMethods; |
| 4083 | std::vector<llvm::Constant*> Methods; |
| 4084 | std::string MethodListName("\01l_OBJC_$_"); |
| 4085 | if (flags & CLS_META) { |
| 4086 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
| 4087 | for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(), |
| 4088 | e = ID->classmeth_end(); i != e; ++i) { |
| 4089 | // Class methods should always be defined. |
| 4090 | Methods.push_back(GetMethodConstant(*i)); |
| 4091 | } |
| 4092 | } else { |
| 4093 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
| 4094 | for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(), |
| 4095 | e = ID->instmeth_end(); i != e; ++i) { |
| 4096 | // Instance methods should always be defined. |
| 4097 | Methods.push_back(GetMethodConstant(*i)); |
| 4098 | } |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4099 | for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(), |
| 4100 | e = ID->propimpl_end(); i != e; ++i) { |
| 4101 | ObjCPropertyImplDecl *PID = *i; |
| 4102 | |
| 4103 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 4104 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 4105 | |
| 4106 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 4107 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4108 | Methods.push_back(C); |
| 4109 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 4110 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4111 | Methods.push_back(C); |
| 4112 | } |
| 4113 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4114 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4115 | Values[ 5] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4116 | "__DATA, __objc_const", Methods); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4117 | |
| 4118 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4119 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
| 4120 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
| 4121 | + OID->getNameAsString(), |
| 4122 | OID->protocol_begin(), |
| 4123 | OID->protocol_end()); |
| 4124 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4125 | if (flags & CLS_META) |
| 4126 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4127 | else |
| 4128 | Values[ 7] = EmitIvarList(ID); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4129 | // FIXME. weakIvarLayout is currently null. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4130 | Values[ 8] = GetIvarLayoutName(0, ObjCTypes); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4131 | if (flags & CLS_META) |
| 4132 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4133 | else |
| 4134 | Values[ 9] = |
| 4135 | EmitPropertyList( |
| 4136 | "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
| 4137 | ID, ID->getClassInterface(), ObjCTypes); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4138 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
| 4139 | Values); |
| 4140 | llvm::GlobalVariable *CLASS_RO_GV = |
| 4141 | new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false, |
| 4142 | llvm::GlobalValue::InternalLinkage, |
| 4143 | Init, |
| 4144 | (flags & CLS_META) ? |
| 4145 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 4146 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName, |
| 4147 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4148 | CLASS_RO_GV->setAlignment( |
| 4149 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4150 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4151 | return CLASS_RO_GV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4152 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4153 | } |
| 4154 | |
| 4155 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 4156 | /// for the given ClassName for: |
| 4157 | /// struct _class_t { |
| 4158 | /// struct _class_t *isa; |
| 4159 | /// struct _class_t * const superclass; |
| 4160 | /// void *cache; |
| 4161 | /// IMP *vtable; |
| 4162 | /// struct class_ro_t *ro; |
| 4163 | /// } |
| 4164 | /// |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4165 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
| 4166 | std::string &ClassName, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4167 | llvm::Constant *IsAGV, |
| 4168 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4169 | llvm::Constant *ClassRoGV, |
| 4170 | bool HiddenVisibility) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4171 | std::vector<llvm::Constant*> Values(5); |
| 4172 | Values[0] = IsAGV; |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4173 | Values[1] = SuperClassGV |
| 4174 | ? SuperClassGV |
| 4175 | : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4176 | Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar |
| 4177 | Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar |
| 4178 | Values[4] = ClassRoGV; // &CLASS_RO_GV |
| 4179 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
| 4180 | Values); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4181 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 4182 | GV->setInitializer(Init); |
Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 4183 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4184 | GV->setAlignment( |
| 4185 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4186 | if (HiddenVisibility) |
| 4187 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4188 | return GV; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4189 | } |
| 4190 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4191 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 4192 | std::string ClassName = ID->getNameAsString(); |
| 4193 | if (!ObjCEmptyCacheVar) { |
| 4194 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4195 | ObjCTypes.CacheTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4196 | false, |
| 4197 | llvm::GlobalValue::ExternalLinkage, |
| 4198 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4199 | "_objc_empty_cache", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4200 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4201 | |
| 4202 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4203 | ObjCTypes.ImpnfABITy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4204 | false, |
| 4205 | llvm::GlobalValue::ExternalLinkage, |
| 4206 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4207 | "_objc_empty_vtable", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4208 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4209 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4210 | assert(ID->getClassInterface() && |
| 4211 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4212 | uint32_t InstanceStart = |
| 4213 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy); |
| 4214 | uint32_t InstanceSize = InstanceStart; |
| 4215 | uint32_t flags = CLS_META; |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4216 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 4217 | std::string ObjCClassName(getClassSymbolPrefix()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4218 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4219 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4220 | |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4221 | bool classIsHidden = IsClassHidden(ID->getClassInterface()); |
| 4222 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4223 | flags |= OBJC2_CLS_HIDDEN; |
| 4224 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4225 | // class is root |
| 4226 | flags |= CLS_ROOT; |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4227 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4228 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName, false); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4229 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4230 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4231 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 4232 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 4233 | Root = Super; |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4234 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString(), false); |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4235 | // work on super class metadata symbol. |
| 4236 | std::string SuperClassName = |
| 4237 | ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4238 | SuperClassGV = GetClassGlobal(SuperClassName, false); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4239 | } |
| 4240 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 4241 | InstanceStart, |
| 4242 | InstanceSize,ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4243 | std::string TClassName = ObjCMetaClassName + ClassName; |
| 4244 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4245 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 4246 | classIsHidden); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4247 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4248 | // Metadata for the class |
| 4249 | flags = CLS; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4250 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4251 | flags |= OBJC2_CLS_HIDDEN; |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame^] | 4252 | |
| 4253 | if (hasObjCExceptionAttribute(ID->getClassInterface())) |
| 4254 | flags |= CLS_EXCEPTION; |
| 4255 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4256 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4257 | flags |= CLS_ROOT; |
| 4258 | SuperClassGV = 0; |
| 4259 | } |
| 4260 | else { |
| 4261 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4262 | std::string RootClassName = |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4263 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4264 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4265 | } |
Fariborz Jahanian | ebf9ed3 | 2009-03-20 20:48:19 +0000 | [diff] [blame] | 4266 | // FIXME: Gross |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4267 | InstanceStart = InstanceSize = 0; |
| 4268 | if (ObjCInterfaceDecl *OID = |
| 4269 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) { |
| 4270 | // FIXME. Share this with the one in EmitIvarList. |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 4271 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID); |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4272 | |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 4273 | RecordDecl::field_iterator firstField, lastField; |
| 4274 | const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField); |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4275 | |
| 4276 | for (RecordDecl::field_iterator e = RD->field_end(), |
| 4277 | ifield = firstField; ifield != e; ++ifield) |
| 4278 | lastField = ifield; |
| 4279 | |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4280 | if (lastField != RD->field_end()) { |
| 4281 | FieldDecl *Field = *lastField; |
| 4282 | const llvm::Type *FieldTy = |
| 4283 | CGM.getTypes().ConvertTypeForMem(Field->getType()); |
| 4284 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 4285 | InstanceSize = GetIvarBaseOffset(Layout, Field) + Size; |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4286 | if (firstField == RD->field_end()) |
| 4287 | InstanceStart = InstanceSize; |
Fariborz Jahanian | 11894a4 | 2009-03-07 19:43:20 +0000 | [diff] [blame] | 4288 | else { |
| 4289 | Field = *firstField; |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 4290 | InstanceStart = GetIvarBaseOffset(Layout, Field); |
Fariborz Jahanian | 11894a4 | 2009-03-07 19:43:20 +0000 | [diff] [blame] | 4291 | } |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4292 | } |
| 4293 | } |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4294 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4295 | InstanceStart, |
| 4296 | InstanceSize, |
| 4297 | ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4298 | |
| 4299 | TClassName = ObjCClassName + ClassName; |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4300 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4301 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 4302 | classIsHidden); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4303 | UsedGlobals.push_back(ClassMD); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4304 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame^] | 4305 | |
| 4306 | // Force the definition of the EHType if necessary. |
| 4307 | if (flags & CLS_EXCEPTION) |
| 4308 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4309 | } |
| 4310 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4311 | /// GenerateProtocolRef - This routine is called to generate code for |
| 4312 | /// a protocol reference expression; as in: |
| 4313 | /// @code |
| 4314 | /// @protocol(Proto1); |
| 4315 | /// @endcode |
| 4316 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 4317 | /// which will hold address of the protocol meta-data. |
| 4318 | /// |
| 4319 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
| 4320 | const ObjCProtocolDecl *PD) { |
| 4321 | |
| 4322 | llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
| 4323 | ObjCTypes.ExternalProtocolPtrTy); |
| 4324 | |
| 4325 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
| 4326 | ProtocolName += PD->getNameAsCString(); |
| 4327 | |
| 4328 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 4329 | if (PTGV) |
| 4330 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4331 | PTGV = new llvm::GlobalVariable( |
| 4332 | Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4333 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4334 | Init, |
| 4335 | ProtocolName, |
| 4336 | &CGM.getModule()); |
| 4337 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 4338 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4339 | UsedGlobals.push_back(PTGV); |
| 4340 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4341 | } |
| 4342 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4343 | /// GenerateCategory - Build metadata for a category implementation. |
| 4344 | /// struct _category_t { |
| 4345 | /// const char * const name; |
| 4346 | /// struct _class_t *const cls; |
| 4347 | /// const struct _method_list_t * const instance_methods; |
| 4348 | /// const struct _method_list_t * const class_methods; |
| 4349 | /// const struct _protocol_list_t * const protocols; |
| 4350 | /// const struct _prop_list_t * const properties; |
| 4351 | /// } |
| 4352 | /// |
| 4353 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) |
| 4354 | { |
| 4355 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4356 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
| 4357 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4358 | "_$_" + OCD->getNameAsString()); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4359 | std::string ExtClassName(getClassSymbolPrefix() + |
| 4360 | Interface->getNameAsString()); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4361 | |
| 4362 | std::vector<llvm::Constant*> Values(6); |
| 4363 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 4364 | // meta-class entry symbol |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4365 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4366 | Values[1] = ClassGV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4367 | std::vector<llvm::Constant*> Methods; |
| 4368 | std::string MethodListName(Prefix); |
| 4369 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
| 4370 | "_$_" + OCD->getNameAsString(); |
| 4371 | |
| 4372 | for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(), |
| 4373 | e = OCD->instmeth_end(); i != e; ++i) { |
| 4374 | // Instance methods should always be defined. |
| 4375 | Methods.push_back(GetMethodConstant(*i)); |
| 4376 | } |
| 4377 | |
| 4378 | Values[2] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4379 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4380 | Methods); |
| 4381 | |
| 4382 | MethodListName = Prefix; |
| 4383 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 4384 | OCD->getNameAsString(); |
| 4385 | Methods.clear(); |
| 4386 | for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(), |
| 4387 | e = OCD->classmeth_end(); i != e; ++i) { |
| 4388 | // Class methods should always be defined. |
| 4389 | Methods.push_back(GetMethodConstant(*i)); |
| 4390 | } |
| 4391 | |
| 4392 | Values[3] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4393 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4394 | Methods); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4395 | const ObjCCategoryDecl *Category = |
| 4396 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4397 | if (Category) { |
| 4398 | std::string ExtName(Interface->getNameAsString() + "_$_" + |
| 4399 | OCD->getNameAsString()); |
| 4400 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
| 4401 | + Interface->getNameAsString() + "_$_" |
| 4402 | + Category->getNameAsString(), |
| 4403 | Category->protocol_begin(), |
| 4404 | Category->protocol_end()); |
| 4405 | Values[5] = |
| 4406 | EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
| 4407 | OCD, Category, ObjCTypes); |
| 4408 | } |
| 4409 | else { |
| 4410 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4411 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4412 | } |
| 4413 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4414 | llvm::Constant *Init = |
| 4415 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
| 4416 | Values); |
| 4417 | llvm::GlobalVariable *GCATV |
| 4418 | = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy, |
| 4419 | false, |
| 4420 | llvm::GlobalValue::InternalLinkage, |
| 4421 | Init, |
| 4422 | ExtCatName, |
| 4423 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4424 | GCATV->setAlignment( |
| 4425 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4426 | GCATV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4427 | UsedGlobals.push_back(GCATV); |
| 4428 | DefinedCategories.push_back(GCATV); |
| 4429 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4430 | |
| 4431 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 4432 | /// given method if it has been defined. The result is null if the |
| 4433 | /// method has not been defined. The return value has type MethodPtrTy. |
| 4434 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
| 4435 | const ObjCMethodDecl *MD) { |
| 4436 | // FIXME: Use DenseMap::lookup |
| 4437 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 4438 | if (!Fn) |
| 4439 | return 0; |
| 4440 | |
| 4441 | std::vector<llvm::Constant*> Method(3); |
| 4442 | Method[0] = |
| 4443 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4444 | ObjCTypes.SelectorPtrTy); |
| 4445 | Method[1] = GetMethodVarType(MD); |
| 4446 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 4447 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 4448 | } |
| 4449 | |
| 4450 | /// EmitMethodList - Build meta-data for method declarations |
| 4451 | /// struct _method_list_t { |
| 4452 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 4453 | /// uint32_t method_count; |
| 4454 | /// struct _objc_method method_list[method_count]; |
| 4455 | /// } |
| 4456 | /// |
| 4457 | llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList( |
| 4458 | const std::string &Name, |
| 4459 | const char *Section, |
| 4460 | const ConstantVector &Methods) { |
| 4461 | // Return null for empty list. |
| 4462 | if (Methods.empty()) |
| 4463 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
| 4464 | |
| 4465 | std::vector<llvm::Constant*> Values(3); |
| 4466 | // sizeof(struct _objc_method) |
| 4467 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy); |
| 4468 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4469 | // method_count |
| 4470 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 4471 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 4472 | Methods.size()); |
| 4473 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 4474 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4475 | |
| 4476 | llvm::GlobalVariable *GV = |
| 4477 | new llvm::GlobalVariable(Init->getType(), false, |
| 4478 | llvm::GlobalValue::InternalLinkage, |
| 4479 | Init, |
| 4480 | Name, |
| 4481 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4482 | GV->setAlignment( |
| 4483 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4484 | GV->setSection(Section); |
| 4485 | UsedGlobals.push_back(GV); |
| 4486 | return llvm::ConstantExpr::getBitCast(GV, |
| 4487 | ObjCTypes.MethodListnfABIPtrTy); |
| 4488 | } |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4489 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4490 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 4491 | /// the given ivar. |
| 4492 | /// |
| 4493 | llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable( |
| 4494 | std::string &Name, |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 4495 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4496 | const ObjCIvarDecl *Ivar) { |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 4497 | Name += "\01_OBJC_IVAR_$_" + |
| 4498 | getInterfaceDeclForIvar(ID, Ivar)->getNameAsString() + '.' |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4499 | + Ivar->getNameAsString(); |
| 4500 | llvm::GlobalVariable *IvarOffsetGV = |
| 4501 | CGM.getModule().getGlobalVariable(Name); |
| 4502 | if (!IvarOffsetGV) |
| 4503 | IvarOffsetGV = |
| 4504 | new llvm::GlobalVariable(ObjCTypes.LongTy, |
| 4505 | false, |
| 4506 | llvm::GlobalValue::ExternalLinkage, |
| 4507 | 0, |
| 4508 | Name, |
| 4509 | &CGM.getModule()); |
| 4510 | return IvarOffsetGV; |
| 4511 | } |
| 4512 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4513 | llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar( |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4514 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4515 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4516 | unsigned long int Offset) { |
| 4517 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4518 | assert(ID && "EmitIvarOffsetVar - null interface decl."); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4519 | std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.' |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4520 | + Ivar->getNameAsString()); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4521 | llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.LongTy, Offset); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4522 | llvm::GlobalVariable *IvarOffsetGV = |
| 4523 | CGM.getModule().getGlobalVariable(ExternalName); |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4524 | if (IvarOffsetGV) |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4525 | // ivar offset symbol already built due to user code referencing it. |
| 4526 | IvarOffsetGV->setInitializer(Init); |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4527 | else |
| 4528 | IvarOffsetGV = |
| 4529 | new llvm::GlobalVariable(Init->getType(), |
| 4530 | false, |
| 4531 | llvm::GlobalValue::ExternalLinkage, |
| 4532 | Init, |
| 4533 | ExternalName, |
| 4534 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4535 | IvarOffsetGV->setAlignment( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4536 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy)); |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4537 | // @private and @package have hidden visibility. |
| 4538 | bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public || |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4539 | Ivar->getAccessControl() == ObjCIvarDecl::Protected); |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4540 | if (!globalVisibility) |
| 4541 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4542 | else if (IsClassHidden(ID)) |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4543 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4544 | else if (CGM.getLangOptions().getVisibilityMode() == |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4545 | LangOptions::DefaultVisibility) |
| 4546 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4547 | IvarOffsetGV->setSection("__DATA, __objc_const"); |
| 4548 | UsedGlobals.push_back(IvarOffsetGV); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4549 | return IvarOffsetGV; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4550 | } |
| 4551 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4552 | /// EmitIvarList - Emit the ivar list for the given |
| 4553 | /// implementation. If ForClass is true the list of class ivars |
| 4554 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 4555 | /// interface ivars will be emitted. The return value has type |
| 4556 | /// IvarListnfABIPtrTy. |
| 4557 | /// struct _ivar_t { |
| 4558 | /// unsigned long int *offset; // pointer to ivar offset location |
| 4559 | /// char *name; |
| 4560 | /// char *type; |
| 4561 | /// uint32_t alignment; |
| 4562 | /// uint32_t size; |
| 4563 | /// } |
| 4564 | /// struct _ivar_list_t { |
| 4565 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 4566 | /// uint32 count; |
| 4567 | /// struct _iver_t list[count]; |
| 4568 | /// } |
| 4569 | /// |
| 4570 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
| 4571 | const ObjCImplementationDecl *ID) { |
| 4572 | |
| 4573 | std::vector<llvm::Constant*> Ivars, Ivar(5); |
| 4574 | |
| 4575 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4576 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
| 4577 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4578 | // FIXME. Consolidate this with similar code in GenerateClass. |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 4579 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 4580 | |
| 4581 | RecordDecl::field_iterator i,p; |
| 4582 | const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p); |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4583 | // collect declared and synthesized ivars in a small vector. |
| 4584 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
| 4585 | for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), |
| 4586 | E = OID->ivar_end(); I != E; ++I) |
| 4587 | OIvars.push_back(*I); |
| 4588 | for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(), |
| 4589 | E = OID->prop_end(); I != E; ++I) |
| 4590 | if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl()) |
| 4591 | OIvars.push_back(IV); |
Fariborz Jahanian | 99eee36 | 2009-04-01 19:37:34 +0000 | [diff] [blame] | 4592 | |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4593 | unsigned iv = 0; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4594 | for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) { |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4595 | FieldDecl *Field = *i; |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 4596 | uint64_t offset = GetIvarBaseOffset(Layout, Field); |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4597 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), OIvars[iv++], offset); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4598 | if (Field->getIdentifier()) |
| 4599 | Ivar[1] = GetMethodVarName(Field->getIdentifier()); |
| 4600 | else |
| 4601 | Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4602 | Ivar[2] = GetMethodVarType(Field); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4603 | const llvm::Type *FieldTy = |
| 4604 | CGM.getTypes().ConvertTypeForMem(Field->getType()); |
| 4605 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
| 4606 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
| 4607 | Field->getType().getTypePtr()) >> 3; |
| 4608 | Align = llvm::Log2_32(Align); |
| 4609 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Fariborz Jahanian | 07236ba | 2009-01-27 22:27:56 +0000 | [diff] [blame] | 4610 | // NOTE. Size of a bitfield does not match gcc's, because of the way |
| 4611 | // bitfields are treated special in each. But I am told that 'size' |
| 4612 | // for bitfield ivars is ignored by the runtime so it does not matter. |
| 4613 | // (even if it matters, some day, there is enough info. to get the bitfield |
| 4614 | // right! |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4615 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4616 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
| 4617 | } |
| 4618 | // Return null for empty list. |
| 4619 | if (Ivars.empty()) |
| 4620 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4621 | std::vector<llvm::Constant*> Values(3); |
| 4622 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy); |
| 4623 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4624 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 4625 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
| 4626 | Ivars.size()); |
| 4627 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
| 4628 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4629 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 4630 | llvm::GlobalVariable *GV = |
| 4631 | new llvm::GlobalVariable(Init->getType(), false, |
| 4632 | llvm::GlobalValue::InternalLinkage, |
| 4633 | Init, |
| 4634 | Prefix + OID->getNameAsString(), |
| 4635 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4636 | GV->setAlignment( |
| 4637 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4638 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4639 | |
| 4640 | UsedGlobals.push_back(GV); |
| 4641 | return llvm::ConstantExpr::getBitCast(GV, |
| 4642 | ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4643 | } |
| 4644 | |
| 4645 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
| 4646 | const ObjCProtocolDecl *PD) { |
| 4647 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4648 | |
| 4649 | if (!Entry) { |
| 4650 | // We use the initializer as a marker of whether this is a forward |
| 4651 | // reference or not. At module finalization we add the empty |
| 4652 | // contents for protocols which were referenced but never defined. |
| 4653 | Entry = |
| 4654 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
| 4655 | llvm::GlobalValue::ExternalLinkage, |
| 4656 | 0, |
| 4657 | "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(), |
| 4658 | &CGM.getModule()); |
| 4659 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 4660 | UsedGlobals.push_back(Entry); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4661 | } |
| 4662 | |
| 4663 | return Entry; |
| 4664 | } |
| 4665 | |
| 4666 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 4667 | /// @code |
| 4668 | /// struct _protocol_t { |
| 4669 | /// id isa; // NULL |
| 4670 | /// const char * const protocol_name; |
| 4671 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 4672 | /// const struct method_list_t * const instance_methods; |
| 4673 | /// const struct method_list_t * const class_methods; |
| 4674 | /// const struct method_list_t *optionalInstanceMethods; |
| 4675 | /// const struct method_list_t *optionalClassMethods; |
| 4676 | /// const struct _prop_list_t * properties; |
| 4677 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 4678 | /// const uint32_t flags; // = 0 |
| 4679 | /// } |
| 4680 | /// @endcode |
| 4681 | /// |
| 4682 | |
| 4683 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
| 4684 | const ObjCProtocolDecl *PD) { |
| 4685 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4686 | |
| 4687 | // Early exit if a defining object has already been generated. |
| 4688 | if (Entry && Entry->hasInitializer()) |
| 4689 | return Entry; |
| 4690 | |
| 4691 | const char *ProtocolName = PD->getNameAsCString(); |
| 4692 | |
| 4693 | // Construct method lists. |
| 4694 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 4695 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
| 4696 | for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(), |
| 4697 | e = PD->instmeth_end(); i != e; ++i) { |
| 4698 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4699 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4700 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4701 | OptInstanceMethods.push_back(C); |
| 4702 | } else { |
| 4703 | InstanceMethods.push_back(C); |
| 4704 | } |
| 4705 | } |
| 4706 | |
| 4707 | for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(), |
| 4708 | e = PD->classmeth_end(); i != e; ++i) { |
| 4709 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4710 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4711 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4712 | OptClassMethods.push_back(C); |
| 4713 | } else { |
| 4714 | ClassMethods.push_back(C); |
| 4715 | } |
| 4716 | } |
| 4717 | |
| 4718 | std::vector<llvm::Constant*> Values(10); |
| 4719 | // isa is NULL |
| 4720 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
| 4721 | Values[1] = GetClassName(PD->getIdentifier()); |
| 4722 | Values[2] = EmitProtocolList( |
| 4723 | "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(), |
| 4724 | PD->protocol_begin(), |
| 4725 | PD->protocol_end()); |
| 4726 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4727 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4728 | + PD->getNameAsString(), |
| 4729 | "__DATA, __objc_const", |
| 4730 | InstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4731 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4732 | + PD->getNameAsString(), |
| 4733 | "__DATA, __objc_const", |
| 4734 | ClassMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4735 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4736 | + PD->getNameAsString(), |
| 4737 | "__DATA, __objc_const", |
| 4738 | OptInstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4739 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4740 | + PD->getNameAsString(), |
| 4741 | "__DATA, __objc_const", |
| 4742 | OptClassMethods); |
| 4743 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(), |
| 4744 | 0, PD, ObjCTypes); |
| 4745 | uint32_t Size = |
| 4746 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy); |
| 4747 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4748 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
| 4749 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
| 4750 | Values); |
| 4751 | |
| 4752 | if (Entry) { |
| 4753 | // Already created, fix the linkage and update the initializer. |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4754 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4755 | Entry->setInitializer(Init); |
| 4756 | } else { |
| 4757 | Entry = |
| 4758 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4759 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4760 | Init, |
| 4761 | std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName, |
| 4762 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4763 | Entry->setAlignment( |
| 4764 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4765 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4766 | } |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4767 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4768 | |
| 4769 | // Use this protocol meta-data to build protocol list table in section |
| 4770 | // __DATA, __objc_protolist |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4771 | llvm::GlobalVariable *PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4772 | ObjCTypes.ProtocolnfABIPtrTy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4773 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4774 | Entry, |
| 4775 | std::string("\01l_OBJC_LABEL_PROTOCOL_$_") |
| 4776 | +ProtocolName, |
| 4777 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4778 | PTGV->setAlignment( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4779 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4780 | PTGV->setSection("__DATA, __objc_protolist"); |
| 4781 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4782 | UsedGlobals.push_back(PTGV); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4783 | return Entry; |
| 4784 | } |
| 4785 | |
| 4786 | /// EmitProtocolList - Generate protocol list meta-data: |
| 4787 | /// @code |
| 4788 | /// struct _protocol_list_t { |
| 4789 | /// long protocol_count; // Note, this is 32/64 bit |
| 4790 | /// struct _protocol_t[protocol_count]; |
| 4791 | /// } |
| 4792 | /// @endcode |
| 4793 | /// |
| 4794 | llvm::Constant * |
| 4795 | CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name, |
| 4796 | ObjCProtocolDecl::protocol_iterator begin, |
| 4797 | ObjCProtocolDecl::protocol_iterator end) { |
| 4798 | std::vector<llvm::Constant*> ProtocolRefs; |
| 4799 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4800 | // Just return null for empty protocol lists |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4801 | if (begin == end) |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4802 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4803 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4804 | // FIXME: We shouldn't need to do this lookup here, should we? |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4805 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
| 4806 | if (GV) |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4807 | return llvm::ConstantExpr::getBitCast(GV, |
| 4808 | ObjCTypes.ProtocolListnfABIPtrTy); |
| 4809 | |
| 4810 | for (; begin != end; ++begin) |
| 4811 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 4812 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4813 | // This list is null terminated. |
| 4814 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4815 | ObjCTypes.ProtocolnfABIPtrTy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4816 | |
| 4817 | std::vector<llvm::Constant*> Values(2); |
| 4818 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 4819 | Values[1] = |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4820 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4821 | ProtocolRefs.size()), |
| 4822 | ProtocolRefs); |
| 4823 | |
| 4824 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4825 | GV = new llvm::GlobalVariable(Init->getType(), false, |
| 4826 | llvm::GlobalValue::InternalLinkage, |
| 4827 | Init, |
| 4828 | Name, |
| 4829 | &CGM.getModule()); |
| 4830 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4831 | GV->setAlignment( |
| 4832 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4833 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4834 | return llvm::ConstantExpr::getBitCast(GV, |
| 4835 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4836 | } |
| 4837 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4838 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 4839 | /// struct _objc_method { |
| 4840 | /// SEL _cmd; |
| 4841 | /// char *method_type; |
| 4842 | /// char *_imp; |
| 4843 | /// } |
| 4844 | |
| 4845 | llvm::Constant * |
| 4846 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 4847 | std::vector<llvm::Constant*> Desc(3); |
| 4848 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4849 | ObjCTypes.SelectorPtrTy); |
| 4850 | Desc[1] = GetMethodVarType(MD); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4851 | // Protocol methods have no implementation. So, this entry is always NULL. |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4852 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 4853 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
| 4854 | } |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4855 | |
| 4856 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 4857 | /// This code gen. amounts to generating code for: |
| 4858 | /// @code |
| 4859 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 4860 | /// @encode |
| 4861 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4862 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4863 | CodeGen::CodeGenFunction &CGF, |
| 4864 | QualType ObjectTy, |
| 4865 | llvm::Value *BaseValue, |
| 4866 | const ObjCIvarDecl *Ivar, |
| 4867 | const FieldDecl *Field, |
| 4868 | unsigned CVRQualifiers) { |
| 4869 | assert(ObjectTy->isObjCInterfaceType() && |
| 4870 | "CGObjCNonFragileABIMac::EmitObjCValueForIvar"); |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 4871 | ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4872 | std::string ExternalName; |
| 4873 | llvm::GlobalVariable *IvarOffsetGV = |
| 4874 | ObjCIvarOffsetVariable(ExternalName, ID, Ivar); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4875 | |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4876 | // (char *) BaseValue |
| 4877 | llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, |
| 4878 | ObjCTypes.Int8PtrTy); |
| 4879 | llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV); |
| 4880 | // (char*)BaseValue + Offset_symbol |
| 4881 | V = CGF.Builder.CreateGEP(V, Offset, "add.ptr"); |
| 4882 | // (type *)((char*)BaseValue + Offset_symbol) |
| 4883 | const llvm::Type *IvarTy = |
| 4884 | CGM.getTypes().ConvertType(Ivar->getType()); |
| 4885 | llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy); |
| 4886 | V = CGF.Builder.CreateBitCast(V, ptrIvarTy); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4887 | |
Fariborz Jahanian | a6681ae | 2009-03-09 20:44:22 +0000 | [diff] [blame] | 4888 | if (Ivar->isBitField()) { |
| 4889 | CodeGenTypes::BitFieldInfo bitFieldInfo = |
| 4890 | CGM.getTypes().getBitFieldInfo(Field); |
| 4891 | return LValue::MakeBitfield(V, bitFieldInfo.Begin, bitFieldInfo.Size, |
| 4892 | Field->getType()->isSignedIntegerType(), |
| 4893 | Field->getType().getCVRQualifiers()|CVRQualifiers); |
| 4894 | } |
| 4895 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4896 | LValue LV = LValue::MakeAddr(V, |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 4897 | Ivar->getType().getCVRQualifiers()|CVRQualifiers, |
| 4898 | CGM.getContext().getObjCGCAttrKind(Ivar->getType())); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4899 | LValue::SetObjCIvar(LV, true); |
| 4900 | return LV; |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4901 | } |
| 4902 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4903 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
| 4904 | CodeGen::CodeGenFunction &CGF, |
| 4905 | ObjCInterfaceDecl *Interface, |
| 4906 | const ObjCIvarDecl *Ivar) { |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4907 | std::string ExternalName; |
| 4908 | llvm::GlobalVariable *IvarOffsetGV = |
| 4909 | ObjCIvarOffsetVariable(ExternalName, Interface, Ivar); |
| 4910 | |
| 4911 | return CGF.Builder.CreateLoad(IvarOffsetGV, false, "ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4912 | } |
| 4913 | |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4914 | CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend( |
| 4915 | CodeGen::CodeGenFunction &CGF, |
| 4916 | QualType ResultType, |
| 4917 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4918 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4919 | QualType Arg0Ty, |
| 4920 | bool IsSuper, |
| 4921 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4922 | // FIXME. Even though IsSuper is passes. This function doese not |
| 4923 | // handle calls to 'super' receivers. |
| 4924 | CodeGenTypes &Types = CGM.getTypes(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4925 | llvm::Value *Arg0 = Receiver; |
| 4926 | if (!IsSuper) |
| 4927 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4928 | |
| 4929 | // Find the message function name. |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 4930 | // FIXME. This is too much work to get the ABI-specific result type |
| 4931 | // needed to find the message name. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4932 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, |
| 4933 | llvm::SmallVector<QualType, 16>()); |
| 4934 | llvm::Constant *Fn; |
| 4935 | std::string Name("\01l_"); |
| 4936 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4937 | #if 0 |
| 4938 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4939 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
| 4940 | Fn = ObjCTypes.MessageSendIdStretFixupFn; |
| 4941 | // FIXME. Is there a better way of getting these names. |
| 4942 | // They are available in RuntimeFunctions vector pair. |
| 4943 | Name += "objc_msgSendId_stret_fixup"; |
| 4944 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4945 | else |
| 4946 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4947 | if (IsSuper) { |
| 4948 | Fn = ObjCTypes.MessageSendSuper2StretFixupFn; |
| 4949 | Name += "objc_msgSendSuper2_stret_fixup"; |
| 4950 | } |
| 4951 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4952 | { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4953 | Fn = ObjCTypes.MessageSendStretFixupFn; |
| 4954 | Name += "objc_msgSend_stret_fixup"; |
| 4955 | } |
| 4956 | } |
Fariborz Jahanian | 1a6b368 | 2009-02-05 19:35:43 +0000 | [diff] [blame] | 4957 | else if (ResultType->isFloatingType() && |
| 4958 | // Selection of frret API only happens in 32bit nonfragile ABI. |
| 4959 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4960 | Fn = ObjCTypes.MessageSendFpretFixupFn; |
| 4961 | Name += "objc_msgSend_fpret_fixup"; |
| 4962 | } |
| 4963 | else { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4964 | #if 0 |
| 4965 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4966 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
| 4967 | Fn = ObjCTypes.MessageSendIdFixupFn; |
| 4968 | Name += "objc_msgSendId_fixup"; |
| 4969 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4970 | else |
| 4971 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4972 | if (IsSuper) { |
| 4973 | Fn = ObjCTypes.MessageSendSuper2FixupFn; |
| 4974 | Name += "objc_msgSendSuper2_fixup"; |
| 4975 | } |
| 4976 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4977 | { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4978 | Fn = ObjCTypes.MessageSendFixupFn; |
| 4979 | Name += "objc_msgSend_fixup"; |
| 4980 | } |
| 4981 | } |
| 4982 | Name += '_'; |
| 4983 | std::string SelName(Sel.getAsString()); |
| 4984 | // Replace all ':' in selector name with '_' ouch! |
| 4985 | for(unsigned i = 0; i < SelName.size(); i++) |
| 4986 | if (SelName[i] == ':') |
| 4987 | SelName[i] = '_'; |
| 4988 | Name += SelName; |
| 4989 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 4990 | if (!GV) { |
| 4991 | // Build messafe ref table entry. |
| 4992 | std::vector<llvm::Constant*> Values(2); |
| 4993 | Values[0] = Fn; |
| 4994 | Values[1] = GetMethodVarName(Sel); |
| 4995 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4996 | GV = new llvm::GlobalVariable(Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4997 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4998 | Init, |
| 4999 | Name, |
| 5000 | &CGM.getModule()); |
| 5001 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 5002 | GV->setAlignment( |
| 5003 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.MessageRefTy)); |
| 5004 | GV->setSection("__DATA, __objc_msgrefs, coalesced"); |
| 5005 | UsedGlobals.push_back(GV); |
| 5006 | } |
| 5007 | llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5008 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5009 | CallArgList ActualArgs; |
| 5010 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 5011 | ActualArgs.push_back(std::make_pair(RValue::get(Arg1), |
| 5012 | ObjCTypes.MessageRefCPtrTy)); |
| 5013 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5014 | const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs); |
| 5015 | llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0); |
| 5016 | Callee = CGF.Builder.CreateLoad(Callee); |
Fariborz Jahanian | 3ab75bd | 2009-02-14 21:25:36 +0000 | [diff] [blame] | 5017 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5018 | Callee = CGF.Builder.CreateBitCast(Callee, |
| 5019 | llvm::PointerType::getUnqual(FTy)); |
| 5020 | return CGF.EmitCall(FnInfo1, Callee, ActualArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5021 | } |
| 5022 | |
| 5023 | /// Generate code for a message send expression in the nonfragile abi. |
| 5024 | CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend( |
| 5025 | CodeGen::CodeGenFunction &CGF, |
| 5026 | QualType ResultType, |
| 5027 | Selector Sel, |
| 5028 | llvm::Value *Receiver, |
| 5029 | bool IsClassMessage, |
| 5030 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5031 | return EmitMessageSend(CGF, ResultType, Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5032 | Receiver, CGF.getContext().getObjCIdType(), |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5033 | false, CallArgs); |
| 5034 | } |
| 5035 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5036 | llvm::GlobalVariable * |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5037 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name, |
| 5038 | bool AddToUsed) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5039 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5040 | |
Daniel Dunbar | dfff230 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 5041 | if (!GV) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5042 | GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 5043 | llvm::GlobalValue::ExternalLinkage, |
| 5044 | 0, Name, &CGM.getModule()); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5045 | if (AddToUsed) |
| 5046 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5047 | } |
| 5048 | |
| 5049 | return GV; |
| 5050 | } |
| 5051 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5052 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5053 | const ObjCInterfaceDecl *ID, |
| 5054 | bool IsSuper) { |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5055 | |
| 5056 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 5057 | |
| 5058 | if (!Entry) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5059 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5060 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5061 | Entry = |
| 5062 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5063 | llvm::GlobalValue::InternalLinkage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5064 | ClassGV, |
| 5065 | IsSuper ? "\01L_OBJC_CLASSLIST_SUP_REFS_$_" |
| 5066 | : "\01L_OBJC_CLASSLIST_REFERENCES_$_", |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5067 | &CGM.getModule()); |
| 5068 | Entry->setAlignment( |
| 5069 | CGM.getTargetData().getPrefTypeAlignment( |
| 5070 | ObjCTypes.ClassnfABIPtrTy)); |
| 5071 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5072 | if (IsSuper) |
Fariborz Jahanian | 21228b7 | 2009-02-26 22:30:39 +0000 | [diff] [blame] | 5073 | Entry->setSection("__DATA,__objc_superrefs,regular,no_dead_strip"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5074 | else |
Fariborz Jahanian | 21228b7 | 2009-02-26 22:30:39 +0000 | [diff] [blame] | 5075 | Entry->setSection("__DATA,__objc_classrefs,regular,no_dead_strip"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5076 | UsedGlobals.push_back(Entry); |
| 5077 | } |
| 5078 | |
| 5079 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5080 | } |
| 5081 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5082 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 5083 | /// meta-data |
| 5084 | /// |
| 5085 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 5086 | const ObjCInterfaceDecl *ID) { |
| 5087 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 5088 | if (Entry) |
| 5089 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5090 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5091 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
| 5092 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName, false); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5093 | Entry = |
| 5094 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5095 | llvm::GlobalValue::InternalLinkage, |
| 5096 | MetaClassGV, |
| 5097 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 5098 | &CGM.getModule()); |
| 5099 | Entry->setAlignment( |
| 5100 | CGM.getTargetData().getPrefTypeAlignment( |
| 5101 | ObjCTypes.ClassnfABIPtrTy)); |
| 5102 | |
| 5103 | Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip"); |
| 5104 | UsedGlobals.push_back(Entry); |
| 5105 | |
| 5106 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5107 | } |
| 5108 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5109 | /// GetClass - Return a reference to the class for the given interface |
| 5110 | /// decl. |
| 5111 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 5112 | const ObjCInterfaceDecl *ID) { |
| 5113 | return EmitClassRef(Builder, ID); |
| 5114 | } |
| 5115 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5116 | /// Generates a message send where the super is the receiver. This is |
| 5117 | /// a message send to self with special delivery semantics indicating |
| 5118 | /// which class's method should be called. |
| 5119 | CodeGen::RValue |
| 5120 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 5121 | QualType ResultType, |
| 5122 | Selector Sel, |
| 5123 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5124 | bool isCategoryImpl, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5125 | llvm::Value *Receiver, |
| 5126 | bool IsClassMessage, |
| 5127 | const CodeGen::CallArgList &CallArgs) { |
| 5128 | // ... |
| 5129 | // Create and init a super structure; this is a (receiver, class) |
| 5130 | // pair we will pass to objc_msgSendSuper. |
| 5131 | llvm::Value *ObjCSuper = |
| 5132 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 5133 | |
| 5134 | llvm::Value *ReceiverAsObject = |
| 5135 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 5136 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 5137 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
| 5138 | |
| 5139 | // 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] | 5140 | llvm::Value *Target; |
| 5141 | if (IsClassMessage) { |
| 5142 | if (isCategoryImpl) { |
| 5143 | // Message sent to "super' in a class method defined in |
| 5144 | // a category implementation. |
| 5145 | Target = EmitClassRef(CGF.Builder, Class, false); |
| 5146 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 5147 | Target = CGF.Builder.CreateLoad(Target); |
| 5148 | } |
| 5149 | else |
| 5150 | Target = EmitMetaClassRef(CGF.Builder, Class); |
| 5151 | } |
| 5152 | else |
| 5153 | Target = EmitClassRef(CGF.Builder, Class, true); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5154 | |
| 5155 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 5156 | // and ObjCTypes types. |
| 5157 | const llvm::Type *ClassTy = |
| 5158 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 5159 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 5160 | CGF.Builder.CreateStore(Target, |
| 5161 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 5162 | |
| 5163 | return EmitMessageSend(CGF, ResultType, Sel, |
| 5164 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 5165 | true, CallArgs); |
| 5166 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5167 | |
| 5168 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
| 5169 | Selector Sel) { |
| 5170 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 5171 | |
| 5172 | if (!Entry) { |
| 5173 | llvm::Constant *Casted = |
| 5174 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 5175 | ObjCTypes.SelectorPtrTy); |
| 5176 | Entry = |
| 5177 | new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false, |
| 5178 | llvm::GlobalValue::InternalLinkage, |
| 5179 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_", |
| 5180 | &CGM.getModule()); |
| 5181 | Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip"); |
| 5182 | UsedGlobals.push_back(Entry); |
| 5183 | } |
| 5184 | |
| 5185 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5186 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5187 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 5188 | /// objc_assign_ivar (id src, id *dst) |
| 5189 | /// |
| 5190 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 5191 | llvm::Value *src, llvm::Value *dst) |
| 5192 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5193 | const llvm::Type * SrcTy = src->getType(); |
| 5194 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5195 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5196 | assert(Size <= 8 && "does not support size > 8"); |
| 5197 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5198 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5199 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5200 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5201 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5202 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 5203 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn, |
| 5204 | src, dst, "assignivar"); |
| 5205 | return; |
| 5206 | } |
| 5207 | |
| 5208 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 5209 | /// objc_assign_strongCast (id src, id *dst) |
| 5210 | /// |
| 5211 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
| 5212 | CodeGen::CodeGenFunction &CGF, |
| 5213 | llvm::Value *src, llvm::Value *dst) |
| 5214 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5215 | const llvm::Type * SrcTy = src->getType(); |
| 5216 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5217 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5218 | assert(Size <= 8 && "does not support size > 8"); |
| 5219 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5220 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5221 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5222 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5223 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5224 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 5225 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn, |
| 5226 | src, dst, "weakassign"); |
| 5227 | return; |
| 5228 | } |
| 5229 | |
| 5230 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 5231 | /// object: objc_read_weak (id *src) |
| 5232 | /// |
| 5233 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
| 5234 | CodeGen::CodeGenFunction &CGF, |
| 5235 | llvm::Value *AddrWeakObj) |
| 5236 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5237 | const llvm::Type* DestTy = |
| 5238 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5239 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
| 5240 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn, |
| 5241 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5242 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5243 | return read_weak; |
| 5244 | } |
| 5245 | |
| 5246 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 5247 | /// objc_assign_weak (id src, id *dst) |
| 5248 | /// |
| 5249 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 5250 | llvm::Value *src, llvm::Value *dst) |
| 5251 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5252 | const llvm::Type * SrcTy = src->getType(); |
| 5253 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5254 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5255 | assert(Size <= 8 && "does not support size > 8"); |
| 5256 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5257 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5258 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5259 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5260 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5261 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 5262 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn, |
| 5263 | src, dst, "weakassign"); |
| 5264 | return; |
| 5265 | } |
| 5266 | |
| 5267 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 5268 | /// objc_assign_global (id src, id *dst) |
| 5269 | /// |
| 5270 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 5271 | llvm::Value *src, llvm::Value *dst) |
| 5272 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5273 | const llvm::Type * SrcTy = src->getType(); |
| 5274 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5275 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5276 | assert(Size <= 8 && "does not support size > 8"); |
| 5277 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5278 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5279 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5280 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5281 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5282 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 5283 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn, |
| 5284 | src, dst, "globalassign"); |
| 5285 | return; |
| 5286 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5287 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5288 | void |
| 5289 | CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 5290 | const Stmt &S) { |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5291 | bool isTry = isa<ObjCAtTryStmt>(S); |
| 5292 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 5293 | llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5294 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5295 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5296 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5297 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
| 5298 | |
| 5299 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 5300 | // evaluation of the expression must occur before we enter the |
| 5301 | // @synchronized. We can safely avoid a temp here because jumps into |
| 5302 | // @synchronized are illegal & this will dominate uses. |
| 5303 | llvm::Value *SyncArg = 0; |
| 5304 | if (!isTry) { |
| 5305 | SyncArg = |
| 5306 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 5307 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5308 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5309 | } |
| 5310 | |
| 5311 | // Push an EH context entry, used for handling rethrows and jumps |
| 5312 | // through finally. |
| 5313 | CGF.PushCleanupBlock(FinallyBlock); |
| 5314 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5315 | CGF.setInvokeDest(TryHandler); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5316 | |
| 5317 | CGF.EmitBlock(TryBlock); |
| 5318 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 5319 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
| 5320 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5321 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5322 | // Emit the exception handler. |
| 5323 | |
| 5324 | CGF.EmitBlock(TryHandler); |
| 5325 | |
| 5326 | llvm::Value *llvm_eh_exception = |
| 5327 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
| 5328 | llvm::Value *llvm_eh_selector_i64 = |
| 5329 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64); |
| 5330 | llvm::Value *llvm_eh_typeid_for_i64 = |
| 5331 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64); |
| 5332 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5333 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow"); |
| 5334 | |
| 5335 | llvm::SmallVector<llvm::Value*, 8> SelectorArgs; |
| 5336 | SelectorArgs.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5337 | SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5338 | |
| 5339 | // Construct the lists of (type, catch body) to handle. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5340 | llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5341 | bool HasCatchAll = false; |
| 5342 | if (isTry) { |
| 5343 | if (const ObjCAtCatchStmt* CatchStmt = |
| 5344 | cast<ObjCAtTryStmt>(S).getCatchStmts()) { |
| 5345 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5346 | const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5347 | Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody())); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5348 | |
| 5349 | // catch(...) always matches. |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5350 | if (!CatchDecl) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5351 | // Use i8* null here to signal this is a catch all, not a cleanup. |
| 5352 | llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 5353 | SelectorArgs.push_back(Null); |
| 5354 | HasCatchAll = true; |
| 5355 | break; |
| 5356 | } |
| 5357 | |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5358 | if (CGF.getContext().isObjCIdType(CatchDecl->getType()) || |
| 5359 | CatchDecl->getType()->isObjCQualifiedIdType()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5360 | llvm::Value *IDEHType = |
| 5361 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 5362 | if (!IDEHType) |
| 5363 | IDEHType = |
| 5364 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5365 | llvm::GlobalValue::ExternalLinkage, |
| 5366 | 0, "OBJC_EHTYPE_id", &CGM.getModule()); |
| 5367 | SelectorArgs.push_back(IDEHType); |
| 5368 | HasCatchAll = true; |
| 5369 | break; |
| 5370 | } |
| 5371 | |
| 5372 | // All other types should be Objective-C interface pointer types. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5373 | const PointerType *PT = CatchDecl->getType()->getAsPointerType(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5374 | assert(PT && "Invalid @catch type."); |
| 5375 | const ObjCInterfaceType *IT = |
| 5376 | PT->getPointeeType()->getAsObjCInterfaceType(); |
| 5377 | assert(IT && "Invalid @catch type."); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame^] | 5378 | llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5379 | SelectorArgs.push_back(EHType); |
| 5380 | } |
| 5381 | } |
| 5382 | } |
| 5383 | |
| 5384 | // We use a cleanup unless there was already a catch all. |
| 5385 | if (!HasCatchAll) { |
| 5386 | SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5387 | Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5388 | } |
| 5389 | |
| 5390 | llvm::Value *Selector = |
| 5391 | CGF.Builder.CreateCall(llvm_eh_selector_i64, |
| 5392 | SelectorArgs.begin(), SelectorArgs.end(), |
| 5393 | "selector"); |
| 5394 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5395 | const ParmVarDecl *CatchParam = Handlers[i].first; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5396 | const Stmt *CatchBody = Handlers[i].second; |
| 5397 | |
| 5398 | llvm::BasicBlock *Next = 0; |
| 5399 | |
| 5400 | // The last handler always matches. |
| 5401 | if (i + 1 != e) { |
| 5402 | assert(CatchParam && "Only last handler can be a catch all."); |
| 5403 | |
| 5404 | llvm::BasicBlock *Match = CGF.createBasicBlock("match"); |
| 5405 | Next = CGF.createBasicBlock("catch.next"); |
| 5406 | llvm::Value *Id = |
| 5407 | CGF.Builder.CreateCall(llvm_eh_typeid_for_i64, |
| 5408 | CGF.Builder.CreateBitCast(SelectorArgs[i+2], |
| 5409 | ObjCTypes.Int8PtrTy)); |
| 5410 | CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id), |
| 5411 | Match, Next); |
| 5412 | |
| 5413 | CGF.EmitBlock(Match); |
| 5414 | } |
| 5415 | |
| 5416 | if (CatchBody) { |
| 5417 | llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end"); |
| 5418 | llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler"); |
| 5419 | |
| 5420 | // Cleanups must call objc_end_catch. |
| 5421 | // |
| 5422 | // FIXME: It seems incorrect for objc_begin_catch to be inside |
| 5423 | // this context, but this matches gcc. |
| 5424 | CGF.PushCleanupBlock(MatchEnd); |
| 5425 | CGF.setInvokeDest(MatchHandler); |
| 5426 | |
| 5427 | llvm::Value *ExcObject = |
| 5428 | CGF.Builder.CreateCall(ObjCTypes.ObjCBeginCatchFn, Exc); |
| 5429 | |
| 5430 | // Bind the catch parameter if it exists. |
| 5431 | if (CatchParam) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5432 | ExcObject = |
| 5433 | CGF.Builder.CreateBitCast(ExcObject, |
| 5434 | CGF.ConvertType(CatchParam->getType())); |
| 5435 | // CatchParam is a ParmVarDecl because of the grammar |
| 5436 | // construction used to handle this, but for codegen purposes |
| 5437 | // we treat this as a local decl. |
| 5438 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
| 5439 | CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5440 | } |
| 5441 | |
| 5442 | CGF.ObjCEHValueStack.push_back(ExcObject); |
| 5443 | CGF.EmitStmt(CatchBody); |
| 5444 | CGF.ObjCEHValueStack.pop_back(); |
| 5445 | |
| 5446 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5447 | |
| 5448 | CGF.EmitBlock(MatchHandler); |
| 5449 | |
| 5450 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5451 | // We are required to emit this call to satisfy LLVM, even |
| 5452 | // though we don't use the result. |
| 5453 | llvm::SmallVector<llvm::Value*, 8> Args; |
| 5454 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5455 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5456 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5457 | 0)); |
| 5458 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5459 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5460 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5461 | |
| 5462 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5463 | |
| 5464 | CGF.EmitBlock(MatchEnd); |
| 5465 | |
| 5466 | // Unfortunately, we also have to generate another EH frame here |
| 5467 | // in case this throws. |
| 5468 | llvm::BasicBlock *MatchEndHandler = |
| 5469 | CGF.createBasicBlock("match.end.handler"); |
| 5470 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
| 5471 | CGF.Builder.CreateInvoke(ObjCTypes.ObjCEndCatchFn, |
| 5472 | Cont, MatchEndHandler, |
| 5473 | Args.begin(), Args.begin()); |
| 5474 | |
| 5475 | CGF.EmitBlock(Cont); |
| 5476 | if (Info.SwitchBlock) |
| 5477 | CGF.EmitBlock(Info.SwitchBlock); |
| 5478 | if (Info.EndBlock) |
| 5479 | CGF.EmitBlock(Info.EndBlock); |
| 5480 | |
| 5481 | CGF.EmitBlock(MatchEndHandler); |
| 5482 | Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5483 | // We are required to emit this call to satisfy LLVM, even |
| 5484 | // though we don't use the result. |
| 5485 | Args.clear(); |
| 5486 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5487 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5488 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5489 | 0)); |
| 5490 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5491 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5492 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5493 | |
| 5494 | if (Next) |
| 5495 | CGF.EmitBlock(Next); |
| 5496 | } else { |
| 5497 | assert(!Next && "catchup should be last handler."); |
| 5498 | |
| 5499 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5500 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5501 | } |
| 5502 | } |
| 5503 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5504 | // Pop the cleanup entry, the @finally is outside this cleanup |
| 5505 | // scope. |
| 5506 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5507 | CGF.setInvokeDest(PrevLandingPad); |
| 5508 | |
| 5509 | CGF.EmitBlock(FinallyBlock); |
| 5510 | |
| 5511 | if (isTry) { |
| 5512 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 5513 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 5514 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 5515 | } else { |
| 5516 | // Emit 'objc_sync_exit(expr)' as finally's sole statement for |
| 5517 | // @synchronized. |
| 5518 | CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5519 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5520 | |
| 5521 | if (Info.SwitchBlock) |
| 5522 | CGF.EmitBlock(Info.SwitchBlock); |
| 5523 | if (Info.EndBlock) |
| 5524 | CGF.EmitBlock(Info.EndBlock); |
| 5525 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5526 | // Branch around the rethrow code. |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5527 | CGF.EmitBranch(FinallyEnd); |
| 5528 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5529 | CGF.EmitBlock(FinallyRethrow); |
| 5530 | CGF.Builder.CreateCall(ObjCTypes.UnwindResumeOrRethrowFn, |
| 5531 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5532 | CGF.Builder.CreateUnreachable(); |
| 5533 | |
| 5534 | CGF.EmitBlock(FinallyEnd); |
| 5535 | } |
| 5536 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5537 | /// EmitThrowStmt - Generate code for a throw statement. |
| 5538 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 5539 | const ObjCAtThrowStmt &S) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5540 | llvm::Value *Exception; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5541 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5542 | Exception = CGF.EmitScalarExpr(ThrowExpr); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5543 | } else { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5544 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
| 5545 | "Unexpected rethrow outside @catch block."); |
| 5546 | Exception = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5547 | } |
| 5548 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5549 | llvm::Value *ExceptionAsObject = |
| 5550 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 5551 | llvm::BasicBlock *InvokeDest = CGF.getInvokeDest(); |
| 5552 | if (InvokeDest) { |
| 5553 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
| 5554 | CGF.Builder.CreateInvoke(ObjCTypes.ExceptionThrowFn, |
| 5555 | Cont, InvokeDest, |
| 5556 | &ExceptionAsObject, &ExceptionAsObject + 1); |
| 5557 | CGF.EmitBlock(Cont); |
| 5558 | } else |
| 5559 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject); |
| 5560 | CGF.Builder.CreateUnreachable(); |
| 5561 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5562 | // Clear the insertion point to indicate we are in unreachable code. |
| 5563 | CGF.Builder.ClearInsertionPoint(); |
| 5564 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5565 | |
| 5566 | llvm::Value * |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame^] | 5567 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 5568 | bool ForDefinition) { |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5569 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5570 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame^] | 5571 | // If we don't need a definition, return the entry if found or check |
| 5572 | // if we use an external reference. |
| 5573 | if (!ForDefinition) { |
| 5574 | if (Entry) |
| 5575 | return Entry; |
Daniel Dunbar | 7e075cb | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 5576 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame^] | 5577 | // If this type (or a super class) has the __objc_exception__ |
| 5578 | // attribute, emit an external reference. |
| 5579 | if (hasObjCExceptionAttribute(ID)) |
| 5580 | return Entry = |
| 5581 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5582 | llvm::GlobalValue::ExternalLinkage, |
| 5583 | 0, |
| 5584 | (std::string("OBJC_EHTYPE_$_") + |
| 5585 | ID->getIdentifier()->getName()), |
| 5586 | &CGM.getModule()); |
| 5587 | } |
| 5588 | |
| 5589 | // Otherwise we need to either make a new entry or fill in the |
| 5590 | // initializer. |
| 5591 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5592 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5593 | std::string VTableName = "objc_ehtype_vtable"; |
| 5594 | llvm::GlobalVariable *VTableGV = |
| 5595 | CGM.getModule().getGlobalVariable(VTableName); |
| 5596 | if (!VTableGV) |
| 5597 | VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false, |
| 5598 | llvm::GlobalValue::ExternalLinkage, |
| 5599 | 0, VTableName, &CGM.getModule()); |
| 5600 | |
| 5601 | llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2); |
| 5602 | |
| 5603 | std::vector<llvm::Constant*> Values(3); |
| 5604 | Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1); |
| 5605 | Values[1] = GetClassName(ID->getIdentifier()); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5606 | Values[2] = GetClassGlobal(ClassName, false); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5607 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
| 5608 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame^] | 5609 | if (Entry) { |
| 5610 | Entry->setInitializer(Init); |
| 5611 | } else { |
| 5612 | Entry = new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5613 | llvm::GlobalValue::WeakAnyLinkage, |
| 5614 | Init, |
| 5615 | (std::string("OBJC_EHTYPE_$_") + |
| 5616 | ID->getIdentifier()->getName()), |
| 5617 | &CGM.getModule()); |
| 5618 | } |
| 5619 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5620 | if (CGM.getLangOptions().getVisibilityMode() == |
| 5621 | LangOptions::HiddenVisibility) |
| 5622 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame^] | 5623 | Entry->setAlignment(8); |
| 5624 | |
| 5625 | if (ForDefinition) { |
| 5626 | Entry->setSection("__DATA,__objc_const"); |
| 5627 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 5628 | } else { |
| 5629 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 5630 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5631 | |
| 5632 | return Entry; |
| 5633 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5634 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5635 | /* *** */ |
| 5636 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5637 | CodeGen::CGObjCRuntime * |
| 5638 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 5639 | return new CGObjCMac(CGM); |
| 5640 | } |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5641 | |
| 5642 | CodeGen::CGObjCRuntime * |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5643 | CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5644 | return new CGObjCNonFragileABIMac(CGM); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5645 | } |