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