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