blob: fa8ed4dd8238223754b76a7734330115b720aa2b [file] [log] [blame]
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001//===------- 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 Dunbarf77ac862008-08-11 21:35:06 +000015
16#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000017#include "CodeGenFunction.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000020#include "clang/AST/DeclObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000021#include "clang/Basic/LangOptions.h"
22
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000023#include "llvm/Module.h"
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000024#include "llvm/Support/IRBuilder.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000025#include "llvm/Target/TargetData.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000026#include <sstream>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000027
28using namespace clang;
29
30namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000031
Daniel Dunbarae226fa2008-08-27 02:31:56 +000032 typedef std::vector<llvm::Constant*> ConstantVector;
33
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000034 // FIXME: We should find a nicer way to make the labels for
35 // metadata, string concatenation is lame.
36
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000037/// ObjCTypesHelper - Helper class that encapsulates lazy
38/// construction of varies types used during ObjC generation.
39class ObjCTypesHelper {
40private:
41 CodeGen::CodeGenModule &CGM;
42
Daniel Dunbar14c80b72008-08-23 09:25:55 +000043 llvm::Function *MessageSendFn, *MessageSendStretFn;
44 llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn;
Daniel Dunbar259d93d2008-08-12 03:39:23 +000045
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000046public:
Daniel Dunbar27f9d772008-08-21 04:36:09 +000047 const llvm::Type *ShortTy, *IntTy, *LongTy;
48 const llvm::Type *Int8PtrTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +000049
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000050 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
51 const llvm::Type *ObjectPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000052 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000053 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000054 /// ProtocolPtrTy - LLVM type for external protocol handles
55 /// (typeof(Protocol))
56 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000057
Daniel Dunbare8b470d2008-08-23 04:28:29 +000058 /// SuperTy - LLVM type for struct objc_super.
59 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +000060 /// SuperPtrTy - LLVM type for struct objc_super *.
61 const llvm::Type *SuperPtrTy;
Daniel Dunbare8b470d2008-08-23 04:28:29 +000062
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000063 /// SymtabTy - LLVM type for struct objc_symtab.
64 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +000065 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
66 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000067 /// ModuleTy - LLVM type for struct objc_module.
68 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +000069
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000070 /// ProtocolTy - LLVM type for struct objc_protocol.
71 const llvm::StructType *ProtocolTy;
72 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
73 const llvm::Type *ProtocolPtrTy;
74 /// ProtocolExtensionTy - LLVM type for struct
75 /// objc_protocol_extension.
76 const llvm::StructType *ProtocolExtensionTy;
77 /// ProtocolExtensionTy - LLVM type for struct
78 /// objc_protocol_extension *.
79 const llvm::Type *ProtocolExtensionPtrTy;
80 /// MethodDescriptionTy - LLVM type for struct
81 /// objc_method_description.
82 const llvm::StructType *MethodDescriptionTy;
83 /// MethodDescriptionListTy - LLVM type for struct
84 /// objc_method_description_list.
85 const llvm::StructType *MethodDescriptionListTy;
86 /// MethodDescriptionListPtrTy - LLVM type for struct
87 /// objc_method_description_list *.
88 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +000089 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
90 /// in GCC parlance).
91 const llvm::StructType *PropertyTy;
92 /// PropertyListTy - LLVM type for struct objc_property_list
93 /// (_prop_list_t in GCC parlance).
94 const llvm::StructType *PropertyListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000095 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
96 const llvm::Type *PropertyListPtrTy;
97 /// ProtocolListTy - LLVM type for struct objc_property_list.
98 const llvm::Type *ProtocolListTy;
99 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
100 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000101 /// CategoryTy - LLVM type for struct objc_category.
102 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000103 /// ClassTy - LLVM type for struct objc_class.
104 const llvm::StructType *ClassTy;
105 /// ClassPtrTy - LLVM type for struct objc_class *.
106 const llvm::Type *ClassPtrTy;
107 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
108 const llvm::StructType *ClassExtensionTy;
109 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
110 const llvm::Type *ClassExtensionPtrTy;
111 /// CacheTy - LLVM type for struct objc_cache.
112 const llvm::Type *CacheTy;
113 /// CachePtrTy - LLVM type for struct objc_cache *.
114 const llvm::Type *CachePtrTy;
115 // IvarTy - LLVM type for struct objc_ivar.
116 const llvm::StructType *IvarTy;
117 /// IvarListTy - LLVM type for struct objc_ivar_list.
118 const llvm::Type *IvarListTy;
119 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
120 const llvm::Type *IvarListPtrTy;
121 // MethodTy - LLVM type for struct objc_method.
122 const llvm::StructType *MethodTy;
123 /// MethodListTy - LLVM type for struct objc_method_list.
124 const llvm::Type *MethodListTy;
125 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
126 const llvm::Type *MethodListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000127
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000128public:
129 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
130 ~ObjCTypesHelper();
131
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000132 llvm::Value *getMessageSendFn(bool IsSuper, const llvm::Type *ReturnTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000133};
134
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000135class CGObjCMac : public CodeGen::CGObjCRuntime {
136private:
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000137 CodeGen::CodeGenModule &CGM;
138 ObjCTypesHelper ObjCTypes;
139 /// ObjCABI - FIXME: Not sure yet.
140 unsigned ObjCABI;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000141
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000142 /// LazySymbols - Symbols to generate a lazy reference for. See
143 /// DefinedSymbols and FinishModule().
144 std::set<IdentifierInfo*> LazySymbols;
145
146 /// DefinedSymbols - External symbols which are defined by this
147 /// module. The symbols in this list and LazySymbols are used to add
148 /// special linker symbols which ensure that Objective-C modules are
149 /// linked properly.
150 std::set<IdentifierInfo*> DefinedSymbols;
151
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000152 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000153 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000154
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000155 /// MethodVarNames - uniqued method variable names.
156 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
157
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000158 /// MethodVarTypes - uniqued method type signatures. We have to use
159 /// a StringMap here because have no other unique reference.
160 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
161
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000162 /// MethodDefinitions - map of methods which have been defined in
163 /// this translation unit.
164 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
165
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000166 /// PropertyNames - uniqued method variable names.
167 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
168
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000169 /// ClassReferences - uniqued class references.
170 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
171
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000172 /// SelectorReferences - uniqued selector references.
173 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
174
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000175 /// Protocols - Protocols for which an objc_protocol structure has
176 /// been emitted. Forward declarations are handled by creating an
177 /// empty structure whose initializer is filled in when/if defined.
178 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
179
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000180 /// DefinedClasses - List of defined classes.
181 std::vector<llvm::GlobalValue*> DefinedClasses;
182
183 /// DefinedCategories - List of defined categories.
184 std::vector<llvm::GlobalValue*> DefinedCategories;
185
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000186 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000187 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000188 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000189
190 /// EmitImageInfo - Emit the image info marker used to encode some module
191 /// level information.
192 void EmitImageInfo();
193
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000194 /// EmitModuleInfo - Another marker encoding module level
195 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000196 void EmitModuleInfo();
197
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000198 /// EmitModuleSymols - Emit module symbols, the list of defined
199 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000200 llvm::Constant *EmitModuleSymbols();
201
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000202 /// FinishModule - Write out global data structures at the end of
203 /// processing a translation unit.
204 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000205
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000206 /// EmitClassExtension - Generate the class extension structure used
207 /// to store the weak ivar layout and properties. The return value
208 /// has type ClassExtensionPtrTy.
209 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
210
211 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
212 /// for the given class.
213 llvm::Value *EmitClassRef(llvm::IRBuilder<> &Builder,
214 const ObjCInterfaceDecl *ID);
215
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000216 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
217 const ObjCMessageExpr *E,
218 llvm::Value *Arg0,
219 bool IsSuper);
220
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000221 /// EmitIvarList - Emit the ivar list for the given
222 /// implementation. If ForClass is true the list of class ivars
223 /// (i.e. metaclass ivars) is emitted, otherwise the list of
224 /// interface ivars will be emitted. The return value has type
225 /// IvarListPtrTy.
226 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
227 bool ForClass,
228 const llvm::Type *InterfaceTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000229
230 /// EmitMetaClass - Emit a forward reference to the class structure
231 /// for the metaclass of the given interface. The return value has
232 /// type ClassPtrTy.
233 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
234
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000235 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000236 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000237 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
238 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000239 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000240 const ConstantVector &Methods);
241
242 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
243
244 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000245
246 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000247 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000248 llvm::Constant *EmitMethodList(const std::string &Name,
249 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000250 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000251
252 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000253 /// method declarations.
254 /// - TypeName: The name for the type containing the methods.
255 /// - IsProtocol: True iff these methods are for a protocol.
256 /// - ClassMethds: True iff these are class methods.
257 /// - Required: When true, only "required" methods are
258 /// listed. Similarly, when false only "optional" methods are
259 /// listed. For classes this should always be true.
260 /// - begin, end: The method list to output.
261 ///
262 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000263 llvm::Constant *EmitMethodDescList(const std::string &Name,
264 const char *Section,
265 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000266
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000267 /// EmitPropertyList - Emit the given property list. The return
268 /// value has type PropertyListPtrTy.
269 llvm::Constant *EmitPropertyList(const std::string &Name,
270 ObjCPropertyDecl * const *begin,
271 ObjCPropertyDecl * const *end);
272
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000273 /// EmitProtocolExtension - Generate the protocol extension
274 /// structure used to store optional instance and class methods, and
275 /// protocol properties. The return value has type
276 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000277 llvm::Constant *
278 EmitProtocolExtension(const ObjCProtocolDecl *PD,
279 const ConstantVector &OptInstanceMethods,
280 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000281
282 /// EmitProtocolList - Generate the list of referenced
283 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +0000284 llvm::Constant *EmitProtocolList(const std::string &Name,
285 ObjCProtocolDecl::protocol_iterator begin,
286 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000287
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000288 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
289 /// for the given selector.
290 llvm::Value *EmitSelector(llvm::IRBuilder<> &Builder, Selector Sel);
291
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000292 /// GetProtocolRef - Return a reference to the internal protocol
293 /// description, creating an empty one if it has not been
294 /// defined. The return value has type pointer-to ProtocolTy.
295 llvm::GlobalVariable *GetProtocolRef(const ObjCProtocolDecl *PD);
296
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000297 /// GetClassName - Return a unique constant for the given selector's
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000298 /// name. The return value has type char *.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000299 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000300
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000301 /// GetMethodVarName - Return a unique constant for the given
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000302 /// selector's name. The return value has type char *.
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000303 llvm::Constant *GetMethodVarName(Selector Sel);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000304 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000305 llvm::Constant *GetMethodVarName(const std::string &Name);
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000306
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000307 /// GetMethodVarType - Return a unique constant for the given
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000308 /// selector's name. The return value has type char *.
309
310 // FIXME: This is a horrible name.
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000311 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000312 llvm::Constant *GetMethodVarType(const std::string &Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000313
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000314 /// GetPropertyName - Return a unique constant for the given
315 /// name. The return value has type char *.
316 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
317
318 // FIXME: This is a horrible name too.
319 llvm::Constant *GetPropertyType(const ObjCPropertyDecl *PD);
320
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000321 /// GetNameForMethod - Return a name for the given method.
322 /// \param[out] NameOut - The return value.
323 void GetNameForMethod(const ObjCMethodDecl *OMD,
324 std::string &NameOut);
325
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000326public:
327 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000328 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000329
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000330 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
331 const ObjCMessageExpr *E,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000332 llvm::Value *Receiver,
333 bool IsClassMessage);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000334
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000335 virtual CodeGen::RValue
336 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
337 const ObjCMessageExpr *E,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000338 const ObjCInterfaceDecl *Class,
339 llvm::Value *Receiver,
340 bool IsClassMessage);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000341
342 virtual llvm::Value *GetClass(llvm::IRBuilder<> &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000343 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000344
345 virtual llvm::Value *GetSelector(llvm::IRBuilder<> &Builder, Selector Sel);
346
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000347 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000348
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000349 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000350
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000351 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000352
353 virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder<> &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000354 const ObjCProtocolDecl *PD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000355
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000356 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000357
358 virtual llvm::Function *ModuleInitFunction();
359};
360} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000361
362/* *** Helper Functions *** */
363
364/// getConstantGEP() - Help routine to construct simple GEPs.
365static llvm::Constant *getConstantGEP(llvm::Constant *C,
366 unsigned idx0,
367 unsigned idx1) {
368 llvm::Value *Idxs[] = {
369 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
370 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
371 };
372 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
373}
374
375/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000376
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000377CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm)
378 : CGM(cgm),
379 ObjCTypes(cgm),
380 ObjCABI(1)
381{
382 // FIXME: How does this get set in GCC? And what does it even mean?
383 if (ObjCTypes.LongTy != CGM.getTypes().ConvertType(CGM.getContext().IntTy))
384 ObjCABI = 2;
385
386 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000387}
388
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000389/// GetClass - Return a reference to the class for the given interface
390/// decl.
391llvm::Value *CGObjCMac::GetClass(llvm::IRBuilder<> &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000392 const ObjCInterfaceDecl *ID) {
393 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000394}
395
396/// GetSelector - Return the pointer to the unique'd string for this selector.
397llvm::Value *CGObjCMac::GetSelector(llvm::IRBuilder<> &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000398 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000399}
400
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000401/// Generate a constant CFString object.
402/*
403 struct __builtin_CFString {
404 const int *isa; // point to __CFConstantStringClassReference
405 int flags;
406 const char *str;
407 long length;
408 };
409*/
410
411llvm::Constant *CGObjCMac::GenerateConstantString(const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000412 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000413}
414
415/// Generates a message send where the super is the receiver. This is
416/// a message send to self with special delivery semantics indicating
417/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000418CodeGen::RValue
419CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
420 const ObjCMessageExpr *E,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000421 const ObjCInterfaceDecl *Class,
422 llvm::Value *Receiver,
423 bool IsClassMessage) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000424 // Create and init a super structure; this is a (receiver, class)
425 // pair we will pass to objc_msgSendSuper.
426 llvm::Value *ObjCSuper =
427 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
428 llvm::Value *ReceiverAsObject =
429 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
430 CGF.Builder.CreateStore(ReceiverAsObject,
431 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000432
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000433 // If this is a class message the metaclass is passed as the target.
434 llvm::Value *Target;
435 if (IsClassMessage) {
436 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
437 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
438 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
439 Target = Super;
440 } else {
441 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
442 }
443 CGF.Builder.CreateStore(Target,
444 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
445
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000446 return EmitMessageSend(CGF, E, ObjCSuper, true);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000447}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000448
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000449/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000450CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
451 const ObjCMessageExpr *E,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000452 llvm::Value *Receiver,
453 bool IsClassMessage) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000454 llvm::Value *Arg0 =
455 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
456 return EmitMessageSend(CGF, E, Arg0, false);
457}
458
459CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
460 const ObjCMessageExpr *E,
461 llvm::Value *Arg0,
462 bool IsSuper) {
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000463 llvm::Value *Args[2];
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000464 Args[0] = Arg0;
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000465 Args[1] = EmitSelector(CGF.Builder, E->getSelector());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000466
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000467 // FIXME: This is a hack, we are implicitly coordinating with
468 // EmitCallExprExt, which will move the return type to the first
469 // parameter and set the structure return flag. See
470 // getMessageSendFn().
471
472
473 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(E->getType());
474 return CGF.EmitCallExprExt(ObjCTypes.getMessageSendFn(IsSuper, ReturnTy),
475 E->getType(),
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000476 E->arg_begin(),
477 E->arg_end(),
478 Args, 2);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000479}
480
481llvm::Value *CGObjCMac::GenerateProtocolRef(llvm::IRBuilder<> &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000482 const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000483 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
484 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000485}
486
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000487/*
488 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
489 struct _objc_protocol {
490 struct _objc_protocol_extension *isa;
491 char *protocol_name;
492 struct _objc_protocol_list *protocol_list;
493 struct _objc__method_prototype_list *instance_methods;
494 struct _objc__method_prototype_list *class_methods
495 };
496
497 See EmitProtocolExtension().
498*/
499void CGObjCMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000500 // FIXME: I don't understand why gcc generates this, or where it is
501 // resolved. Investigate. Its also wasteful to look this up over and
502 // over.
503 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
504
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000505 const char *ProtocolName = PD->getName();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000506
507 // Construct method lists.
508 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
509 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
510 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
511 e = PD->instmeth_end(); i != e; ++i) {
512 ObjCMethodDecl *MD = *i;
513 llvm::Constant *C = GetMethodDescriptionConstant(MD);
514 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
515 OptInstanceMethods.push_back(C);
516 } else {
517 InstanceMethods.push_back(C);
518 }
519 }
520
521 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
522 e = PD->classmeth_end(); i != e; ++i) {
523 ObjCMethodDecl *MD = *i;
524 llvm::Constant *C = GetMethodDescriptionConstant(MD);
525 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
526 OptClassMethods.push_back(C);
527 } else {
528 ClassMethods.push_back(C);
529 }
530 }
531
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000532 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000533 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000534 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000535 Values[2] =
536 EmitProtocolList(std::string("\01L_OBJC_PROTOCOL_REFS_")+PD->getName(),
537 PD->protocol_begin(),
538 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000539 Values[3] =
540 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_")
541 + PD->getName(),
542 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
543 InstanceMethods);
544 Values[4] =
545 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_CLASS_METHODS_")
546 + PD->getName(),
547 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
548 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000549 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
550 Values);
551
552 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
553 if (Entry) {
554 // Already created, just update the initializer
555 Entry->setInitializer(Init);
556 } else {
557 Entry =
558 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
559 llvm::GlobalValue::InternalLinkage,
560 Init,
561 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
562 &CGM.getModule());
563 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
564 UsedGlobals.push_back(Entry);
565 // FIXME: Is this necessary? Why only for protocol?
566 Entry->setAlignment(4);
567 }
568}
569
570llvm::GlobalVariable *CGObjCMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
571 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
572
573 if (!Entry) {
574 std::vector<llvm::Constant*> Values(5);
575 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
576 Values[1] = GetClassName(PD->getIdentifier());
577 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
578 Values[3] = Values[4] =
579 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
580 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
581 Values);
582
583 Entry =
584 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
585 llvm::GlobalValue::InternalLinkage,
586 Init,
587 std::string("\01L_OBJC_PROTOCOL_")+PD->getName(),
588 &CGM.getModule());
589 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
590 UsedGlobals.push_back(Entry);
591 // FIXME: Is this necessary? Why only for protocol?
592 Entry->setAlignment(4);
593 }
594
595 return Entry;
596}
597
598/*
599 struct _objc_protocol_extension {
600 uint32_t size;
601 struct objc_method_description_list *optional_instance_methods;
602 struct objc_method_description_list *optional_class_methods;
603 struct objc_property_list *instance_properties;
604 };
605*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000606llvm::Constant *
607CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
608 const ConstantVector &OptInstanceMethods,
609 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000610 uint64_t Size =
611 CGM.getTargetData().getABITypeSize(ObjCTypes.ProtocolExtensionTy);
612 std::vector<llvm::Constant*> Values(4);
613 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000614 Values[1] =
615 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_")
616 + PD->getName(),
617 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
618 OptInstanceMethods);
619 Values[2] =
620 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_")
621 + PD->getName(),
622 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
623 OptClassMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000624 Values[3] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_PROTO_LIST_") +
625 PD->getName(),
626 PD->classprop_begin(),
627 PD->classprop_end());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000628
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000629 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000630 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
631 Values[3]->isNullValue())
632 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
633
634 llvm::Constant *Init =
635 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
636 llvm::GlobalVariable *GV =
637 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
638 llvm::GlobalValue::InternalLinkage,
639 Init,
640 (std::string("\01L_OBJC_PROTOCOLEXT_") +
641 PD->getName()),
642 &CGM.getModule());
643 // No special section, but goes in llvm.used
644 UsedGlobals.push_back(GV);
645
646 return GV;
647}
648
649/*
650 struct objc_protocol_list {
651 struct objc_protocol_list *next;
652 long count;
653 Protocol *list[];
654 };
655*/
Daniel Dunbardbc933702008-08-21 21:57:41 +0000656llvm::Constant *
657CGObjCMac::EmitProtocolList(const std::string &Name,
658 ObjCProtocolDecl::protocol_iterator begin,
659 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000660 std::vector<llvm::Constant*> ProtocolRefs;
661
Daniel Dunbardbc933702008-08-21 21:57:41 +0000662 for (; begin != end; ++begin)
663 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000664
665 // Just return null for empty protocol lists
666 if (ProtocolRefs.empty())
667 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
668
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000669 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000670 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
671
672 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000673 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000674 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
675 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
676 Values[2] =
677 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
678 ProtocolRefs.size()),
679 ProtocolRefs);
680
681 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
682 llvm::GlobalVariable *GV =
683 new llvm::GlobalVariable(Init->getType(), false,
684 llvm::GlobalValue::InternalLinkage,
685 Init,
Daniel Dunbardbc933702008-08-21 21:57:41 +0000686 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000687 &CGM.getModule());
688 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
689 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
690}
691
692/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000693 struct _objc_property {
694 const char * const name;
695 const char * const attributes;
696 };
697
698 struct _objc_property_list {
699 uint32_t entsize; // sizeof (struct _objc_property)
700 uint32_t prop_count;
701 struct _objc_property[prop_count];
702 };
703*/
704llvm::Constant *CGObjCMac::EmitPropertyList(const std::string &Name,
705 ObjCPropertyDecl * const *begin,
706 ObjCPropertyDecl * const *end) {
707 std::vector<llvm::Constant*> Properties, Prop(2);
708 for (; begin != end; ++begin) {
709 const ObjCPropertyDecl *PD = *begin;
710 Prop[0] = GetPropertyName(PD->getIdentifier());
711 Prop[1] = GetPropertyType(PD);
712 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
713 Prop));
714 }
715
716 // Return null for empty list.
717 if (Properties.empty())
718 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
719
720 unsigned PropertySize =
721 CGM.getTargetData().getABITypeSize(ObjCTypes.PropertyTy);
722 std::vector<llvm::Constant*> Values(3);
723 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
724 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
725 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
726 Properties.size());
727 Values[2] = llvm::ConstantArray::get(AT, Properties);
728 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
729
730 llvm::GlobalVariable *GV =
731 new llvm::GlobalVariable(Init->getType(), false,
732 llvm::GlobalValue::InternalLinkage,
733 Init,
734 Name,
735 &CGM.getModule());
736 // No special section on property lists?
737 UsedGlobals.push_back(GV);
738 return llvm::ConstantExpr::getBitCast(GV,
739 ObjCTypes.PropertyListPtrTy);
740
741}
742
743/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000744 struct objc_method_description_list {
745 int count;
746 struct objc_method_description list[];
747 };
748*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000749llvm::Constant *
750CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
751 std::vector<llvm::Constant*> Desc(2);
752 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
753 ObjCTypes.SelectorPtrTy);
754 Desc[1] = GetMethodVarType(MD);
755 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
756 Desc);
757}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000758
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000759llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
760 const char *Section,
761 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000762 // Return null for empty list.
763 if (Methods.empty())
764 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
765
766 std::vector<llvm::Constant*> Values(2);
767 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
768 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
769 Methods.size());
770 Values[1] = llvm::ConstantArray::get(AT, Methods);
771 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
772
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000773 llvm::GlobalVariable *GV =
774 new llvm::GlobalVariable(Init->getType(), false,
775 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000776 Init, Name, &CGM.getModule());
777 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000778 UsedGlobals.push_back(GV);
779 return llvm::ConstantExpr::getBitCast(GV,
780 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000781}
782
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000783/*
784 struct _objc_category {
785 char *category_name;
786 char *class_name;
787 struct _objc_method_list *instance_methods;
788 struct _objc_method_list *class_methods;
789 struct _objc_protocol_list *protocols;
790 uint32_t size; // <rdar://4585769>
791 struct _objc_property_list *instance_properties;
792 };
793 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000794void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000795 unsigned Size = CGM.getTargetData().getABITypeSize(ObjCTypes.CategoryTy);
796
Daniel Dunbar86e2f402008-08-26 23:03:11 +0000797 // FIXME: This is poor design, the OCD should have a pointer to the
798 // category decl. Additionally, note that Category can be null for
799 // the @implementation w/o an @interface case. Sema should just
800 // create one for us as it does for @implementation so everyone else
801 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000802 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +0000803 const ObjCCategoryDecl *Category =
804 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000805 std::string ExtName(std::string(Interface->getName()) +
806 "_" +
807 OCD->getName());
808
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000809 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
810 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
811 e = OCD->instmeth_end(); i != e; ++i) {
812 // Instance methods should always be defined.
813 InstanceMethods.push_back(GetMethodConstant(*i));
814 }
815 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
816 e = OCD->classmeth_end(); i != e; ++i) {
817 // Class methods should always be defined.
818 ClassMethods.push_back(GetMethodConstant(*i));
819 }
820
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000821 std::vector<llvm::Constant*> Values(7);
822 Values[0] = GetClassName(OCD->getIdentifier());
823 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000824 Values[2] =
825 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
826 ExtName,
827 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000828 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000829 Values[3] =
830 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
831 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000832 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000833 if (Category) {
834 Values[4] =
835 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
836 Category->protocol_begin(),
837 Category->protocol_end());
838 } else {
839 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
840 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000841 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +0000842
843 // If there is no category @interface then there can be no properties.
844 if (Category) {
845 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
846 Category->classprop_begin(),
847 Category->classprop_end());
848 } else {
849 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
850 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000851
852 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
853 Values);
854
855 llvm::GlobalVariable *GV =
856 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
857 llvm::GlobalValue::InternalLinkage,
858 Init,
859 std::string("\01L_OBJC_CATEGORY_")+ExtName,
860 &CGM.getModule());
861 GV->setSection("__OBJC,__category,regular,no_dead_strip");
862 UsedGlobals.push_back(GV);
863 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000864}
865
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000866// FIXME: Get from somewhere?
867enum ClassFlags {
868 eClassFlags_Factory = 0x00001,
869 eClassFlags_Meta = 0x00002,
870 // <rdr://5142207>
871 eClassFlags_HasCXXStructors = 0x02000,
872 eClassFlags_Hidden = 0x20000,
873 eClassFlags_ABI2_Hidden = 0x00010,
874 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
875};
876
877// <rdr://5142207&4705298&4843145>
878static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
879 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
880 // FIXME: Support -fvisibility
881 switch (attr->getVisibility()) {
882 default:
883 assert(0 && "Unknown visibility");
884 return false;
885 case VisibilityAttr::DefaultVisibility:
886 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
887 return false;
888 case VisibilityAttr::HiddenVisibility:
889 return true;
890 }
891 } else {
892 return false; // FIXME: Support -fvisibility
893 }
894}
895
896/*
897 struct _objc_class {
898 Class isa;
899 Class super_class;
900 const char *name;
901 long version;
902 long info;
903 long instance_size;
904 struct _objc_ivar_list *ivars;
905 struct _objc_method_list *methods;
906 struct _objc_cache *cache;
907 struct _objc_protocol_list *protocols;
908 // Objective-C 1.0 extensions (<rdr://4585769>)
909 const char *ivar_layout;
910 struct _objc_class_ext *ext;
911 };
912
913 See EmitClassExtension();
914 */
915void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000916 DefinedSymbols.insert(ID->getIdentifier());
917
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000918 const char *ClassName = ID->getName();
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000919 // FIXME: Gross
920 ObjCInterfaceDecl *Interface =
921 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000922 llvm::Constant *Protocols =
923 EmitProtocolList(std::string("\01L_OBJC_CLASS_PROTOCOLS_") + ID->getName(),
924 Interface->protocol_begin(),
925 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000926 const llvm::Type *InterfaceTy =
927 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
928 unsigned Flags = eClassFlags_Factory;
929 unsigned Size = CGM.getTargetData().getABITypeSize(InterfaceTy);
930
931 // FIXME: Set CXX-structors flag.
932 if (IsClassHidden(ID->getClassInterface()))
933 Flags |= eClassFlags_Hidden;
934
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000935 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
936 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
937 e = ID->instmeth_end(); i != e; ++i) {
938 // Instance methods should always be defined.
939 InstanceMethods.push_back(GetMethodConstant(*i));
940 }
941 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
942 e = ID->classmeth_end(); i != e; ++i) {
943 // Class methods should always be defined.
944 ClassMethods.push_back(GetMethodConstant(*i));
945 }
946
947 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
948 e = ID->propimpl_end(); i != e; ++i) {
949 ObjCPropertyImplDecl *PID = *i;
950
951 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
952 ObjCPropertyDecl *PD = PID->getPropertyDecl();
953
954 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
955 if (llvm::Constant *C = GetMethodConstant(MD))
956 InstanceMethods.push_back(C);
957 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
958 if (llvm::Constant *C = GetMethodConstant(MD))
959 InstanceMethods.push_back(C);
960 }
961 }
962
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000963 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000964 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000965 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000966 // Record a reference to the super class.
967 LazySymbols.insert(Super->getIdentifier());
968
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000969 Values[ 1] =
970 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
971 ObjCTypes.ClassPtrTy);
972 } else {
973 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
974 }
975 Values[ 2] = GetClassName(ID->getIdentifier());
976 // Version is always 0.
977 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
978 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
979 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
980 Values[ 6] = EmitIvarList(ID, false, InterfaceTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000981 Values[ 7] =
982 EmitMethodList(std::string("\01L_OBJC_INSTANCE_METHODS_") + ID->getName(),
983 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000984 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000985 // cache is always NULL.
986 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
987 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000988 // FIXME: Set ivar_layout
989 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000990 Values[11] = EmitClassExtension(ID);
991 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
992 Values);
993
994 llvm::GlobalVariable *GV =
995 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
996 llvm::GlobalValue::InternalLinkage,
997 Init,
998 std::string("\01L_OBJC_CLASS_")+ClassName,
999 &CGM.getModule());
1000 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1001 UsedGlobals.push_back(GV);
1002 // FIXME: Why?
1003 GV->setAlignment(32);
1004 DefinedClasses.push_back(GV);
1005}
1006
1007llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1008 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001009 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001010 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001011 const char *ClassName = ID->getName();
1012 unsigned Flags = eClassFlags_Meta;
1013 unsigned Size = CGM.getTargetData().getABITypeSize(ObjCTypes.ClassTy);
1014
1015 if (IsClassHidden(ID->getClassInterface()))
1016 Flags |= eClassFlags_Hidden;
1017
1018 std::vector<llvm::Constant*> Values(12);
1019 // The isa for the metaclass is the root of the hierarchy.
1020 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1021 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1022 Root = Super;
1023 Values[ 0] =
1024 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1025 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001026 // The super class for the metaclass is emitted as the name of the
1027 // super class. The runtime fixes this up to point to the
1028 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001029 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1030 Values[ 1] =
1031 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1032 ObjCTypes.ClassPtrTy);
1033 } else {
1034 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1035 }
1036 Values[ 2] = GetClassName(ID->getIdentifier());
1037 // Version is always 0.
1038 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1039 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1040 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
1041 Values[ 6] = EmitIvarList(ID, true, InterfaceTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001042 Values[ 7] =
1043 EmitMethodList(std::string("\01L_OBJC_CLASS_METHODS_") + ID->getName(),
1044 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001045 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001046 // cache is always NULL.
1047 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1048 Values[ 9] = Protocols;
1049 // ivar_layout for metaclass is always NULL.
1050 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1051 // The class extension is always unused for metaclasses.
1052 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1053 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1054 Values);
1055
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001056 std::string Name("\01L_OBJC_METACLASS_");
1057 Name += ClassName;
1058
1059 // Check for a forward reference.
1060 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1061 if (GV) {
1062 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1063 "Forward metaclass reference has incorrect type.");
1064 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1065 GV->setInitializer(Init);
1066 } else {
1067 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1068 llvm::GlobalValue::InternalLinkage,
1069 Init, Name,
1070 &CGM.getModule());
1071 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001072 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1073 UsedGlobals.push_back(GV);
1074 // FIXME: Why?
1075 GV->setAlignment(32);
1076
1077 return GV;
1078}
1079
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001080llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
1081 std::string Name("\01L_OBJC_METACLASS_");
1082 Name += ID->getName();
1083
1084 // FIXME: Should we look these up somewhere other than the
1085 // module. Its a bit silly since we only generate these while
1086 // processing an implementation, so exactly one pointer would work
1087 // if know when we entered/exitted an implementation block.
1088
1089 // Check for an existing forward reference.
1090 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name)) {
1091 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1092 "Forward metaclass reference has incorrect type.");
1093 return GV;
1094 } else {
1095 // Generate as an external reference to keep a consistent
1096 // module. This will be patched up when we emit the metaclass.
1097 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1098 llvm::GlobalValue::ExternalLinkage,
1099 0,
1100 Name,
1101 &CGM.getModule());
1102 }
1103}
1104
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001105/*
1106 struct objc_class_ext {
1107 uint32_t size;
1108 const char *weak_ivar_layout;
1109 struct _objc_property_list *properties;
1110 };
1111*/
1112llvm::Constant *
1113CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1114 uint64_t Size =
1115 CGM.getTargetData().getABITypeSize(ObjCTypes.ClassExtensionTy);
1116
1117 std::vector<llvm::Constant*> Values(3);
1118 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001119 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001120 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001121 Values[2] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") +
1122 ID->getName(),
1123 ID->getClassInterface()->classprop_begin(),
1124 ID->getClassInterface()->classprop_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001125
1126 // Return null if no extension bits are used.
1127 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1128 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1129
1130 llvm::Constant *Init =
1131 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1132 llvm::GlobalVariable *GV =
1133 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1134 llvm::GlobalValue::InternalLinkage,
1135 Init,
1136 (std::string("\01L_OBJC_CLASSEXT_") +
1137 ID->getName()),
1138 &CGM.getModule());
1139 // No special section, but goes in llvm.used
1140 UsedGlobals.push_back(GV);
1141
1142 return GV;
1143}
1144
1145/*
1146 struct objc_ivar {
1147 char *ivar_name;
1148 char *ivar_type;
1149 int ivar_offset;
1150 };
1151
1152 struct objc_ivar_list {
1153 int ivar_count;
1154 struct objc_ivar list[count];
1155 };
1156 */
1157llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
1158 bool ForClass,
1159 const llvm::Type *InterfaceTy) {
1160 std::vector<llvm::Constant*> Ivars, Ivar(3);
1161
1162 // When emitting the root class GCC emits ivar entries for the
1163 // actual class structure. It is not clear if we need to follow this
1164 // behavior; for now lets try and get away with not doing it. If so,
1165 // the cleanest solution would be to make up an ObjCInterfaceDecl
1166 // for the class.
1167 if (ForClass)
1168 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1169
1170 const llvm::StructLayout *Layout =
1171 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
1172 for (ObjCInterfaceDecl::ivar_iterator
1173 i = ID->getClassInterface()->ivar_begin(),
1174 e = ID->getClassInterface()->ivar_end(); i != e; ++i) {
1175 ObjCIvarDecl *V = *i;
1176 unsigned Offset =
1177 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(V));
1178 std::string TypeStr;
1179 llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes;
1180 Ivar[0] = GetMethodVarName(V->getIdentifier());
1181 CGM.getContext().getObjCEncodingForType(V->getType(), TypeStr,
1182 EncodingRecordTypes);
1183 Ivar[1] = GetMethodVarType(TypeStr);
1184 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
1185 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy,
1186 Ivar));
1187 }
1188
1189 // Return null for empty list.
1190 if (Ivars.empty())
1191 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1192
1193 std::vector<llvm::Constant*> Values(2);
1194 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1195 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1196 Ivars.size());
1197 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1198 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1199
1200 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1201 "\01L_OBJC_INSTANCE_VARIABLES_");
1202 llvm::GlobalVariable *GV =
1203 new llvm::GlobalVariable(Init->getType(), false,
1204 llvm::GlobalValue::InternalLinkage,
1205 Init,
1206 std::string(Prefix) + ID->getName(),
1207 &CGM.getModule());
1208 if (ForClass) {
1209 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1210 // FIXME: Why is this only here?
1211 GV->setAlignment(32);
1212 } else {
1213 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1214 }
1215 UsedGlobals.push_back(GV);
1216 return llvm::ConstantExpr::getBitCast(GV,
1217 ObjCTypes.IvarListPtrTy);
1218}
1219
1220/*
1221 struct objc_method {
1222 SEL method_name;
1223 char *method_types;
1224 void *method;
1225 };
1226
1227 struct objc_method_list {
1228 struct objc_method_list *obsolete;
1229 int count;
1230 struct objc_method methods_list[count];
1231 };
1232*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001233
1234/// GetMethodConstant - Return a struct objc_method constant for the
1235/// given method if it has been defined. The result is null if the
1236/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001237llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001238 // FIXME: Use DenseMap::lookup
1239 llvm::Function *Fn = MethodDefinitions[MD];
1240 if (!Fn)
1241 return 0;
1242
1243 std::vector<llvm::Constant*> Method(3);
1244 Method[0] =
1245 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1246 ObjCTypes.SelectorPtrTy);
1247 Method[1] = GetMethodVarType(MD);
1248 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1249 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1250}
1251
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001252llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1253 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001254 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001255 // Return null for empty list.
1256 if (Methods.empty())
1257 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1258
1259 std::vector<llvm::Constant*> Values(3);
1260 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1261 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1262 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1263 Methods.size());
1264 Values[2] = llvm::ConstantArray::get(AT, Methods);
1265 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1266
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001267 llvm::GlobalVariable *GV =
1268 new llvm::GlobalVariable(Init->getType(), false,
1269 llvm::GlobalValue::InternalLinkage,
1270 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001271 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001272 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001273 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001274 UsedGlobals.push_back(GV);
1275 return llvm::ConstantExpr::getBitCast(GV,
1276 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001277}
1278
1279llvm::Function *CGObjCMac::GenerateMethod(const ObjCMethodDecl *OMD) {
1280 const llvm::Type *ReturnTy =
1281 CGM.getTypes().ConvertReturnType(OMD->getResultType());
1282 const llvm::Type *SelfTy =
1283 CGM.getTypes().ConvertType(OMD->getSelfDecl()->getType());
1284
1285 std::vector<const llvm::Type*> ArgTys;
1286 ArgTys.reserve(1 + 2 + OMD->param_size());
1287
1288 // FIXME: This is not something we should have to be dealing with
1289 // here.
1290 bool useStructRet =
1291 CodeGen::CodeGenFunction::hasAggregateLLVMType(OMD->getResultType());
1292 if (useStructRet) {
1293 ArgTys.push_back(llvm::PointerType::getUnqual(ReturnTy));
1294 ReturnTy = llvm::Type::VoidTy;
1295 }
1296
1297 // Implicit arguments
1298 ArgTys.push_back(SelfTy);
1299 ArgTys.push_back(ObjCTypes.SelectorPtrTy);
1300
1301 for (ObjCMethodDecl::param_const_iterator
1302 i = OMD->param_begin(), e = OMD->param_end();
1303 i != e; ++i) {
1304 const llvm::Type *Ty = CGM.getTypes().ConvertType((*i)->getType());
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001305 if (Ty->isSingleValueType()) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001306 ArgTys.push_back(Ty);
1307 } else {
1308 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
1309 }
1310 }
1311
1312 std::string Name;
1313 GetNameForMethod(OMD, Name);
1314
1315 llvm::Function *Method =
1316 llvm::Function::Create(llvm::FunctionType::get(ReturnTy,
1317 ArgTys,
1318 OMD->isVariadic()),
1319 llvm::GlobalValue::InternalLinkage,
1320 Name,
1321 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001322 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001323
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001324 unsigned Offset = 3; // Return plus self and selector implicit args.
1325 if (useStructRet) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001326 Method->addParamAttr(1, llvm::ParamAttr::StructRet);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001327 ++Offset;
1328 }
1329
1330 // FIXME: This is horrible, we need to be reusing the machinery in
1331 // CodeGenModule.cpp (SetFunctionAttributes).
1332 for (ObjCMethodDecl::param_const_iterator
1333 i = OMD->param_begin(), e = OMD->param_end();
1334 i != e; ++i, ++Offset) {
1335 const llvm::Type *Ty = CGM.getTypes().ConvertType((*i)->getType());
1336 if (!Ty->isSingleValueType())
1337 Method->addParamAttr(Offset, llvm::ParamAttr::ByVal);
1338 }
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001339
1340 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001341}
1342
1343llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001344 // Abuse this interface function as a place to finalize.
1345 FinishModule();
1346
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001347 return NULL;
1348}
1349
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001350/* *** Private Interface *** */
1351
1352/// EmitImageInfo - Emit the image info marker used to encode some module
1353/// level information.
1354///
1355/// See: <rdr://4810609&4810587&4810587>
1356/// struct IMAGE_INFO {
1357/// unsigned version;
1358/// unsigned flags;
1359/// };
1360enum ImageInfoFlags {
1361 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
1362 eImageInfo_GarbageCollected = (1 << 1),
1363 eImageInfo_GCOnly = (1 << 2)
1364};
1365
1366void CGObjCMac::EmitImageInfo() {
1367 unsigned version = 0; // Version is unused?
1368 unsigned flags = 0;
1369
1370 // FIXME: Fix and continue?
1371 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
1372 flags |= eImageInfo_GarbageCollected;
1373 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
1374 flags |= eImageInfo_GCOnly;
1375
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001376 // Emitted as int[2];
1377 llvm::Constant *values[2] = {
1378 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
1379 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
1380 };
1381 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001382 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001383 new llvm::GlobalVariable(AT, true,
1384 llvm::GlobalValue::InternalLinkage,
1385 llvm::ConstantArray::get(AT, values, 2),
1386 "\01L_OBJC_IMAGE_INFO",
1387 &CGM.getModule());
1388
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001389 if (ObjCABI == 1) {
1390 GV->setSection("__OBJC, __image_info,regular");
1391 } else {
1392 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
1393 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001394
1395 UsedGlobals.push_back(GV);
1396}
1397
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001398
1399// struct objc_module {
1400// unsigned long version;
1401// unsigned long size;
1402// const char *name;
1403// Symtab symtab;
1404// };
1405
1406// FIXME: Get from somewhere
1407static const int ModuleVersion = 7;
1408
1409void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001410 uint64_t Size = CGM.getTargetData().getABITypeSize(ObjCTypes.ModuleTy);
1411
1412 std::vector<llvm::Constant*> Values(4);
1413 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
1414 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001415 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001416 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001417 Values[3] = EmitModuleSymbols();
1418
1419 llvm::GlobalVariable *GV =
1420 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
1421 llvm::GlobalValue::InternalLinkage,
1422 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
1423 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001424 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001425 &CGM.getModule());
1426 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
1427 UsedGlobals.push_back(GV);
1428}
1429
1430llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001431 unsigned NumClasses = DefinedClasses.size();
1432 unsigned NumCategories = DefinedCategories.size();
1433
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001434 // Return null if no symbols were defined.
1435 if (!NumClasses && !NumCategories)
1436 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
1437
1438 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001439 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1440 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
1441 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
1442 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
1443
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001444 // The runtime expects exactly the list of defined classes followed
1445 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001446 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001447 for (unsigned i=0; i<NumClasses; i++)
1448 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
1449 ObjCTypes.Int8PtrTy);
1450 for (unsigned i=0; i<NumCategories; i++)
1451 Symbols[NumClasses + i] =
1452 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
1453 ObjCTypes.Int8PtrTy);
1454
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001455 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001456 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001457 NumClasses + NumCategories),
1458 Symbols);
1459
1460 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1461
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001462 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001463 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001464 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001465 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001466 "\01L_OBJC_SYMBOLS",
1467 &CGM.getModule());
1468 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
1469 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001470 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
1471}
1472
1473llvm::Value *CGObjCMac::EmitClassRef(llvm::IRBuilder<> &Builder,
1474 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001475 LazySymbols.insert(ID->getIdentifier());
1476
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001477 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
1478
1479 if (!Entry) {
1480 llvm::Constant *Casted =
1481 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
1482 ObjCTypes.ClassPtrTy);
1483 Entry =
1484 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
1485 llvm::GlobalValue::InternalLinkage,
1486 Casted, "\01L_OBJC_CLASS_REFERENCES_",
1487 &CGM.getModule());
1488 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
1489 UsedGlobals.push_back(Entry);
1490 }
1491
1492 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001493}
1494
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001495llvm::Value *CGObjCMac::EmitSelector(llvm::IRBuilder<> &Builder, Selector Sel) {
1496 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
1497
1498 if (!Entry) {
1499 llvm::Constant *Casted =
1500 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
1501 ObjCTypes.SelectorPtrTy);
1502 Entry =
1503 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
1504 llvm::GlobalValue::InternalLinkage,
1505 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
1506 &CGM.getModule());
1507 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
1508 UsedGlobals.push_back(Entry);
1509 }
1510
1511 return Builder.CreateLoad(Entry, false, "tmp");
1512}
1513
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001514llvm::Constant *CGObjCMac::GetClassName(IdentifierInfo *Ident) {
1515 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001516
1517 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001518 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001519 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001520 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001521 llvm::GlobalValue::InternalLinkage,
1522 C, "\01L_OBJC_CLASS_NAME_",
1523 &CGM.getModule());
1524 Entry->setSection("__TEXT,__cstring,cstring_literals");
1525 UsedGlobals.push_back(Entry);
1526 }
1527
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001528 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001529}
1530
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001531llvm::Constant *CGObjCMac::GetMethodVarName(Selector Sel) {
1532 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
1533
1534 if (!Entry) {
1535 llvm::Constant *C = llvm::ConstantArray::get(Sel.getName());
1536 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001537 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001538 llvm::GlobalValue::InternalLinkage,
1539 C, "\01L_OBJC_METH_VAR_NAME_",
1540 &CGM.getModule());
1541 Entry->setSection("__TEXT,__cstring,cstring_literals");
1542 UsedGlobals.push_back(Entry);
1543 }
1544
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001545 return getConstantGEP(Entry, 0, 0);
1546}
1547
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001548// FIXME: Merge into a single cstring creation function.
1549llvm::Constant *CGObjCMac::GetMethodVarName(IdentifierInfo *ID) {
1550 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
1551}
1552
1553// FIXME: Merge into a single cstring creation function.
1554llvm::Constant *CGObjCMac::GetMethodVarName(const std::string &Name) {
1555 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
1556}
1557
1558llvm::Constant *CGObjCMac::GetMethodVarType(const std::string &Name) {
1559 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001560
1561 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001562 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001563 Entry =
1564 new llvm::GlobalVariable(C->getType(), false,
1565 llvm::GlobalValue::InternalLinkage,
1566 C, "\01L_OBJC_METH_VAR_TYPE_",
1567 &CGM.getModule());
1568 Entry->setSection("__TEXT,__cstring,cstring_literals");
1569 UsedGlobals.push_back(Entry);
1570 }
1571
1572 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001573}
1574
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001575// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001576llvm::Constant *CGObjCMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001577 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001578 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
1579 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001580 return GetMethodVarType(TypeStr);
1581}
1582
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001583// FIXME: Merge into a single cstring creation function.
1584llvm::Constant *CGObjCMac::GetPropertyName(IdentifierInfo *Ident) {
1585 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
1586
1587 if (!Entry) {
1588 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
1589 Entry =
1590 new llvm::GlobalVariable(C->getType(), false,
1591 llvm::GlobalValue::InternalLinkage,
1592 C, "\01L_OBJC_PROP_NAME_ATTR_",
1593 &CGM.getModule());
1594 Entry->setSection("__TEXT,__cstring,cstring_literals");
1595 UsedGlobals.push_back(Entry);
1596 }
1597
1598 return getConstantGEP(Entry, 0, 0);
1599}
1600
1601// FIXME: Merge into a single cstring creation function.
1602llvm::Constant *CGObjCMac::GetPropertyType(const ObjCPropertyDecl *PD) {
1603 std::string TypeStr("MOOO!");
1604 //CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr);
1605 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
1606}
1607
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001608void CGObjCMac::GetNameForMethod(const ObjCMethodDecl *D,
1609 std::string &NameOut) {
1610 // FIXME: Find the mangling GCC uses.
1611 std::stringstream s;
1612 s << (D->isInstance() ? "-" : "+");
1613 s << "[";
1614 s << D->getClassInterface()->getName();
1615 s << " ";
1616 s << D->getSelector().getName();
1617 s << "]";
1618 NameOut = s.str();
1619}
1620
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001621void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001622 EmitModuleInfo();
1623
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001624 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001625
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001626 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001627 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001628 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001629 }
1630
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001631 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001632 llvm::GlobalValue *GV =
1633 new llvm::GlobalVariable(AT, false,
1634 llvm::GlobalValue::AppendingLinkage,
1635 llvm::ConstantArray::get(AT, Used),
1636 "llvm.used",
1637 &CGM.getModule());
1638
1639 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001640
1641 // Add assembler directives to add lazy undefined symbol references
1642 // for classes which are referenced but not defined. This is
1643 // important for correct linker interaction.
1644
1645 // FIXME: Uh, this isn't particularly portable.
1646 std::stringstream s;
1647 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
1648 e = LazySymbols.end(); i != e; ++i) {
1649 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
1650 }
1651 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
1652 e = DefinedSymbols.end(); i != e; ++i) {
1653 s << "\t.objc_class_name_" << (*i)->getName() << " = 0\n"
1654 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
1655 }
1656 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001657}
1658
1659/* *** */
1660
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001661ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Daniel Dunbar3e9df992008-08-23 18:37:06 +00001662 : CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001663{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001664 CodeGen::CodeGenTypes &Types = CGM.getTypes();
1665 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001666
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001667 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001668 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001669 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001670 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1671
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001672 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
1673 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001674
1675 // FIXME: It would be nice to unify this with the opaque type, so
1676 // that the IR comes out a bit cleaner.
1677 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
1678 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001679
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001680 MethodDescriptionTy =
1681 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001682 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001683 NULL);
1684 CGM.getModule().addTypeName("struct._objc_method_description",
1685 MethodDescriptionTy);
1686
1687 MethodDescriptionListTy =
1688 llvm::StructType::get(IntTy,
1689 llvm::ArrayType::get(MethodDescriptionTy, 0),
1690 NULL);
1691 CGM.getModule().addTypeName("struct._objc_method_description_list",
1692 MethodDescriptionListTy);
1693 MethodDescriptionListPtrTy =
1694 llvm::PointerType::getUnqual(MethodDescriptionListTy);
1695
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001696 PropertyTy = llvm::StructType::get(Int8PtrTy,
1697 Int8PtrTy,
1698 NULL);
1699 CGM.getModule().addTypeName("struct._objc_property",
1700 PropertyTy);
1701
1702 PropertyListTy = llvm::StructType::get(IntTy,
1703 IntTy,
1704 llvm::ArrayType::get(PropertyTy, 0),
1705 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001706 CGM.getModule().addTypeName("struct._objc_property_list",
1707 PropertyListTy);
1708 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
1709
1710 // Protocol description structures
1711
1712 ProtocolExtensionTy =
1713 llvm::StructType::get(Types.ConvertType(Ctx.IntTy),
1714 llvm::PointerType::getUnqual(MethodDescriptionListTy),
1715 llvm::PointerType::getUnqual(MethodDescriptionListTy),
1716 PropertyListPtrTy,
1717 NULL);
1718 CGM.getModule().addTypeName("struct._objc_protocol_extension",
1719 ProtocolExtensionTy);
1720 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
1721
1722 // Handle recursive construction of Protocl and ProtocolList types
1723
1724 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
1725 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
1726
1727 T = llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
1728 LongTy,
1729 llvm::ArrayType::get(ProtocolTyHolder, 0),
1730 NULL);
1731 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
1732
1733 T = llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolExtensionTy),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001734 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001735 llvm::PointerType::getUnqual(ProtocolListTyHolder),
1736 MethodDescriptionListPtrTy,
1737 MethodDescriptionListPtrTy,
1738 NULL);
1739 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
1740
1741 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
1742 CGM.getModule().addTypeName("struct._objc_protocol_list",
1743 ProtocolListTy);
1744 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
1745
1746 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
1747 CGM.getModule().addTypeName("struct.__objc_protocol", ProtocolTy);
1748 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001749
1750 // Class description structures
1751
1752 IvarTy = llvm::StructType::get(Int8PtrTy,
1753 Int8PtrTy,
1754 IntTy,
1755 NULL);
1756 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
1757
1758 IvarListTy = llvm::OpaqueType::get();
1759 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
1760 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
1761
1762 MethodTy = llvm::StructType::get(SelectorPtrTy,
1763 Int8PtrTy,
1764 Int8PtrTy,
1765 NULL);
1766 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
1767
1768 MethodListTy = llvm::OpaqueType::get();
1769 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
1770 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
1771
1772 CacheTy = llvm::OpaqueType::get();
1773 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
1774 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
1775
1776 ClassExtensionTy =
1777 llvm::StructType::get(IntTy,
1778 Int8PtrTy,
1779 PropertyListPtrTy,
1780 NULL);
1781 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
1782 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
1783
1784 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
1785
1786 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
1787 llvm::PointerType::getUnqual(ClassTyHolder),
1788 Int8PtrTy,
1789 LongTy,
1790 LongTy,
1791 LongTy,
1792 IvarListPtrTy,
1793 MethodListPtrTy,
1794 CachePtrTy,
1795 ProtocolListPtrTy,
1796 Int8PtrTy,
1797 ClassExtensionPtrTy,
1798 NULL);
1799 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
1800
1801 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
1802 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
1803 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
1804
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001805 CategoryTy = llvm::StructType::get(Int8PtrTy,
1806 Int8PtrTy,
1807 MethodListPtrTy,
1808 MethodListPtrTy,
1809 ProtocolListPtrTy,
1810 IntTy,
1811 PropertyListPtrTy,
1812 NULL);
1813 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
1814
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001815 SuperTy =
1816 llvm::StructType::get(ObjectPtrTy,
1817 ClassPtrTy,
1818 NULL);
1819 CGM.getModule().addTypeName("struct._objc_super",
1820 SuperTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001821 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001822
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001823 // Global metadata structures
1824
1825 SymtabTy = llvm::StructType::get(LongTy,
1826 SelectorPtrTy,
1827 ShortTy,
1828 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001829 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001830 NULL);
1831 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
1832 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
1833
1834 ModuleTy =
1835 llvm::StructType::get(LongTy,
1836 LongTy,
1837 Int8PtrTy,
1838 SymtabPtrTy,
1839 NULL);
1840 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001841
1842 // Message send functions
1843
1844 std::vector<const llvm::Type*> Params;
1845 Params.push_back(ObjectPtrTy);
1846 Params.push_back(SelectorPtrTy);
1847 MessageSendFn = llvm::Function::Create(llvm::FunctionType::get(ObjectPtrTy,
1848 Params,
1849 true),
1850 llvm::Function::ExternalLinkage,
1851 "objc_msgSend",
1852 &CGM.getModule());
1853
1854 Params.clear();
1855 Params.push_back(Int8PtrTy);
1856 Params.push_back(ObjectPtrTy);
1857 Params.push_back(SelectorPtrTy);
1858 MessageSendStretFn =
1859 llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy,
1860 Params,
1861 true),
1862 llvm::Function::ExternalLinkage,
1863 "objc_msgSend_stret",
1864 &CGM.getModule());
1865
1866 Params.clear();
1867 Params.push_back(SuperPtrTy);
1868 Params.push_back(SelectorPtrTy);
1869 MessageSendSuperFn =
1870 llvm::Function::Create(llvm::FunctionType::get(ObjectPtrTy,
1871 Params,
1872 true),
1873 llvm::Function::ExternalLinkage,
1874 "objc_msgSendSuper",
1875 &CGM.getModule());
1876
1877 Params.clear();
1878 Params.push_back(Int8PtrTy);
1879 Params.push_back(SuperPtrTy);
1880 Params.push_back(SelectorPtrTy);
1881 MessageSendSuperStretFn =
1882 llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy,
1883 Params,
1884 true),
1885 llvm::Function::ExternalLinkage,
1886 "objc_msgSendSuper_stret",
1887 &CGM.getModule());
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001888}
1889
1890ObjCTypesHelper::~ObjCTypesHelper() {
1891}
1892
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001893llvm::Value *ObjCTypesHelper::getMessageSendFn(bool IsSuper,
1894 const llvm::Type *ReturnTy) {
1895 llvm::Function *F;
1896 llvm::FunctionType *CallFTy;
1897
1898 // FIXME: Should we be caching any of this?
1899 if (!ReturnTy->isSingleValueType()) {
1900 F = IsSuper ? MessageSendSuperStretFn : MessageSendStretFn;
1901 std::vector<const llvm::Type*> Params(3);
1902 Params[0] = llvm::PointerType::getUnqual(ReturnTy);
1903 Params[1] = IsSuper ? SuperPtrTy : ObjectPtrTy;
1904 Params[2] = SelectorPtrTy;
1905 CallFTy = llvm::FunctionType::get(llvm::Type::VoidTy, Params, true);
1906 } else { // XXX floating point?
1907 F = IsSuper ? MessageSendSuperFn : MessageSendFn;
1908 std::vector<const llvm::Type*> Params(2);
1909 Params[0] = IsSuper ? SuperPtrTy : ObjectPtrTy;
1910 Params[1] = SelectorPtrTy;
1911 CallFTy = llvm::FunctionType::get(ReturnTy, Params, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001912 }
1913
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001914 return llvm::ConstantExpr::getBitCast(F,
1915 llvm::PointerType::getUnqual(CallFTy));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001916}
1917
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001918/* *** */
1919
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001920CodeGen::CGObjCRuntime *
1921CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001922 return new CGObjCMac(CGM);
1923}