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