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 | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 23 | #include "llvm/Module.h" |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/DenseSet.h" |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetData.h" |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 26 | #include <sstream> |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 29 | using namespace CodeGen; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 30 | |
| 31 | namespace { |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 32 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 33 | typedef std::vector<llvm::Constant*> ConstantVector; |
| 34 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 35 | // FIXME: We should find a nicer way to make the labels for |
| 36 | // metadata, string concatenation is lame. |
| 37 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 38 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 39 | /// construction of varies types used during ObjC generation. |
| 40 | class ObjCTypesHelper { |
| 41 | private: |
| 42 | CodeGen::CodeGenModule &CGM; |
| 43 | |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 44 | llvm::Function *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn; |
| 45 | llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn, |
| 46 | *MessageSendSuperFpretFn; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 47 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 48 | public: |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 49 | const llvm::Type *ShortTy, *IntTy, *LongTy; |
| 50 | const llvm::Type *Int8PtrTy; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 51 | |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 52 | /// ObjectPtrTy - LLVM type for object handles (typeof(id)) |
| 53 | const llvm::Type *ObjectPtrTy; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 54 | /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL)) |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 55 | const llvm::Type *SelectorPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 56 | /// ProtocolPtrTy - LLVM type for external protocol handles |
| 57 | /// (typeof(Protocol)) |
| 58 | const llvm::Type *ExternalProtocolPtrTy; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 59 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 60 | // SuperCTy - clang type for struct objc_super. |
| 61 | QualType SuperCTy; |
| 62 | // SuperPtrCTy - clang type for struct objc_super *. |
| 63 | QualType SuperPtrCTy; |
| 64 | |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 65 | /// SuperTy - LLVM type for struct objc_super. |
| 66 | const llvm::StructType *SuperTy; |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 67 | /// SuperPtrTy - LLVM type for struct objc_super *. |
| 68 | const llvm::Type *SuperPtrTy; |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 69 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 70 | /// SymtabTy - LLVM type for struct objc_symtab. |
| 71 | const llvm::StructType *SymtabTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 72 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
| 73 | const llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 74 | /// ModuleTy - LLVM type for struct objc_module. |
| 75 | const llvm::StructType *ModuleTy; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 76 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 77 | /// ProtocolTy - LLVM type for struct objc_protocol. |
| 78 | const llvm::StructType *ProtocolTy; |
| 79 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
| 80 | const llvm::Type *ProtocolPtrTy; |
| 81 | /// ProtocolExtensionTy - LLVM type for struct |
| 82 | /// objc_protocol_extension. |
| 83 | const llvm::StructType *ProtocolExtensionTy; |
| 84 | /// ProtocolExtensionTy - LLVM type for struct |
| 85 | /// objc_protocol_extension *. |
| 86 | const llvm::Type *ProtocolExtensionPtrTy; |
| 87 | /// MethodDescriptionTy - LLVM type for struct |
| 88 | /// objc_method_description. |
| 89 | const llvm::StructType *MethodDescriptionTy; |
| 90 | /// MethodDescriptionListTy - LLVM type for struct |
| 91 | /// objc_method_description_list. |
| 92 | const llvm::StructType *MethodDescriptionListTy; |
| 93 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 94 | /// objc_method_description_list *. |
| 95 | const llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 96 | /// PropertyTy - LLVM type for struct objc_property (struct _prop_t |
| 97 | /// in GCC parlance). |
| 98 | const llvm::StructType *PropertyTy; |
| 99 | /// PropertyListTy - LLVM type for struct objc_property_list |
| 100 | /// (_prop_list_t in GCC parlance). |
| 101 | const llvm::StructType *PropertyListTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 102 | /// PropertyListPtrTy - LLVM type for struct objc_property_list*. |
| 103 | const llvm::Type *PropertyListPtrTy; |
| 104 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
| 105 | const llvm::Type *ProtocolListTy; |
| 106 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
| 107 | const llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 108 | /// CategoryTy - LLVM type for struct objc_category. |
| 109 | const llvm::StructType *CategoryTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 110 | /// ClassTy - LLVM type for struct objc_class. |
| 111 | const llvm::StructType *ClassTy; |
| 112 | /// ClassPtrTy - LLVM type for struct objc_class *. |
| 113 | const llvm::Type *ClassPtrTy; |
| 114 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
| 115 | const llvm::StructType *ClassExtensionTy; |
| 116 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
| 117 | const llvm::Type *ClassExtensionPtrTy; |
| 118 | /// CacheTy - LLVM type for struct objc_cache. |
| 119 | const llvm::Type *CacheTy; |
| 120 | /// CachePtrTy - LLVM type for struct objc_cache *. |
| 121 | const llvm::Type *CachePtrTy; |
| 122 | // IvarTy - LLVM type for struct objc_ivar. |
| 123 | const llvm::StructType *IvarTy; |
| 124 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
| 125 | const llvm::Type *IvarListTy; |
| 126 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
| 127 | const llvm::Type *IvarListPtrTy; |
| 128 | // MethodTy - LLVM type for struct objc_method. |
| 129 | const llvm::StructType *MethodTy; |
| 130 | /// MethodListTy - LLVM type for struct objc_method_list. |
| 131 | const llvm::Type *MethodListTy; |
| 132 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
| 133 | const llvm::Type *MethodListPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 134 | |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 135 | llvm::Function *GetPropertyFn, *SetPropertyFn; |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 136 | llvm::Function *EnumerationMutationFn; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 137 | |
| 138 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
| 139 | const llvm::Type *ExceptionDataTy; |
| 140 | |
| 141 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
| 142 | llvm::Function *ExceptionThrowFn; |
| 143 | |
| 144 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
| 145 | llvm::Function *ExceptionTryEnterFn; |
| 146 | |
| 147 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
| 148 | llvm::Function *ExceptionTryExitFn; |
| 149 | |
| 150 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
| 151 | llvm::Function *ExceptionExtractFn; |
| 152 | |
| 153 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
| 154 | llvm::Function *ExceptionMatchFn; |
| 155 | |
| 156 | /// SetJmpFn - LLVM _setjmp function. |
| 157 | llvm::Function *SetJmpFn; |
| 158 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 159 | public: |
| 160 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
| 161 | ~ObjCTypesHelper(); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 162 | |
| 163 | |
| 164 | llvm::Function *getSendFn(bool IsSuper) { |
| 165 | return IsSuper ? MessageSendSuperFn : MessageSendFn; |
| 166 | } |
| 167 | |
| 168 | llvm::Function *getSendStretFn(bool IsSuper) { |
| 169 | return IsSuper ? MessageSendSuperStretFn : MessageSendStretFn; |
| 170 | } |
| 171 | |
| 172 | llvm::Function *getSendFpretFn(bool IsSuper) { |
| 173 | return IsSuper ? MessageSendSuperFpretFn : MessageSendFpretFn; |
| 174 | } |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 177 | class CGObjCMac : public CodeGen::CGObjCRuntime { |
| 178 | private: |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 179 | CodeGen::CodeGenModule &CGM; |
| 180 | ObjCTypesHelper ObjCTypes; |
| 181 | /// ObjCABI - FIXME: Not sure yet. |
| 182 | unsigned ObjCABI; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 183 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 184 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 185 | /// DefinedSymbols and FinishModule(). |
| 186 | std::set<IdentifierInfo*> LazySymbols; |
| 187 | |
| 188 | /// DefinedSymbols - External symbols which are defined by this |
| 189 | /// module. The symbols in this list and LazySymbols are used to add |
| 190 | /// special linker symbols which ensure that Objective-C modules are |
| 191 | /// linked properly. |
| 192 | std::set<IdentifierInfo*> DefinedSymbols; |
| 193 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 194 | /// ClassNames - uniqued class names. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 195 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 196 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 197 | /// MethodVarNames - uniqued method variable names. |
| 198 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
| 199 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 200 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 201 | /// a StringMap here because have no other unique reference. |
| 202 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
| 203 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 204 | /// MethodDefinitions - map of methods which have been defined in |
| 205 | /// this translation unit. |
| 206 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
| 207 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 208 | /// PropertyNames - uniqued method variable names. |
| 209 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
| 210 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 211 | /// ClassReferences - uniqued class references. |
| 212 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
| 213 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 214 | /// SelectorReferences - uniqued selector references. |
| 215 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
| 216 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 217 | /// Protocols - Protocols for which an objc_protocol structure has |
| 218 | /// been emitted. Forward declarations are handled by creating an |
| 219 | /// empty structure whose initializer is filled in when/if defined. |
| 220 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
| 221 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 222 | /// DefinedProtocols - Protocols which have actually been |
| 223 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 224 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
| 225 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 226 | /// DefinedClasses - List of defined classes. |
| 227 | std::vector<llvm::GlobalValue*> DefinedClasses; |
| 228 | |
| 229 | /// DefinedCategories - List of defined categories. |
| 230 | std::vector<llvm::GlobalValue*> DefinedCategories; |
| 231 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 232 | /// UsedGlobals - List of globals to pack into the llvm.used metadata |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 233 | /// to prevent them from being clobbered. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 234 | std::vector<llvm::GlobalVariable*> UsedGlobals; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 235 | |
| 236 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 237 | /// level information. |
| 238 | void EmitImageInfo(); |
| 239 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 240 | /// EmitModuleInfo - Another marker encoding module level |
| 241 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 242 | void EmitModuleInfo(); |
| 243 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 244 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 245 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 246 | llvm::Constant *EmitModuleSymbols(); |
| 247 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 248 | /// FinishModule - Write out global data structures at the end of |
| 249 | /// processing a translation unit. |
| 250 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 251 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 252 | /// EmitClassExtension - Generate the class extension structure used |
| 253 | /// to store the weak ivar layout and properties. The return value |
| 254 | /// has type ClassExtensionPtrTy. |
| 255 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 256 | |
| 257 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 258 | /// for the given class. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 259 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 260 | const ObjCInterfaceDecl *ID); |
| 261 | |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 262 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 263 | QualType ResultType, |
| 264 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 265 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 266 | QualType Arg0Ty, |
| 267 | bool IsSuper, |
| 268 | const CallArgList &CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 269 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 270 | /// EmitIvarList - Emit the ivar list for the given |
| 271 | /// implementation. If ForClass is true the list of class ivars |
| 272 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 273 | /// interface ivars will be emitted. The return value has type |
| 274 | /// IvarListPtrTy. |
| 275 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
| 276 | bool ForClass, |
| 277 | const llvm::Type *InterfaceTy); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 278 | |
| 279 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 280 | /// for the metaclass of the given interface. The return value has |
| 281 | /// type ClassPtrTy. |
| 282 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 283 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 284 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 285 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 286 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 287 | llvm::Constant *Protocols, |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 288 | const llvm::Type *InterfaceTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 289 | const ConstantVector &Methods); |
| 290 | |
| 291 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
| 292 | |
| 293 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 294 | |
| 295 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 296 | /// implementation. The return value has type MethodListPtrTy. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 297 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 298 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 299 | const ConstantVector &Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 300 | |
| 301 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 302 | /// method declarations. |
| 303 | /// - TypeName: The name for the type containing the methods. |
| 304 | /// - IsProtocol: True iff these methods are for a protocol. |
| 305 | /// - ClassMethds: True iff these are class methods. |
| 306 | /// - Required: When true, only "required" methods are |
| 307 | /// listed. Similarly, when false only "optional" methods are |
| 308 | /// listed. For classes this should always be true. |
| 309 | /// - begin, end: The method list to output. |
| 310 | /// |
| 311 | /// The return value has type MethodDescriptionListPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 312 | llvm::Constant *EmitMethodDescList(const std::string &Name, |
| 313 | const char *Section, |
| 314 | const ConstantVector &Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 315 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 316 | /// EmitPropertyList - Emit the given property list. The return |
| 317 | /// value has type PropertyListPtrTy. |
| 318 | llvm::Constant *EmitPropertyList(const std::string &Name, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 319 | const Decl *Container, |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 320 | ObjCPropertyDecl * const *begin, |
| 321 | ObjCPropertyDecl * const *end); |
| 322 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 323 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 324 | /// declaration, emitting it if necessary. The return value has type |
| 325 | /// ProtocolPtrTy. |
| 326 | llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
| 327 | |
| 328 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 329 | /// object for the given declaration, emitting it if needed. These |
| 330 | /// forward references will be filled in with empty bodies if no |
| 331 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 332 | llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
| 333 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 334 | /// EmitProtocolExtension - Generate the protocol extension |
| 335 | /// structure used to store optional instance and class methods, and |
| 336 | /// protocol properties. The return value has type |
| 337 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 338 | llvm::Constant * |
| 339 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 340 | const ConstantVector &OptInstanceMethods, |
| 341 | const ConstantVector &OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 342 | |
| 343 | /// EmitProtocolList - Generate the list of referenced |
| 344 | /// protocols. The return value has type ProtocolListPtrTy. |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 345 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 346 | ObjCProtocolDecl::protocol_iterator begin, |
| 347 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 348 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 349 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 350 | /// for the given selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 351 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 352 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 353 | /// GetProtocolRef - Return a reference to the internal protocol |
| 354 | /// description, creating an empty one if it has not been |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 355 | /// defined. The return value has type ProtocolPtrTy. |
| 356 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 357 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 358 | /// GetClassName - Return a unique constant for the given selector's |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 359 | /// name. The return value has type char *. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 360 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 361 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 362 | /// GetMethodVarName - Return a unique constant for the given |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 363 | /// selector's name. The return value has type char *. |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 364 | llvm::Constant *GetMethodVarName(Selector Sel); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 365 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 366 | llvm::Constant *GetMethodVarName(const std::string &Name); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 367 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 368 | /// GetMethodVarType - Return a unique constant for the given |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 369 | /// selector's name. The return value has type char *. |
| 370 | |
| 371 | // FIXME: This is a horrible name. |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 372 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 373 | llvm::Constant *GetMethodVarType(const std::string &Name); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 374 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 375 | /// GetPropertyName - Return a unique constant for the given |
| 376 | /// name. The return value has type char *. |
| 377 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
| 378 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 379 | // FIXME: This can be dropped once string functions are unified. |
| 380 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 381 | const Decl *Container); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 382 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 383 | /// GetNameForMethod - Return a name for the given method. |
| 384 | /// \param[out] NameOut - The return value. |
| 385 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 386 | std::string &NameOut); |
| 387 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 388 | public: |
| 389 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 390 | virtual llvm::Constant *GenerateConstantString(const std::string &String); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 391 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 392 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 393 | QualType ResultType, |
| 394 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 395 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 396 | bool IsClassMessage, |
| 397 | const CallArgList &CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 398 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 399 | virtual CodeGen::RValue |
| 400 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 401 | QualType ResultType, |
| 402 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 403 | const ObjCInterfaceDecl *Class, |
| 404 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 405 | bool IsClassMessage, |
| 406 | const CallArgList &CallArgs); |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 407 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 408 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 409 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 410 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 411 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 412 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 413 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 414 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 415 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 416 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 417 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 418 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 419 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 420 | const ObjCProtocolDecl *PD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 421 | |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 422 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 423 | |
| 424 | virtual llvm::Function *ModuleInitFunction(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 425 | virtual llvm::Function *GetPropertyGetFunction(); |
| 426 | virtual llvm::Function *GetPropertySetFunction(); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 427 | virtual llvm::Function *EnumerationMutationFunction(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 428 | |
| 429 | virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 430 | const ObjCAtTryStmt &S); |
| 431 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 432 | const ObjCAtThrowStmt &S); |
| 433 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 434 | }; |
| 435 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 436 | |
| 437 | /* *** Helper Functions *** */ |
| 438 | |
| 439 | /// getConstantGEP() - Help routine to construct simple GEPs. |
| 440 | static llvm::Constant *getConstantGEP(llvm::Constant *C, |
| 441 | unsigned idx0, |
| 442 | unsigned idx1) { |
| 443 | llvm::Value *Idxs[] = { |
| 444 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0), |
| 445 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1) |
| 446 | }; |
| 447 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2); |
| 448 | } |
| 449 | |
| 450 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 451 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 452 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) |
| 453 | : CGM(cgm), |
| 454 | ObjCTypes(cgm), |
| 455 | ObjCABI(1) |
| 456 | { |
| 457 | // FIXME: How does this get set in GCC? And what does it even mean? |
| 458 | if (ObjCTypes.LongTy != CGM.getTypes().ConvertType(CGM.getContext().IntTy)) |
| 459 | ObjCABI = 2; |
| 460 | |
| 461 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 464 | /// GetClass - Return a reference to the class for the given interface |
| 465 | /// decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 466 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 467 | const ObjCInterfaceDecl *ID) { |
| 468 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 472 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 473 | return EmitSelector(Builder, Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 476 | /// Generate a constant CFString object. |
| 477 | /* |
| 478 | struct __builtin_CFString { |
| 479 | const int *isa; // point to __CFConstantStringClassReference |
| 480 | int flags; |
| 481 | const char *str; |
| 482 | long length; |
| 483 | }; |
| 484 | */ |
| 485 | |
| 486 | llvm::Constant *CGObjCMac::GenerateConstantString(const std::string &String) { |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 487 | return CGM.GetAddrOfConstantCFString(String); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | /// Generates a message send where the super is the receiver. This is |
| 491 | /// a message send to self with special delivery semantics indicating |
| 492 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 493 | CodeGen::RValue |
| 494 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 495 | QualType ResultType, |
| 496 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 497 | const ObjCInterfaceDecl *Class, |
| 498 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 499 | bool IsClassMessage, |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 500 | const CodeGen::CallArgList &CallArgs) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 501 | // Create and init a super structure; this is a (receiver, class) |
| 502 | // pair we will pass to objc_msgSendSuper. |
| 503 | llvm::Value *ObjCSuper = |
| 504 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 505 | llvm::Value *ReceiverAsObject = |
| 506 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 507 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 508 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 509 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 510 | // If this is a class message the metaclass is passed as the target. |
| 511 | llvm::Value *Target; |
| 512 | if (IsClassMessage) { |
| 513 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 514 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 515 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 516 | Target = Super; |
| 517 | } else { |
| 518 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 519 | } |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 520 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 521 | // and ObjCTypes types. |
| 522 | const llvm::Type *ClassTy = |
| 523 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 524 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 525 | CGF.Builder.CreateStore(Target, |
| 526 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 527 | |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 528 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 529 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 530 | true, CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 531 | } |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 532 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 533 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 534 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 535 | QualType ResultType, |
| 536 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 537 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 538 | bool IsClassMessage, |
| 539 | const CallArgList &CallArgs) { |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 540 | llvm::Value *Arg0 = |
| 541 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 542 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 543 | Arg0, CGF.getContext().getObjCIdType(), |
| 544 | false, CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 548 | QualType ResultType, |
| 549 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 550 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 551 | QualType Arg0Ty, |
| 552 | bool IsSuper, |
| 553 | const CallArgList &CallArgs) { |
| 554 | CallArgList ActualArgs; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 555 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 556 | ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder, |
| 557 | Sel)), |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 558 | CGF.getContext().getObjCSelType())); |
| 559 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 560 | |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 561 | const llvm::FunctionType *FTy = |
| 562 | CGM.getTypes().GetFunctionType(CGCallInfo(ResultType, ActualArgs), |
| 563 | false); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 564 | |
| 565 | llvm::Constant *Fn; |
| 566 | if (CGM.ReturnTypeUsesSret(ResultType)) { |
| 567 | Fn = ObjCTypes.getSendStretFn(IsSuper); |
| 568 | } else if (ResultType->isFloatingType()) { |
| 569 | // FIXME: Sadly, this is wrong. This actually depends on the |
| 570 | // architecture. This happens to be right for x86-32 though. |
| 571 | Fn = ObjCTypes.getSendFpretFn(IsSuper); |
| 572 | } else { |
| 573 | Fn = ObjCTypes.getSendFn(IsSuper); |
| 574 | } |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 575 | Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy)); |
Daniel Dunbar | 3913f18 | 2008-09-09 23:48:28 +0000 | [diff] [blame] | 576 | return CGF.EmitCall(Fn, ResultType, ActualArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 579 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 580 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 581 | // FIXME: I don't understand why gcc generates this, or where it is |
| 582 | // resolved. Investigate. Its also wasteful to look this up over and |
| 583 | // over. |
| 584 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 585 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 586 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
| 587 | ObjCTypes.ExternalProtocolPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 590 | void CGObjCMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
| 591 | // FIXME: We shouldn't need this, the protocol decl should contain |
| 592 | // enough information to tell us whether this was a declaration or a |
| 593 | // definition. |
| 594 | DefinedProtocols.insert(PD->getIdentifier()); |
| 595 | |
| 596 | // If we have generated a forward reference to this protocol, emit |
| 597 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 598 | // emitted. |
| 599 | if (Protocols.count(PD->getIdentifier())) |
| 600 | GetOrEmitProtocol(PD); |
| 601 | } |
| 602 | |
| 603 | llvm::Constant *CGObjCMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
| 604 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 605 | return GetOrEmitProtocol(PD); |
| 606 | return GetOrEmitProtocolRef(PD); |
| 607 | } |
| 608 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 609 | /* |
| 610 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 611 | struct _objc_protocol { |
| 612 | struct _objc_protocol_extension *isa; |
| 613 | char *protocol_name; |
| 614 | struct _objc_protocol_list *protocol_list; |
| 615 | struct _objc__method_prototype_list *instance_methods; |
| 616 | struct _objc__method_prototype_list *class_methods |
| 617 | }; |
| 618 | |
| 619 | See EmitProtocolExtension(). |
| 620 | */ |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 621 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
| 622 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 623 | |
| 624 | // Early exit if a defining object has already been generated. |
| 625 | if (Entry && Entry->hasInitializer()) |
| 626 | return Entry; |
| 627 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 628 | // FIXME: I don't understand why gcc generates this, or where it is |
| 629 | // resolved. Investigate. Its also wasteful to look this up over and |
| 630 | // over. |
| 631 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 632 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 633 | const char *ProtocolName = PD->getName(); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 634 | |
| 635 | // Construct method lists. |
| 636 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 637 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
| 638 | for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(), |
| 639 | e = PD->instmeth_end(); i != e; ++i) { |
| 640 | ObjCMethodDecl *MD = *i; |
| 641 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 642 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 643 | OptInstanceMethods.push_back(C); |
| 644 | } else { |
| 645 | InstanceMethods.push_back(C); |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(), |
| 650 | e = PD->classmeth_end(); i != e; ++i) { |
| 651 | ObjCMethodDecl *MD = *i; |
| 652 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 653 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 654 | OptClassMethods.push_back(C); |
| 655 | } else { |
| 656 | ClassMethods.push_back(C); |
| 657 | } |
| 658 | } |
| 659 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 660 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 661 | Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 662 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 663 | Values[2] = |
| 664 | EmitProtocolList(std::string("\01L_OBJC_PROTOCOL_REFS_")+PD->getName(), |
| 665 | PD->protocol_begin(), |
| 666 | PD->protocol_end()); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 667 | Values[3] = |
| 668 | EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_") |
| 669 | + PD->getName(), |
| 670 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 671 | InstanceMethods); |
| 672 | Values[4] = |
| 673 | EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_CLASS_METHODS_") |
| 674 | + PD->getName(), |
| 675 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 676 | ClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 677 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 678 | Values); |
| 679 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 680 | if (Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 681 | // Already created, fix the linkage and update the initializer. |
| 682 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 683 | Entry->setInitializer(Init); |
| 684 | } else { |
| 685 | Entry = |
| 686 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
| 687 | llvm::GlobalValue::InternalLinkage, |
| 688 | Init, |
| 689 | std::string("\01L_OBJC_PROTOCOL_")+ProtocolName, |
| 690 | &CGM.getModule()); |
| 691 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
| 692 | UsedGlobals.push_back(Entry); |
| 693 | // FIXME: Is this necessary? Why only for protocol? |
| 694 | Entry->setAlignment(4); |
| 695 | } |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 696 | |
| 697 | return Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 700 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 701 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 702 | |
| 703 | if (!Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 704 | // We use the initializer as a marker of whether this is a forward |
| 705 | // reference or not. At module finalization we add the empty |
| 706 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 707 | Entry = |
| 708 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 709 | llvm::GlobalValue::ExternalLinkage, |
| 710 | 0, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 711 | std::string("\01L_OBJC_PROTOCOL_")+PD->getName(), |
| 712 | &CGM.getModule()); |
| 713 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
| 714 | UsedGlobals.push_back(Entry); |
| 715 | // FIXME: Is this necessary? Why only for protocol? |
| 716 | Entry->setAlignment(4); |
| 717 | } |
| 718 | |
| 719 | return Entry; |
| 720 | } |
| 721 | |
| 722 | /* |
| 723 | struct _objc_protocol_extension { |
| 724 | uint32_t size; |
| 725 | struct objc_method_description_list *optional_instance_methods; |
| 726 | struct objc_method_description_list *optional_class_methods; |
| 727 | struct objc_property_list *instance_properties; |
| 728 | }; |
| 729 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 730 | llvm::Constant * |
| 731 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 732 | const ConstantVector &OptInstanceMethods, |
| 733 | const ConstantVector &OptClassMethods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 734 | uint64_t Size = |
| 735 | CGM.getTargetData().getABITypeSize(ObjCTypes.ProtocolExtensionTy); |
| 736 | std::vector<llvm::Constant*> Values(4); |
| 737 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 738 | Values[1] = |
| 739 | EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_") |
| 740 | + PD->getName(), |
| 741 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 742 | OptInstanceMethods); |
| 743 | Values[2] = |
| 744 | EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_") |
| 745 | + PD->getName(), |
| 746 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 747 | OptClassMethods); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 748 | Values[3] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_PROTO_LIST_") + |
| 749 | PD->getName(), |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 750 | 0, |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 751 | PD->classprop_begin(), |
| 752 | PD->classprop_end()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 753 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 754 | // Return null if no extension bits are used. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 755 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
| 756 | Values[3]->isNullValue()) |
| 757 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 758 | |
| 759 | llvm::Constant *Init = |
| 760 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
| 761 | llvm::GlobalVariable *GV = |
| 762 | new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false, |
| 763 | llvm::GlobalValue::InternalLinkage, |
| 764 | Init, |
| 765 | (std::string("\01L_OBJC_PROTOCOLEXT_") + |
| 766 | PD->getName()), |
| 767 | &CGM.getModule()); |
| 768 | // No special section, but goes in llvm.used |
| 769 | UsedGlobals.push_back(GV); |
| 770 | |
| 771 | return GV; |
| 772 | } |
| 773 | |
| 774 | /* |
| 775 | struct objc_protocol_list { |
| 776 | struct objc_protocol_list *next; |
| 777 | long count; |
| 778 | Protocol *list[]; |
| 779 | }; |
| 780 | */ |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 781 | llvm::Constant * |
| 782 | CGObjCMac::EmitProtocolList(const std::string &Name, |
| 783 | ObjCProtocolDecl::protocol_iterator begin, |
| 784 | ObjCProtocolDecl::protocol_iterator end) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 785 | std::vector<llvm::Constant*> ProtocolRefs; |
| 786 | |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 787 | for (; begin != end; ++begin) |
| 788 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 789 | |
| 790 | // Just return null for empty protocol lists |
| 791 | if (ProtocolRefs.empty()) |
| 792 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 793 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 794 | // This list is null terminated. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 795 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
| 796 | |
| 797 | std::vector<llvm::Constant*> Values(3); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 798 | // This field is only used by the runtime. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 799 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 800 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 801 | Values[2] = |
| 802 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 803 | ProtocolRefs.size()), |
| 804 | ProtocolRefs); |
| 805 | |
| 806 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 807 | llvm::GlobalVariable *GV = |
| 808 | new llvm::GlobalVariable(Init->getType(), false, |
| 809 | llvm::GlobalValue::InternalLinkage, |
| 810 | Init, |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 811 | Name, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 812 | &CGM.getModule()); |
| 813 | GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip"); |
| 814 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
| 815 | } |
| 816 | |
| 817 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 818 | struct _objc_property { |
| 819 | const char * const name; |
| 820 | const char * const attributes; |
| 821 | }; |
| 822 | |
| 823 | struct _objc_property_list { |
| 824 | uint32_t entsize; // sizeof (struct _objc_property) |
| 825 | uint32_t prop_count; |
| 826 | struct _objc_property[prop_count]; |
| 827 | }; |
| 828 | */ |
| 829 | llvm::Constant *CGObjCMac::EmitPropertyList(const std::string &Name, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 830 | const Decl *Container, |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 831 | ObjCPropertyDecl * const *begin, |
| 832 | ObjCPropertyDecl * const *end) { |
| 833 | std::vector<llvm::Constant*> Properties, Prop(2); |
| 834 | for (; begin != end; ++begin) { |
| 835 | const ObjCPropertyDecl *PD = *begin; |
| 836 | Prop[0] = GetPropertyName(PD->getIdentifier()); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 837 | Prop[1] = GetPropertyTypeString(PD, Container); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 838 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
| 839 | Prop)); |
| 840 | } |
| 841 | |
| 842 | // Return null for empty list. |
| 843 | if (Properties.empty()) |
| 844 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 845 | |
| 846 | unsigned PropertySize = |
| 847 | CGM.getTargetData().getABITypeSize(ObjCTypes.PropertyTy); |
| 848 | std::vector<llvm::Constant*> Values(3); |
| 849 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 850 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
| 851 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
| 852 | Properties.size()); |
| 853 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
| 854 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 855 | |
| 856 | llvm::GlobalVariable *GV = |
| 857 | new llvm::GlobalVariable(Init->getType(), false, |
| 858 | llvm::GlobalValue::InternalLinkage, |
| 859 | Init, |
| 860 | Name, |
| 861 | &CGM.getModule()); |
| 862 | // No special section on property lists? |
| 863 | UsedGlobals.push_back(GV); |
| 864 | return llvm::ConstantExpr::getBitCast(GV, |
| 865 | ObjCTypes.PropertyListPtrTy); |
| 866 | |
| 867 | } |
| 868 | |
| 869 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 870 | struct objc_method_description_list { |
| 871 | int count; |
| 872 | struct objc_method_description list[]; |
| 873 | }; |
| 874 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 875 | llvm::Constant * |
| 876 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 877 | std::vector<llvm::Constant*> Desc(2); |
| 878 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 879 | ObjCTypes.SelectorPtrTy); |
| 880 | Desc[1] = GetMethodVarType(MD); |
| 881 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
| 882 | Desc); |
| 883 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 884 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 885 | llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name, |
| 886 | const char *Section, |
| 887 | const ConstantVector &Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 888 | // Return null for empty list. |
| 889 | if (Methods.empty()) |
| 890 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 891 | |
| 892 | std::vector<llvm::Constant*> Values(2); |
| 893 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 894 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
| 895 | Methods.size()); |
| 896 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
| 897 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 898 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 899 | llvm::GlobalVariable *GV = |
| 900 | new llvm::GlobalVariable(Init->getType(), false, |
| 901 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 902 | Init, Name, &CGM.getModule()); |
| 903 | GV->setSection(Section); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 904 | UsedGlobals.push_back(GV); |
| 905 | return llvm::ConstantExpr::getBitCast(GV, |
| 906 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 909 | /* |
| 910 | struct _objc_category { |
| 911 | char *category_name; |
| 912 | char *class_name; |
| 913 | struct _objc_method_list *instance_methods; |
| 914 | struct _objc_method_list *class_methods; |
| 915 | struct _objc_protocol_list *protocols; |
| 916 | uint32_t size; // <rdar://4585769> |
| 917 | struct _objc_property_list *instance_properties; |
| 918 | }; |
| 919 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 920 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 921 | unsigned Size = CGM.getTargetData().getABITypeSize(ObjCTypes.CategoryTy); |
| 922 | |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 923 | // FIXME: This is poor design, the OCD should have a pointer to the |
| 924 | // category decl. Additionally, note that Category can be null for |
| 925 | // the @implementation w/o an @interface case. Sema should just |
| 926 | // create one for us as it does for @implementation so everyone else |
| 927 | // can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 928 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 929 | const ObjCCategoryDecl *Category = |
| 930 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 931 | std::string ExtName(std::string(Interface->getName()) + |
| 932 | "_" + |
| 933 | OCD->getName()); |
| 934 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 935 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 936 | for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(), |
| 937 | e = OCD->instmeth_end(); i != e; ++i) { |
| 938 | // Instance methods should always be defined. |
| 939 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 940 | } |
| 941 | for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(), |
| 942 | e = OCD->classmeth_end(); i != e; ++i) { |
| 943 | // Class methods should always be defined. |
| 944 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 945 | } |
| 946 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 947 | std::vector<llvm::Constant*> Values(7); |
| 948 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 949 | Values[1] = GetClassName(Interface->getIdentifier()); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 950 | Values[2] = |
| 951 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") + |
| 952 | ExtName, |
| 953 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 954 | InstanceMethods); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 955 | Values[3] = |
| 956 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName, |
| 957 | "__OBJC,__cat_class_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 958 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 959 | if (Category) { |
| 960 | Values[4] = |
| 961 | EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName, |
| 962 | Category->protocol_begin(), |
| 963 | Category->protocol_end()); |
| 964 | } else { |
| 965 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 966 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 967 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 968 | |
| 969 | // If there is no category @interface then there can be no properties. |
| 970 | if (Category) { |
| 971 | Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 972 | OCD, |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 973 | Category->classprop_begin(), |
| 974 | Category->classprop_end()); |
| 975 | } else { |
| 976 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 977 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 978 | |
| 979 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
| 980 | Values); |
| 981 | |
| 982 | llvm::GlobalVariable *GV = |
| 983 | new llvm::GlobalVariable(ObjCTypes.CategoryTy, false, |
| 984 | llvm::GlobalValue::InternalLinkage, |
| 985 | Init, |
| 986 | std::string("\01L_OBJC_CATEGORY_")+ExtName, |
| 987 | &CGM.getModule()); |
| 988 | GV->setSection("__OBJC,__category,regular,no_dead_strip"); |
| 989 | UsedGlobals.push_back(GV); |
| 990 | DefinedCategories.push_back(GV); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 991 | } |
| 992 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 993 | // FIXME: Get from somewhere? |
| 994 | enum ClassFlags { |
| 995 | eClassFlags_Factory = 0x00001, |
| 996 | eClassFlags_Meta = 0x00002, |
| 997 | // <rdr://5142207> |
| 998 | eClassFlags_HasCXXStructors = 0x02000, |
| 999 | eClassFlags_Hidden = 0x20000, |
| 1000 | eClassFlags_ABI2_Hidden = 0x00010, |
| 1001 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 1002 | }; |
| 1003 | |
| 1004 | // <rdr://5142207&4705298&4843145> |
| 1005 | static bool IsClassHidden(const ObjCInterfaceDecl *ID) { |
| 1006 | if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) { |
| 1007 | // FIXME: Support -fvisibility |
| 1008 | switch (attr->getVisibility()) { |
| 1009 | default: |
| 1010 | assert(0 && "Unknown visibility"); |
| 1011 | return false; |
| 1012 | case VisibilityAttr::DefaultVisibility: |
| 1013 | case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here? |
| 1014 | return false; |
| 1015 | case VisibilityAttr::HiddenVisibility: |
| 1016 | return true; |
| 1017 | } |
| 1018 | } else { |
| 1019 | return false; // FIXME: Support -fvisibility |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | /* |
| 1024 | struct _objc_class { |
| 1025 | Class isa; |
| 1026 | Class super_class; |
| 1027 | const char *name; |
| 1028 | long version; |
| 1029 | long info; |
| 1030 | long instance_size; |
| 1031 | struct _objc_ivar_list *ivars; |
| 1032 | struct _objc_method_list *methods; |
| 1033 | struct _objc_cache *cache; |
| 1034 | struct _objc_protocol_list *protocols; |
| 1035 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 1036 | const char *ivar_layout; |
| 1037 | struct _objc_class_ext *ext; |
| 1038 | }; |
| 1039 | |
| 1040 | See EmitClassExtension(); |
| 1041 | */ |
| 1042 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1043 | DefinedSymbols.insert(ID->getIdentifier()); |
| 1044 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1045 | const char *ClassName = ID->getName(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1046 | // FIXME: Gross |
| 1047 | ObjCInterfaceDecl *Interface = |
| 1048 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1049 | llvm::Constant *Protocols = |
| 1050 | EmitProtocolList(std::string("\01L_OBJC_CLASS_PROTOCOLS_") + ID->getName(), |
| 1051 | Interface->protocol_begin(), |
| 1052 | Interface->protocol_end()); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1053 | const llvm::Type *InterfaceTy = |
| 1054 | CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface)); |
| 1055 | unsigned Flags = eClassFlags_Factory; |
| 1056 | unsigned Size = CGM.getTargetData().getABITypeSize(InterfaceTy); |
| 1057 | |
| 1058 | // FIXME: Set CXX-structors flag. |
| 1059 | if (IsClassHidden(ID->getClassInterface())) |
| 1060 | Flags |= eClassFlags_Hidden; |
| 1061 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1062 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1063 | for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(), |
| 1064 | e = ID->instmeth_end(); i != e; ++i) { |
| 1065 | // Instance methods should always be defined. |
| 1066 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1067 | } |
| 1068 | for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(), |
| 1069 | e = ID->classmeth_end(); i != e; ++i) { |
| 1070 | // Class methods should always be defined. |
| 1071 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1072 | } |
| 1073 | |
| 1074 | for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(), |
| 1075 | e = ID->propimpl_end(); i != e; ++i) { |
| 1076 | ObjCPropertyImplDecl *PID = *i; |
| 1077 | |
| 1078 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 1079 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 1080 | |
| 1081 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 1082 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1083 | InstanceMethods.push_back(C); |
| 1084 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 1085 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1086 | InstanceMethods.push_back(C); |
| 1087 | } |
| 1088 | } |
| 1089 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1090 | std::vector<llvm::Constant*> Values(12); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1091 | Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1092 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1093 | // Record a reference to the super class. |
| 1094 | LazySymbols.insert(Super->getIdentifier()); |
| 1095 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1096 | Values[ 1] = |
| 1097 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1098 | ObjCTypes.ClassPtrTy); |
| 1099 | } else { |
| 1100 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1101 | } |
| 1102 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1103 | // Version is always 0. |
| 1104 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1105 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1106 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
| 1107 | Values[ 6] = EmitIvarList(ID, false, InterfaceTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1108 | Values[ 7] = |
| 1109 | EmitMethodList(std::string("\01L_OBJC_INSTANCE_METHODS_") + ID->getName(), |
| 1110 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1111 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1112 | // cache is always NULL. |
| 1113 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1114 | Values[ 9] = Protocols; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1115 | // FIXME: Set ivar_layout |
| 1116 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1117 | Values[11] = EmitClassExtension(ID); |
| 1118 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1119 | Values); |
| 1120 | |
| 1121 | llvm::GlobalVariable *GV = |
| 1122 | new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1123 | llvm::GlobalValue::InternalLinkage, |
| 1124 | Init, |
| 1125 | std::string("\01L_OBJC_CLASS_")+ClassName, |
| 1126 | &CGM.getModule()); |
| 1127 | GV->setSection("__OBJC,__class,regular,no_dead_strip"); |
| 1128 | UsedGlobals.push_back(GV); |
| 1129 | // FIXME: Why? |
| 1130 | GV->setAlignment(32); |
| 1131 | DefinedClasses.push_back(GV); |
| 1132 | } |
| 1133 | |
| 1134 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1135 | llvm::Constant *Protocols, |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1136 | const llvm::Type *InterfaceTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1137 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1138 | const char *ClassName = ID->getName(); |
| 1139 | unsigned Flags = eClassFlags_Meta; |
| 1140 | unsigned Size = CGM.getTargetData().getABITypeSize(ObjCTypes.ClassTy); |
| 1141 | |
| 1142 | if (IsClassHidden(ID->getClassInterface())) |
| 1143 | Flags |= eClassFlags_Hidden; |
| 1144 | |
| 1145 | std::vector<llvm::Constant*> Values(12); |
| 1146 | // The isa for the metaclass is the root of the hierarchy. |
| 1147 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 1148 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 1149 | Root = Super; |
| 1150 | Values[ 0] = |
| 1151 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
| 1152 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1153 | // The super class for the metaclass is emitted as the name of the |
| 1154 | // super class. The runtime fixes this up to point to the |
| 1155 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1156 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
| 1157 | Values[ 1] = |
| 1158 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1159 | ObjCTypes.ClassPtrTy); |
| 1160 | } else { |
| 1161 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1162 | } |
| 1163 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1164 | // Version is always 0. |
| 1165 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1166 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1167 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
| 1168 | Values[ 6] = EmitIvarList(ID, true, InterfaceTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1169 | Values[ 7] = |
| 1170 | EmitMethodList(std::string("\01L_OBJC_CLASS_METHODS_") + ID->getName(), |
| 1171 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1172 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1173 | // cache is always NULL. |
| 1174 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1175 | Values[ 9] = Protocols; |
| 1176 | // ivar_layout for metaclass is always NULL. |
| 1177 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 1178 | // The class extension is always unused for metaclasses. |
| 1179 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 1180 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1181 | Values); |
| 1182 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1183 | std::string Name("\01L_OBJC_METACLASS_"); |
| 1184 | Name += ClassName; |
| 1185 | |
| 1186 | // Check for a forward reference. |
| 1187 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 1188 | if (GV) { |
| 1189 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 1190 | "Forward metaclass reference has incorrect type."); |
| 1191 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 1192 | GV->setInitializer(Init); |
| 1193 | } else { |
| 1194 | GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1195 | llvm::GlobalValue::InternalLinkage, |
| 1196 | Init, Name, |
| 1197 | &CGM.getModule()); |
| 1198 | } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1199 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
| 1200 | UsedGlobals.push_back(GV); |
| 1201 | // FIXME: Why? |
| 1202 | GV->setAlignment(32); |
| 1203 | |
| 1204 | return GV; |
| 1205 | } |
| 1206 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1207 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
| 1208 | std::string Name("\01L_OBJC_METACLASS_"); |
| 1209 | Name += ID->getName(); |
| 1210 | |
| 1211 | // FIXME: Should we look these up somewhere other than the |
| 1212 | // module. Its a bit silly since we only generate these while |
| 1213 | // processing an implementation, so exactly one pointer would work |
| 1214 | // if know when we entered/exitted an implementation block. |
| 1215 | |
| 1216 | // Check for an existing forward reference. |
| 1217 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name)) { |
| 1218 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 1219 | "Forward metaclass reference has incorrect type."); |
| 1220 | return GV; |
| 1221 | } else { |
| 1222 | // Generate as an external reference to keep a consistent |
| 1223 | // module. This will be patched up when we emit the metaclass. |
| 1224 | return new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1225 | llvm::GlobalValue::ExternalLinkage, |
| 1226 | 0, |
| 1227 | Name, |
| 1228 | &CGM.getModule()); |
| 1229 | } |
| 1230 | } |
| 1231 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1232 | /* |
| 1233 | struct objc_class_ext { |
| 1234 | uint32_t size; |
| 1235 | const char *weak_ivar_layout; |
| 1236 | struct _objc_property_list *properties; |
| 1237 | }; |
| 1238 | */ |
| 1239 | llvm::Constant * |
| 1240 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
| 1241 | uint64_t Size = |
| 1242 | CGM.getTargetData().getABITypeSize(ObjCTypes.ClassExtensionTy); |
| 1243 | |
| 1244 | std::vector<llvm::Constant*> Values(3); |
| 1245 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1246 | // FIXME: Output weak_ivar_layout string. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1247 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1248 | Values[2] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + |
| 1249 | ID->getName(), |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1250 | ID, |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1251 | ID->getClassInterface()->classprop_begin(), |
| 1252 | ID->getClassInterface()->classprop_end()); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1253 | |
| 1254 | // Return null if no extension bits are used. |
| 1255 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
| 1256 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 1257 | |
| 1258 | llvm::Constant *Init = |
| 1259 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
| 1260 | llvm::GlobalVariable *GV = |
| 1261 | new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false, |
| 1262 | llvm::GlobalValue::InternalLinkage, |
| 1263 | Init, |
| 1264 | (std::string("\01L_OBJC_CLASSEXT_") + |
| 1265 | ID->getName()), |
| 1266 | &CGM.getModule()); |
| 1267 | // No special section, but goes in llvm.used |
| 1268 | UsedGlobals.push_back(GV); |
| 1269 | |
| 1270 | return GV; |
| 1271 | } |
| 1272 | |
| 1273 | /* |
| 1274 | struct objc_ivar { |
| 1275 | char *ivar_name; |
| 1276 | char *ivar_type; |
| 1277 | int ivar_offset; |
| 1278 | }; |
| 1279 | |
| 1280 | struct objc_ivar_list { |
| 1281 | int ivar_count; |
| 1282 | struct objc_ivar list[count]; |
| 1283 | }; |
| 1284 | */ |
| 1285 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
| 1286 | bool ForClass, |
| 1287 | const llvm::Type *InterfaceTy) { |
| 1288 | std::vector<llvm::Constant*> Ivars, Ivar(3); |
| 1289 | |
| 1290 | // When emitting the root class GCC emits ivar entries for the |
| 1291 | // actual class structure. It is not clear if we need to follow this |
| 1292 | // behavior; for now lets try and get away with not doing it. If so, |
| 1293 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 1294 | // for the class. |
| 1295 | if (ForClass) |
| 1296 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
| 1297 | |
| 1298 | const llvm::StructLayout *Layout = |
| 1299 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy)); |
| 1300 | for (ObjCInterfaceDecl::ivar_iterator |
| 1301 | i = ID->getClassInterface()->ivar_begin(), |
| 1302 | e = ID->getClassInterface()->ivar_end(); i != e; ++i) { |
| 1303 | ObjCIvarDecl *V = *i; |
| 1304 | unsigned Offset = |
| 1305 | Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(V)); |
| 1306 | std::string TypeStr; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1307 | Ivar[0] = GetMethodVarName(V->getIdentifier()); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1308 | CGM.getContext().getObjCEncodingForType(V->getType(), TypeStr, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1309 | Ivar[1] = GetMethodVarType(TypeStr); |
| 1310 | Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1311 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | // Return null for empty list. |
| 1315 | if (Ivars.empty()) |
| 1316 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
| 1317 | |
| 1318 | std::vector<llvm::Constant*> Values(2); |
| 1319 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 1320 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
| 1321 | Ivars.size()); |
| 1322 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
| 1323 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1324 | |
| 1325 | const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" : |
| 1326 | "\01L_OBJC_INSTANCE_VARIABLES_"); |
| 1327 | llvm::GlobalVariable *GV = |
| 1328 | new llvm::GlobalVariable(Init->getType(), false, |
| 1329 | llvm::GlobalValue::InternalLinkage, |
| 1330 | Init, |
| 1331 | std::string(Prefix) + ID->getName(), |
| 1332 | &CGM.getModule()); |
| 1333 | if (ForClass) { |
| 1334 | GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip"); |
| 1335 | // FIXME: Why is this only here? |
| 1336 | GV->setAlignment(32); |
| 1337 | } else { |
| 1338 | GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip"); |
| 1339 | } |
| 1340 | UsedGlobals.push_back(GV); |
| 1341 | return llvm::ConstantExpr::getBitCast(GV, |
| 1342 | ObjCTypes.IvarListPtrTy); |
| 1343 | } |
| 1344 | |
| 1345 | /* |
| 1346 | struct objc_method { |
| 1347 | SEL method_name; |
| 1348 | char *method_types; |
| 1349 | void *method; |
| 1350 | }; |
| 1351 | |
| 1352 | struct objc_method_list { |
| 1353 | struct objc_method_list *obsolete; |
| 1354 | int count; |
| 1355 | struct objc_method methods_list[count]; |
| 1356 | }; |
| 1357 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1358 | |
| 1359 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 1360 | /// given method if it has been defined. The result is null if the |
| 1361 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1362 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1363 | // FIXME: Use DenseMap::lookup |
| 1364 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 1365 | if (!Fn) |
| 1366 | return 0; |
| 1367 | |
| 1368 | std::vector<llvm::Constant*> Method(3); |
| 1369 | Method[0] = |
| 1370 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1371 | ObjCTypes.SelectorPtrTy); |
| 1372 | Method[1] = GetMethodVarType(MD); |
| 1373 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 1374 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 1375 | } |
| 1376 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1377 | llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name, |
| 1378 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1379 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1380 | // Return null for empty list. |
| 1381 | if (Methods.empty()) |
| 1382 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
| 1383 | |
| 1384 | std::vector<llvm::Constant*> Values(3); |
| 1385 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 1386 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 1387 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 1388 | Methods.size()); |
| 1389 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 1390 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1391 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1392 | llvm::GlobalVariable *GV = |
| 1393 | new llvm::GlobalVariable(Init->getType(), false, |
| 1394 | llvm::GlobalValue::InternalLinkage, |
| 1395 | Init, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1396 | Name, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1397 | &CGM.getModule()); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1398 | GV->setSection(Section); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1399 | UsedGlobals.push_back(GV); |
| 1400 | return llvm::ConstantExpr::getBitCast(GV, |
| 1401 | ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | llvm::Function *CGObjCMac::GenerateMethod(const ObjCMethodDecl *OMD) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1405 | std::string Name; |
| 1406 | GetNameForMethod(OMD, Name); |
| 1407 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1408 | const llvm::FunctionType *MethodTy = |
| 1409 | CGM.getTypes().GetFunctionType(CGFunctionInfo(OMD, CGM.getContext())); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1410 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1411 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1412 | llvm::GlobalValue::InternalLinkage, |
| 1413 | Name, |
| 1414 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1415 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1416 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1417 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1421 | // Abuse this interface function as a place to finalize. |
| 1422 | FinishModule(); |
| 1423 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1424 | return NULL; |
| 1425 | } |
| 1426 | |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 1427 | llvm::Function *CGObjCMac::GetPropertyGetFunction() { |
| 1428 | return ObjCTypes.GetPropertyFn; |
| 1429 | } |
| 1430 | |
| 1431 | llvm::Function *CGObjCMac::GetPropertySetFunction() { |
| 1432 | return ObjCTypes.SetPropertyFn; |
| 1433 | } |
| 1434 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1435 | llvm::Function *CGObjCMac::EnumerationMutationFunction() |
| 1436 | { |
| 1437 | return ObjCTypes.EnumerationMutationFn; |
| 1438 | } |
| 1439 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1440 | /* |
| 1441 | |
| 1442 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 1443 | -- |
| 1444 | |
| 1445 | The basic framework for a @try-catch-finally is as follows: |
| 1446 | { |
| 1447 | objc_exception_data d; |
| 1448 | id _rethrow = null; |
| 1449 | |
| 1450 | objc_exception_try_enter(&d); |
| 1451 | if (!setjmp(d.jmp_buf)) { |
| 1452 | ... try body ... |
| 1453 | } else { |
| 1454 | // exception path |
| 1455 | id _caught = objc_exception_extract(&d); |
| 1456 | |
| 1457 | // enter new try scope for handlers |
| 1458 | if (!setjmp(d.jmp_buf)) { |
| 1459 | ... match exception and execute catch blocks ... |
| 1460 | |
| 1461 | // fell off end, rethrow. |
| 1462 | _rethrow = _caught; |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1463 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1464 | } else { |
| 1465 | // exception in catch block |
| 1466 | _rethrow = objc_exception_extract(&d); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1467 | ... jump-through-finally_no_exit to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1468 | } |
| 1469 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1470 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1471 | |
| 1472 | finally: |
| 1473 | // match either the initial try_enter or the catch try_enter, |
| 1474 | // depending on the path followed. |
| 1475 | objc_exception_try_exit(&d); |
| 1476 | finally_no_exit: |
| 1477 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1478 | ... dispatch to finally destination ... |
| 1479 | |
| 1480 | finally_rethrow: |
| 1481 | objc_exception_throw(_rethrow); |
| 1482 | |
| 1483 | finally_end: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | This framework differs slightly from the one gcc uses, in that gcc |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1487 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 1488 | and if the object should be rethrown. This breaks in the face of |
| 1489 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1490 | |
| 1491 | We specialize this framework for a few particular circumstances: |
| 1492 | |
| 1493 | - If there are no catch blocks, then we avoid emitting the second |
| 1494 | exception handling context. |
| 1495 | |
| 1496 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 1497 | e)) we avoid emitting the code to rethrow an uncaught exception. |
| 1498 | |
| 1499 | - FIXME: If there is no @finally block we can do a few more |
| 1500 | simplifications. |
| 1501 | |
| 1502 | Rethrows and Jumps-Through-Finally |
| 1503 | -- |
| 1504 | |
| 1505 | Support for implicit rethrows and jumping through the finally block is |
| 1506 | handled by storing the current exception-handling context in |
| 1507 | ObjCEHStack. |
| 1508 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1509 | In order to implement proper @finally semantics, we support one basic |
| 1510 | mechanism for jumping through the finally block to an arbitrary |
| 1511 | destination. Constructs which generate exits from a @try or @catch |
| 1512 | block use this mechanism to implement the proper semantics by chaining |
| 1513 | jumps, as necessary. |
| 1514 | |
| 1515 | This mechanism works like the one used for indirect goto: we |
| 1516 | arbitrarily assign an ID to each destination and store the ID for the |
| 1517 | destination in a variable prior to entering the finally block. At the |
| 1518 | end of the finally block we simply create a switch to the proper |
| 1519 | destination. |
| 1520 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1521 | */ |
| 1522 | |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1523 | void CGObjCMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1524 | const ObjCAtTryStmt &S) { |
| 1525 | // Create various blocks we refer to for handling @finally. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 1526 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
| 1527 | llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit"); |
| 1528 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
| 1529 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1530 | llvm::Value *DestCode = |
| 1531 | CGF.CreateTempAlloca(llvm::Type::Int32Ty, "finally.dst"); |
| 1532 | |
| 1533 | // Generate jump code. Done here so we can directly add things to |
| 1534 | // the switch instruction. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 1535 | llvm::BasicBlock *FinallyJump = CGF.createBasicBlock("finally.jump"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1536 | llvm::SwitchInst *FinallySwitch = |
| 1537 | llvm::SwitchInst::Create(new llvm::LoadInst(DestCode, "", FinallyJump), |
| 1538 | FinallyEnd, 10, FinallyJump); |
| 1539 | |
| 1540 | // Push an EH context entry, used for handling rethrows and jumps |
| 1541 | // through finally. |
| 1542 | CodeGenFunction::ObjCEHEntry EHEntry(FinallyBlock, FinallyNoExit, |
| 1543 | FinallySwitch, DestCode); |
| 1544 | CGF.ObjCEHStack.push_back(&EHEntry); |
| 1545 | |
| 1546 | // Allocate memory for the exception data and rethrow pointer. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1547 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 1548 | "exceptiondata.ptr"); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1549 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy, |
| 1550 | "_rethrow"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1551 | |
| 1552 | // Enter a new try block and call setjmp. |
| 1553 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData); |
| 1554 | llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0, |
| 1555 | "jmpbufarray"); |
| 1556 | JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp"); |
| 1557 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn, |
| 1558 | JmpBufPtr, "result"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1559 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 1560 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 1561 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 1562 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 1563 | TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1564 | |
| 1565 | // Emit the @try block. |
| 1566 | CGF.EmitBlock(TryBlock); |
| 1567 | CGF.EmitStmt(S.getTryBody()); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1568 | CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1569 | |
| 1570 | // Emit the "exception in @try" block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 1571 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 1572 | |
| 1573 | // Retrieve the exception object. We may emit multiple blocks but |
| 1574 | // nothing can cross this so the value is already in SSA form. |
| 1575 | llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn, |
| 1576 | ExceptionData, |
| 1577 | "caught"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1578 | EHEntry.Exception = Caught; |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 1579 | if (const ObjCAtCatchStmt* CatchStmt = S.getCatchStmts()) { |
| 1580 | // Enter a new exception try block (in case a @catch block throws |
| 1581 | // an exception). |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1582 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 1583 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1584 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn, |
| 1585 | JmpBufPtr, "result"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 1586 | llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1587 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 1588 | llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch"); |
| 1589 | llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler"); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 1590 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1591 | |
| 1592 | CGF.EmitBlock(CatchBlock); |
| 1593 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 1594 | // Handle catch list. As a special case we check if everything is |
| 1595 | // matched and avoid generating code for falling off the end if |
| 1596 | // so. |
| 1597 | bool AllMatched = false; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1598 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 1599 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1600 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 1601 | const DeclStmt *CatchParam = |
| 1602 | cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt()); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 1603 | const VarDecl *VD = 0; |
| 1604 | const PointerType *PT = 0; |
| 1605 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1606 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 1607 | if (!CatchParam) { |
| 1608 | AllMatched = true; |
| 1609 | } else { |
Ted Kremenek | de3b8fb | 2008-10-06 20:58:56 +0000 | [diff] [blame] | 1610 | VD = cast<VarDecl>(CatchParam->getSolitaryDecl()); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 1611 | PT = VD->getType()->getAsPointerType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1612 | |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 1613 | // catch(id e) always matches. |
| 1614 | // FIXME: For the time being we also match id<X>; this should |
| 1615 | // be rejected by Sema instead. |
| 1616 | if ((PT && CGF.getContext().isObjCIdType(PT->getPointeeType())) || |
| 1617 | VD->getType()->isObjCQualifiedIdType()) |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 1618 | AllMatched = true; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 1621 | if (AllMatched) { |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 1622 | if (CatchParam) { |
| 1623 | CGF.EmitStmt(CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame^] | 1624 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 1625 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 1626 | } |
Anders Carlsson | 1452f55 | 2008-09-11 08:21:54 +0000 | [diff] [blame] | 1627 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 1628 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1629 | CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1630 | break; |
| 1631 | } |
| 1632 | |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 1633 | assert(PT && "Unexpected non-pointer type in @catch"); |
| 1634 | QualType T = PT->getPointeeType(); |
Anders Carlsson | 4b7ff6e | 2008-09-11 06:35:14 +0000 | [diff] [blame] | 1635 | const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1636 | assert(ObjCType && "Catch parameter must have Objective-C type!"); |
| 1637 | |
| 1638 | // Check if the @catch block matches the exception object. |
| 1639 | llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl()); |
| 1640 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1641 | llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn, |
| 1642 | Class, Caught, "match"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1643 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 1644 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1645 | |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 1646 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 1647 | MatchedBlock, NextCatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1648 | |
| 1649 | // Emit the @catch block. |
| 1650 | CGF.EmitBlock(MatchedBlock); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1651 | CGF.EmitStmt(CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame^] | 1652 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1653 | |
| 1654 | llvm::Value *Tmp = |
| 1655 | CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()), |
| 1656 | "tmp"); |
| 1657 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 1658 | |
| 1659 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1660 | CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1661 | |
| 1662 | CGF.EmitBlock(NextCatchBlock); |
| 1663 | } |
| 1664 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 1665 | if (!AllMatched) { |
| 1666 | // None of the handlers caught the exception, so store it to be |
| 1667 | // rethrown at the end of the @finally block. |
| 1668 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1669 | CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | // Emit the exception handler for the @catch blocks. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 1673 | CGF.EmitBlock(CatchHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 1674 | CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn, |
| 1675 | ExceptionData), |
| 1676 | RethrowPtr); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1677 | CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 1678 | } else { |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1679 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1680 | CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1683 | // Pop the exception-handling stack entry. It is important to do |
| 1684 | // this now, because the code in the @finally block is not in this |
| 1685 | // context. |
| 1686 | CGF.ObjCEHStack.pop_back(); |
| 1687 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1688 | // Emit the @finally block. |
| 1689 | CGF.EmitBlock(FinallyBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1690 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 1691 | |
| 1692 | CGF.EmitBlock(FinallyNoExit); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1693 | if (const ObjCAtFinallyStmt* FinallyStmt = S.getFinallyStmt()) |
| 1694 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 1695 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1696 | CGF.EmitBlock(FinallyJump); |
| 1697 | |
| 1698 | CGF.EmitBlock(FinallyRethrow); |
| 1699 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, |
| 1700 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 1701 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1702 | |
| 1703 | CGF.EmitBlock(FinallyEnd); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1707 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 1708 | llvm::Value *ExceptionAsObject; |
| 1709 | |
| 1710 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 1711 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
| 1712 | ExceptionAsObject = |
| 1713 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 1714 | } else { |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1715 | assert((!CGF.ObjCEHStack.empty() && CGF.ObjCEHStack.back()->Exception) && |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1716 | "Unexpected rethrow outside @catch block."); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1717 | ExceptionAsObject = CGF.ObjCEHStack.back()->Exception; |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
| 1720 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1721 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame^] | 1722 | |
| 1723 | // Clear the insertion point to indicate we are in unreachable code. |
| 1724 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1727 | void CodeGenFunction::EmitJumpThroughFinally(ObjCEHEntry *E, |
| 1728 | llvm::BasicBlock *Dst, |
| 1729 | bool ExecuteTryExit) { |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame^] | 1730 | if (!HaveInsertPoint()) |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1731 | return; |
| 1732 | |
| 1733 | // Find the destination code for this block. We always use 0 for the |
| 1734 | // fallthrough block (default destination). |
| 1735 | llvm::SwitchInst *SI = E->FinallySwitch; |
| 1736 | llvm::ConstantInt *ID; |
| 1737 | if (Dst == SI->getDefaultDest()) { |
| 1738 | ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 1739 | } else { |
| 1740 | ID = SI->findCaseDest(Dst); |
| 1741 | if (!ID) { |
| 1742 | // No code found, get a new unique one by just using the number |
| 1743 | // of switch successors. |
| 1744 | ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, SI->getNumSuccessors()); |
| 1745 | SI->addCase(ID, Dst); |
| 1746 | } |
| 1747 | } |
| 1748 | |
| 1749 | // Set the destination code and branch. |
| 1750 | Builder.CreateStore(ID, E->DestCode); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame^] | 1751 | EmitBranch(ExecuteTryExit ? E->FinallyBlock : E->FinallyNoExit); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1754 | /* *** Private Interface *** */ |
| 1755 | |
| 1756 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 1757 | /// level information. |
| 1758 | /// |
| 1759 | /// See: <rdr://4810609&4810587&4810587> |
| 1760 | /// struct IMAGE_INFO { |
| 1761 | /// unsigned version; |
| 1762 | /// unsigned flags; |
| 1763 | /// }; |
| 1764 | enum ImageInfoFlags { |
| 1765 | eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies |
| 1766 | eImageInfo_GarbageCollected = (1 << 1), |
| 1767 | eImageInfo_GCOnly = (1 << 2) |
| 1768 | }; |
| 1769 | |
| 1770 | void CGObjCMac::EmitImageInfo() { |
| 1771 | unsigned version = 0; // Version is unused? |
| 1772 | unsigned flags = 0; |
| 1773 | |
| 1774 | // FIXME: Fix and continue? |
| 1775 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 1776 | flags |= eImageInfo_GarbageCollected; |
| 1777 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 1778 | flags |= eImageInfo_GCOnly; |
| 1779 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1780 | // Emitted as int[2]; |
| 1781 | llvm::Constant *values[2] = { |
| 1782 | llvm::ConstantInt::get(llvm::Type::Int32Ty, version), |
| 1783 | llvm::ConstantInt::get(llvm::Type::Int32Ty, flags) |
| 1784 | }; |
| 1785 | llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1786 | llvm::GlobalVariable *GV = |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1787 | new llvm::GlobalVariable(AT, true, |
| 1788 | llvm::GlobalValue::InternalLinkage, |
| 1789 | llvm::ConstantArray::get(AT, values, 2), |
| 1790 | "\01L_OBJC_IMAGE_INFO", |
| 1791 | &CGM.getModule()); |
| 1792 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1793 | if (ObjCABI == 1) { |
| 1794 | GV->setSection("__OBJC, __image_info,regular"); |
| 1795 | } else { |
| 1796 | GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); |
| 1797 | } |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1798 | |
| 1799 | UsedGlobals.push_back(GV); |
| 1800 | } |
| 1801 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1802 | |
| 1803 | // struct objc_module { |
| 1804 | // unsigned long version; |
| 1805 | // unsigned long size; |
| 1806 | // const char *name; |
| 1807 | // Symtab symtab; |
| 1808 | // }; |
| 1809 | |
| 1810 | // FIXME: Get from somewhere |
| 1811 | static const int ModuleVersion = 7; |
| 1812 | |
| 1813 | void CGObjCMac::EmitModuleInfo() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1814 | uint64_t Size = CGM.getTargetData().getABITypeSize(ObjCTypes.ModuleTy); |
| 1815 | |
| 1816 | std::vector<llvm::Constant*> Values(4); |
| 1817 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion); |
| 1818 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1819 | // This used to be the filename, now it is unused. <rdr://4327263> |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1820 | Values[2] = GetClassName(&CGM.getContext().Idents.get("")); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1821 | Values[3] = EmitModuleSymbols(); |
| 1822 | |
| 1823 | llvm::GlobalVariable *GV = |
| 1824 | new llvm::GlobalVariable(ObjCTypes.ModuleTy, false, |
| 1825 | llvm::GlobalValue::InternalLinkage, |
| 1826 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, |
| 1827 | Values), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1828 | "\01L_OBJC_MODULES", |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1829 | &CGM.getModule()); |
| 1830 | GV->setSection("__OBJC,__module_info,regular,no_dead_strip"); |
| 1831 | UsedGlobals.push_back(GV); |
| 1832 | } |
| 1833 | |
| 1834 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1835 | unsigned NumClasses = DefinedClasses.size(); |
| 1836 | unsigned NumCategories = DefinedCategories.size(); |
| 1837 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1838 | // Return null if no symbols were defined. |
| 1839 | if (!NumClasses && !NumCategories) |
| 1840 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
| 1841 | |
| 1842 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1843 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1844 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
| 1845 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 1846 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
| 1847 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1848 | // The runtime expects exactly the list of defined classes followed |
| 1849 | // by the list of defined categories, in a single array. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1850 | std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1851 | for (unsigned i=0; i<NumClasses; i++) |
| 1852 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 1853 | ObjCTypes.Int8PtrTy); |
| 1854 | for (unsigned i=0; i<NumCategories; i++) |
| 1855 | Symbols[NumClasses + i] = |
| 1856 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 1857 | ObjCTypes.Int8PtrTy); |
| 1858 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1859 | Values[4] = |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1860 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1861 | NumClasses + NumCategories), |
| 1862 | Symbols); |
| 1863 | |
| 1864 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1865 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1866 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1867 | new llvm::GlobalVariable(Init->getType(), false, |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1868 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1869 | Init, |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1870 | "\01L_OBJC_SYMBOLS", |
| 1871 | &CGM.getModule()); |
| 1872 | GV->setSection("__OBJC,__symbols,regular,no_dead_strip"); |
| 1873 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1874 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
| 1875 | } |
| 1876 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1877 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1878 | const ObjCInterfaceDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1879 | LazySymbols.insert(ID->getIdentifier()); |
| 1880 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1881 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 1882 | |
| 1883 | if (!Entry) { |
| 1884 | llvm::Constant *Casted = |
| 1885 | llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()), |
| 1886 | ObjCTypes.ClassPtrTy); |
| 1887 | Entry = |
| 1888 | new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false, |
| 1889 | llvm::GlobalValue::InternalLinkage, |
| 1890 | Casted, "\01L_OBJC_CLASS_REFERENCES_", |
| 1891 | &CGM.getModule()); |
| 1892 | Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip"); |
| 1893 | UsedGlobals.push_back(Entry); |
| 1894 | } |
| 1895 | |
| 1896 | return Builder.CreateLoad(Entry, false, "tmp"); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1897 | } |
| 1898 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1899 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 1900 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 1901 | |
| 1902 | if (!Entry) { |
| 1903 | llvm::Constant *Casted = |
| 1904 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 1905 | ObjCTypes.SelectorPtrTy); |
| 1906 | Entry = |
| 1907 | new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false, |
| 1908 | llvm::GlobalValue::InternalLinkage, |
| 1909 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_", |
| 1910 | &CGM.getModule()); |
| 1911 | Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip"); |
| 1912 | UsedGlobals.push_back(Entry); |
| 1913 | } |
| 1914 | |
| 1915 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 1916 | } |
| 1917 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1918 | llvm::Constant *CGObjCMac::GetClassName(IdentifierInfo *Ident) { |
| 1919 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1920 | |
| 1921 | if (!Entry) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1922 | llvm::Constant *C = llvm::ConstantArray::get(Ident->getName()); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1923 | Entry = |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1924 | new llvm::GlobalVariable(C->getType(), false, |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1925 | llvm::GlobalValue::InternalLinkage, |
| 1926 | C, "\01L_OBJC_CLASS_NAME_", |
| 1927 | &CGM.getModule()); |
| 1928 | Entry->setSection("__TEXT,__cstring,cstring_literals"); |
| 1929 | UsedGlobals.push_back(Entry); |
| 1930 | } |
| 1931 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1932 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 1935 | llvm::Constant *CGObjCMac::GetMethodVarName(Selector Sel) { |
| 1936 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 1937 | |
| 1938 | if (!Entry) { |
| 1939 | llvm::Constant *C = llvm::ConstantArray::get(Sel.getName()); |
| 1940 | Entry = |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1941 | new llvm::GlobalVariable(C->getType(), false, |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 1942 | llvm::GlobalValue::InternalLinkage, |
| 1943 | C, "\01L_OBJC_METH_VAR_NAME_", |
| 1944 | &CGM.getModule()); |
| 1945 | Entry->setSection("__TEXT,__cstring,cstring_literals"); |
| 1946 | UsedGlobals.push_back(Entry); |
| 1947 | } |
| 1948 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1949 | return getConstantGEP(Entry, 0, 0); |
| 1950 | } |
| 1951 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1952 | // FIXME: Merge into a single cstring creation function. |
| 1953 | llvm::Constant *CGObjCMac::GetMethodVarName(IdentifierInfo *ID) { |
| 1954 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 1955 | } |
| 1956 | |
| 1957 | // FIXME: Merge into a single cstring creation function. |
| 1958 | llvm::Constant *CGObjCMac::GetMethodVarName(const std::string &Name) { |
| 1959 | return GetMethodVarName(&CGM.getContext().Idents.get(Name)); |
| 1960 | } |
| 1961 | |
| 1962 | llvm::Constant *CGObjCMac::GetMethodVarType(const std::string &Name) { |
| 1963 | llvm::GlobalVariable *&Entry = MethodVarTypes[Name]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1964 | |
| 1965 | if (!Entry) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1966 | llvm::Constant *C = llvm::ConstantArray::get(Name); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1967 | Entry = |
| 1968 | new llvm::GlobalVariable(C->getType(), false, |
| 1969 | llvm::GlobalValue::InternalLinkage, |
| 1970 | C, "\01L_OBJC_METH_VAR_TYPE_", |
| 1971 | &CGM.getModule()); |
| 1972 | Entry->setSection("__TEXT,__cstring,cstring_literals"); |
| 1973 | UsedGlobals.push_back(Entry); |
| 1974 | } |
| 1975 | |
| 1976 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 1977 | } |
| 1978 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1979 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1980 | llvm::Constant *CGObjCMac::GetMethodVarType(const ObjCMethodDecl *D) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1981 | std::string TypeStr; |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1982 | CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D), |
| 1983 | TypeStr); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1984 | return GetMethodVarType(TypeStr); |
| 1985 | } |
| 1986 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1987 | // FIXME: Merge into a single cstring creation function. |
| 1988 | llvm::Constant *CGObjCMac::GetPropertyName(IdentifierInfo *Ident) { |
| 1989 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
| 1990 | |
| 1991 | if (!Entry) { |
| 1992 | llvm::Constant *C = llvm::ConstantArray::get(Ident->getName()); |
| 1993 | Entry = |
| 1994 | new llvm::GlobalVariable(C->getType(), false, |
| 1995 | llvm::GlobalValue::InternalLinkage, |
| 1996 | C, "\01L_OBJC_PROP_NAME_ATTR_", |
| 1997 | &CGM.getModule()); |
| 1998 | Entry->setSection("__TEXT,__cstring,cstring_literals"); |
| 1999 | UsedGlobals.push_back(Entry); |
| 2000 | } |
| 2001 | |
| 2002 | return getConstantGEP(Entry, 0, 0); |
| 2003 | } |
| 2004 | |
| 2005 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2006 | // FIXME: This Decl should be more precise. |
| 2007 | llvm::Constant *CGObjCMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 2008 | const Decl *Container) { |
| 2009 | std::string TypeStr; |
| 2010 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2011 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 2012 | } |
| 2013 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2014 | void CGObjCMac::GetNameForMethod(const ObjCMethodDecl *D, |
| 2015 | std::string &NameOut) { |
| 2016 | // FIXME: Find the mangling GCC uses. |
| 2017 | std::stringstream s; |
| 2018 | s << (D->isInstance() ? "-" : "+"); |
| 2019 | s << "["; |
| 2020 | s << D->getClassInterface()->getName(); |
| 2021 | s << " "; |
| 2022 | s << D->getSelector().getName(); |
| 2023 | s << "]"; |
| 2024 | NameOut = s.str(); |
| 2025 | } |
| 2026 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2027 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2028 | EmitModuleInfo(); |
| 2029 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2030 | // Emit the dummy bodies for any protocols which were referenced but |
| 2031 | // never defined. |
| 2032 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
| 2033 | i = Protocols.begin(), e = Protocols.end(); i != e; ++i) { |
| 2034 | if (i->second->hasInitializer()) |
| 2035 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2036 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2037 | std::vector<llvm::Constant*> Values(5); |
| 2038 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 2039 | Values[1] = GetClassName(i->first); |
| 2040 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 2041 | Values[3] = Values[4] = |
| 2042 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 2043 | i->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 2044 | i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 2045 | Values)); |
| 2046 | } |
| 2047 | |
| 2048 | std::vector<llvm::Constant*> Used; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2049 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2050 | e = UsedGlobals.end(); i != e; ++i) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2051 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2054 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2055 | llvm::GlobalValue *GV = |
| 2056 | new llvm::GlobalVariable(AT, false, |
| 2057 | llvm::GlobalValue::AppendingLinkage, |
| 2058 | llvm::ConstantArray::get(AT, Used), |
| 2059 | "llvm.used", |
| 2060 | &CGM.getModule()); |
| 2061 | |
| 2062 | GV->setSection("llvm.metadata"); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2063 | |
| 2064 | // Add assembler directives to add lazy undefined symbol references |
| 2065 | // for classes which are referenced but not defined. This is |
| 2066 | // important for correct linker interaction. |
| 2067 | |
| 2068 | // FIXME: Uh, this isn't particularly portable. |
| 2069 | std::stringstream s; |
| 2070 | for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(), |
| 2071 | e = LazySymbols.end(); i != e; ++i) { |
| 2072 | s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n"; |
| 2073 | } |
| 2074 | for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(), |
| 2075 | e = DefinedSymbols.end(); i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2076 | s << "\t.objc_class_name_" << (*i)->getName() << "=0\n" |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2077 | << "\t.globl .objc_class_name_" << (*i)->getName() << "\n"; |
| 2078 | } |
| 2079 | CGM.getModule().appendModuleInlineAsm(s.str()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
| 2082 | /* *** */ |
| 2083 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 2084 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 2085 | : CGM(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 2086 | { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2087 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 2088 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2089 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2090 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2091 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2092 | LongTy = Types.ConvertType(Ctx.LongTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2093 | Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 2094 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2095 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
| 2096 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2097 | |
| 2098 | // FIXME: It would be nice to unify this with the opaque type, so |
| 2099 | // that the IR comes out a bit cleaner. |
| 2100 | const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 2101 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2102 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2103 | MethodDescriptionTy = |
| 2104 | llvm::StructType::get(SelectorPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2105 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2106 | NULL); |
| 2107 | CGM.getModule().addTypeName("struct._objc_method_description", |
| 2108 | MethodDescriptionTy); |
| 2109 | |
| 2110 | MethodDescriptionListTy = |
| 2111 | llvm::StructType::get(IntTy, |
| 2112 | llvm::ArrayType::get(MethodDescriptionTy, 0), |
| 2113 | NULL); |
| 2114 | CGM.getModule().addTypeName("struct._objc_method_description_list", |
| 2115 | MethodDescriptionListTy); |
| 2116 | MethodDescriptionListPtrTy = |
| 2117 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
| 2118 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2119 | PropertyTy = llvm::StructType::get(Int8PtrTy, |
| 2120 | Int8PtrTy, |
| 2121 | NULL); |
| 2122 | CGM.getModule().addTypeName("struct._objc_property", |
| 2123 | PropertyTy); |
| 2124 | |
| 2125 | PropertyListTy = llvm::StructType::get(IntTy, |
| 2126 | IntTy, |
| 2127 | llvm::ArrayType::get(PropertyTy, 0), |
| 2128 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2129 | CGM.getModule().addTypeName("struct._objc_property_list", |
| 2130 | PropertyListTy); |
| 2131 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
| 2132 | |
| 2133 | // Protocol description structures |
| 2134 | |
| 2135 | ProtocolExtensionTy = |
| 2136 | llvm::StructType::get(Types.ConvertType(Ctx.IntTy), |
| 2137 | llvm::PointerType::getUnqual(MethodDescriptionListTy), |
| 2138 | llvm::PointerType::getUnqual(MethodDescriptionListTy), |
| 2139 | PropertyListPtrTy, |
| 2140 | NULL); |
| 2141 | CGM.getModule().addTypeName("struct._objc_protocol_extension", |
| 2142 | ProtocolExtensionTy); |
| 2143 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
| 2144 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2145 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2146 | |
| 2147 | llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(); |
| 2148 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 2149 | |
| 2150 | T = llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 2151 | LongTy, |
| 2152 | llvm::ArrayType::get(ProtocolTyHolder, 0), |
| 2153 | NULL); |
| 2154 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T); |
| 2155 | |
| 2156 | T = llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolExtensionTy), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2157 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2158 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 2159 | MethodDescriptionListPtrTy, |
| 2160 | MethodDescriptionListPtrTy, |
| 2161 | NULL); |
| 2162 | cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T); |
| 2163 | |
| 2164 | ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get()); |
| 2165 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 2166 | ProtocolListTy); |
| 2167 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
| 2168 | |
| 2169 | ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get()); |
| 2170 | CGM.getModule().addTypeName("struct.__objc_protocol", ProtocolTy); |
| 2171 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2172 | |
| 2173 | // Class description structures |
| 2174 | |
| 2175 | IvarTy = llvm::StructType::get(Int8PtrTy, |
| 2176 | Int8PtrTy, |
| 2177 | IntTy, |
| 2178 | NULL); |
| 2179 | CGM.getModule().addTypeName("struct._objc_ivar", IvarTy); |
| 2180 | |
| 2181 | IvarListTy = llvm::OpaqueType::get(); |
| 2182 | CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy); |
| 2183 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
| 2184 | |
| 2185 | MethodTy = llvm::StructType::get(SelectorPtrTy, |
| 2186 | Int8PtrTy, |
| 2187 | Int8PtrTy, |
| 2188 | NULL); |
| 2189 | CGM.getModule().addTypeName("struct._objc_method", MethodTy); |
| 2190 | |
| 2191 | MethodListTy = llvm::OpaqueType::get(); |
| 2192 | CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy); |
| 2193 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
| 2194 | |
| 2195 | CacheTy = llvm::OpaqueType::get(); |
| 2196 | CGM.getModule().addTypeName("struct._objc_cache", CacheTy); |
| 2197 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
| 2198 | |
| 2199 | ClassExtensionTy = |
| 2200 | llvm::StructType::get(IntTy, |
| 2201 | Int8PtrTy, |
| 2202 | PropertyListPtrTy, |
| 2203 | NULL); |
| 2204 | CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy); |
| 2205 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
| 2206 | |
| 2207 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 2208 | |
| 2209 | T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 2210 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 2211 | Int8PtrTy, |
| 2212 | LongTy, |
| 2213 | LongTy, |
| 2214 | LongTy, |
| 2215 | IvarListPtrTy, |
| 2216 | MethodListPtrTy, |
| 2217 | CachePtrTy, |
| 2218 | ProtocolListPtrTy, |
| 2219 | Int8PtrTy, |
| 2220 | ClassExtensionPtrTy, |
| 2221 | NULL); |
| 2222 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T); |
| 2223 | |
| 2224 | ClassTy = cast<llvm::StructType>(ClassTyHolder.get()); |
| 2225 | CGM.getModule().addTypeName("struct._objc_class", ClassTy); |
| 2226 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
| 2227 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2228 | CategoryTy = llvm::StructType::get(Int8PtrTy, |
| 2229 | Int8PtrTy, |
| 2230 | MethodListPtrTy, |
| 2231 | MethodListPtrTy, |
| 2232 | ProtocolListPtrTy, |
| 2233 | IntTy, |
| 2234 | PropertyListPtrTy, |
| 2235 | NULL); |
| 2236 | CGM.getModule().addTypeName("struct._objc_category", CategoryTy); |
| 2237 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 2238 | // I'm not sure I like this. The implicit coordination is a bit |
| 2239 | // gross. We should solve this in a reasonable fashion because this |
| 2240 | // is a pretty common task (match some runtime data structure with |
| 2241 | // an LLVM data structure). |
| 2242 | |
| 2243 | // FIXME: This is leaked. |
| 2244 | // FIXME: Merge with rewriter code? |
| 2245 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 2246 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2247 | &Ctx.Idents.get("_objc_super")); |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 2248 | FieldDecl *FieldDecls[2]; |
| 2249 | FieldDecls[0] = FieldDecl::Create(Ctx, SourceLocation(), 0, |
| 2250 | Ctx.getObjCIdType()); |
| 2251 | FieldDecls[1] = FieldDecl::Create(Ctx, SourceLocation(), 0, |
| 2252 | Ctx.getObjCClassType()); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2253 | RD->defineBody(Ctx, FieldDecls, 2); |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 2254 | |
| 2255 | SuperCTy = Ctx.getTagDeclType(RD); |
| 2256 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
| 2257 | |
| 2258 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 2259 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 2260 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2261 | // Global metadata structures |
| 2262 | |
| 2263 | SymtabTy = llvm::StructType::get(LongTy, |
| 2264 | SelectorPtrTy, |
| 2265 | ShortTy, |
| 2266 | ShortTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2267 | llvm::ArrayType::get(Int8PtrTy, 0), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2268 | NULL); |
| 2269 | CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy); |
| 2270 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
| 2271 | |
| 2272 | ModuleTy = |
| 2273 | llvm::StructType::get(LongTy, |
| 2274 | LongTy, |
| 2275 | Int8PtrTy, |
| 2276 | SymtabPtrTy, |
| 2277 | NULL); |
| 2278 | CGM.getModule().addTypeName("struct._objc_module", ModuleTy); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 2279 | |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2280 | // Message send functions. |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 2281 | |
| 2282 | std::vector<const llvm::Type*> Params; |
| 2283 | Params.push_back(ObjectPtrTy); |
| 2284 | Params.push_back(SelectorPtrTy); |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 2285 | MessageSendFn = |
| 2286 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 2287 | Params, |
| 2288 | true), |
| 2289 | "objc_msgSend"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 2290 | |
| 2291 | Params.clear(); |
| 2292 | Params.push_back(Int8PtrTy); |
| 2293 | Params.push_back(ObjectPtrTy); |
| 2294 | Params.push_back(SelectorPtrTy); |
| 2295 | MessageSendStretFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 2296 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 2297 | Params, |
| 2298 | true), |
| 2299 | "objc_msgSend_stret"); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 2300 | |
| 2301 | Params.clear(); |
| 2302 | Params.push_back(ObjectPtrTy); |
| 2303 | Params.push_back(SelectorPtrTy); |
| 2304 | // FIXME: This should be long double on x86_64? |
| 2305 | MessageSendFpretFn = |
| 2306 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy, |
| 2307 | Params, |
| 2308 | true), |
| 2309 | "objc_msgSend_fpret"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 2310 | |
| 2311 | Params.clear(); |
| 2312 | Params.push_back(SuperPtrTy); |
| 2313 | Params.push_back(SelectorPtrTy); |
| 2314 | MessageSendSuperFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 2315 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 2316 | Params, |
| 2317 | true), |
| 2318 | "objc_msgSendSuper"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 2319 | |
| 2320 | Params.clear(); |
| 2321 | Params.push_back(Int8PtrTy); |
| 2322 | Params.push_back(SuperPtrTy); |
| 2323 | Params.push_back(SelectorPtrTy); |
| 2324 | MessageSendSuperStretFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 2325 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 2326 | Params, |
| 2327 | true), |
| 2328 | "objc_msgSendSuper_stret"); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 2329 | |
| 2330 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 2331 | MessageSendSuperFpretFn = MessageSendSuperFn; |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2332 | |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2333 | // Property manipulation functions. |
| 2334 | |
| 2335 | Params.clear(); |
| 2336 | Params.push_back(ObjectPtrTy); |
| 2337 | Params.push_back(SelectorPtrTy); |
| 2338 | Params.push_back(LongTy); |
| 2339 | Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy)); |
| 2340 | GetPropertyFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 2341 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 2342 | Params, |
| 2343 | false), |
| 2344 | "objc_getProperty"); |
| 2345 | |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2346 | Params.clear(); |
| 2347 | Params.push_back(ObjectPtrTy); |
| 2348 | Params.push_back(SelectorPtrTy); |
| 2349 | Params.push_back(LongTy); |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 2350 | Params.push_back(ObjectPtrTy); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2351 | Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy)); |
| 2352 | Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy)); |
| 2353 | SetPropertyFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 2354 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 2355 | Params, |
| 2356 | false), |
| 2357 | "objc_setProperty"); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2358 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2359 | // Enumeration mutation. |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2360 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2361 | Params.clear(); |
| 2362 | Params.push_back(ObjectPtrTy); |
| 2363 | EnumerationMutationFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 2364 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 2365 | Params, |
| 2366 | false), |
| 2367 | "objc_enumerationMutation"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 2368 | |
| 2369 | // FIXME: This is the size of the setjmp buffer and should be |
| 2370 | // target specific. 18 is what's used on 32-bit X86. |
| 2371 | uint64_t SetJmpBufferSize = 18; |
| 2372 | |
| 2373 | // Exceptions |
| 2374 | const llvm::Type *StackPtrTy = |
Daniel Dunbar | 1000491 | 2008-09-27 06:32:25 +0000 | [diff] [blame] | 2375 | llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 2376 | |
| 2377 | ExceptionDataTy = |
| 2378 | llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty, |
| 2379 | SetJmpBufferSize), |
| 2380 | StackPtrTy, NULL); |
| 2381 | CGM.getModule().addTypeName("struct._objc_exception_data", |
| 2382 | ExceptionDataTy); |
| 2383 | |
| 2384 | Params.clear(); |
| 2385 | Params.push_back(ObjectPtrTy); |
| 2386 | ExceptionThrowFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 2387 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 2388 | Params, |
| 2389 | false), |
| 2390 | "objc_exception_throw"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 2391 | |
| 2392 | Params.clear(); |
| 2393 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 2394 | ExceptionTryEnterFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 2395 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 2396 | Params, |
| 2397 | false), |
| 2398 | "objc_exception_try_enter"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 2399 | ExceptionTryExitFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 2400 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 2401 | Params, |
| 2402 | false), |
| 2403 | "objc_exception_try_exit"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 2404 | ExceptionExtractFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 2405 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 2406 | Params, |
| 2407 | false), |
| 2408 | "objc_exception_extract"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 2409 | |
| 2410 | Params.clear(); |
| 2411 | Params.push_back(ClassPtrTy); |
| 2412 | Params.push_back(ObjectPtrTy); |
| 2413 | ExceptionMatchFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 2414 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 2415 | Params, |
| 2416 | false), |
| 2417 | "objc_exception_match"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 2418 | |
| 2419 | Params.clear(); |
| 2420 | Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty)); |
| 2421 | SetJmpFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 2422 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 2423 | Params, |
| 2424 | false), |
| 2425 | "_setjmp"); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 2426 | } |
| 2427 | |
| 2428 | ObjCTypesHelper::~ObjCTypesHelper() { |
| 2429 | } |
| 2430 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 2431 | /* *** */ |
| 2432 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2433 | CodeGen::CGObjCRuntime * |
| 2434 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2435 | return new CGObjCMac(CGM); |
| 2436 | } |