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