blob: a64b16041a6be809975ebadd6dcfcb117a35f89f [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;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000029using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000030
31namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000032
Daniel Dunbarae226fa2008-08-27 02:31:56 +000033 typedef std::vector<llvm::Constant*> ConstantVector;
34
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000035 // FIXME: We should find a nicer way to make the labels for
36 // metadata, string concatenation is lame.
37
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000038/// ObjCTypesHelper - Helper class that encapsulates lazy
39/// construction of varies types used during ObjC generation.
40class ObjCTypesHelper {
41private:
42 CodeGen::CodeGenModule &CGM;
43
Daniel Dunbar14c80b72008-08-23 09:25:55 +000044 llvm::Function *MessageSendFn, *MessageSendStretFn;
45 llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn;
Daniel Dunbar259d93d2008-08-12 03:39:23 +000046
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000047public:
Daniel Dunbar27f9d772008-08-21 04:36:09 +000048 const llvm::Type *ShortTy, *IntTy, *LongTy;
49 const llvm::Type *Int8PtrTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +000050
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000051 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
52 const llvm::Type *ObjectPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000053 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000054 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000055 /// ProtocolPtrTy - LLVM type for external protocol handles
56 /// (typeof(Protocol))
57 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000058
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000059 // SuperCTy - clang type for struct objc_super.
60 QualType SuperCTy;
61 // SuperPtrCTy - clang type for struct objc_super *.
62 QualType SuperPtrCTy;
63
Daniel Dunbare8b470d2008-08-23 04:28:29 +000064 /// SuperTy - LLVM type for struct objc_super.
65 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +000066 /// SuperPtrTy - LLVM type for struct objc_super *.
67 const llvm::Type *SuperPtrTy;
Daniel Dunbare8b470d2008-08-23 04:28:29 +000068
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000069 /// SymtabTy - LLVM type for struct objc_symtab.
70 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +000071 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
72 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000073 /// ModuleTy - LLVM type for struct objc_module.
74 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +000075
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000076 /// ProtocolTy - LLVM type for struct objc_protocol.
77 const llvm::StructType *ProtocolTy;
78 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
79 const llvm::Type *ProtocolPtrTy;
80 /// ProtocolExtensionTy - LLVM type for struct
81 /// objc_protocol_extension.
82 const llvm::StructType *ProtocolExtensionTy;
83 /// ProtocolExtensionTy - LLVM type for struct
84 /// objc_protocol_extension *.
85 const llvm::Type *ProtocolExtensionPtrTy;
86 /// MethodDescriptionTy - LLVM type for struct
87 /// objc_method_description.
88 const llvm::StructType *MethodDescriptionTy;
89 /// MethodDescriptionListTy - LLVM type for struct
90 /// objc_method_description_list.
91 const llvm::StructType *MethodDescriptionListTy;
92 /// MethodDescriptionListPtrTy - LLVM type for struct
93 /// objc_method_description_list *.
94 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +000095 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
96 /// in GCC parlance).
97 const llvm::StructType *PropertyTy;
98 /// PropertyListTy - LLVM type for struct objc_property_list
99 /// (_prop_list_t in GCC parlance).
100 const llvm::StructType *PropertyListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000101 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
102 const llvm::Type *PropertyListPtrTy;
103 /// ProtocolListTy - LLVM type for struct objc_property_list.
104 const llvm::Type *ProtocolListTy;
105 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
106 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000107 /// CategoryTy - LLVM type for struct objc_category.
108 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000109 /// ClassTy - LLVM type for struct objc_class.
110 const llvm::StructType *ClassTy;
111 /// ClassPtrTy - LLVM type for struct objc_class *.
112 const llvm::Type *ClassPtrTy;
113 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
114 const llvm::StructType *ClassExtensionTy;
115 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
116 const llvm::Type *ClassExtensionPtrTy;
117 /// CacheTy - LLVM type for struct objc_cache.
118 const llvm::Type *CacheTy;
119 /// CachePtrTy - LLVM type for struct objc_cache *.
120 const llvm::Type *CachePtrTy;
121 // IvarTy - LLVM type for struct objc_ivar.
122 const llvm::StructType *IvarTy;
123 /// IvarListTy - LLVM type for struct objc_ivar_list.
124 const llvm::Type *IvarListTy;
125 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
126 const llvm::Type *IvarListPtrTy;
127 // MethodTy - LLVM type for struct objc_method.
128 const llvm::StructType *MethodTy;
129 /// MethodListTy - LLVM type for struct objc_method_list.
130 const llvm::Type *MethodListTy;
131 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
132 const llvm::Type *MethodListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000133
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000134 llvm::Function *EnumerationMutationFn;
Anders Carlsson124526b2008-09-09 10:10:21 +0000135
136 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
137 const llvm::Type *ExceptionDataTy;
138
139 /// ExceptionThrowFn - LLVM objc_exception_throw function.
140 llvm::Function *ExceptionThrowFn;
141
142 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
143 llvm::Function *ExceptionTryEnterFn;
144
145 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
146 llvm::Function *ExceptionTryExitFn;
147
148 /// ExceptionExtractFn - LLVM objc_exception_extract function.
149 llvm::Function *ExceptionExtractFn;
150
151 /// ExceptionMatchFn - LLVM objc_exception_match function.
152 llvm::Function *ExceptionMatchFn;
153
154 /// SetJmpFn - LLVM _setjmp function.
155 llvm::Function *SetJmpFn;
156
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000157public:
158 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
159 ~ObjCTypesHelper();
160
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000161 llvm::Value *getMessageSendFn(bool IsSuper, const llvm::Type *ReturnTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000162};
163
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000164class CGObjCMac : public CodeGen::CGObjCRuntime {
165private:
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000166 CodeGen::CodeGenModule &CGM;
167 ObjCTypesHelper ObjCTypes;
168 /// ObjCABI - FIXME: Not sure yet.
169 unsigned ObjCABI;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000170
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000171 /// LazySymbols - Symbols to generate a lazy reference for. See
172 /// DefinedSymbols and FinishModule().
173 std::set<IdentifierInfo*> LazySymbols;
174
175 /// DefinedSymbols - External symbols which are defined by this
176 /// module. The symbols in this list and LazySymbols are used to add
177 /// special linker symbols which ensure that Objective-C modules are
178 /// linked properly.
179 std::set<IdentifierInfo*> DefinedSymbols;
180
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000181 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000182 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000183
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000184 /// MethodVarNames - uniqued method variable names.
185 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
186
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000187 /// MethodVarTypes - uniqued method type signatures. We have to use
188 /// a StringMap here because have no other unique reference.
189 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
190
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000191 /// MethodDefinitions - map of methods which have been defined in
192 /// this translation unit.
193 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
194
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000195 /// PropertyNames - uniqued method variable names.
196 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
197
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000198 /// ClassReferences - uniqued class references.
199 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
200
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000201 /// SelectorReferences - uniqued selector references.
202 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
203
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000204 /// Protocols - Protocols for which an objc_protocol structure has
205 /// been emitted. Forward declarations are handled by creating an
206 /// empty structure whose initializer is filled in when/if defined.
207 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
208
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000209 /// DefinedClasses - List of defined classes.
210 std::vector<llvm::GlobalValue*> DefinedClasses;
211
212 /// DefinedCategories - List of defined categories.
213 std::vector<llvm::GlobalValue*> DefinedCategories;
214
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000215 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000216 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000217 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000218
219 /// EmitImageInfo - Emit the image info marker used to encode some module
220 /// level information.
221 void EmitImageInfo();
222
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000223 /// EmitModuleInfo - Another marker encoding module level
224 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000225 void EmitModuleInfo();
226
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000227 /// EmitModuleSymols - Emit module symbols, the list of defined
228 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000229 llvm::Constant *EmitModuleSymbols();
230
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000231 /// FinishModule - Write out global data structures at the end of
232 /// processing a translation unit.
233 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000234
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000235 /// EmitClassExtension - Generate the class extension structure used
236 /// to store the weak ivar layout and properties. The return value
237 /// has type ClassExtensionPtrTy.
238 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
239
240 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
241 /// for the given class.
242 llvm::Value *EmitClassRef(llvm::IRBuilder<> &Builder,
243 const ObjCInterfaceDecl *ID);
244
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000245 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000246 QualType ResultType,
247 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000248 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000249 QualType Arg0Ty,
250 bool IsSuper,
251 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000252
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000253 /// EmitIvarList - Emit the ivar list for the given
254 /// implementation. If ForClass is true the list of class ivars
255 /// (i.e. metaclass ivars) is emitted, otherwise the list of
256 /// interface ivars will be emitted. The return value has type
257 /// IvarListPtrTy.
258 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
259 bool ForClass,
260 const llvm::Type *InterfaceTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000261
262 /// EmitMetaClass - Emit a forward reference to the class structure
263 /// for the metaclass of the given interface. The return value has
264 /// type ClassPtrTy.
265 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
266
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000267 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000268 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000269 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
270 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000271 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000272 const ConstantVector &Methods);
273
274 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
275
276 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000277
278 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000279 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000280 llvm::Constant *EmitMethodList(const std::string &Name,
281 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000282 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000283
284 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000285 /// method declarations.
286 /// - TypeName: The name for the type containing the methods.
287 /// - IsProtocol: True iff these methods are for a protocol.
288 /// - ClassMethds: True iff these are class methods.
289 /// - Required: When true, only "required" methods are
290 /// listed. Similarly, when false only "optional" methods are
291 /// listed. For classes this should always be true.
292 /// - begin, end: The method list to output.
293 ///
294 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000295 llvm::Constant *EmitMethodDescList(const std::string &Name,
296 const char *Section,
297 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000298
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000299 /// EmitPropertyList - Emit the given property list. The return
300 /// value has type PropertyListPtrTy.
301 llvm::Constant *EmitPropertyList(const std::string &Name,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000302 const Decl *Container,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000303 ObjCPropertyDecl * const *begin,
304 ObjCPropertyDecl * const *end);
305
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000306 /// EmitProtocolExtension - Generate the protocol extension
307 /// structure used to store optional instance and class methods, and
308 /// protocol properties. The return value has type
309 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000310 llvm::Constant *
311 EmitProtocolExtension(const ObjCProtocolDecl *PD,
312 const ConstantVector &OptInstanceMethods,
313 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000314
315 /// EmitProtocolList - Generate the list of referenced
316 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +0000317 llvm::Constant *EmitProtocolList(const std::string &Name,
318 ObjCProtocolDecl::protocol_iterator begin,
319 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000320
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000321 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
322 /// for the given selector.
323 llvm::Value *EmitSelector(llvm::IRBuilder<> &Builder, Selector Sel);
324
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000325 /// GetProtocolRef - Return a reference to the internal protocol
326 /// description, creating an empty one if it has not been
327 /// defined. The return value has type pointer-to ProtocolTy.
328 llvm::GlobalVariable *GetProtocolRef(const ObjCProtocolDecl *PD);
329
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000330 /// GetClassName - Return a unique constant for the given selector's
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000331 /// name. The return value has type char *.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000332 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000333
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000334 /// GetMethodVarName - Return a unique constant for the given
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000335 /// selector's name. The return value has type char *.
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000336 llvm::Constant *GetMethodVarName(Selector Sel);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000337 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000338 llvm::Constant *GetMethodVarName(const std::string &Name);
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000339
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000340 /// GetMethodVarType - Return a unique constant for the given
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000341 /// selector's name. The return value has type char *.
342
343 // FIXME: This is a horrible name.
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000344 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000345 llvm::Constant *GetMethodVarType(const std::string &Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000346
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000347 /// GetPropertyName - Return a unique constant for the given
348 /// name. The return value has type char *.
349 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
350
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000351 // FIXME: This can be dropped once string functions are unified.
352 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
353 const Decl *Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000354
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000355 /// GetNameForMethod - Return a name for the given method.
356 /// \param[out] NameOut - The return value.
357 void GetNameForMethod(const ObjCMethodDecl *OMD,
358 std::string &NameOut);
359
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000360public:
361 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000362 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000363
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000364 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000365 QualType ResultType,
366 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000367 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000368 bool IsClassMessage,
369 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000370
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000371 virtual CodeGen::RValue
372 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000373 QualType ResultType,
374 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000375 const ObjCInterfaceDecl *Class,
376 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000377 bool IsClassMessage,
378 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000379
380 virtual llvm::Value *GetClass(llvm::IRBuilder<> &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000381 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000382
383 virtual llvm::Value *GetSelector(llvm::IRBuilder<> &Builder, Selector Sel);
384
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000385 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000386
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000387 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000388
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000389 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000390
391 virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder<> &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000392 const ObjCProtocolDecl *PD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000393
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000394 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000395
396 virtual llvm::Function *ModuleInitFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000397 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000398
399 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
400 const ObjCAtTryStmt &S);
401 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
402 const ObjCAtThrowStmt &S);
403
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000404};
405} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000406
407/* *** Helper Functions *** */
408
409/// getConstantGEP() - Help routine to construct simple GEPs.
410static llvm::Constant *getConstantGEP(llvm::Constant *C,
411 unsigned idx0,
412 unsigned idx1) {
413 llvm::Value *Idxs[] = {
414 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
415 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
416 };
417 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
418}
419
420/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000421
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000422CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm)
423 : CGM(cgm),
424 ObjCTypes(cgm),
425 ObjCABI(1)
426{
427 // FIXME: How does this get set in GCC? And what does it even mean?
428 if (ObjCTypes.LongTy != CGM.getTypes().ConvertType(CGM.getContext().IntTy))
429 ObjCABI = 2;
430
431 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000432}
433
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000434/// GetClass - Return a reference to the class for the given interface
435/// decl.
436llvm::Value *CGObjCMac::GetClass(llvm::IRBuilder<> &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000437 const ObjCInterfaceDecl *ID) {
438 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000439}
440
441/// GetSelector - Return the pointer to the unique'd string for this selector.
442llvm::Value *CGObjCMac::GetSelector(llvm::IRBuilder<> &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000443 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000444}
445
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000446/// Generate a constant CFString object.
447/*
448 struct __builtin_CFString {
449 const int *isa; // point to __CFConstantStringClassReference
450 int flags;
451 const char *str;
452 long length;
453 };
454*/
455
456llvm::Constant *CGObjCMac::GenerateConstantString(const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000457 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000458}
459
460/// Generates a message send where the super is the receiver. This is
461/// a message send to self with special delivery semantics indicating
462/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000463CodeGen::RValue
464CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000465 QualType ResultType,
466 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000467 const ObjCInterfaceDecl *Class,
468 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000469 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000470 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000471 // Create and init a super structure; this is a (receiver, class)
472 // pair we will pass to objc_msgSendSuper.
473 llvm::Value *ObjCSuper =
474 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
475 llvm::Value *ReceiverAsObject =
476 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
477 CGF.Builder.CreateStore(ReceiverAsObject,
478 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000479
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000480 // If this is a class message the metaclass is passed as the target.
481 llvm::Value *Target;
482 if (IsClassMessage) {
483 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
484 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
485 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
486 Target = Super;
487 } else {
488 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
489 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000490 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
491 // and ObjCTypes types.
492 const llvm::Type *ClassTy =
493 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
494 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000495 CGF.Builder.CreateStore(Target,
496 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
497
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000498 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000499 ObjCSuper, ObjCTypes.SuperPtrCTy,
500 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000501}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000502
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000503/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000504CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000505 QualType ResultType,
506 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000507 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000508 bool IsClassMessage,
509 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000510 llvm::Value *Arg0 =
511 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000512 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000513 Arg0, CGF.getContext().getObjCIdType(),
514 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000515}
516
517CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000518 QualType ResultType,
519 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000520 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000521 QualType Arg0Ty,
522 bool IsSuper,
523 const CallArgList &CallArgs) {
524 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000525 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
526 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
527 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000528 CGF.getContext().getObjCSelType()));
529 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000530
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000531 // FIXME: This is a hack, we are implicitly coordinating with
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000532 // EmitCall, which will move the return type to the first parameter
533 // and set the structure return flag. See getMessageSendFn().
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000534
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000535 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(ResultType);
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000536 return CGF.EmitCall(ObjCTypes.getMessageSendFn(IsSuper, ReturnTy),
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000537 ResultType, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000538}
539
540llvm::Value *CGObjCMac::GenerateProtocolRef(llvm::IRBuilder<> &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000541 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +0000542 // FIXME: I don't understand why gcc generates this, or where it is
543 // resolved. Investigate. Its also wasteful to look this up over and
544 // over.
545 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
546
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000547 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
548 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000549}
550
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000551/*
552 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
553 struct _objc_protocol {
554 struct _objc_protocol_extension *isa;
555 char *protocol_name;
556 struct _objc_protocol_list *protocol_list;
557 struct _objc__method_prototype_list *instance_methods;
558 struct _objc__method_prototype_list *class_methods
559 };
560
561 See EmitProtocolExtension().
562*/
563void CGObjCMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000564 // FIXME: I don't understand why gcc generates this, or where it is
565 // resolved. Investigate. Its also wasteful to look this up over and
566 // over.
567 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
568
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000569 const char *ProtocolName = PD->getName();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000570
571 // Construct method lists.
572 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
573 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
574 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
575 e = PD->instmeth_end(); i != e; ++i) {
576 ObjCMethodDecl *MD = *i;
577 llvm::Constant *C = GetMethodDescriptionConstant(MD);
578 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
579 OptInstanceMethods.push_back(C);
580 } else {
581 InstanceMethods.push_back(C);
582 }
583 }
584
585 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
586 e = PD->classmeth_end(); i != e; ++i) {
587 ObjCMethodDecl *MD = *i;
588 llvm::Constant *C = GetMethodDescriptionConstant(MD);
589 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
590 OptClassMethods.push_back(C);
591 } else {
592 ClassMethods.push_back(C);
593 }
594 }
595
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000596 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000597 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000598 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000599 Values[2] =
600 EmitProtocolList(std::string("\01L_OBJC_PROTOCOL_REFS_")+PD->getName(),
601 PD->protocol_begin(),
602 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000603 Values[3] =
604 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_")
605 + PD->getName(),
606 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
607 InstanceMethods);
608 Values[4] =
609 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_CLASS_METHODS_")
610 + PD->getName(),
611 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
612 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000613 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
614 Values);
615
616 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
617 if (Entry) {
618 // Already created, just update the initializer
619 Entry->setInitializer(Init);
620 } else {
621 Entry =
622 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
623 llvm::GlobalValue::InternalLinkage,
624 Init,
625 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
626 &CGM.getModule());
627 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
628 UsedGlobals.push_back(Entry);
629 // FIXME: Is this necessary? Why only for protocol?
630 Entry->setAlignment(4);
631 }
632}
633
634llvm::GlobalVariable *CGObjCMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
635 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
636
637 if (!Entry) {
638 std::vector<llvm::Constant*> Values(5);
639 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
640 Values[1] = GetClassName(PD->getIdentifier());
641 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
642 Values[3] = Values[4] =
643 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
644 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
645 Values);
646
647 Entry =
648 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
649 llvm::GlobalValue::InternalLinkage,
650 Init,
651 std::string("\01L_OBJC_PROTOCOL_")+PD->getName(),
652 &CGM.getModule());
653 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
654 UsedGlobals.push_back(Entry);
655 // FIXME: Is this necessary? Why only for protocol?
656 Entry->setAlignment(4);
657 }
658
659 return Entry;
660}
661
662/*
663 struct _objc_protocol_extension {
664 uint32_t size;
665 struct objc_method_description_list *optional_instance_methods;
666 struct objc_method_description_list *optional_class_methods;
667 struct objc_property_list *instance_properties;
668 };
669*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000670llvm::Constant *
671CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
672 const ConstantVector &OptInstanceMethods,
673 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000674 uint64_t Size =
675 CGM.getTargetData().getABITypeSize(ObjCTypes.ProtocolExtensionTy);
676 std::vector<llvm::Constant*> Values(4);
677 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000678 Values[1] =
679 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_")
680 + PD->getName(),
681 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
682 OptInstanceMethods);
683 Values[2] =
684 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_")
685 + PD->getName(),
686 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
687 OptClassMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000688 Values[3] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_PROTO_LIST_") +
689 PD->getName(),
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000690 0,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000691 PD->classprop_begin(),
692 PD->classprop_end());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000693
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000694 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000695 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
696 Values[3]->isNullValue())
697 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
698
699 llvm::Constant *Init =
700 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
701 llvm::GlobalVariable *GV =
702 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
703 llvm::GlobalValue::InternalLinkage,
704 Init,
705 (std::string("\01L_OBJC_PROTOCOLEXT_") +
706 PD->getName()),
707 &CGM.getModule());
708 // No special section, but goes in llvm.used
709 UsedGlobals.push_back(GV);
710
711 return GV;
712}
713
714/*
715 struct objc_protocol_list {
716 struct objc_protocol_list *next;
717 long count;
718 Protocol *list[];
719 };
720*/
Daniel Dunbardbc933702008-08-21 21:57:41 +0000721llvm::Constant *
722CGObjCMac::EmitProtocolList(const std::string &Name,
723 ObjCProtocolDecl::protocol_iterator begin,
724 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000725 std::vector<llvm::Constant*> ProtocolRefs;
726
Daniel Dunbardbc933702008-08-21 21:57:41 +0000727 for (; begin != end; ++begin)
728 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000729
730 // Just return null for empty protocol lists
731 if (ProtocolRefs.empty())
732 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
733
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000734 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000735 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
736
737 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000738 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000739 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
740 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
741 Values[2] =
742 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
743 ProtocolRefs.size()),
744 ProtocolRefs);
745
746 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
747 llvm::GlobalVariable *GV =
748 new llvm::GlobalVariable(Init->getType(), false,
749 llvm::GlobalValue::InternalLinkage,
750 Init,
Daniel Dunbardbc933702008-08-21 21:57:41 +0000751 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000752 &CGM.getModule());
753 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
754 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
755}
756
757/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000758 struct _objc_property {
759 const char * const name;
760 const char * const attributes;
761 };
762
763 struct _objc_property_list {
764 uint32_t entsize; // sizeof (struct _objc_property)
765 uint32_t prop_count;
766 struct _objc_property[prop_count];
767 };
768*/
769llvm::Constant *CGObjCMac::EmitPropertyList(const std::string &Name,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000770 const Decl *Container,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000771 ObjCPropertyDecl * const *begin,
772 ObjCPropertyDecl * const *end) {
773 std::vector<llvm::Constant*> Properties, Prop(2);
774 for (; begin != end; ++begin) {
775 const ObjCPropertyDecl *PD = *begin;
776 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000777 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000778 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
779 Prop));
780 }
781
782 // Return null for empty list.
783 if (Properties.empty())
784 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
785
786 unsigned PropertySize =
787 CGM.getTargetData().getABITypeSize(ObjCTypes.PropertyTy);
788 std::vector<llvm::Constant*> Values(3);
789 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
790 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
791 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
792 Properties.size());
793 Values[2] = llvm::ConstantArray::get(AT, Properties);
794 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
795
796 llvm::GlobalVariable *GV =
797 new llvm::GlobalVariable(Init->getType(), false,
798 llvm::GlobalValue::InternalLinkage,
799 Init,
800 Name,
801 &CGM.getModule());
802 // No special section on property lists?
803 UsedGlobals.push_back(GV);
804 return llvm::ConstantExpr::getBitCast(GV,
805 ObjCTypes.PropertyListPtrTy);
806
807}
808
809/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000810 struct objc_method_description_list {
811 int count;
812 struct objc_method_description list[];
813 };
814*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000815llvm::Constant *
816CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
817 std::vector<llvm::Constant*> Desc(2);
818 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
819 ObjCTypes.SelectorPtrTy);
820 Desc[1] = GetMethodVarType(MD);
821 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
822 Desc);
823}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000824
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000825llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
826 const char *Section,
827 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000828 // Return null for empty list.
829 if (Methods.empty())
830 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
831
832 std::vector<llvm::Constant*> Values(2);
833 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
834 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
835 Methods.size());
836 Values[1] = llvm::ConstantArray::get(AT, Methods);
837 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
838
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000839 llvm::GlobalVariable *GV =
840 new llvm::GlobalVariable(Init->getType(), false,
841 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000842 Init, Name, &CGM.getModule());
843 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000844 UsedGlobals.push_back(GV);
845 return llvm::ConstantExpr::getBitCast(GV,
846 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000847}
848
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000849/*
850 struct _objc_category {
851 char *category_name;
852 char *class_name;
853 struct _objc_method_list *instance_methods;
854 struct _objc_method_list *class_methods;
855 struct _objc_protocol_list *protocols;
856 uint32_t size; // <rdar://4585769>
857 struct _objc_property_list *instance_properties;
858 };
859 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000860void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000861 unsigned Size = CGM.getTargetData().getABITypeSize(ObjCTypes.CategoryTy);
862
Daniel Dunbar86e2f402008-08-26 23:03:11 +0000863 // FIXME: This is poor design, the OCD should have a pointer to the
864 // category decl. Additionally, note that Category can be null for
865 // the @implementation w/o an @interface case. Sema should just
866 // create one for us as it does for @implementation so everyone else
867 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000868 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +0000869 const ObjCCategoryDecl *Category =
870 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000871 std::string ExtName(std::string(Interface->getName()) +
872 "_" +
873 OCD->getName());
874
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000875 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
876 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
877 e = OCD->instmeth_end(); i != e; ++i) {
878 // Instance methods should always be defined.
879 InstanceMethods.push_back(GetMethodConstant(*i));
880 }
881 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
882 e = OCD->classmeth_end(); i != e; ++i) {
883 // Class methods should always be defined.
884 ClassMethods.push_back(GetMethodConstant(*i));
885 }
886
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000887 std::vector<llvm::Constant*> Values(7);
888 Values[0] = GetClassName(OCD->getIdentifier());
889 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000890 Values[2] =
891 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
892 ExtName,
893 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000894 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000895 Values[3] =
896 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
897 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000898 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000899 if (Category) {
900 Values[4] =
901 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
902 Category->protocol_begin(),
903 Category->protocol_end());
904 } else {
905 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
906 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000907 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +0000908
909 // If there is no category @interface then there can be no properties.
910 if (Category) {
911 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000912 OCD,
Daniel Dunbar86e2f402008-08-26 23:03:11 +0000913 Category->classprop_begin(),
914 Category->classprop_end());
915 } else {
916 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
917 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000918
919 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
920 Values);
921
922 llvm::GlobalVariable *GV =
923 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
924 llvm::GlobalValue::InternalLinkage,
925 Init,
926 std::string("\01L_OBJC_CATEGORY_")+ExtName,
927 &CGM.getModule());
928 GV->setSection("__OBJC,__category,regular,no_dead_strip");
929 UsedGlobals.push_back(GV);
930 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000931}
932
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000933// FIXME: Get from somewhere?
934enum ClassFlags {
935 eClassFlags_Factory = 0x00001,
936 eClassFlags_Meta = 0x00002,
937 // <rdr://5142207>
938 eClassFlags_HasCXXStructors = 0x02000,
939 eClassFlags_Hidden = 0x20000,
940 eClassFlags_ABI2_Hidden = 0x00010,
941 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
942};
943
944// <rdr://5142207&4705298&4843145>
945static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
946 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
947 // FIXME: Support -fvisibility
948 switch (attr->getVisibility()) {
949 default:
950 assert(0 && "Unknown visibility");
951 return false;
952 case VisibilityAttr::DefaultVisibility:
953 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
954 return false;
955 case VisibilityAttr::HiddenVisibility:
956 return true;
957 }
958 } else {
959 return false; // FIXME: Support -fvisibility
960 }
961}
962
963/*
964 struct _objc_class {
965 Class isa;
966 Class super_class;
967 const char *name;
968 long version;
969 long info;
970 long instance_size;
971 struct _objc_ivar_list *ivars;
972 struct _objc_method_list *methods;
973 struct _objc_cache *cache;
974 struct _objc_protocol_list *protocols;
975 // Objective-C 1.0 extensions (<rdr://4585769>)
976 const char *ivar_layout;
977 struct _objc_class_ext *ext;
978 };
979
980 See EmitClassExtension();
981 */
982void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000983 DefinedSymbols.insert(ID->getIdentifier());
984
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000985 const char *ClassName = ID->getName();
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000986 // FIXME: Gross
987 ObjCInterfaceDecl *Interface =
988 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000989 llvm::Constant *Protocols =
990 EmitProtocolList(std::string("\01L_OBJC_CLASS_PROTOCOLS_") + ID->getName(),
991 Interface->protocol_begin(),
992 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000993 const llvm::Type *InterfaceTy =
994 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
995 unsigned Flags = eClassFlags_Factory;
996 unsigned Size = CGM.getTargetData().getABITypeSize(InterfaceTy);
997
998 // FIXME: Set CXX-structors flag.
999 if (IsClassHidden(ID->getClassInterface()))
1000 Flags |= eClassFlags_Hidden;
1001
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001002 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1003 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1004 e = ID->instmeth_end(); i != e; ++i) {
1005 // Instance methods should always be defined.
1006 InstanceMethods.push_back(GetMethodConstant(*i));
1007 }
1008 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1009 e = ID->classmeth_end(); i != e; ++i) {
1010 // Class methods should always be defined.
1011 ClassMethods.push_back(GetMethodConstant(*i));
1012 }
1013
1014 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1015 e = ID->propimpl_end(); i != e; ++i) {
1016 ObjCPropertyImplDecl *PID = *i;
1017
1018 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1019 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1020
1021 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1022 if (llvm::Constant *C = GetMethodConstant(MD))
1023 InstanceMethods.push_back(C);
1024 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1025 if (llvm::Constant *C = GetMethodConstant(MD))
1026 InstanceMethods.push_back(C);
1027 }
1028 }
1029
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001030 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001031 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001032 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001033 // Record a reference to the super class.
1034 LazySymbols.insert(Super->getIdentifier());
1035
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001036 Values[ 1] =
1037 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1038 ObjCTypes.ClassPtrTy);
1039 } else {
1040 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1041 }
1042 Values[ 2] = GetClassName(ID->getIdentifier());
1043 // Version is always 0.
1044 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1045 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1046 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
1047 Values[ 6] = EmitIvarList(ID, false, InterfaceTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001048 Values[ 7] =
1049 EmitMethodList(std::string("\01L_OBJC_INSTANCE_METHODS_") + ID->getName(),
1050 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001051 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001052 // cache is always NULL.
1053 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1054 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001055 // FIXME: Set ivar_layout
1056 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001057 Values[11] = EmitClassExtension(ID);
1058 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1059 Values);
1060
1061 llvm::GlobalVariable *GV =
1062 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1063 llvm::GlobalValue::InternalLinkage,
1064 Init,
1065 std::string("\01L_OBJC_CLASS_")+ClassName,
1066 &CGM.getModule());
1067 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1068 UsedGlobals.push_back(GV);
1069 // FIXME: Why?
1070 GV->setAlignment(32);
1071 DefinedClasses.push_back(GV);
1072}
1073
1074llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1075 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001076 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001077 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001078 const char *ClassName = ID->getName();
1079 unsigned Flags = eClassFlags_Meta;
1080 unsigned Size = CGM.getTargetData().getABITypeSize(ObjCTypes.ClassTy);
1081
1082 if (IsClassHidden(ID->getClassInterface()))
1083 Flags |= eClassFlags_Hidden;
1084
1085 std::vector<llvm::Constant*> Values(12);
1086 // The isa for the metaclass is the root of the hierarchy.
1087 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1088 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1089 Root = Super;
1090 Values[ 0] =
1091 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1092 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001093 // The super class for the metaclass is emitted as the name of the
1094 // super class. The runtime fixes this up to point to the
1095 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001096 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1097 Values[ 1] =
1098 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1099 ObjCTypes.ClassPtrTy);
1100 } else {
1101 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1102 }
1103 Values[ 2] = GetClassName(ID->getIdentifier());
1104 // Version is always 0.
1105 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1106 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1107 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
1108 Values[ 6] = EmitIvarList(ID, true, InterfaceTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001109 Values[ 7] =
1110 EmitMethodList(std::string("\01L_OBJC_CLASS_METHODS_") + ID->getName(),
1111 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001112 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001113 // cache is always NULL.
1114 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1115 Values[ 9] = Protocols;
1116 // ivar_layout for metaclass is always NULL.
1117 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1118 // The class extension is always unused for metaclasses.
1119 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1120 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1121 Values);
1122
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001123 std::string Name("\01L_OBJC_METACLASS_");
1124 Name += ClassName;
1125
1126 // Check for a forward reference.
1127 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1128 if (GV) {
1129 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1130 "Forward metaclass reference has incorrect type.");
1131 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1132 GV->setInitializer(Init);
1133 } else {
1134 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1135 llvm::GlobalValue::InternalLinkage,
1136 Init, Name,
1137 &CGM.getModule());
1138 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001139 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1140 UsedGlobals.push_back(GV);
1141 // FIXME: Why?
1142 GV->setAlignment(32);
1143
1144 return GV;
1145}
1146
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001147llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
1148 std::string Name("\01L_OBJC_METACLASS_");
1149 Name += ID->getName();
1150
1151 // FIXME: Should we look these up somewhere other than the
1152 // module. Its a bit silly since we only generate these while
1153 // processing an implementation, so exactly one pointer would work
1154 // if know when we entered/exitted an implementation block.
1155
1156 // Check for an existing forward reference.
1157 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name)) {
1158 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1159 "Forward metaclass reference has incorrect type.");
1160 return GV;
1161 } else {
1162 // Generate as an external reference to keep a consistent
1163 // module. This will be patched up when we emit the metaclass.
1164 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1165 llvm::GlobalValue::ExternalLinkage,
1166 0,
1167 Name,
1168 &CGM.getModule());
1169 }
1170}
1171
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001172/*
1173 struct objc_class_ext {
1174 uint32_t size;
1175 const char *weak_ivar_layout;
1176 struct _objc_property_list *properties;
1177 };
1178*/
1179llvm::Constant *
1180CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1181 uint64_t Size =
1182 CGM.getTargetData().getABITypeSize(ObjCTypes.ClassExtensionTy);
1183
1184 std::vector<llvm::Constant*> Values(3);
1185 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001186 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001187 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001188 Values[2] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") +
1189 ID->getName(),
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001190 ID,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001191 ID->getClassInterface()->classprop_begin(),
1192 ID->getClassInterface()->classprop_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001193
1194 // Return null if no extension bits are used.
1195 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1196 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1197
1198 llvm::Constant *Init =
1199 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1200 llvm::GlobalVariable *GV =
1201 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1202 llvm::GlobalValue::InternalLinkage,
1203 Init,
1204 (std::string("\01L_OBJC_CLASSEXT_") +
1205 ID->getName()),
1206 &CGM.getModule());
1207 // No special section, but goes in llvm.used
1208 UsedGlobals.push_back(GV);
1209
1210 return GV;
1211}
1212
1213/*
1214 struct objc_ivar {
1215 char *ivar_name;
1216 char *ivar_type;
1217 int ivar_offset;
1218 };
1219
1220 struct objc_ivar_list {
1221 int ivar_count;
1222 struct objc_ivar list[count];
1223 };
1224 */
1225llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
1226 bool ForClass,
1227 const llvm::Type *InterfaceTy) {
1228 std::vector<llvm::Constant*> Ivars, Ivar(3);
1229
1230 // When emitting the root class GCC emits ivar entries for the
1231 // actual class structure. It is not clear if we need to follow this
1232 // behavior; for now lets try and get away with not doing it. If so,
1233 // the cleanest solution would be to make up an ObjCInterfaceDecl
1234 // for the class.
1235 if (ForClass)
1236 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1237
1238 const llvm::StructLayout *Layout =
1239 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
1240 for (ObjCInterfaceDecl::ivar_iterator
1241 i = ID->getClassInterface()->ivar_begin(),
1242 e = ID->getClassInterface()->ivar_end(); i != e; ++i) {
1243 ObjCIvarDecl *V = *i;
1244 unsigned Offset =
1245 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(V));
1246 std::string TypeStr;
1247 llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes;
1248 Ivar[0] = GetMethodVarName(V->getIdentifier());
1249 CGM.getContext().getObjCEncodingForType(V->getType(), TypeStr,
1250 EncodingRecordTypes);
1251 Ivar[1] = GetMethodVarType(TypeStr);
1252 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
1253 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy,
1254 Ivar));
1255 }
1256
1257 // Return null for empty list.
1258 if (Ivars.empty())
1259 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1260
1261 std::vector<llvm::Constant*> Values(2);
1262 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1263 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1264 Ivars.size());
1265 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1266 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1267
1268 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1269 "\01L_OBJC_INSTANCE_VARIABLES_");
1270 llvm::GlobalVariable *GV =
1271 new llvm::GlobalVariable(Init->getType(), false,
1272 llvm::GlobalValue::InternalLinkage,
1273 Init,
1274 std::string(Prefix) + ID->getName(),
1275 &CGM.getModule());
1276 if (ForClass) {
1277 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1278 // FIXME: Why is this only here?
1279 GV->setAlignment(32);
1280 } else {
1281 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1282 }
1283 UsedGlobals.push_back(GV);
1284 return llvm::ConstantExpr::getBitCast(GV,
1285 ObjCTypes.IvarListPtrTy);
1286}
1287
1288/*
1289 struct objc_method {
1290 SEL method_name;
1291 char *method_types;
1292 void *method;
1293 };
1294
1295 struct objc_method_list {
1296 struct objc_method_list *obsolete;
1297 int count;
1298 struct objc_method methods_list[count];
1299 };
1300*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001301
1302/// GetMethodConstant - Return a struct objc_method constant for the
1303/// given method if it has been defined. The result is null if the
1304/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001305llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001306 // FIXME: Use DenseMap::lookup
1307 llvm::Function *Fn = MethodDefinitions[MD];
1308 if (!Fn)
1309 return 0;
1310
1311 std::vector<llvm::Constant*> Method(3);
1312 Method[0] =
1313 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1314 ObjCTypes.SelectorPtrTy);
1315 Method[1] = GetMethodVarType(MD);
1316 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1317 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1318}
1319
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001320llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1321 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001322 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001323 // Return null for empty list.
1324 if (Methods.empty())
1325 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1326
1327 std::vector<llvm::Constant*> Values(3);
1328 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1329 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1330 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1331 Methods.size());
1332 Values[2] = llvm::ConstantArray::get(AT, Methods);
1333 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1334
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001335 llvm::GlobalVariable *GV =
1336 new llvm::GlobalVariable(Init->getType(), false,
1337 llvm::GlobalValue::InternalLinkage,
1338 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001339 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001340 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001341 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001342 UsedGlobals.push_back(GV);
1343 return llvm::ConstantExpr::getBitCast(GV,
1344 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001345}
1346
1347llvm::Function *CGObjCMac::GenerateMethod(const ObjCMethodDecl *OMD) {
1348 const llvm::Type *ReturnTy =
1349 CGM.getTypes().ConvertReturnType(OMD->getResultType());
1350 const llvm::Type *SelfTy =
1351 CGM.getTypes().ConvertType(OMD->getSelfDecl()->getType());
1352
1353 std::vector<const llvm::Type*> ArgTys;
1354 ArgTys.reserve(1 + 2 + OMD->param_size());
1355
1356 // FIXME: This is not something we should have to be dealing with
1357 // here.
1358 bool useStructRet =
1359 CodeGen::CodeGenFunction::hasAggregateLLVMType(OMD->getResultType());
1360 if (useStructRet) {
1361 ArgTys.push_back(llvm::PointerType::getUnqual(ReturnTy));
1362 ReturnTy = llvm::Type::VoidTy;
1363 }
1364
1365 // Implicit arguments
1366 ArgTys.push_back(SelfTy);
1367 ArgTys.push_back(ObjCTypes.SelectorPtrTy);
1368
1369 for (ObjCMethodDecl::param_const_iterator
1370 i = OMD->param_begin(), e = OMD->param_end();
1371 i != e; ++i) {
1372 const llvm::Type *Ty = CGM.getTypes().ConvertType((*i)->getType());
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001373 if (Ty->isSingleValueType()) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001374 ArgTys.push_back(Ty);
1375 } else {
1376 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
1377 }
1378 }
1379
1380 std::string Name;
1381 GetNameForMethod(OMD, Name);
1382
1383 llvm::Function *Method =
1384 llvm::Function::Create(llvm::FunctionType::get(ReturnTy,
1385 ArgTys,
1386 OMD->isVariadic()),
1387 llvm::GlobalValue::InternalLinkage,
1388 Name,
1389 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001390 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001391
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001392 unsigned Offset = 3; // Return plus self and selector implicit args.
1393 if (useStructRet) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001394 Method->addParamAttr(1, llvm::ParamAttr::StructRet);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001395 ++Offset;
1396 }
1397
1398 // FIXME: This is horrible, we need to be reusing the machinery in
1399 // CodeGenModule.cpp (SetFunctionAttributes).
1400 for (ObjCMethodDecl::param_const_iterator
1401 i = OMD->param_begin(), e = OMD->param_end();
1402 i != e; ++i, ++Offset) {
1403 const llvm::Type *Ty = CGM.getTypes().ConvertType((*i)->getType());
1404 if (!Ty->isSingleValueType())
1405 Method->addParamAttr(Offset, llvm::ParamAttr::ByVal);
1406 }
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001407
1408 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001409}
1410
1411llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001412 // Abuse this interface function as a place to finalize.
1413 FinishModule();
1414
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001415 return NULL;
1416}
1417
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001418llvm::Function *CGObjCMac::EnumerationMutationFunction()
1419{
1420 return ObjCTypes.EnumerationMutationFn;
1421}
1422
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001423void CGObjCMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1424 const ObjCAtTryStmt &S)
1425{
Anders Carlsson80f25672008-09-09 17:59:25 +00001426 // Allocate exception data.
1427 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1428 "exceptiondata.ptr");
1429
1430 // Allocate memory for the rethrow pointer.
1431 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy);
1432 CGF.Builder.CreateStore(llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy),
1433 RethrowPtr);
1434
1435 // Enter a new try block and call setjmp.
1436 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1437 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1438 "jmpbufarray");
1439 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1440 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1441 JmpBufPtr, "result");
1442
1443
1444 llvm::BasicBlock *FinallyBlock = llvm::BasicBlock::Create("finally");
1445
1446 llvm::BasicBlock *TryBlock = llvm::BasicBlock::Create("try");
1447 llvm::BasicBlock *ExceptionInTryBlock =
1448 llvm::BasicBlock::Create("exceptionintry");
1449
1450 // If setjmp returns 1, there was an exception in the @try block.
1451 llvm::Value *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
1452 llvm::Value *IsZero = CGF.Builder.CreateICmpEQ(SetJmpResult, Zero, "iszero");
1453 CGF.Builder.CreateCondBr(IsZero, TryBlock, ExceptionInTryBlock);
1454
1455 // Emit the @try block.
1456 CGF.EmitBlock(TryBlock);
1457 CGF.EmitStmt(S.getTryBody());
1458 CGF.Builder.CreateBr(FinallyBlock);
1459
1460 // Emit the "exception in @try" block.
1461 CGF.EmitBlock(ExceptionInTryBlock);
1462
1463 if (const ObjCAtCatchStmt* CatchStmt = S.getCatchStmts()) {
1464 // Allocate memory for the caught exception and extract it from the
1465 // exception data.
1466 llvm::Value *CaughtPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy);
1467 llvm::Value *Extract = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1468 ExceptionData);
1469 CGF.Builder.CreateStore(Extract, CaughtPtr);
1470
1471 // Enter a new exception try block
1472 // (in case a @catch block throws an exception).
1473 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1474
1475 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1476 JmpBufPtr, "result");
1477
1478
1479 llvm::Value *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
1480 llvm::Value *IsZero = CGF.Builder.CreateICmpEQ(SetJmpResult, Zero,
1481 "iszero");
1482
1483 llvm::BasicBlock *CatchBlock = llvm::BasicBlock::Create("catch");
1484 llvm::BasicBlock *ExceptionInCatchBlock =
1485 llvm::BasicBlock::Create("exceptionincatch");
1486 CGF.Builder.CreateCondBr(IsZero, CatchBlock, ExceptionInCatchBlock);
1487
1488 CGF.EmitBlock(CatchBlock);
1489
1490 // Handle catch list
1491 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
1492 llvm::BasicBlock *NextCatchBlock = llvm::BasicBlock::Create("nextcatch");
1493
1494 QualType T;
1495 bool MatchesAll = false;
1496
1497 // catch(...) always matches.
1498 if (CatchStmt->hasEllipsis())
1499 MatchesAll = true;
1500 else {
1501 const DeclStmt *DS = cast<DeclStmt>(CatchStmt->getCatchParamStmt());
1502 QualType PT = cast<ValueDecl>(DS->getDecl())->getType();
1503 T = PT->getAsPointerType()->getPointeeType();
1504
1505 // catch(id e) always matches.
1506 if (CGF.getContext().isObjCIdType(T))
1507 MatchesAll = true;
1508 }
1509
1510 if (MatchesAll) {
1511 CGF.EmitStmt(CatchStmt->getCatchBody());
1512 CGF.Builder.CreateBr(FinallyBlock);
1513
1514 CGF.EmitBlock(NextCatchBlock);
1515 break;
1516 }
1517
1518 const ObjCInterfaceType *ObjCType = T->getAsPointerToObjCInterfaceType();
1519 assert(ObjCType && "Catch parameter must have Objective-C type!");
1520
1521 // Check if the @catch block matches the exception object.
1522 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
1523
1524 llvm::Value *Caught = CGF.Builder.CreateLoad(CaughtPtr, "caught");
1525 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
1526 Class, Caught, "match");
1527
1528 llvm::Value *DidMatch = CGF.Builder.CreateICmpNE(Match, Zero, "iszero");
1529
1530 llvm::BasicBlock *MatchedBlock = llvm::BasicBlock::Create("matched");
1531
1532 CGF.Builder.CreateCondBr(DidMatch, MatchedBlock, NextCatchBlock);
1533
1534 // Emit the @catch block.
1535 CGF.EmitBlock(MatchedBlock);
1536 CGF.EmitStmt(CatchStmt->getCatchBody());
1537 CGF.Builder.CreateBr(FinallyBlock);
1538
1539 CGF.EmitBlock(NextCatchBlock);
1540 }
1541
1542 // None of the handlers caught the exception, so store it and rethrow
1543 // it later.
1544 llvm::Value *Caught = CGF.Builder.CreateLoad(CaughtPtr, "caught");
1545 CGF.Builder.CreateStore(Caught, RethrowPtr);
1546 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn,
1547 ExceptionData);
1548
1549 CGF.Builder.CreateBr(FinallyBlock);
1550
1551 CGF.EmitBlock(ExceptionInCatchBlock);
1552
1553 Extract = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1554 ExceptionData);
1555 CGF.Builder.CreateStore(Extract, RethrowPtr);
1556 }
1557
1558 // Emit the @finally block.
1559 CGF.EmitBlock(FinallyBlock);
1560
1561 llvm::Value *Rethrow = CGF.Builder.CreateLoad(RethrowPtr);
1562 llvm::Value *ZeroPtr = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
1563
1564 llvm::Value *RethrowIsZero = CGF.Builder.CreateICmpEQ(Rethrow, ZeroPtr);
1565
1566 llvm::BasicBlock *TryExitBlock = llvm::BasicBlock::Create("tryexit");
1567 llvm::BasicBlock *AfterTryExitBlock =
1568 llvm::BasicBlock::Create("aftertryexit");
1569
1570 CGF.Builder.CreateCondBr(RethrowIsZero, TryExitBlock, AfterTryExitBlock);
1571 CGF.EmitBlock(TryExitBlock);
1572 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
1573 CGF.EmitBlock(AfterTryExitBlock);
1574
1575 if (const ObjCAtFinallyStmt* FinallyStmt = S.getFinallyStmt())
1576 CGF.EmitStmt(FinallyStmt->getFinallyBody());
1577
1578 llvm::Value *RethrowIsNotZero = CGF.Builder.CreateICmpNE(Rethrow, ZeroPtr);
1579
1580 llvm::BasicBlock *RethrowBlock = llvm::BasicBlock::Create("rethrow");
1581 llvm::BasicBlock *FinallyEndBlock = llvm::BasicBlock::Create("finallyend");
1582
1583 // If necessary, rethrow the exception.
1584 CGF.Builder.CreateCondBr(RethrowIsNotZero, RethrowBlock, FinallyEndBlock);
1585 CGF.EmitBlock(RethrowBlock);
1586 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, Rethrow);
1587 CGF.EmitBlock(FinallyEndBlock);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001588}
1589
1590void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1591 const ObjCAtThrowStmt &S)
1592{
Anders Carlsson2b1e3112008-09-09 16:16:55 +00001593 llvm::Value *ExceptionAsObject;
1594
1595 if (const Expr *ThrowExpr = S.getThrowExpr()) {
1596 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
1597 ExceptionAsObject =
1598 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
1599 } else {
1600 assert(0 && "FIXME: rethrows not supported!");
1601 }
1602
1603 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00001604 CGF.Builder.CreateUnreachable();
1605 CGF.EmitBlock(llvm::BasicBlock::Create("bb"));
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001606}
1607
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001608/* *** Private Interface *** */
1609
1610/// EmitImageInfo - Emit the image info marker used to encode some module
1611/// level information.
1612///
1613/// See: <rdr://4810609&4810587&4810587>
1614/// struct IMAGE_INFO {
1615/// unsigned version;
1616/// unsigned flags;
1617/// };
1618enum ImageInfoFlags {
1619 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
1620 eImageInfo_GarbageCollected = (1 << 1),
1621 eImageInfo_GCOnly = (1 << 2)
1622};
1623
1624void CGObjCMac::EmitImageInfo() {
1625 unsigned version = 0; // Version is unused?
1626 unsigned flags = 0;
1627
1628 // FIXME: Fix and continue?
1629 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
1630 flags |= eImageInfo_GarbageCollected;
1631 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
1632 flags |= eImageInfo_GCOnly;
1633
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001634 // Emitted as int[2];
1635 llvm::Constant *values[2] = {
1636 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
1637 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
1638 };
1639 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001640 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001641 new llvm::GlobalVariable(AT, true,
1642 llvm::GlobalValue::InternalLinkage,
1643 llvm::ConstantArray::get(AT, values, 2),
1644 "\01L_OBJC_IMAGE_INFO",
1645 &CGM.getModule());
1646
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001647 if (ObjCABI == 1) {
1648 GV->setSection("__OBJC, __image_info,regular");
1649 } else {
1650 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
1651 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001652
1653 UsedGlobals.push_back(GV);
1654}
1655
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001656
1657// struct objc_module {
1658// unsigned long version;
1659// unsigned long size;
1660// const char *name;
1661// Symtab symtab;
1662// };
1663
1664// FIXME: Get from somewhere
1665static const int ModuleVersion = 7;
1666
1667void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001668 uint64_t Size = CGM.getTargetData().getABITypeSize(ObjCTypes.ModuleTy);
1669
1670 std::vector<llvm::Constant*> Values(4);
1671 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
1672 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001673 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001674 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001675 Values[3] = EmitModuleSymbols();
1676
1677 llvm::GlobalVariable *GV =
1678 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
1679 llvm::GlobalValue::InternalLinkage,
1680 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
1681 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001682 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001683 &CGM.getModule());
1684 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
1685 UsedGlobals.push_back(GV);
1686}
1687
1688llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001689 unsigned NumClasses = DefinedClasses.size();
1690 unsigned NumCategories = DefinedCategories.size();
1691
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001692 // Return null if no symbols were defined.
1693 if (!NumClasses && !NumCategories)
1694 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
1695
1696 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001697 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1698 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
1699 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
1700 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
1701
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001702 // The runtime expects exactly the list of defined classes followed
1703 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001704 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001705 for (unsigned i=0; i<NumClasses; i++)
1706 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
1707 ObjCTypes.Int8PtrTy);
1708 for (unsigned i=0; i<NumCategories; i++)
1709 Symbols[NumClasses + i] =
1710 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
1711 ObjCTypes.Int8PtrTy);
1712
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001713 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001714 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001715 NumClasses + NumCategories),
1716 Symbols);
1717
1718 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1719
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001720 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001721 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001722 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001723 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001724 "\01L_OBJC_SYMBOLS",
1725 &CGM.getModule());
1726 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
1727 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001728 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
1729}
1730
1731llvm::Value *CGObjCMac::EmitClassRef(llvm::IRBuilder<> &Builder,
1732 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001733 LazySymbols.insert(ID->getIdentifier());
1734
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001735 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
1736
1737 if (!Entry) {
1738 llvm::Constant *Casted =
1739 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
1740 ObjCTypes.ClassPtrTy);
1741 Entry =
1742 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
1743 llvm::GlobalValue::InternalLinkage,
1744 Casted, "\01L_OBJC_CLASS_REFERENCES_",
1745 &CGM.getModule());
1746 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
1747 UsedGlobals.push_back(Entry);
1748 }
1749
1750 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001751}
1752
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001753llvm::Value *CGObjCMac::EmitSelector(llvm::IRBuilder<> &Builder, Selector Sel) {
1754 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
1755
1756 if (!Entry) {
1757 llvm::Constant *Casted =
1758 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
1759 ObjCTypes.SelectorPtrTy);
1760 Entry =
1761 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
1762 llvm::GlobalValue::InternalLinkage,
1763 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
1764 &CGM.getModule());
1765 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
1766 UsedGlobals.push_back(Entry);
1767 }
1768
1769 return Builder.CreateLoad(Entry, false, "tmp");
1770}
1771
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001772llvm::Constant *CGObjCMac::GetClassName(IdentifierInfo *Ident) {
1773 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001774
1775 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001776 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001777 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001778 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001779 llvm::GlobalValue::InternalLinkage,
1780 C, "\01L_OBJC_CLASS_NAME_",
1781 &CGM.getModule());
1782 Entry->setSection("__TEXT,__cstring,cstring_literals");
1783 UsedGlobals.push_back(Entry);
1784 }
1785
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001786 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001787}
1788
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001789llvm::Constant *CGObjCMac::GetMethodVarName(Selector Sel) {
1790 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
1791
1792 if (!Entry) {
1793 llvm::Constant *C = llvm::ConstantArray::get(Sel.getName());
1794 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001795 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001796 llvm::GlobalValue::InternalLinkage,
1797 C, "\01L_OBJC_METH_VAR_NAME_",
1798 &CGM.getModule());
1799 Entry->setSection("__TEXT,__cstring,cstring_literals");
1800 UsedGlobals.push_back(Entry);
1801 }
1802
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001803 return getConstantGEP(Entry, 0, 0);
1804}
1805
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001806// FIXME: Merge into a single cstring creation function.
1807llvm::Constant *CGObjCMac::GetMethodVarName(IdentifierInfo *ID) {
1808 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
1809}
1810
1811// FIXME: Merge into a single cstring creation function.
1812llvm::Constant *CGObjCMac::GetMethodVarName(const std::string &Name) {
1813 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
1814}
1815
1816llvm::Constant *CGObjCMac::GetMethodVarType(const std::string &Name) {
1817 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001818
1819 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001820 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001821 Entry =
1822 new llvm::GlobalVariable(C->getType(), false,
1823 llvm::GlobalValue::InternalLinkage,
1824 C, "\01L_OBJC_METH_VAR_TYPE_",
1825 &CGM.getModule());
1826 Entry->setSection("__TEXT,__cstring,cstring_literals");
1827 UsedGlobals.push_back(Entry);
1828 }
1829
1830 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001831}
1832
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001833// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001834llvm::Constant *CGObjCMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001835 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001836 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
1837 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001838 return GetMethodVarType(TypeStr);
1839}
1840
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001841// FIXME: Merge into a single cstring creation function.
1842llvm::Constant *CGObjCMac::GetPropertyName(IdentifierInfo *Ident) {
1843 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
1844
1845 if (!Entry) {
1846 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
1847 Entry =
1848 new llvm::GlobalVariable(C->getType(), false,
1849 llvm::GlobalValue::InternalLinkage,
1850 C, "\01L_OBJC_PROP_NAME_ATTR_",
1851 &CGM.getModule());
1852 Entry->setSection("__TEXT,__cstring,cstring_literals");
1853 UsedGlobals.push_back(Entry);
1854 }
1855
1856 return getConstantGEP(Entry, 0, 0);
1857}
1858
1859// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001860// FIXME: This Decl should be more precise.
1861llvm::Constant *CGObjCMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
1862 const Decl *Container) {
1863 std::string TypeStr;
1864 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001865 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
1866}
1867
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001868void CGObjCMac::GetNameForMethod(const ObjCMethodDecl *D,
1869 std::string &NameOut) {
1870 // FIXME: Find the mangling GCC uses.
1871 std::stringstream s;
1872 s << (D->isInstance() ? "-" : "+");
1873 s << "[";
1874 s << D->getClassInterface()->getName();
1875 s << " ";
1876 s << D->getSelector().getName();
1877 s << "]";
1878 NameOut = s.str();
1879}
1880
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001881void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001882 EmitModuleInfo();
1883
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001884 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001885
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001886 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001887 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001888 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001889 }
1890
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001891 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001892 llvm::GlobalValue *GV =
1893 new llvm::GlobalVariable(AT, false,
1894 llvm::GlobalValue::AppendingLinkage,
1895 llvm::ConstantArray::get(AT, Used),
1896 "llvm.used",
1897 &CGM.getModule());
1898
1899 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001900
1901 // Add assembler directives to add lazy undefined symbol references
1902 // for classes which are referenced but not defined. This is
1903 // important for correct linker interaction.
1904
1905 // FIXME: Uh, this isn't particularly portable.
1906 std::stringstream s;
1907 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
1908 e = LazySymbols.end(); i != e; ++i) {
1909 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
1910 }
1911 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
1912 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001913 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001914 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
1915 }
1916 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001917}
1918
1919/* *** */
1920
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001921ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Daniel Dunbar3e9df992008-08-23 18:37:06 +00001922 : CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001923{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001924 CodeGen::CodeGenTypes &Types = CGM.getTypes();
1925 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001926
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001927 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001928 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001929 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001930 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1931
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001932 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
1933 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001934
1935 // FIXME: It would be nice to unify this with the opaque type, so
1936 // that the IR comes out a bit cleaner.
1937 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
1938 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001939
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001940 MethodDescriptionTy =
1941 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001942 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001943 NULL);
1944 CGM.getModule().addTypeName("struct._objc_method_description",
1945 MethodDescriptionTy);
1946
1947 MethodDescriptionListTy =
1948 llvm::StructType::get(IntTy,
1949 llvm::ArrayType::get(MethodDescriptionTy, 0),
1950 NULL);
1951 CGM.getModule().addTypeName("struct._objc_method_description_list",
1952 MethodDescriptionListTy);
1953 MethodDescriptionListPtrTy =
1954 llvm::PointerType::getUnqual(MethodDescriptionListTy);
1955
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001956 PropertyTy = llvm::StructType::get(Int8PtrTy,
1957 Int8PtrTy,
1958 NULL);
1959 CGM.getModule().addTypeName("struct._objc_property",
1960 PropertyTy);
1961
1962 PropertyListTy = llvm::StructType::get(IntTy,
1963 IntTy,
1964 llvm::ArrayType::get(PropertyTy, 0),
1965 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001966 CGM.getModule().addTypeName("struct._objc_property_list",
1967 PropertyListTy);
1968 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
1969
1970 // Protocol description structures
1971
1972 ProtocolExtensionTy =
1973 llvm::StructType::get(Types.ConvertType(Ctx.IntTy),
1974 llvm::PointerType::getUnqual(MethodDescriptionListTy),
1975 llvm::PointerType::getUnqual(MethodDescriptionListTy),
1976 PropertyListPtrTy,
1977 NULL);
1978 CGM.getModule().addTypeName("struct._objc_protocol_extension",
1979 ProtocolExtensionTy);
1980 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
1981
1982 // Handle recursive construction of Protocl and ProtocolList types
1983
1984 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
1985 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
1986
1987 T = llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
1988 LongTy,
1989 llvm::ArrayType::get(ProtocolTyHolder, 0),
1990 NULL);
1991 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
1992
1993 T = llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolExtensionTy),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001994 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001995 llvm::PointerType::getUnqual(ProtocolListTyHolder),
1996 MethodDescriptionListPtrTy,
1997 MethodDescriptionListPtrTy,
1998 NULL);
1999 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2000
2001 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2002 CGM.getModule().addTypeName("struct._objc_protocol_list",
2003 ProtocolListTy);
2004 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2005
2006 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
2007 CGM.getModule().addTypeName("struct.__objc_protocol", ProtocolTy);
2008 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002009
2010 // Class description structures
2011
2012 IvarTy = llvm::StructType::get(Int8PtrTy,
2013 Int8PtrTy,
2014 IntTy,
2015 NULL);
2016 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2017
2018 IvarListTy = llvm::OpaqueType::get();
2019 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2020 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2021
2022 MethodTy = llvm::StructType::get(SelectorPtrTy,
2023 Int8PtrTy,
2024 Int8PtrTy,
2025 NULL);
2026 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
2027
2028 MethodListTy = llvm::OpaqueType::get();
2029 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2030 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2031
2032 CacheTy = llvm::OpaqueType::get();
2033 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2034 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
2035
2036 ClassExtensionTy =
2037 llvm::StructType::get(IntTy,
2038 Int8PtrTy,
2039 PropertyListPtrTy,
2040 NULL);
2041 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2042 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2043
2044 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2045
2046 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2047 llvm::PointerType::getUnqual(ClassTyHolder),
2048 Int8PtrTy,
2049 LongTy,
2050 LongTy,
2051 LongTy,
2052 IvarListPtrTy,
2053 MethodListPtrTy,
2054 CachePtrTy,
2055 ProtocolListPtrTy,
2056 Int8PtrTy,
2057 ClassExtensionPtrTy,
2058 NULL);
2059 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2060
2061 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2062 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2063 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2064
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002065 CategoryTy = llvm::StructType::get(Int8PtrTy,
2066 Int8PtrTy,
2067 MethodListPtrTy,
2068 MethodListPtrTy,
2069 ProtocolListPtrTy,
2070 IntTy,
2071 PropertyListPtrTy,
2072 NULL);
2073 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2074
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00002075 // I'm not sure I like this. The implicit coordination is a bit
2076 // gross. We should solve this in a reasonable fashion because this
2077 // is a pretty common task (match some runtime data structure with
2078 // an LLVM data structure).
2079
2080 // FIXME: This is leaked.
2081 // FIXME: Merge with rewriter code?
2082 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2083 SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00002084 &Ctx.Idents.get("_objc_super"));
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00002085 FieldDecl *FieldDecls[2];
2086 FieldDecls[0] = FieldDecl::Create(Ctx, SourceLocation(), 0,
2087 Ctx.getObjCIdType());
2088 FieldDecls[1] = FieldDecl::Create(Ctx, SourceLocation(), 0,
2089 Ctx.getObjCClassType());
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002090 RD->defineBody(Ctx, FieldDecls, 2);
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00002091
2092 SuperCTy = Ctx.getTagDeclType(RD);
2093 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2094
2095 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002096 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
Daniel Dunbare8b470d2008-08-23 04:28:29 +00002097
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002098 // Global metadata structures
2099
2100 SymtabTy = llvm::StructType::get(LongTy,
2101 SelectorPtrTy,
2102 ShortTy,
2103 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002104 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002105 NULL);
2106 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2107 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2108
2109 ModuleTy =
2110 llvm::StructType::get(LongTy,
2111 LongTy,
2112 Int8PtrTy,
2113 SymtabPtrTy,
2114 NULL);
2115 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002116
2117 // Message send functions
2118
2119 std::vector<const llvm::Type*> Params;
2120 Params.push_back(ObjectPtrTy);
2121 Params.push_back(SelectorPtrTy);
2122 MessageSendFn = llvm::Function::Create(llvm::FunctionType::get(ObjectPtrTy,
2123 Params,
2124 true),
2125 llvm::Function::ExternalLinkage,
2126 "objc_msgSend",
2127 &CGM.getModule());
2128
2129 Params.clear();
2130 Params.push_back(Int8PtrTy);
2131 Params.push_back(ObjectPtrTy);
2132 Params.push_back(SelectorPtrTy);
2133 MessageSendStretFn =
2134 llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy,
2135 Params,
2136 true),
2137 llvm::Function::ExternalLinkage,
2138 "objc_msgSend_stret",
2139 &CGM.getModule());
2140
2141 Params.clear();
2142 Params.push_back(SuperPtrTy);
2143 Params.push_back(SelectorPtrTy);
2144 MessageSendSuperFn =
2145 llvm::Function::Create(llvm::FunctionType::get(ObjectPtrTy,
2146 Params,
2147 true),
2148 llvm::Function::ExternalLinkage,
2149 "objc_msgSendSuper",
2150 &CGM.getModule());
2151
2152 Params.clear();
2153 Params.push_back(Int8PtrTy);
2154 Params.push_back(SuperPtrTy);
2155 Params.push_back(SelectorPtrTy);
2156 MessageSendSuperStretFn =
2157 llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy,
2158 Params,
2159 true),
2160 llvm::Function::ExternalLinkage,
2161 "objc_msgSendSuper_stret",
2162 &CGM.getModule());
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002163
2164 // Enumeration mutation.
2165
2166 Params.clear();
2167 Params.push_back(ObjectPtrTy);
2168 EnumerationMutationFn =
2169 llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy,
2170 Params,
2171 false),
2172 llvm::Function::ExternalLinkage,
2173 "objc_enumerationMutation",
2174 &CGM.getModule());
Anders Carlsson124526b2008-09-09 10:10:21 +00002175
2176 // FIXME: This is the size of the setjmp buffer and should be
2177 // target specific. 18 is what's used on 32-bit X86.
2178 uint64_t SetJmpBufferSize = 18;
2179
2180 // Exceptions
2181 const llvm::Type *StackPtrTy =
2182 llvm::PointerType::getUnqual(llvm::ArrayType::get(llvm::Type::Int8Ty, 4));
2183
2184 ExceptionDataTy =
2185 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
2186 SetJmpBufferSize),
2187 StackPtrTy, NULL);
2188 CGM.getModule().addTypeName("struct._objc_exception_data",
2189 ExceptionDataTy);
2190
2191 Params.clear();
2192 Params.push_back(ObjectPtrTy);
2193 ExceptionThrowFn =
2194 llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy,
2195 Params,
2196 false),
2197 llvm::Function::ExternalLinkage,
2198 "objc_exception_throw",
2199 &CGM.getModule());
2200
2201 Params.clear();
2202 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
2203 ExceptionTryEnterFn =
2204 llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy,
2205 Params,
2206 false),
2207 llvm::Function::ExternalLinkage,
2208 "objc_exception_try_enter",
2209 &CGM.getModule());
2210 ExceptionTryExitFn =
2211 llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy,
2212 Params,
2213 false),
2214 llvm::Function::ExternalLinkage,
2215 "objc_exception_try_exit",
2216 &CGM.getModule());
2217 ExceptionExtractFn =
2218 llvm::Function::Create(llvm::FunctionType::get(ObjectPtrTy,
2219 Params,
2220 false),
2221 llvm::Function::ExternalLinkage,
2222 "objc_exception_extract",
2223 &CGM.getModule());
2224
2225 Params.clear();
2226 Params.push_back(ClassPtrTy);
2227 Params.push_back(ObjectPtrTy);
2228 ExceptionMatchFn =
2229 llvm::Function::Create(llvm::FunctionType::get(llvm::Type::Int32Ty,
2230 Params,
2231 false),
2232 llvm::Function::ExternalLinkage,
2233 "objc_exception_match",
2234 &CGM.getModule());
2235
2236 Params.clear();
2237 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
2238 SetJmpFn =
2239 llvm::Function::Create(llvm::FunctionType::get(llvm::Type::Int32Ty,
2240 Params,
2241 false),
2242 llvm::Function::ExternalLinkage,
2243 "_setjmp",
2244 &CGM.getModule());
2245
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002246}
2247
2248ObjCTypesHelper::~ObjCTypesHelper() {
2249}
2250
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002251llvm::Value *ObjCTypesHelper::getMessageSendFn(bool IsSuper,
2252 const llvm::Type *ReturnTy) {
2253 llvm::Function *F;
2254 llvm::FunctionType *CallFTy;
2255
2256 // FIXME: Should we be caching any of this?
2257 if (!ReturnTy->isSingleValueType()) {
2258 F = IsSuper ? MessageSendSuperStretFn : MessageSendStretFn;
2259 std::vector<const llvm::Type*> Params(3);
2260 Params[0] = llvm::PointerType::getUnqual(ReturnTy);
2261 Params[1] = IsSuper ? SuperPtrTy : ObjectPtrTy;
2262 Params[2] = SelectorPtrTy;
2263 CallFTy = llvm::FunctionType::get(llvm::Type::VoidTy, Params, true);
Daniel Dunbar662174c82008-08-29 17:28:43 +00002264 } else { // FIXME: floating point?
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002265 F = IsSuper ? MessageSendSuperFn : MessageSendFn;
2266 std::vector<const llvm::Type*> Params(2);
2267 Params[0] = IsSuper ? SuperPtrTy : ObjectPtrTy;
2268 Params[1] = SelectorPtrTy;
2269 CallFTy = llvm::FunctionType::get(ReturnTy, Params, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002270 }
2271
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002272 return llvm::ConstantExpr::getBitCast(F,
2273 llvm::PointerType::getUnqual(CallFTy));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00002274}
2275
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002276/* *** */
2277
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002278CodeGen::CGObjCRuntime *
2279CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002280 return new CGObjCMac(CGM);
2281}