blob: 9413d7ebc0d6abb3b32e81096e853327b67e04ed [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 Dunbar6efc0c52008-08-13 03:21:16 +000032 // FIXME: We should find a nicer way to make the labels for
33 // metadata, string concatenation is lame.
34
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000035/// ObjCTypesHelper - Helper class that encapsulates lazy
36/// construction of varies types used during ObjC generation.
37class ObjCTypesHelper {
38private:
39 CodeGen::CodeGenModule &CGM;
40
Daniel Dunbar14c80b72008-08-23 09:25:55 +000041 llvm::Function *MessageSendFn, *MessageSendStretFn;
42 llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn;
Daniel Dunbar259d93d2008-08-12 03:39:23 +000043
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000044public:
Daniel Dunbar27f9d772008-08-21 04:36:09 +000045 const llvm::Type *ShortTy, *IntTy, *LongTy;
46 const llvm::Type *Int8PtrTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +000047
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000048 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
49 const llvm::Type *ObjectPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000050 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000051 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000052 /// ProtocolPtrTy - LLVM type for external protocol handles
53 /// (typeof(Protocol))
54 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000055
Daniel Dunbare8b470d2008-08-23 04:28:29 +000056 /// SuperTy - LLVM type for struct objc_super.
57 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +000058 /// SuperPtrTy - LLVM type for struct objc_super *.
59 const llvm::Type *SuperPtrTy;
Daniel Dunbare8b470d2008-08-23 04:28:29 +000060
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000061 /// SymtabTy - LLVM type for struct objc_symtab.
62 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +000063 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
64 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000065 /// ModuleTy - LLVM type for struct objc_module.
66 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +000067
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000068 /// 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 Dunbarc8ef5512008-08-23 00:19:03 +000087 /// 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 Dunbar6efc0c52008-08-13 03:21:16 +000093 /// 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 Dunbar86e253a2008-08-22 20:34:54 +000099 /// CategoryTy - LLVM type for struct objc_category.
100 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000101 /// 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 Dunbar6efc0c52008-08-13 03:21:16 +0000125
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000126public:
127 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
128 ~ObjCTypesHelper();
129
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000130 llvm::Value *getMessageSendFn(bool IsSuper, const llvm::Type *ReturnTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000131};
132
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000133class CGObjCMac : public CodeGen::CGObjCRuntime {
134private:
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000135 CodeGen::CodeGenModule &CGM;
136 ObjCTypesHelper ObjCTypes;
137 /// ObjCABI - FIXME: Not sure yet.
138 unsigned ObjCABI;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000139
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000140 /// 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 Dunbar4e2d7d02008-08-12 06:48:42 +0000150 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000151 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000152
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000153 /// MethodVarNames - uniqued method variable names.
154 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
155
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000156 /// 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 Dunbarc45ef602008-08-26 21:51:14 +0000160 /// MethodDefinitions - map of methods which have been defined in
161 /// this translation unit.
162 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
163
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000164 /// PropertyNames - uniqued method variable names.
165 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
166
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000167 /// ClassReferences - uniqued class references.
168 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
169
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000170 /// SelectorReferences - uniqued selector references.
171 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
172
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000173 /// 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 Dunbar27f9d772008-08-21 04:36:09 +0000178 /// 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 Dunbar6efc0c52008-08-13 03:21:16 +0000184 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000185 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000186 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000187
188 /// EmitImageInfo - Emit the image info marker used to encode some module
189 /// level information.
190 void EmitImageInfo();
191
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000192 /// EmitModuleInfo - Another marker encoding module level
193 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000194 void EmitModuleInfo();
195
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000196 /// EmitModuleSymols - Emit module symbols, the list of defined
197 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000198 llvm::Constant *EmitModuleSymbols();
199
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000200 /// FinishModule - Write out global data structures at the end of
201 /// processing a translation unit.
202 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000203
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000204 /// 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 Dunbar14c80b72008-08-23 09:25:55 +0000214 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
215 const ObjCMessageExpr *E,
216 llvm::Value *Arg0,
217 bool IsSuper);
218
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000219 /// 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 Dunbarf56f1912008-08-25 08:19:24 +0000227
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 Dunbar27f9d772008-08-21 04:36:09 +0000233 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000234 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000235 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
236 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000237 const llvm::Type *InterfaceTy,
238 const std::vector<llvm::Constant*> &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000239
240 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000241 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000242 llvm::Constant *EmitMethodList(const std::string &Name,
243 const char *Section,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000244 const std::vector<llvm::Constant*> &Methods);
245
246 llvm::Constant *GetMethodConstant(ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000247
248 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000249 /// 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 Dunbar27f9d772008-08-21 04:36:09 +0000259 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 Dunbar6efc0c52008-08-13 03:21:16 +0000265
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000266 /// 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 Dunbar6efc0c52008-08-13 03:21:16 +0000272 /// 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 Dunbardbc93372008-08-21 21:57:41 +0000280 llvm::Constant *EmitProtocolList(const std::string &Name,
281 ObjCProtocolDecl::protocol_iterator begin,
282 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000283
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000284 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
285 /// for the given selector.
286 llvm::Value *EmitSelector(llvm::IRBuilder<> &Builder, Selector Sel);
287
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000288 /// 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 Dunbar4e2d7d02008-08-12 06:48:42 +0000293 /// GetClassName - Return a unique constant for the given selector's
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000294 /// name. The return value has type char *.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000295 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000296
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000297 /// GetMethodVarName - Return a unique constant for the given
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000298 /// selector's name. The return value has type char *.
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000299 llvm::Constant *GetMethodVarName(Selector Sel);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000300 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000301 llvm::Constant *GetMethodVarName(const std::string &Name);
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000302
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000303 /// GetMethodVarType - Return a unique constant for the given
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000304 /// selector's name. The return value has type char *.
305
306 // FIXME: This is a horrible name.
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000307 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000308 llvm::Constant *GetMethodVarType(const std::string &Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000309
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000310 /// 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 Dunbarb7ec2462008-08-16 03:19:19 +0000317 /// 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 Dunbarc17a4d32008-08-11 02:45:11 +0000322public:
323 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000324 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000325
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000326 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
327 const ObjCMessageExpr *E,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000328 llvm::Value *Receiver,
329 bool IsClassMessage);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000330
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000331 virtual CodeGen::RValue
332 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
333 const ObjCMessageExpr *E,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000334 const ObjCInterfaceDecl *Class,
335 llvm::Value *Receiver,
336 bool IsClassMessage);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000337
338 virtual llvm::Value *GetClass(llvm::IRBuilder<> &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000339 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000340
341 virtual llvm::Value *GetSelector(llvm::IRBuilder<> &Builder, Selector Sel);
342
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000343 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000344
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000345 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000346
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000347 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000348
349 virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder<> &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000350 const ObjCProtocolDecl *PD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000351
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000352 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000353
354 virtual llvm::Function *ModuleInitFunction();
355};
356} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000357
358/* *** Helper Functions *** */
359
360/// getConstantGEP() - Help routine to construct simple GEPs.
361static 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 Dunbarc17a4d32008-08-11 02:45:11 +0000372
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000373CGObjCMac::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 Dunbarc17a4d32008-08-11 02:45:11 +0000383}
384
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000385/// GetClass - Return a reference to the class for the given interface
386/// decl.
387llvm::Value *CGObjCMac::GetClass(llvm::IRBuilder<> &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000388 const ObjCInterfaceDecl *ID) {
389 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000390}
391
392/// GetSelector - Return the pointer to the unique'd string for this selector.
393llvm::Value *CGObjCMac::GetSelector(llvm::IRBuilder<> &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000394 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000395}
396
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000397/// 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
407llvm::Constant *CGObjCMac::GenerateConstantString(const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000408 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000409}
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 Dunbar8f2926b2008-08-23 03:46:30 +0000414CodeGen::RValue
415CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
416 const ObjCMessageExpr *E,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000417 const ObjCInterfaceDecl *Class,
418 llvm::Value *Receiver,
419 bool IsClassMessage) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000420 // 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 Dunbare8b470d2008-08-23 04:28:29 +0000428
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000429 // 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 Dunbar14c80b72008-08-23 09:25:55 +0000442 return EmitMessageSend(CGF, E, ObjCSuper, true);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000443}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000444
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000445/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000446CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
447 const ObjCMessageExpr *E,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000448 llvm::Value *Receiver,
449 bool IsClassMessage) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000450 llvm::Value *Arg0 =
451 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
452 return EmitMessageSend(CGF, E, Arg0, false);
453}
454
455CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
456 const ObjCMessageExpr *E,
457 llvm::Value *Arg0,
458 bool IsSuper) {
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000459 llvm::Value *Args[2];
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000460 Args[0] = Arg0;
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000461 Args[1] = EmitSelector(CGF.Builder, E->getSelector());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000462
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000463 // 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 Dunbar8f2926b2008-08-23 03:46:30 +0000472 E->arg_begin(),
473 E->arg_end(),
474 Args, 2);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000475}
476
477llvm::Value *CGObjCMac::GenerateProtocolRef(llvm::IRBuilder<> &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000478 const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000479 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
480 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000481}
482
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000483/*
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*/
495void CGObjCMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000496 // 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 Dunbar6efc0c52008-08-13 03:21:16 +0000501 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 Dunbardbc93372008-08-21 21:57:41 +0000506 Values[2] =
507 EmitProtocolList(std::string("\01L_OBJC_PROTOCOL_REFS_")+PD->getName(),
508 PD->protocol_begin(),
509 PD->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000510 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 Dunbar6efc0c52008-08-13 03:21:16 +0000522 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
543llvm::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*/
579llvm::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 Dunbar27f9d772008-08-21 04:36:09 +0000584 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 Dunbarc8ef5512008-08-23 00:19:03 +0000596 Values[3] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_PROTO_LIST_") +
597 PD->getName(),
598 PD->classprop_begin(),
599 PD->classprop_end());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000600
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000601 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000602 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 Dunbardbc93372008-08-21 21:57:41 +0000628llvm::Constant *
629CGObjCMac::EmitProtocolList(const std::string &Name,
630 ObjCProtocolDecl::protocol_iterator begin,
631 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000632 std::vector<llvm::Constant*> ProtocolRefs;
633
Daniel Dunbardbc93372008-08-21 21:57:41 +0000634 for (; begin != end; ++begin)
635 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000636
637 // Just return null for empty protocol lists
638 if (ProtocolRefs.empty())
639 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
640
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000641 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000642 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
643
644 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000645 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000646 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 Dunbardbc93372008-08-21 21:57:41 +0000658 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000659 &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 Dunbarc8ef5512008-08-23 00:19:03 +0000665 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*/
676llvm::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 Dunbar6efc0c52008-08-13 03:21:16 +0000716 struct objc_method_description_list {
717 int count;
718 struct objc_method_description list[];
719 };
720*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000721llvm::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 Dunbar6efc0c52008-08-13 03:21:16 +0000727 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 Dunbarc17a4d32008-08-11 02:45:11 +0000774}
775
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000776/*
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 Dunbar7ded7f42008-08-15 22:20:32 +0000787void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000788 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 Dunbarc45ef602008-08-26 21:51:14 +0000795 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 Dunbar86e253a2008-08-22 20:34:54 +0000807 std::vector<llvm::Constant*> Values(7);
808 Values[0] = GetClassName(OCD->getIdentifier());
809 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000810 Values[2] =
811 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
812 ExtName,
813 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000814 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000815 Values[3] =
816 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
817 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000818 ClassMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000819 Values[4] =
820 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
821 Interface->protocol_begin(),
822 Interface->protocol_end());
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000823 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000824 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
825 Interface->classprop_begin(),
826 Interface->classprop_end());
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000827
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 Dunbarc17a4d32008-08-11 02:45:11 +0000840}
841
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000842// FIXME: Get from somewhere?
843enum 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>
854static 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 */
891void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000892 DefinedSymbols.insert(ID->getIdentifier());
893
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000894 const char *ClassName = ID->getName();
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000895 // FIXME: Gross
896 ObjCInterfaceDecl *Interface =
897 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc93372008-08-21 21:57:41 +0000898 llvm::Constant *Protocols =
899 EmitProtocolList(std::string("\01L_OBJC_CLASS_PROTOCOLS_") + ID->getName(),
900 Interface->protocol_begin(),
901 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000902 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 Dunbarc45ef602008-08-26 21:51:14 +0000911 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 Dunbar27f9d772008-08-21 04:36:09 +0000939 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000940 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000941 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000942 // Record a reference to the super class.
943 LazySymbols.insert(Super->getIdentifier());
944
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000945 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 Dunbarc8ef5512008-08-23 00:19:03 +0000957 Values[ 7] =
958 EmitMethodList(std::string("\01L_OBJC_INSTANCE_METHODS_") + ID->getName(),
959 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000960 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000961 // cache is always NULL.
962 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
963 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000964 // FIXME: Set ivar_layout
965 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000966 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
983llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
984 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000985 const llvm::Type *InterfaceTy,
986 const std::vector<llvm::Constant*> &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000987 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 Dunbar86e253a2008-08-22 20:34:54 +00001002 // 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 Dunbar27f9d772008-08-21 04:36:09 +00001005 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 Dunbarc8ef5512008-08-23 00:19:03 +00001018 Values[ 7] =
1019 EmitMethodList(std::string("\01L_OBJC_CLASS_METHODS_") + ID->getName(),
1020 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001021 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001022 // 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 Dunbarf56f1912008-08-25 08:19:24 +00001032 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 Dunbar27f9d772008-08-21 04:36:09 +00001048 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 Dunbarf56f1912008-08-25 08:19:24 +00001056llvm::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 Dunbar27f9d772008-08-21 04:36:09 +00001081/*
1082 struct objc_class_ext {
1083 uint32_t size;
1084 const char *weak_ivar_layout;
1085 struct _objc_property_list *properties;
1086 };
1087*/
1088llvm::Constant *
1089CGObjCMac::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 Dunbar86e253a2008-08-22 20:34:54 +00001095 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001096 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001097 Values[2] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") +
1098 ID->getName(),
1099 ID->getClassInterface()->classprop_begin(),
1100 ID->getClassInterface()->classprop_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001101
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 */
1133llvm::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 Dunbarc45ef602008-08-26 21:51:14 +00001209
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.
1213llvm::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 Dunbar86e253a2008-08-22 20:34:54 +00001228llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1229 const char *Section,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001230 const std::vector<llvm::Constant*>
1231 &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001232 // 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 Dunbar27f9d772008-08-21 04:36:09 +00001244 llvm::GlobalVariable *GV =
1245 new llvm::GlobalVariable(Init->getType(), false,
1246 llvm::GlobalValue::InternalLinkage,
1247 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001248 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001249 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001250 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001251 UsedGlobals.push_back(GV);
1252 return llvm::ConstantExpr::getBitCast(GV,
1253 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001254}
1255
1256llvm::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 Dunbar8f2926b2008-08-23 03:46:30 +00001282 if (Ty->isSingleValueType()) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001283 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 Dunbarc45ef602008-08-26 21:51:14 +00001299 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001300
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001301 unsigned Offset = 3; // Return plus self and selector implicit args.
1302 if (useStructRet) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001303 Method->addParamAttr(1, llvm::ParamAttr::StructRet);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001304 ++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 Dunbarb7ec2462008-08-16 03:19:19 +00001316
1317 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001318}
1319
1320llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001321 // Abuse this interface function as a place to finalize.
1322 FinishModule();
1323
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001324 return NULL;
1325}
1326
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001327/* *** 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/// };
1337enum ImageInfoFlags {
1338 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
1339 eImageInfo_GarbageCollected = (1 << 1),
1340 eImageInfo_GCOnly = (1 << 2)
1341};
1342
1343void 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 Dunbarf77ac862008-08-11 21:35:06 +00001353 // 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 Dunbar4e2d7d02008-08-12 06:48:42 +00001359 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001360 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 Dunbarbbce49b2008-08-12 00:12:39 +00001366 if (ObjCABI == 1) {
1367 GV->setSection("__OBJC, __image_info,regular");
1368 } else {
1369 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
1370 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001371
1372 UsedGlobals.push_back(GV);
1373}
1374
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001375
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
1384static const int ModuleVersion = 7;
1385
1386void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001387 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 Dunbar7ded7f42008-08-15 22:20:32 +00001392 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001393 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001394 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 Dunbar6efc0c52008-08-13 03:21:16 +00001401 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001402 &CGM.getModule());
1403 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
1404 UsedGlobals.push_back(GV);
1405}
1406
1407llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001408 unsigned NumClasses = DefinedClasses.size();
1409 unsigned NumCategories = DefinedCategories.size();
1410
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001411 // 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 Dunbar27f9d772008-08-21 04:36:09 +00001416 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 Dunbar86e253a2008-08-22 20:34:54 +00001421 // The runtime expects exactly the list of defined classes followed
1422 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001423 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001424 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 Dunbar27f9d772008-08-21 04:36:09 +00001432 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001433 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001434 NumClasses + NumCategories),
1435 Symbols);
1436
1437 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1438
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001439 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001440 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001441 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001442 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001443 "\01L_OBJC_SYMBOLS",
1444 &CGM.getModule());
1445 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
1446 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001447 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
1448}
1449
1450llvm::Value *CGObjCMac::EmitClassRef(llvm::IRBuilder<> &Builder,
1451 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001452 LazySymbols.insert(ID->getIdentifier());
1453
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001454 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 Dunbar4e2d7d02008-08-12 06:48:42 +00001470}
1471
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001472llvm::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 Dunbar6efc0c52008-08-13 03:21:16 +00001491llvm::Constant *CGObjCMac::GetClassName(IdentifierInfo *Ident) {
1492 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001493
1494 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001495 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001496 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001497 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001498 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 Dunbar6efc0c52008-08-13 03:21:16 +00001505 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001506}
1507
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001508llvm::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 Dunbar6efc0c52008-08-13 03:21:16 +00001514 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001515 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 Dunbar6efc0c52008-08-13 03:21:16 +00001522 return getConstantGEP(Entry, 0, 0);
1523}
1524
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001525// FIXME: Merge into a single cstring creation function.
1526llvm::Constant *CGObjCMac::GetMethodVarName(IdentifierInfo *ID) {
1527 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
1528}
1529
1530// FIXME: Merge into a single cstring creation function.
1531llvm::Constant *CGObjCMac::GetMethodVarName(const std::string &Name) {
1532 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
1533}
1534
1535llvm::Constant *CGObjCMac::GetMethodVarType(const std::string &Name) {
1536 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001537
1538 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001539 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001540 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 Dunbar259d93d2008-08-12 03:39:23 +00001550}
1551
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001552// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001553llvm::Constant *CGObjCMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001554 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001555 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
1556 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001557 return GetMethodVarType(TypeStr);
1558}
1559
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001560// FIXME: Merge into a single cstring creation function.
1561llvm::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.
1579llvm::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 Dunbarb7ec2462008-08-16 03:19:19 +00001585void 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 Dunbarf77ac862008-08-11 21:35:06 +00001598void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001599 EmitModuleInfo();
1600
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001601 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001602
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001603 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001604 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001605 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001606 }
1607
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001608 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001609 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 Dunbar242d4dc2008-08-25 06:02:07 +00001617
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 Dunbarf77ac862008-08-11 21:35:06 +00001634}
1635
1636/* *** */
1637
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001638ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Daniel Dunbar3e9df992008-08-23 18:37:06 +00001639 : CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001640{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001641 CodeGen::CodeGenTypes &Types = CGM.getTypes();
1642 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001643
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001644 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001645 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001646 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001647 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1648
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001649 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
1650 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001651
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 Dunbar4e2d7d02008-08-12 06:48:42 +00001656
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001657 MethodDescriptionTy =
1658 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001659 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001660 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 Dunbarc8ef5512008-08-23 00:19:03 +00001673 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 Dunbar6efc0c52008-08-13 03:21:16 +00001683 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 Dunbar27f9d772008-08-21 04:36:09 +00001711 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001712 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 Dunbar27f9d772008-08-21 04:36:09 +00001726
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 Dunbar86e253a2008-08-22 20:34:54 +00001782 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 Dunbare8b470d2008-08-23 04:28:29 +00001792 SuperTy =
1793 llvm::StructType::get(ObjectPtrTy,
1794 ClassPtrTy,
1795 NULL);
1796 CGM.getModule().addTypeName("struct._objc_super",
1797 SuperTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001798 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001799
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001800 // Global metadata structures
1801
1802 SymtabTy = llvm::StructType::get(LongTy,
1803 SelectorPtrTy,
1804 ShortTy,
1805 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001806 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001807 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 Dunbar14c80b72008-08-23 09:25:55 +00001818
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 Dunbarbbce49b2008-08-12 00:12:39 +00001865}
1866
1867ObjCTypesHelper::~ObjCTypesHelper() {
1868}
1869
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001870llvm::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 Dunbar259d93d2008-08-12 03:39:23 +00001889 }
1890
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001891 return llvm::ConstantExpr::getBitCast(F,
1892 llvm::PointerType::getUnqual(CallFTy));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001893}
1894
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001895/* *** */
1896
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001897CodeGen::CGObjCRuntime *
1898CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001899 return new CGObjCMac(CGM);
1900}