blob: 6c35041b97146b52de6ec1142e33e2b5e98409f4 [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 Dunbar0c0e7a62008-10-29 22:36:39 +000024#include "llvm/ADT/DenseSet.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
Fariborz Jahanianee0af742009-01-21 22:04:16 +000038class ObjCCommonTypesHelper {
39protected:
40 CodeGen::CodeGenModule &CGM;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000041
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000042public:
Daniel Dunbar27f9d772008-08-21 04:36:09 +000043 const llvm::Type *ShortTy, *IntTy, *LongTy;
44 const llvm::Type *Int8PtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000045
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000046 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
47 const llvm::Type *ObjectPtrTy;
Fariborz Jahanian6d657c42008-11-18 20:18:11 +000048
49 /// PtrObjectPtrTy - LLVM type for id *
50 const llvm::Type *PtrObjectPtrTy;
51
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000052 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000053 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000054 /// ProtocolPtrTy - LLVM type for external protocol handles
55 /// (typeof(Protocol))
56 const llvm::Type *ExternalProtocolPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000057
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000058 // SuperCTy - clang type for struct objc_super.
59 QualType SuperCTy;
60 // SuperPtrCTy - clang type for struct objc_super *.
61 QualType SuperPtrCTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000062
Daniel Dunbare8b470d2008-08-23 04:28:29 +000063 /// SuperTy - LLVM type for struct objc_super.
64 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +000065 /// SuperPtrTy - LLVM type for struct objc_super *.
66 const llvm::Type *SuperPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000067
Fariborz Jahanian30bc5712009-01-22 23:02:58 +000068 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
69 /// in GCC parlance).
70 const llvm::StructType *PropertyTy;
71
72 /// PropertyListTy - LLVM type for struct objc_property_list
73 /// (_prop_list_t in GCC parlance).
74 const llvm::StructType *PropertyListTy;
75 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
76 const llvm::Type *PropertyListPtrTy;
77
78 // MethodTy - LLVM type for struct objc_method.
79 const llvm::StructType *MethodTy;
80
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +000081 /// CacheTy - LLVM type for struct objc_cache.
82 const llvm::Type *CacheTy;
83 /// CachePtrTy - LLVM type for struct objc_cache *.
84 const llvm::Type *CachePtrTy;
85
Fariborz Jahaniandb286862009-01-22 00:37:21 +000086 llvm::Function *GetPropertyFn, *SetPropertyFn;
87
88 llvm::Function *EnumerationMutationFn;
89
90 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
91 llvm::Function *GcReadWeakFn;
92
93 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
94 llvm::Function *GcAssignWeakFn;
95
96 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
97 llvm::Function *GcAssignGlobalFn;
98
99 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
100 llvm::Function *GcAssignIvarFn;
101
102 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
103 llvm::Function *GcAssignStrongCastFn;
104
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000105 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
106 ~ObjCCommonTypesHelper(){}
107};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000108
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000109/// ObjCTypesHelper - Helper class that encapsulates lazy
110/// construction of varies types used during ObjC generation.
111class ObjCTypesHelper : public ObjCCommonTypesHelper {
112private:
113
114 llvm::Function *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn;
115 llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn,
116 *MessageSendSuperFpretFn;
117
118public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000119 /// SymtabTy - LLVM type for struct objc_symtab.
120 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000121 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
122 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000123 /// ModuleTy - LLVM type for struct objc_module.
124 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000125
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000126 /// ProtocolTy - LLVM type for struct objc_protocol.
127 const llvm::StructType *ProtocolTy;
128 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
129 const llvm::Type *ProtocolPtrTy;
130 /// ProtocolExtensionTy - LLVM type for struct
131 /// objc_protocol_extension.
132 const llvm::StructType *ProtocolExtensionTy;
133 /// ProtocolExtensionTy - LLVM type for struct
134 /// objc_protocol_extension *.
135 const llvm::Type *ProtocolExtensionPtrTy;
136 /// MethodDescriptionTy - LLVM type for struct
137 /// objc_method_description.
138 const llvm::StructType *MethodDescriptionTy;
139 /// MethodDescriptionListTy - LLVM type for struct
140 /// objc_method_description_list.
141 const llvm::StructType *MethodDescriptionListTy;
142 /// MethodDescriptionListPtrTy - LLVM type for struct
143 /// objc_method_description_list *.
144 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000145 /// ProtocolListTy - LLVM type for struct objc_property_list.
146 const llvm::Type *ProtocolListTy;
147 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
148 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000149 /// CategoryTy - LLVM type for struct objc_category.
150 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000151 /// ClassTy - LLVM type for struct objc_class.
152 const llvm::StructType *ClassTy;
153 /// ClassPtrTy - LLVM type for struct objc_class *.
154 const llvm::Type *ClassPtrTy;
155 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
156 const llvm::StructType *ClassExtensionTy;
157 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
158 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000159 // IvarTy - LLVM type for struct objc_ivar.
160 const llvm::StructType *IvarTy;
161 /// IvarListTy - LLVM type for struct objc_ivar_list.
162 const llvm::Type *IvarListTy;
163 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
164 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000165 /// MethodListTy - LLVM type for struct objc_method_list.
166 const llvm::Type *MethodListTy;
167 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
168 const llvm::Type *MethodListPtrTy;
Anders Carlsson124526b2008-09-09 10:10:21 +0000169
170 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
171 const llvm::Type *ExceptionDataTy;
172
173 /// ExceptionThrowFn - LLVM objc_exception_throw function.
174 llvm::Function *ExceptionThrowFn;
175
176 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
177 llvm::Function *ExceptionTryEnterFn;
178
179 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
180 llvm::Function *ExceptionTryExitFn;
181
182 /// ExceptionExtractFn - LLVM objc_exception_extract function.
183 llvm::Function *ExceptionExtractFn;
184
185 /// ExceptionMatchFn - LLVM objc_exception_match function.
186 llvm::Function *ExceptionMatchFn;
187
188 /// SetJmpFn - LLVM _setjmp function.
189 llvm::Function *SetJmpFn;
Chris Lattner10cac6f2008-11-15 21:26:17 +0000190
191 /// SyncEnterFn - LLVM object_sync_enter function.
192 llvm::Function *SyncEnterFn;
193
194 /// SyncExitFn - LLVM object_sync_exit function.
195 llvm::Function *SyncExitFn;
196
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000197public:
198 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000199 ~ObjCTypesHelper() {}
Daniel Dunbar5669e572008-10-17 03:24:53 +0000200
201
202 llvm::Function *getSendFn(bool IsSuper) {
203 return IsSuper ? MessageSendSuperFn : MessageSendFn;
204 }
205
206 llvm::Function *getSendStretFn(bool IsSuper) {
207 return IsSuper ? MessageSendSuperStretFn : MessageSendStretFn;
208 }
209
210 llvm::Function *getSendFpretFn(bool IsSuper) {
211 return IsSuper ? MessageSendSuperFpretFn : MessageSendFpretFn;
212 }
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000213};
214
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000215/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000216/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000217class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000218public:
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000219 // MethodListnfABITy - LLVM for struct _method_list_t
220 const llvm::StructType *MethodListnfABITy;
221
222 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
223 const llvm::Type *MethodListnfABIPtrTy;
224
225 // ProtocolnfABITy = LLVM for struct _protocol_t
226 const llvm::StructType *ProtocolnfABITy;
227
228 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
229 const llvm::StructType *ProtocolListnfABITy;
230
231 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
232 const llvm::Type *ProtocolListnfABIPtrTy;
233
234 // ClassnfABITy - LLVM for struct _class_t
235 const llvm::StructType *ClassnfABITy;
236
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000237 // ClassnfABIPtrTy - LLVM for struct _class_t*
238 const llvm::Type *ClassnfABIPtrTy;
239
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000240 // IvarnfABITy - LLVM for struct _ivar_t
241 const llvm::StructType *IvarnfABITy;
242
243 // IvarListnfABITy - LLVM for struct _ivar_list_t
244 const llvm::StructType *IvarListnfABITy;
245
246 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
247 const llvm::Type *IvarListnfABIPtrTy;
248
249 // ClassRonfABITy - LLVM for struct _class_ro_t
250 const llvm::StructType *ClassRonfABITy;
251
252 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
253 const llvm::Type *ImpnfABITy;
254
255 // CategorynfABITy - LLVM for struct _category_t
256 const llvm::StructType *CategorynfABITy;
257
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000258 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
259 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000260};
261
262class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
263protected:
264 CodeGen::CodeGenModule &CGM;
265 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000266 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000267
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000268 /// LazySymbols - Symbols to generate a lazy reference for. See
269 /// DefinedSymbols and FinishModule().
270 std::set<IdentifierInfo*> LazySymbols;
271
272 /// DefinedSymbols - External symbols which are defined by this
273 /// module. The symbols in this list and LazySymbols are used to add
274 /// special linker symbols which ensure that Objective-C modules are
275 /// linked properly.
276 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000277
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000278 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000279 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000280
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000281 /// MethodVarNames - uniqued method variable names.
282 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000283
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000284 /// MethodVarTypes - uniqued method type signatures. We have to use
285 /// a StringMap here because have no other unique reference.
286 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000287
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000288 /// MethodDefinitions - map of methods which have been defined in
289 /// this translation unit.
290 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000291
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000292 /// PropertyNames - uniqued method variable names.
293 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000294
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000295 /// ClassReferences - uniqued class references.
296 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000297
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000298 /// SelectorReferences - uniqued selector references.
299 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000300
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000301 /// Protocols - Protocols for which an objc_protocol structure has
302 /// been emitted. Forward declarations are handled by creating an
303 /// empty structure whose initializer is filled in when/if defined.
304 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000305
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000306 /// DefinedProtocols - Protocols which have actually been
307 /// defined. We should not need this, see FIXME in GenerateProtocol.
308 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000309
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000310 /// DefinedClasses - List of defined classes.
311 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000312
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000313 /// DefinedCategories - List of defined categories.
314 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000315
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000316 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000317 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000318 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000319
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000320 /// GetNameForMethod - Return a name for the given method.
321 /// \param[out] NameOut - The return value.
322 void GetNameForMethod(const ObjCMethodDecl *OMD,
323 const ObjCContainerDecl *CD,
324 std::string &NameOut);
325
326 /// GetMethodVarName - Return a unique constant for the given
327 /// selector's name. The return value has type char *.
328 llvm::Constant *GetMethodVarName(Selector Sel);
329 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
330 llvm::Constant *GetMethodVarName(const std::string &Name);
331
332 /// GetMethodVarType - Return a unique constant for the given
333 /// selector's name. The return value has type char *.
334
335 // FIXME: This is a horrible name.
336 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
337 llvm::Constant *GetMethodVarType(const std::string &Name);
338
339 /// GetPropertyName - Return a unique constant for the given
340 /// name. The return value has type char *.
341 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
342
343 // FIXME: This can be dropped once string functions are unified.
344 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
345 const Decl *Container);
346
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000347 /// GetClassName - Return a unique constant for the given selector's
348 /// name. The return value has type char *.
349 llvm::Constant *GetClassName(IdentifierInfo *Ident);
350
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000351 const RecordDecl *GetFirstIvarInRecord(const ObjCInterfaceDecl *OID,
352 RecordDecl::field_iterator &FIV,
353 RecordDecl::field_iterator &PIV);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000354 /// EmitPropertyList - Emit the given property list. The return
355 /// value has type PropertyListPtrTy.
356 llvm::Constant *EmitPropertyList(const std::string &Name,
357 const Decl *Container,
358 const ObjCContainerDecl *OCD,
359 const ObjCCommonTypesHelper &ObjCTypes);
360
Fariborz Jahanianda320092009-01-29 19:24:30 +0000361 /// GetProtocolRef - Return a reference to the internal protocol
362 /// description, creating an empty one if it has not been
363 /// defined. The return value has type ProtocolPtrTy.
364 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
365
Fariborz Jahanian014858b2009-01-30 23:15:42 +0000366 /// GetPointerAlign - get alignment of a pointer.
367 uint32_t GetPointerAlign(void) {
368 return CGM.getContext().getTypeAlign(CGM.getContext().VoidPtrTy) >> 3;
369 }
370
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000371public:
372 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
373 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000374
375 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000376
377 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
378 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000379
380 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
381
382 /// GetOrEmitProtocol - Get the protocol object for the given
383 /// declaration, emitting it if necessary. The return value has type
384 /// ProtocolPtrTy.
385 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
386
387 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
388 /// object for the given declaration, emitting it if needed. These
389 /// forward references will be filled in with empty bodies if no
390 /// definition is seen. The return value has type ProtocolPtrTy.
391 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000392};
393
394class CGObjCMac : public CGObjCCommonMac {
395private:
396 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000397 /// EmitImageInfo - Emit the image info marker used to encode some module
398 /// level information.
399 void EmitImageInfo();
400
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000401 /// EmitModuleInfo - Another marker encoding module level
402 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000403 void EmitModuleInfo();
404
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000405 /// EmitModuleSymols - Emit module symbols, the list of defined
406 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000407 llvm::Constant *EmitModuleSymbols();
408
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000409 /// FinishModule - Write out global data structures at the end of
410 /// processing a translation unit.
411 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000412
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000413 /// EmitClassExtension - Generate the class extension structure used
414 /// to store the weak ivar layout and properties. The return value
415 /// has type ClassExtensionPtrTy.
416 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
417
418 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
419 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000420 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000421 const ObjCInterfaceDecl *ID);
422
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000423 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000424 QualType ResultType,
425 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000426 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000427 QualType Arg0Ty,
428 bool IsSuper,
429 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000430
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000431 /// EmitIvarList - Emit the ivar list for the given
432 /// implementation. If ForClass is true the list of class ivars
433 /// (i.e. metaclass ivars) is emitted, otherwise the list of
434 /// interface ivars will be emitted. The return value has type
435 /// IvarListPtrTy.
436 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000437 bool ForClass);
438
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000439 /// EmitMetaClass - Emit a forward reference to the class structure
440 /// for the metaclass of the given interface. The return value has
441 /// type ClassPtrTy.
442 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
443
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000444 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000445 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000446 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
447 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000448 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000449 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000450
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000451 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000452
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000453 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000454
455 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000456 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000457 llvm::Constant *EmitMethodList(const std::string &Name,
458 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000459 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000460
461 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000462 /// method declarations.
463 /// - TypeName: The name for the type containing the methods.
464 /// - IsProtocol: True iff these methods are for a protocol.
465 /// - ClassMethds: True iff these are class methods.
466 /// - Required: When true, only "required" methods are
467 /// listed. Similarly, when false only "optional" methods are
468 /// listed. For classes this should always be true.
469 /// - begin, end: The method list to output.
470 ///
471 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000472 llvm::Constant *EmitMethodDescList(const std::string &Name,
473 const char *Section,
474 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000475
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000476 /// GetOrEmitProtocol - Get the protocol object for the given
477 /// declaration, emitting it if necessary. The return value has type
478 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000479 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000480
481 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
482 /// object for the given declaration, emitting it if needed. These
483 /// forward references will be filled in with empty bodies if no
484 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000485 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000486
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000487 /// EmitProtocolExtension - Generate the protocol extension
488 /// structure used to store optional instance and class methods, and
489 /// protocol properties. The return value has type
490 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000491 llvm::Constant *
492 EmitProtocolExtension(const ObjCProtocolDecl *PD,
493 const ConstantVector &OptInstanceMethods,
494 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000495
496 /// EmitProtocolList - Generate the list of referenced
497 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +0000498 llvm::Constant *EmitProtocolList(const std::string &Name,
499 ObjCProtocolDecl::protocol_iterator begin,
500 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000501
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000502 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
503 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000504 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000505
Fariborz Jahanianda320092009-01-29 19:24:30 +0000506 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000507 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000508
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000509 virtual llvm::Function *ModuleInitFunction();
510
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000511 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000512 QualType ResultType,
513 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000514 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000515 bool IsClassMessage,
516 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000517
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000518 virtual CodeGen::RValue
519 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000520 QualType ResultType,
521 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000522 const ObjCInterfaceDecl *Class,
523 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000524 bool IsClassMessage,
525 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000526
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000527 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000528 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000529
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000530 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000531
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000532 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000533
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000534 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000535
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000536 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000537 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000538
Daniel Dunbar49f66022008-09-24 03:38:44 +0000539 virtual llvm::Function *GetPropertyGetFunction();
540 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000541 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000542
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000543 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
544 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000545 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
546 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000547 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000548 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000549 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
550 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000551 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
552 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000553 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
554 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000555 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
556 llvm::Value *src, llvm::Value *dest);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000557};
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000558
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000559class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000560private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000561 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000562 llvm::GlobalVariable* ObjCEmptyCacheVar;
563 llvm::GlobalVariable* ObjCEmptyVtableVar;
564
565 /// FinishNonFragileABIModule - Write out global data structures at the end of
566 /// processing a translation unit.
567 void FinishNonFragileABIModule();
568
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000569 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
570 unsigned InstanceStart,
571 unsigned InstanceSize,
572 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +0000573 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
574 llvm::Constant *IsAGV,
575 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +0000576 llvm::Constant *ClassRoGV,
577 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000578
579 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
580
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +0000581 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
582
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000583 /// EmitMethodList - Emit the method list for the given
584 /// implementation. The return value has type MethodListnfABITy.
585 llvm::Constant *EmitMethodList(const std::string &Name,
586 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +0000587 const ConstantVector &Methods);
588 /// EmitIvarList - Emit the ivar list for the given
589 /// implementation. If ForClass is true the list of class ivars
590 /// (i.e. metaclass ivars) is emitted, otherwise the list of
591 /// interface ivars will be emitted. The return value has type
592 /// IvarListnfABIPtrTy.
593 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000594
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000595 llvm::Constant *EmitIvarOffsetVar(const ObjCImplementationDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +0000596 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000597 unsigned long int offset);
598
Fariborz Jahanianda320092009-01-29 19:24:30 +0000599 /// GetOrEmitProtocol - Get the protocol object for the given
600 /// declaration, emitting it if necessary. The return value has type
601 /// ProtocolPtrTy.
602 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
603
604 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
605 /// object for the given declaration, emitting it if needed. These
606 /// forward references will be filled in with empty bodies if no
607 /// definition is seen. The return value has type ProtocolPtrTy.
608 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
609
610 /// EmitProtocolList - Generate the list of referenced
611 /// protocols. The return value has type ProtocolListPtrTy.
612 llvm::Constant *EmitProtocolList(const std::string &Name,
613 ObjCProtocolDecl::protocol_iterator begin,
614 ObjCProtocolDecl::protocol_iterator end);
615
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000616public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000617 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000618 // FIXME. All stubs for now!
619 virtual llvm::Function *ModuleInitFunction();
620
621 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
622 QualType ResultType,
623 Selector Sel,
624 llvm::Value *Receiver,
625 bool IsClassMessage,
626 const CallArgList &CallArgs)
627 {return RValue::get(0);}
628
629 virtual CodeGen::RValue
630 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
631 QualType ResultType,
632 Selector Sel,
633 const ObjCInterfaceDecl *Class,
634 llvm::Value *Receiver,
635 bool IsClassMessage,
636 const CallArgList &CallArgs){ return RValue::get(0);}
637
638 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
639 const ObjCInterfaceDecl *ID){ return 0; }
640
641 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
642 { return 0; }
643
Fariborz Jahanianeb062d92009-01-26 18:32:24 +0000644 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000645
646 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000647 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +0000648 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000649
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000650 virtual llvm::Function *GetPropertyGetFunction(){ return 0; }
651 virtual llvm::Function *GetPropertySetFunction()
652 { return 0; }
653 virtual llvm::Function *EnumerationMutationFunction()
654 { return 0; }
655
656 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
657 const Stmt &S)
658 { return; }
659 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
660 const ObjCAtThrowStmt &S)
661 { return; }
662 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
663 llvm::Value *AddrWeakObj)
664 { return 0; }
665 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
666 llvm::Value *src, llvm::Value *dst)
667 { return; }
668 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
669 llvm::Value *src, llvm::Value *dest)
670 { return; }
671 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
672 llvm::Value *src, llvm::Value *dest)
673 { return; }
674 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
675 llvm::Value *src, llvm::Value *dest)
676 { return; }
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000677};
678
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000679} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000680
681/* *** Helper Functions *** */
682
683/// getConstantGEP() - Help routine to construct simple GEPs.
684static llvm::Constant *getConstantGEP(llvm::Constant *C,
685 unsigned idx0,
686 unsigned idx1) {
687 llvm::Value *Idxs[] = {
688 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
689 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
690 };
691 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
692}
693
694/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000695
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000696CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
697 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000698{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000699 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000700 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000701}
702
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000703/// GetClass - Return a reference to the class for the given interface
704/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000705llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000706 const ObjCInterfaceDecl *ID) {
707 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000708}
709
710/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000711llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000712 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000713}
714
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000715/// Generate a constant CFString object.
716/*
717 struct __builtin_CFString {
718 const int *isa; // point to __CFConstantStringClassReference
719 int flags;
720 const char *str;
721 long length;
722 };
723*/
724
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000725llvm::Constant *CGObjCCommonMac::GenerateConstantString(
726 const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000727 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000728}
729
730/// Generates a message send where the super is the receiver. This is
731/// a message send to self with special delivery semantics indicating
732/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000733CodeGen::RValue
734CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000735 QualType ResultType,
736 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000737 const ObjCInterfaceDecl *Class,
738 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000739 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000740 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000741 // Create and init a super structure; this is a (receiver, class)
742 // pair we will pass to objc_msgSendSuper.
743 llvm::Value *ObjCSuper =
744 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
745 llvm::Value *ReceiverAsObject =
746 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
747 CGF.Builder.CreateStore(ReceiverAsObject,
748 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000749
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000750 // If this is a class message the metaclass is passed as the target.
751 llvm::Value *Target;
752 if (IsClassMessage) {
753 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
754 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
755 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
756 Target = Super;
757 } else {
758 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
759 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000760 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
761 // and ObjCTypes types.
762 const llvm::Type *ClassTy =
763 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000764 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000765 CGF.Builder.CreateStore(Target,
766 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
767
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000768 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000769 ObjCSuper, ObjCTypes.SuperPtrCTy,
770 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000771}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000772
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000773/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000774CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000775 QualType ResultType,
776 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000777 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000778 bool IsClassMessage,
779 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000780 llvm::Value *Arg0 =
781 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000782 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000783 Arg0, CGF.getContext().getObjCIdType(),
784 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000785}
786
787CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000788 QualType ResultType,
789 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000790 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000791 QualType Arg0Ty,
792 bool IsSuper,
793 const CallArgList &CallArgs) {
794 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000795 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
796 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
797 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000798 CGF.getContext().getObjCSelType()));
799 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000800
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000801 const llvm::FunctionType *FTy =
802 CGM.getTypes().GetFunctionType(CGCallInfo(ResultType, ActualArgs),
803 false);
Daniel Dunbar5669e572008-10-17 03:24:53 +0000804
805 llvm::Constant *Fn;
806 if (CGM.ReturnTypeUsesSret(ResultType)) {
807 Fn = ObjCTypes.getSendStretFn(IsSuper);
808 } else if (ResultType->isFloatingType()) {
809 // FIXME: Sadly, this is wrong. This actually depends on the
810 // architecture. This happens to be right for x86-32 though.
811 Fn = ObjCTypes.getSendFpretFn(IsSuper);
812 } else {
813 Fn = ObjCTypes.getSendFn(IsSuper);
814 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000815 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar3913f182008-09-09 23:48:28 +0000816 return CGF.EmitCall(Fn, ResultType, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000817}
818
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000819llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000820 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +0000821 // FIXME: I don't understand why gcc generates this, or where it is
822 // resolved. Investigate. Its also wasteful to look this up over and
823 // over.
824 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
825
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000826 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
827 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000828}
829
Fariborz Jahanianda320092009-01-29 19:24:30 +0000830void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000831 // FIXME: We shouldn't need this, the protocol decl should contain
832 // enough information to tell us whether this was a declaration or a
833 // definition.
834 DefinedProtocols.insert(PD->getIdentifier());
835
836 // If we have generated a forward reference to this protocol, emit
837 // it now. Otherwise do nothing, the protocol objects are lazily
838 // emitted.
839 if (Protocols.count(PD->getIdentifier()))
840 GetOrEmitProtocol(PD);
841}
842
Fariborz Jahanianda320092009-01-29 19:24:30 +0000843llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000844 if (DefinedProtocols.count(PD->getIdentifier()))
845 return GetOrEmitProtocol(PD);
846 return GetOrEmitProtocolRef(PD);
847}
848
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000849/*
850 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
851 struct _objc_protocol {
852 struct _objc_protocol_extension *isa;
853 char *protocol_name;
854 struct _objc_protocol_list *protocol_list;
855 struct _objc__method_prototype_list *instance_methods;
856 struct _objc__method_prototype_list *class_methods
857 };
858
859 See EmitProtocolExtension().
860*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000861llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
862 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
863
864 // Early exit if a defining object has already been generated.
865 if (Entry && Entry->hasInitializer())
866 return Entry;
867
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000868 // FIXME: I don't understand why gcc generates this, or where it is
869 // resolved. Investigate. Its also wasteful to look this up over and
870 // over.
871 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
872
Chris Lattner8ec03f52008-11-24 03:54:41 +0000873 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000874
875 // Construct method lists.
876 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
877 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
878 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
879 e = PD->instmeth_end(); i != e; ++i) {
880 ObjCMethodDecl *MD = *i;
881 llvm::Constant *C = GetMethodDescriptionConstant(MD);
882 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
883 OptInstanceMethods.push_back(C);
884 } else {
885 InstanceMethods.push_back(C);
886 }
887 }
888
889 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
890 e = PD->classmeth_end(); i != e; ++i) {
891 ObjCMethodDecl *MD = *i;
892 llvm::Constant *C = GetMethodDescriptionConstant(MD);
893 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
894 OptClassMethods.push_back(C);
895 } else {
896 ClassMethods.push_back(C);
897 }
898 }
899
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000900 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000901 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000902 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000903 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000904 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +0000905 PD->protocol_begin(),
906 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000907 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000908 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
909 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000910 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
911 InstanceMethods);
912 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000913 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
914 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000915 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
916 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000917 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
918 Values);
919
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000920 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000921 // Already created, fix the linkage and update the initializer.
922 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000923 Entry->setInitializer(Init);
924 } else {
925 Entry =
926 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
927 llvm::GlobalValue::InternalLinkage,
928 Init,
929 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
930 &CGM.getModule());
931 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
932 UsedGlobals.push_back(Entry);
933 // FIXME: Is this necessary? Why only for protocol?
934 Entry->setAlignment(4);
935 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000936
937 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000938}
939
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000940llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000941 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
942
943 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000944 // We use the initializer as a marker of whether this is a forward
945 // reference or not. At module finalization we add the empty
946 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000947 Entry =
948 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000949 llvm::GlobalValue::ExternalLinkage,
950 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000951 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000952 &CGM.getModule());
953 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
954 UsedGlobals.push_back(Entry);
955 // FIXME: Is this necessary? Why only for protocol?
956 Entry->setAlignment(4);
957 }
958
959 return Entry;
960}
961
962/*
963 struct _objc_protocol_extension {
964 uint32_t size;
965 struct objc_method_description_list *optional_instance_methods;
966 struct objc_method_description_list *optional_class_methods;
967 struct objc_property_list *instance_properties;
968 };
969*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000970llvm::Constant *
971CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
972 const ConstantVector &OptInstanceMethods,
973 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000974 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +0000975 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000976 std::vector<llvm::Constant*> Values(4);
977 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000978 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000979 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
980 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000981 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
982 OptInstanceMethods);
983 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000984 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
985 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000986 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
987 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000988 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
989 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000990 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000991
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000992 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000993 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
994 Values[3]->isNullValue())
995 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
996
997 llvm::Constant *Init =
998 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
999 llvm::GlobalVariable *GV =
1000 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
1001 llvm::GlobalValue::InternalLinkage,
1002 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001003 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001004 &CGM.getModule());
1005 // No special section, but goes in llvm.used
1006 UsedGlobals.push_back(GV);
1007
1008 return GV;
1009}
1010
1011/*
1012 struct objc_protocol_list {
1013 struct objc_protocol_list *next;
1014 long count;
1015 Protocol *list[];
1016 };
1017*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001018llvm::Constant *
1019CGObjCMac::EmitProtocolList(const std::string &Name,
1020 ObjCProtocolDecl::protocol_iterator begin,
1021 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001022 std::vector<llvm::Constant*> ProtocolRefs;
1023
Daniel Dunbardbc933702008-08-21 21:57:41 +00001024 for (; begin != end; ++begin)
1025 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001026
1027 // Just return null for empty protocol lists
1028 if (ProtocolRefs.empty())
1029 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1030
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001031 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001032 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1033
1034 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001035 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001036 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1037 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1038 Values[2] =
1039 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1040 ProtocolRefs.size()),
1041 ProtocolRefs);
1042
1043 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1044 llvm::GlobalVariable *GV =
1045 new llvm::GlobalVariable(Init->getType(), false,
1046 llvm::GlobalValue::InternalLinkage,
1047 Init,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001048 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001049 &CGM.getModule());
1050 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
1051 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1052}
1053
1054/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001055 struct _objc_property {
1056 const char * const name;
1057 const char * const attributes;
1058 };
1059
1060 struct _objc_property_list {
1061 uint32_t entsize; // sizeof (struct _objc_property)
1062 uint32_t prop_count;
1063 struct _objc_property[prop_count];
1064 };
1065*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001066llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1067 const Decl *Container,
1068 const ObjCContainerDecl *OCD,
1069 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001070 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +00001071 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1072 E = OCD->prop_end(); I != E; ++I) {
1073 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001074 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001075 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001076 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1077 Prop));
1078 }
1079
1080 // Return null for empty list.
1081 if (Properties.empty())
1082 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1083
1084 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001085 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001086 std::vector<llvm::Constant*> Values(3);
1087 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1088 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1089 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1090 Properties.size());
1091 Values[2] = llvm::ConstantArray::get(AT, Properties);
1092 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1093
1094 llvm::GlobalVariable *GV =
1095 new llvm::GlobalVariable(Init->getType(), false,
1096 llvm::GlobalValue::InternalLinkage,
1097 Init,
1098 Name,
1099 &CGM.getModule());
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001100 if (ObjCABI == 2)
1101 GV->setSection("__DATA, __objc_const");
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001102 // No special section on property lists?
1103 UsedGlobals.push_back(GV);
1104 return llvm::ConstantExpr::getBitCast(GV,
1105 ObjCTypes.PropertyListPtrTy);
1106
1107}
1108
1109/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001110 struct objc_method_description_list {
1111 int count;
1112 struct objc_method_description list[];
1113 };
1114*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001115llvm::Constant *
1116CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1117 std::vector<llvm::Constant*> Desc(2);
1118 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1119 ObjCTypes.SelectorPtrTy);
1120 Desc[1] = GetMethodVarType(MD);
1121 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1122 Desc);
1123}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001124
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001125llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1126 const char *Section,
1127 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001128 // Return null for empty list.
1129 if (Methods.empty())
1130 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1131
1132 std::vector<llvm::Constant*> Values(2);
1133 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1134 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1135 Methods.size());
1136 Values[1] = llvm::ConstantArray::get(AT, Methods);
1137 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1138
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001139 llvm::GlobalVariable *GV =
1140 new llvm::GlobalVariable(Init->getType(), false,
1141 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001142 Init, Name, &CGM.getModule());
1143 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001144 UsedGlobals.push_back(GV);
1145 return llvm::ConstantExpr::getBitCast(GV,
1146 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001147}
1148
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001149/*
1150 struct _objc_category {
1151 char *category_name;
1152 char *class_name;
1153 struct _objc_method_list *instance_methods;
1154 struct _objc_method_list *class_methods;
1155 struct _objc_protocol_list *protocols;
1156 uint32_t size; // <rdar://4585769>
1157 struct _objc_property_list *instance_properties;
1158 };
1159 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001160void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001161 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001162
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001163 // FIXME: This is poor design, the OCD should have a pointer to the
1164 // category decl. Additionally, note that Category can be null for
1165 // the @implementation w/o an @interface case. Sema should just
1166 // create one for us as it does for @implementation so everyone else
1167 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001168 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001169 const ObjCCategoryDecl *Category =
1170 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001171 std::string ExtName(Interface->getNameAsString() + "_" +
1172 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001173
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001174 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1175 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1176 e = OCD->instmeth_end(); i != e; ++i) {
1177 // Instance methods should always be defined.
1178 InstanceMethods.push_back(GetMethodConstant(*i));
1179 }
1180 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1181 e = OCD->classmeth_end(); i != e; ++i) {
1182 // Class methods should always be defined.
1183 ClassMethods.push_back(GetMethodConstant(*i));
1184 }
1185
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001186 std::vector<llvm::Constant*> Values(7);
1187 Values[0] = GetClassName(OCD->getIdentifier());
1188 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001189 Values[2] =
1190 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1191 ExtName,
1192 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001193 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001194 Values[3] =
1195 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1196 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001197 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001198 if (Category) {
1199 Values[4] =
1200 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1201 Category->protocol_begin(),
1202 Category->protocol_end());
1203 } else {
1204 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1205 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001206 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001207
1208 // If there is no category @interface then there can be no properties.
1209 if (Category) {
1210 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001211 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001212 } else {
1213 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1214 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001215
1216 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1217 Values);
1218
1219 llvm::GlobalVariable *GV =
1220 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1221 llvm::GlobalValue::InternalLinkage,
1222 Init,
1223 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1224 &CGM.getModule());
1225 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1226 UsedGlobals.push_back(GV);
1227 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001228}
1229
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001230// FIXME: Get from somewhere?
1231enum ClassFlags {
1232 eClassFlags_Factory = 0x00001,
1233 eClassFlags_Meta = 0x00002,
1234 // <rdr://5142207>
1235 eClassFlags_HasCXXStructors = 0x02000,
1236 eClassFlags_Hidden = 0x20000,
1237 eClassFlags_ABI2_Hidden = 0x00010,
1238 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1239};
1240
1241// <rdr://5142207&4705298&4843145>
1242static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1243 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1244 // FIXME: Support -fvisibility
1245 switch (attr->getVisibility()) {
1246 default:
1247 assert(0 && "Unknown visibility");
1248 return false;
1249 case VisibilityAttr::DefaultVisibility:
1250 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1251 return false;
1252 case VisibilityAttr::HiddenVisibility:
1253 return true;
1254 }
1255 } else {
1256 return false; // FIXME: Support -fvisibility
1257 }
1258}
1259
1260/*
1261 struct _objc_class {
1262 Class isa;
1263 Class super_class;
1264 const char *name;
1265 long version;
1266 long info;
1267 long instance_size;
1268 struct _objc_ivar_list *ivars;
1269 struct _objc_method_list *methods;
1270 struct _objc_cache *cache;
1271 struct _objc_protocol_list *protocols;
1272 // Objective-C 1.0 extensions (<rdr://4585769>)
1273 const char *ivar_layout;
1274 struct _objc_class_ext *ext;
1275 };
1276
1277 See EmitClassExtension();
1278 */
1279void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001280 DefinedSymbols.insert(ID->getIdentifier());
1281
Chris Lattner8ec03f52008-11-24 03:54:41 +00001282 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001283 // FIXME: Gross
1284 ObjCInterfaceDecl *Interface =
1285 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001286 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001287 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001288 Interface->protocol_begin(),
1289 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001290 const llvm::Type *InterfaceTy =
1291 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
1292 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001293 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001294
1295 // FIXME: Set CXX-structors flag.
1296 if (IsClassHidden(ID->getClassInterface()))
1297 Flags |= eClassFlags_Hidden;
1298
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001299 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1300 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1301 e = ID->instmeth_end(); i != e; ++i) {
1302 // Instance methods should always be defined.
1303 InstanceMethods.push_back(GetMethodConstant(*i));
1304 }
1305 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1306 e = ID->classmeth_end(); i != e; ++i) {
1307 // Class methods should always be defined.
1308 ClassMethods.push_back(GetMethodConstant(*i));
1309 }
1310
1311 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1312 e = ID->propimpl_end(); i != e; ++i) {
1313 ObjCPropertyImplDecl *PID = *i;
1314
1315 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1316 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1317
1318 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1319 if (llvm::Constant *C = GetMethodConstant(MD))
1320 InstanceMethods.push_back(C);
1321 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1322 if (llvm::Constant *C = GetMethodConstant(MD))
1323 InstanceMethods.push_back(C);
1324 }
1325 }
1326
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001327 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001328 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001329 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001330 // Record a reference to the super class.
1331 LazySymbols.insert(Super->getIdentifier());
1332
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001333 Values[ 1] =
1334 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1335 ObjCTypes.ClassPtrTy);
1336 } else {
1337 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1338 }
1339 Values[ 2] = GetClassName(ID->getIdentifier());
1340 // Version is always 0.
1341 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1342 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1343 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001344 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001345 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001346 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001347 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001348 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001349 // cache is always NULL.
1350 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1351 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001352 // FIXME: Set ivar_layout
1353 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001354 Values[11] = EmitClassExtension(ID);
1355 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1356 Values);
1357
1358 llvm::GlobalVariable *GV =
1359 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1360 llvm::GlobalValue::InternalLinkage,
1361 Init,
1362 std::string("\01L_OBJC_CLASS_")+ClassName,
1363 &CGM.getModule());
1364 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1365 UsedGlobals.push_back(GV);
1366 // FIXME: Why?
1367 GV->setAlignment(32);
1368 DefinedClasses.push_back(GV);
1369}
1370
1371llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1372 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001373 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001374 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001375 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001376 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001377
1378 if (IsClassHidden(ID->getClassInterface()))
1379 Flags |= eClassFlags_Hidden;
1380
1381 std::vector<llvm::Constant*> Values(12);
1382 // The isa for the metaclass is the root of the hierarchy.
1383 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1384 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1385 Root = Super;
1386 Values[ 0] =
1387 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1388 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001389 // The super class for the metaclass is emitted as the name of the
1390 // super class. The runtime fixes this up to point to the
1391 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001392 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1393 Values[ 1] =
1394 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1395 ObjCTypes.ClassPtrTy);
1396 } else {
1397 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1398 }
1399 Values[ 2] = GetClassName(ID->getIdentifier());
1400 // Version is always 0.
1401 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1402 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1403 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001404 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001405 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001406 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001407 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001408 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001409 // cache is always NULL.
1410 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1411 Values[ 9] = Protocols;
1412 // ivar_layout for metaclass is always NULL.
1413 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1414 // The class extension is always unused for metaclasses.
1415 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1416 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1417 Values);
1418
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001419 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001420 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001421
1422 // Check for a forward reference.
1423 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1424 if (GV) {
1425 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1426 "Forward metaclass reference has incorrect type.");
1427 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1428 GV->setInitializer(Init);
1429 } else {
1430 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1431 llvm::GlobalValue::InternalLinkage,
1432 Init, Name,
1433 &CGM.getModule());
1434 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001435 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1436 UsedGlobals.push_back(GV);
1437 // FIXME: Why?
1438 GV->setAlignment(32);
1439
1440 return GV;
1441}
1442
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001443llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001444 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001445
1446 // FIXME: Should we look these up somewhere other than the
1447 // module. Its a bit silly since we only generate these while
1448 // processing an implementation, so exactly one pointer would work
1449 // if know when we entered/exitted an implementation block.
1450
1451 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001452 // Previously, metaclass with internal linkage may have been defined.
1453 // pass 'true' as 2nd argument so it is returned.
1454 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001455 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1456 "Forward metaclass reference has incorrect type.");
1457 return GV;
1458 } else {
1459 // Generate as an external reference to keep a consistent
1460 // module. This will be patched up when we emit the metaclass.
1461 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1462 llvm::GlobalValue::ExternalLinkage,
1463 0,
1464 Name,
1465 &CGM.getModule());
1466 }
1467}
1468
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001469/*
1470 struct objc_class_ext {
1471 uint32_t size;
1472 const char *weak_ivar_layout;
1473 struct _objc_property_list *properties;
1474 };
1475*/
1476llvm::Constant *
1477CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1478 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001479 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001480
1481 std::vector<llvm::Constant*> Values(3);
1482 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001483 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001484 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001485 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001486 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001487
1488 // Return null if no extension bits are used.
1489 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1490 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1491
1492 llvm::Constant *Init =
1493 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1494 llvm::GlobalVariable *GV =
1495 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1496 llvm::GlobalValue::InternalLinkage,
1497 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001498 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001499 &CGM.getModule());
1500 // No special section, but goes in llvm.used
1501 UsedGlobals.push_back(GV);
1502
1503 return GV;
1504}
1505
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001506/// countInheritedIvars - count number of ivars in class and its super class(s)
1507///
1508static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1509 int count = 0;
1510 if (!OI)
1511 return 0;
1512 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1513 if (SuperClass)
1514 count += countInheritedIvars(SuperClass);
1515 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1516 E = OI->ivar_end(); I != E; ++I)
1517 ++count;
1518 return count;
1519}
1520
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001521/*
1522 struct objc_ivar {
1523 char *ivar_name;
1524 char *ivar_type;
1525 int ivar_offset;
1526 };
1527
1528 struct objc_ivar_list {
1529 int ivar_count;
1530 struct objc_ivar list[count];
1531 };
1532 */
1533llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001534 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001535 std::vector<llvm::Constant*> Ivars, Ivar(3);
1536
1537 // When emitting the root class GCC emits ivar entries for the
1538 // actual class structure. It is not clear if we need to follow this
1539 // behavior; for now lets try and get away with not doing it. If so,
1540 // the cleanest solution would be to make up an ObjCInterfaceDecl
1541 // for the class.
1542 if (ForClass)
1543 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001544
1545 ObjCInterfaceDecl *OID =
1546 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
1547 const llvm::Type *InterfaceTy =
1548 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001549 const llvm::StructLayout *Layout =
1550 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001551
1552 RecordDecl::field_iterator ifield, pfield;
1553 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001554 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1555 FieldDecl *Field = *ifield;
1556 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1557 getLLVMFieldNo(Field));
1558 if (Field->getIdentifier())
1559 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1560 else
1561 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001562 std::string TypeStr;
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001563 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001564 Ivar[1] = GetMethodVarType(TypeStr);
1565 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001566 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001567 }
1568
1569 // Return null for empty list.
1570 if (Ivars.empty())
1571 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1572
1573 std::vector<llvm::Constant*> Values(2);
1574 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1575 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1576 Ivars.size());
1577 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1578 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1579
1580 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1581 "\01L_OBJC_INSTANCE_VARIABLES_");
1582 llvm::GlobalVariable *GV =
1583 new llvm::GlobalVariable(Init->getType(), false,
1584 llvm::GlobalValue::InternalLinkage,
1585 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001586 Prefix + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001587 &CGM.getModule());
1588 if (ForClass) {
1589 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1590 // FIXME: Why is this only here?
1591 GV->setAlignment(32);
1592 } else {
1593 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1594 }
1595 UsedGlobals.push_back(GV);
1596 return llvm::ConstantExpr::getBitCast(GV,
1597 ObjCTypes.IvarListPtrTy);
1598}
1599
1600/*
1601 struct objc_method {
1602 SEL method_name;
1603 char *method_types;
1604 void *method;
1605 };
1606
1607 struct objc_method_list {
1608 struct objc_method_list *obsolete;
1609 int count;
1610 struct objc_method methods_list[count];
1611 };
1612*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001613
1614/// GetMethodConstant - Return a struct objc_method constant for the
1615/// given method if it has been defined. The result is null if the
1616/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001617llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001618 // FIXME: Use DenseMap::lookup
1619 llvm::Function *Fn = MethodDefinitions[MD];
1620 if (!Fn)
1621 return 0;
1622
1623 std::vector<llvm::Constant*> Method(3);
1624 Method[0] =
1625 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1626 ObjCTypes.SelectorPtrTy);
1627 Method[1] = GetMethodVarType(MD);
1628 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1629 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1630}
1631
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001632llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1633 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001634 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001635 // Return null for empty list.
1636 if (Methods.empty())
1637 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1638
1639 std::vector<llvm::Constant*> Values(3);
1640 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1641 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1642 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1643 Methods.size());
1644 Values[2] = llvm::ConstantArray::get(AT, Methods);
1645 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1646
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001647 llvm::GlobalVariable *GV =
1648 new llvm::GlobalVariable(Init->getType(), false,
1649 llvm::GlobalValue::InternalLinkage,
1650 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001651 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001652 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001653 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001654 UsedGlobals.push_back(GV);
1655 return llvm::ConstantExpr::getBitCast(GV,
1656 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001657}
1658
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001659llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001660 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001661 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001662 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001663
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001664 const llvm::FunctionType *MethodTy =
1665 CGM.getTypes().GetFunctionType(CGFunctionInfo(OMD, CGM.getContext()));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001666 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001667 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001668 llvm::GlobalValue::InternalLinkage,
1669 Name,
1670 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001671 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001672
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001673 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001674}
1675
1676llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001677 // Abuse this interface function as a place to finalize.
1678 FinishModule();
1679
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001680 return NULL;
1681}
1682
Daniel Dunbar49f66022008-09-24 03:38:44 +00001683llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1684 return ObjCTypes.GetPropertyFn;
1685}
1686
1687llvm::Function *CGObjCMac::GetPropertySetFunction() {
1688 return ObjCTypes.SetPropertyFn;
1689}
1690
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001691llvm::Function *CGObjCMac::EnumerationMutationFunction()
1692{
1693 return ObjCTypes.EnumerationMutationFn;
1694}
1695
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001696/*
1697
1698Objective-C setjmp-longjmp (sjlj) Exception Handling
1699--
1700
1701The basic framework for a @try-catch-finally is as follows:
1702{
1703 objc_exception_data d;
1704 id _rethrow = null;
1705
1706 objc_exception_try_enter(&d);
1707 if (!setjmp(d.jmp_buf)) {
1708 ... try body ...
1709 } else {
1710 // exception path
1711 id _caught = objc_exception_extract(&d);
1712
1713 // enter new try scope for handlers
1714 if (!setjmp(d.jmp_buf)) {
1715 ... match exception and execute catch blocks ...
1716
1717 // fell off end, rethrow.
1718 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001719 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001720 } else {
1721 // exception in catch block
1722 _rethrow = objc_exception_extract(&d);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001723 ... jump-through-finally_no_exit to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001724 }
1725 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001726 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001727
1728finally:
1729 // match either the initial try_enter or the catch try_enter,
1730 // depending on the path followed.
1731 objc_exception_try_exit(&d);
1732finally_no_exit:
1733 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001734 ... dispatch to finally destination ...
1735
1736finally_rethrow:
1737 objc_exception_throw(_rethrow);
1738
1739finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001740}
1741
1742This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001743uses _rethrow to determine if objc_exception_try_exit should be called
1744and if the object should be rethrown. This breaks in the face of
1745throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001746
1747We specialize this framework for a few particular circumstances:
1748
1749 - If there are no catch blocks, then we avoid emitting the second
1750 exception handling context.
1751
1752 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1753 e)) we avoid emitting the code to rethrow an uncaught exception.
1754
1755 - FIXME: If there is no @finally block we can do a few more
1756 simplifications.
1757
1758Rethrows and Jumps-Through-Finally
1759--
1760
1761Support for implicit rethrows and jumping through the finally block is
1762handled by storing the current exception-handling context in
1763ObjCEHStack.
1764
Daniel Dunbar898d5082008-09-30 01:06:03 +00001765In order to implement proper @finally semantics, we support one basic
1766mechanism for jumping through the finally block to an arbitrary
1767destination. Constructs which generate exits from a @try or @catch
1768block use this mechanism to implement the proper semantics by chaining
1769jumps, as necessary.
1770
1771This mechanism works like the one used for indirect goto: we
1772arbitrarily assign an ID to each destination and store the ID for the
1773destination in a variable prior to entering the finally block. At the
1774end of the finally block we simply create a switch to the proper
1775destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001776
1777Code gen for @synchronized(expr) stmt;
1778Effectively generating code for:
1779objc_sync_enter(expr);
1780@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001781*/
1782
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001783void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1784 const Stmt &S) {
1785 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001786 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001787 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
1788 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1789 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1790 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001791 llvm::Value *DestCode =
1792 CGF.CreateTempAlloca(llvm::Type::Int32Ty, "finally.dst");
1793
1794 // Generate jump code. Done here so we can directly add things to
1795 // the switch instruction.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001796 llvm::BasicBlock *FinallyJump = CGF.createBasicBlock("finally.jump");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001797 llvm::SwitchInst *FinallySwitch =
1798 llvm::SwitchInst::Create(new llvm::LoadInst(DestCode, "", FinallyJump),
1799 FinallyEnd, 10, FinallyJump);
1800
1801 // Push an EH context entry, used for handling rethrows and jumps
1802 // through finally.
1803 CodeGenFunction::ObjCEHEntry EHEntry(FinallyBlock, FinallyNoExit,
1804 FinallySwitch, DestCode);
1805 CGF.ObjCEHStack.push_back(&EHEntry);
1806
1807 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00001808 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1809 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001810 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1811 "_rethrow");
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001812 if (!isTry) {
1813 // For @synchronized, call objc_sync_enter(sync.expr)
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001814 llvm::Value *Arg = CGF.EmitScalarExpr(
1815 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1816 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1817 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001818 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001819
1820 // Enter a new try block and call setjmp.
1821 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1822 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1823 "jmpbufarray");
1824 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1825 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1826 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001827
Daniel Dunbar55e87422008-11-11 02:29:29 +00001828 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1829 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001830 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001831 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001832
1833 // Emit the @try block.
1834 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001835 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1836 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001837 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001838
1839 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001840 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001841
1842 // Retrieve the exception object. We may emit multiple blocks but
1843 // nothing can cross this so the value is already in SSA form.
1844 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1845 ExceptionData,
1846 "caught");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001847 EHEntry.Exception = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001848 if (!isTry)
1849 {
1850 CGF.Builder.CreateStore(Caught, RethrowPtr);
1851 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
1852 }
1853 else if (const ObjCAtCatchStmt* CatchStmt =
1854 cast<ObjCAtTryStmt>(S).getCatchStmts())
1855 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00001856 // Enter a new exception try block (in case a @catch block throws
1857 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00001858 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001859
Anders Carlsson80f25672008-09-09 17:59:25 +00001860 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1861 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001862 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00001863
Daniel Dunbar55e87422008-11-11 02:29:29 +00001864 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1865 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001866 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001867
1868 CGF.EmitBlock(CatchBlock);
1869
Daniel Dunbar55e40722008-09-27 07:03:52 +00001870 // Handle catch list. As a special case we check if everything is
1871 // matched and avoid generating code for falling off the end if
1872 // so.
1873 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00001874 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00001875 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00001876
Anders Carlssondde0a942008-09-11 09:15:33 +00001877 const DeclStmt *CatchParam =
1878 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001879 const VarDecl *VD = 0;
1880 const PointerType *PT = 0;
1881
Anders Carlsson80f25672008-09-09 17:59:25 +00001882 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00001883 if (!CatchParam) {
1884 AllMatched = true;
1885 } else {
Ted Kremenekde3b8fb2008-10-06 20:58:56 +00001886 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001887 PT = VD->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001888
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001889 // catch(id e) always matches.
1890 // FIXME: For the time being we also match id<X>; this should
1891 // be rejected by Sema instead.
1892 if ((PT && CGF.getContext().isObjCIdType(PT->getPointeeType())) ||
1893 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00001894 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00001895 }
1896
Daniel Dunbar55e40722008-09-27 07:03:52 +00001897 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00001898 if (CatchParam) {
1899 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001900 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar129271a2008-09-27 07:36:24 +00001901 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001902 }
Anders Carlsson1452f552008-09-11 08:21:54 +00001903
Anders Carlssondde0a942008-09-11 09:15:33 +00001904 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001905 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001906 break;
1907 }
1908
Daniel Dunbar129271a2008-09-27 07:36:24 +00001909 assert(PT && "Unexpected non-pointer type in @catch");
1910 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00001911 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001912 assert(ObjCType && "Catch parameter must have Objective-C type!");
1913
1914 // Check if the @catch block matches the exception object.
1915 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
1916
Anders Carlsson80f25672008-09-09 17:59:25 +00001917 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
1918 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00001919
Daniel Dunbar55e87422008-11-11 02:29:29 +00001920 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00001921
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001922 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001923 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001924
1925 // Emit the @catch block.
1926 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001927 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001928 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001929
1930 llvm::Value *Tmp =
1931 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
1932 "tmp");
1933 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001934
1935 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001936 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001937
1938 CGF.EmitBlock(NextCatchBlock);
1939 }
1940
Daniel Dunbar55e40722008-09-27 07:03:52 +00001941 if (!AllMatched) {
1942 // None of the handlers caught the exception, so store it to be
1943 // rethrown at the end of the @finally block.
1944 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001945 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001946 }
1947
1948 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001949 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001950 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1951 ExceptionData),
1952 RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001953 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001954 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00001955 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001956 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Anders Carlsson80f25672008-09-09 17:59:25 +00001957 }
1958
Daniel Dunbar898d5082008-09-30 01:06:03 +00001959 // Pop the exception-handling stack entry. It is important to do
1960 // this now, because the code in the @finally block is not in this
1961 // context.
1962 CGF.ObjCEHStack.pop_back();
1963
Anders Carlsson80f25672008-09-09 17:59:25 +00001964 // Emit the @finally block.
1965 CGF.EmitBlock(FinallyBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001966 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00001967
1968 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001969 if (isTry) {
1970 if (const ObjCAtFinallyStmt* FinallyStmt =
1971 cast<ObjCAtTryStmt>(S).getFinallyStmt())
1972 CGF.EmitStmt(FinallyStmt->getFinallyBody());
1973 }
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001974 else {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001975 // For @synchronized objc_sync_exit(expr); As finally's sole statement.
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001976 // For @synchronized, call objc_sync_enter(sync.expr)
1977 llvm::Value *Arg = CGF.EmitScalarExpr(
1978 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1979 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1980 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg);
1981 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001982
Daniel Dunbar898d5082008-09-30 01:06:03 +00001983 CGF.EmitBlock(FinallyJump);
1984
1985 CGF.EmitBlock(FinallyRethrow);
1986 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
1987 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001988 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00001989
1990 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001991}
1992
1993void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00001994 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00001995 llvm::Value *ExceptionAsObject;
1996
1997 if (const Expr *ThrowExpr = S.getThrowExpr()) {
1998 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
1999 ExceptionAsObject =
2000 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2001 } else {
Daniel Dunbar898d5082008-09-30 01:06:03 +00002002 assert((!CGF.ObjCEHStack.empty() && CGF.ObjCEHStack.back()->Exception) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002003 "Unexpected rethrow outside @catch block.");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002004 ExceptionAsObject = CGF.ObjCEHStack.back()->Exception;
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002005 }
2006
2007 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002008 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002009
2010 // Clear the insertion point to indicate we are in unreachable code.
2011 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002012}
2013
Daniel Dunbar898d5082008-09-30 01:06:03 +00002014void CodeGenFunction::EmitJumpThroughFinally(ObjCEHEntry *E,
2015 llvm::BasicBlock *Dst,
2016 bool ExecuteTryExit) {
Daniel Dunbara448fb22008-11-11 23:11:34 +00002017 if (!HaveInsertPoint())
Daniel Dunbar898d5082008-09-30 01:06:03 +00002018 return;
2019
2020 // Find the destination code for this block. We always use 0 for the
2021 // fallthrough block (default destination).
2022 llvm::SwitchInst *SI = E->FinallySwitch;
2023 llvm::ConstantInt *ID;
2024 if (Dst == SI->getDefaultDest()) {
2025 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
2026 } else {
2027 ID = SI->findCaseDest(Dst);
2028 if (!ID) {
2029 // No code found, get a new unique one by just using the number
2030 // of switch successors.
2031 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, SI->getNumSuccessors());
2032 SI->addCase(ID, Dst);
2033 }
2034 }
2035
2036 // Set the destination code and branch.
2037 Builder.CreateStore(ID, E->DestCode);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002038 EmitBranch(ExecuteTryExit ? E->FinallyBlock : E->FinallyNoExit);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002039}
2040
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002041/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002042/// object: objc_read_weak (id *src)
2043///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002044llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002045 llvm::Value *AddrWeakObj)
2046{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002047 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002048 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002049 AddrWeakObj, "weakread");
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002050 return read_weak;
2051}
2052
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002053/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2054/// objc_assign_weak (id src, id *dst)
2055///
2056void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2057 llvm::Value *src, llvm::Value *dst)
2058{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002059 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2060 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002061 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2062 src, dst, "weakassign");
2063 return;
2064}
2065
Fariborz Jahanian58626502008-11-19 00:59:10 +00002066/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2067/// objc_assign_global (id src, id *dst)
2068///
2069void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2070 llvm::Value *src, llvm::Value *dst)
2071{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002072 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2073 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002074 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2075 src, dst, "globalassign");
2076 return;
2077}
2078
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002079/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2080/// objc_assign_ivar (id src, id *dst)
2081///
2082void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2083 llvm::Value *src, llvm::Value *dst)
2084{
2085 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2086 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2087 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2088 src, dst, "assignivar");
2089 return;
2090}
2091
Fariborz Jahanian58626502008-11-19 00:59:10 +00002092/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2093/// objc_assign_strongCast (id src, id *dst)
2094///
2095void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2096 llvm::Value *src, llvm::Value *dst)
2097{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002098 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2099 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002100 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2101 src, dst, "weakassign");
2102 return;
2103}
2104
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002105/* *** Private Interface *** */
2106
2107/// EmitImageInfo - Emit the image info marker used to encode some module
2108/// level information.
2109///
2110/// See: <rdr://4810609&4810587&4810587>
2111/// struct IMAGE_INFO {
2112/// unsigned version;
2113/// unsigned flags;
2114/// };
2115enum ImageInfoFlags {
2116 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2117 eImageInfo_GarbageCollected = (1 << 1),
2118 eImageInfo_GCOnly = (1 << 2)
2119};
2120
2121void CGObjCMac::EmitImageInfo() {
2122 unsigned version = 0; // Version is unused?
2123 unsigned flags = 0;
2124
2125 // FIXME: Fix and continue?
2126 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2127 flags |= eImageInfo_GarbageCollected;
2128 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2129 flags |= eImageInfo_GCOnly;
2130
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002131 // Emitted as int[2];
2132 llvm::Constant *values[2] = {
2133 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2134 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2135 };
2136 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002137 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002138 new llvm::GlobalVariable(AT, true,
2139 llvm::GlobalValue::InternalLinkage,
2140 llvm::ConstantArray::get(AT, values, 2),
2141 "\01L_OBJC_IMAGE_INFO",
2142 &CGM.getModule());
2143
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002144 if (ObjCABI == 1) {
2145 GV->setSection("__OBJC, __image_info,regular");
2146 } else {
2147 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2148 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002149
2150 UsedGlobals.push_back(GV);
2151}
2152
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002153
2154// struct objc_module {
2155// unsigned long version;
2156// unsigned long size;
2157// const char *name;
2158// Symtab symtab;
2159// };
2160
2161// FIXME: Get from somewhere
2162static const int ModuleVersion = 7;
2163
2164void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002165 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002166
2167 std::vector<llvm::Constant*> Values(4);
2168 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2169 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002170 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002171 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002172 Values[3] = EmitModuleSymbols();
2173
2174 llvm::GlobalVariable *GV =
2175 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2176 llvm::GlobalValue::InternalLinkage,
2177 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2178 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002179 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002180 &CGM.getModule());
2181 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2182 UsedGlobals.push_back(GV);
2183}
2184
2185llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002186 unsigned NumClasses = DefinedClasses.size();
2187 unsigned NumCategories = DefinedCategories.size();
2188
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002189 // Return null if no symbols were defined.
2190 if (!NumClasses && !NumCategories)
2191 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2192
2193 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002194 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2195 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2196 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2197 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2198
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002199 // The runtime expects exactly the list of defined classes followed
2200 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002201 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002202 for (unsigned i=0; i<NumClasses; i++)
2203 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2204 ObjCTypes.Int8PtrTy);
2205 for (unsigned i=0; i<NumCategories; i++)
2206 Symbols[NumClasses + i] =
2207 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2208 ObjCTypes.Int8PtrTy);
2209
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002210 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002211 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002212 NumClasses + NumCategories),
2213 Symbols);
2214
2215 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2216
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002217 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002218 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002219 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002220 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002221 "\01L_OBJC_SYMBOLS",
2222 &CGM.getModule());
2223 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2224 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002225 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2226}
2227
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002228llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002229 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002230 LazySymbols.insert(ID->getIdentifier());
2231
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002232 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2233
2234 if (!Entry) {
2235 llvm::Constant *Casted =
2236 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2237 ObjCTypes.ClassPtrTy);
2238 Entry =
2239 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2240 llvm::GlobalValue::InternalLinkage,
2241 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2242 &CGM.getModule());
2243 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2244 UsedGlobals.push_back(Entry);
2245 }
2246
2247 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002248}
2249
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002250llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002251 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2252
2253 if (!Entry) {
2254 llvm::Constant *Casted =
2255 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2256 ObjCTypes.SelectorPtrTy);
2257 Entry =
2258 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2259 llvm::GlobalValue::InternalLinkage,
2260 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2261 &CGM.getModule());
2262 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2263 UsedGlobals.push_back(Entry);
2264 }
2265
2266 return Builder.CreateLoad(Entry, false, "tmp");
2267}
2268
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002269llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002270 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002271
2272 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002273 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002274 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002275 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002276 llvm::GlobalValue::InternalLinkage,
2277 C, "\01L_OBJC_CLASS_NAME_",
2278 &CGM.getModule());
2279 Entry->setSection("__TEXT,__cstring,cstring_literals");
2280 UsedGlobals.push_back(Entry);
2281 }
2282
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002283 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002284}
2285
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002286llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002287 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2288
2289 if (!Entry) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00002290 // FIXME: Avoid std::string copying.
2291 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002292 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002293 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002294 llvm::GlobalValue::InternalLinkage,
2295 C, "\01L_OBJC_METH_VAR_NAME_",
2296 &CGM.getModule());
2297 Entry->setSection("__TEXT,__cstring,cstring_literals");
2298 UsedGlobals.push_back(Entry);
2299 }
2300
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002301 return getConstantGEP(Entry, 0, 0);
2302}
2303
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002304// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002305llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002306 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2307}
2308
2309// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002310llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002311 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2312}
2313
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002314llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002315 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002316
2317 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002318 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002319 Entry =
2320 new llvm::GlobalVariable(C->getType(), false,
2321 llvm::GlobalValue::InternalLinkage,
2322 C, "\01L_OBJC_METH_VAR_TYPE_",
2323 &CGM.getModule());
2324 Entry->setSection("__TEXT,__cstring,cstring_literals");
2325 UsedGlobals.push_back(Entry);
2326 }
2327
2328 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002329}
2330
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002331// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002332llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002333 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002334 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2335 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002336 return GetMethodVarType(TypeStr);
2337}
2338
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002339// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002340llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002341 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2342
2343 if (!Entry) {
2344 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2345 Entry =
2346 new llvm::GlobalVariable(C->getType(), false,
2347 llvm::GlobalValue::InternalLinkage,
2348 C, "\01L_OBJC_PROP_NAME_ATTR_",
2349 &CGM.getModule());
2350 Entry->setSection("__TEXT,__cstring,cstring_literals");
2351 UsedGlobals.push_back(Entry);
2352 }
2353
2354 return getConstantGEP(Entry, 0, 0);
2355}
2356
2357// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002358// FIXME: This Decl should be more precise.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002359llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002360 const Decl *Container) {
2361 std::string TypeStr;
2362 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002363 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2364}
2365
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002366void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2367 const ObjCContainerDecl *CD,
2368 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002369 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002370 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002371 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002372 assert (CD && "Missing container decl in GetNameForMethod");
2373 NameOut += CD->getNameAsString();
Fariborz Jahanian52847332009-01-26 23:49:05 +00002374 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
2375 // Right now! there is not enough info. to do this.
Chris Lattner077bf5e2008-11-24 03:33:13 +00002376 NameOut += ' ';
2377 NameOut += D->getSelector().getAsString();
2378 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002379}
2380
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002381/// GetFirstIvarInRecord - This routine returns the record for the
2382/// implementation of the fiven class OID. It also returns field
2383/// corresponding to the first ivar in the class in FIV. It also
2384/// returns the one before the first ivar.
2385///
2386const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord(
2387 const ObjCInterfaceDecl *OID,
2388 RecordDecl::field_iterator &FIV,
2389 RecordDecl::field_iterator &PIV) {
2390 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
2391 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
2392 RecordDecl::field_iterator ifield = RD->field_begin();
2393 RecordDecl::field_iterator pfield = RD->field_end();
2394 while (countSuperClassIvars-- > 0) {
2395 pfield = ifield;
2396 ++ifield;
2397 }
2398 FIV = ifield;
2399 PIV = pfield;
2400 return RD;
2401}
2402
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002403void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002404 EmitModuleInfo();
2405
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002406 // Emit the dummy bodies for any protocols which were referenced but
2407 // never defined.
2408 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2409 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2410 if (i->second->hasInitializer())
2411 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002412
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002413 std::vector<llvm::Constant*> Values(5);
2414 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2415 Values[1] = GetClassName(i->first);
2416 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2417 Values[3] = Values[4] =
2418 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2419 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2420 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2421 Values));
2422 }
2423
2424 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002425 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002426 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002427 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002428 }
2429
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002430 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002431 llvm::GlobalValue *GV =
2432 new llvm::GlobalVariable(AT, false,
2433 llvm::GlobalValue::AppendingLinkage,
2434 llvm::ConstantArray::get(AT, Used),
2435 "llvm.used",
2436 &CGM.getModule());
2437
2438 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002439
2440 // Add assembler directives to add lazy undefined symbol references
2441 // for classes which are referenced but not defined. This is
2442 // important for correct linker interaction.
2443
2444 // FIXME: Uh, this isn't particularly portable.
2445 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00002446
2447 if (!CGM.getModule().getModuleInlineAsm().empty())
2448 s << "\n";
2449
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002450 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2451 e = LazySymbols.end(); i != e; ++i) {
2452 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2453 }
2454 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2455 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002456 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002457 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2458 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00002459
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002460 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002461}
2462
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002463CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002464 : CGObjCCommonMac(cgm),
2465 ObjCTypes(cgm)
2466{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002467 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002468 ObjCABI = 2;
2469}
2470
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002471/* *** */
2472
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002473ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2474: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002475{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002476 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2477 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002478
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002479 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002480 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002481 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002482 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2483
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002484 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002485 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002486 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002487
2488 // FIXME: It would be nice to unify this with the opaque type, so
2489 // that the IR comes out a bit cleaner.
2490 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2491 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002492
2493 // I'm not sure I like this. The implicit coordination is a bit
2494 // gross. We should solve this in a reasonable fashion because this
2495 // is a pretty common task (match some runtime data structure with
2496 // an LLVM data structure).
2497
2498 // FIXME: This is leaked.
2499 // FIXME: Merge with rewriter code?
2500
2501 // struct _objc_super {
2502 // id self;
2503 // Class cls;
2504 // }
2505 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2506 SourceLocation(),
2507 &Ctx.Idents.get("_objc_super"));
2508 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2509 Ctx.getObjCIdType(), 0, false));
2510 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2511 Ctx.getObjCClassType(), 0, false));
2512 RD->completeDefinition(Ctx);
2513
2514 SuperCTy = Ctx.getTagDeclType(RD);
2515 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2516
2517 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002518 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2519
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002520 // struct _prop_t {
2521 // char *name;
2522 // char *attributes;
2523 // }
2524 PropertyTy = llvm::StructType::get(Int8PtrTy,
2525 Int8PtrTy,
2526 NULL);
2527 CGM.getModule().addTypeName("struct._prop_t",
2528 PropertyTy);
2529
2530 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002531 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002532 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002533 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002534 // }
2535 PropertyListTy = llvm::StructType::get(IntTy,
2536 IntTy,
2537 llvm::ArrayType::get(PropertyTy, 0),
2538 NULL);
2539 CGM.getModule().addTypeName("struct._prop_list_t",
2540 PropertyListTy);
2541 // struct _prop_list_t *
2542 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2543
2544 // struct _objc_method {
2545 // SEL _cmd;
2546 // char *method_type;
2547 // char *_imp;
2548 // }
2549 MethodTy = llvm::StructType::get(SelectorPtrTy,
2550 Int8PtrTy,
2551 Int8PtrTy,
2552 NULL);
2553 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002554
2555 // struct _objc_cache *
2556 CacheTy = llvm::OpaqueType::get();
2557 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2558 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002559
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002560 // Property manipulation functions.
2561
2562 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
2563 std::vector<const llvm::Type*> Params;
2564 Params.push_back(ObjectPtrTy);
2565 Params.push_back(SelectorPtrTy);
2566 Params.push_back(LongTy);
2567 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2568 GetPropertyFn =
2569 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2570 Params,
2571 false),
2572 "objc_getProperty");
2573
2574 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2575 Params.clear();
2576 Params.push_back(ObjectPtrTy);
2577 Params.push_back(SelectorPtrTy);
2578 Params.push_back(LongTy);
2579 Params.push_back(ObjectPtrTy);
2580 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2581 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2582 SetPropertyFn =
2583 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2584 Params,
2585 false),
2586 "objc_setProperty");
2587 // Enumeration mutation.
2588
2589 Params.clear();
2590 Params.push_back(ObjectPtrTy);
2591 EnumerationMutationFn =
2592 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2593 Params,
2594 false),
2595 "objc_enumerationMutation");
2596
2597 // gc's API
2598 // id objc_read_weak (id *)
2599 Params.clear();
2600 Params.push_back(PtrObjectPtrTy);
2601 GcReadWeakFn =
2602 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2603 Params,
2604 false),
2605 "objc_read_weak");
2606 // id objc_assign_weak (id, id *)
2607 Params.clear();
2608 Params.push_back(ObjectPtrTy);
2609 Params.push_back(PtrObjectPtrTy);
2610 GcAssignWeakFn =
2611 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2612 Params,
2613 false),
2614 "objc_assign_weak");
2615 GcAssignGlobalFn =
2616 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2617 Params,
2618 false),
2619 "objc_assign_global");
2620 GcAssignIvarFn =
2621 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2622 Params,
2623 false),
2624 "objc_assign_ivar");
2625 GcAssignStrongCastFn =
2626 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2627 Params,
2628 false),
2629 "objc_assign_strongCast");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002630}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002631
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002632ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2633 : ObjCCommonTypesHelper(cgm)
2634{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002635 // struct _objc_method_description {
2636 // SEL name;
2637 // char *types;
2638 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002639 MethodDescriptionTy =
2640 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002641 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002642 NULL);
2643 CGM.getModule().addTypeName("struct._objc_method_description",
2644 MethodDescriptionTy);
2645
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002646 // struct _objc_method_description_list {
2647 // int count;
2648 // struct _objc_method_description[1];
2649 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002650 MethodDescriptionListTy =
2651 llvm::StructType::get(IntTy,
2652 llvm::ArrayType::get(MethodDescriptionTy, 0),
2653 NULL);
2654 CGM.getModule().addTypeName("struct._objc_method_description_list",
2655 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002656
2657 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002658 MethodDescriptionListPtrTy =
2659 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2660
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002661 // Protocol description structures
2662
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002663 // struct _objc_protocol_extension {
2664 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2665 // struct _objc_method_description_list *optional_instance_methods;
2666 // struct _objc_method_description_list *optional_class_methods;
2667 // struct _objc_property_list *instance_properties;
2668 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002669 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002670 llvm::StructType::get(IntTy,
2671 MethodDescriptionListPtrTy,
2672 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002673 PropertyListPtrTy,
2674 NULL);
2675 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2676 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002677
2678 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002679 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2680
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002681 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002682
2683 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2684 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2685
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002686 const llvm::Type *T =
2687 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2688 LongTy,
2689 llvm::ArrayType::get(ProtocolTyHolder, 0),
2690 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002691 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2692
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002693 // struct _objc_protocol {
2694 // struct _objc_protocol_extension *isa;
2695 // char *protocol_name;
2696 // struct _objc_protocol **_objc_protocol_list;
2697 // struct _objc_method_description_list *instance_methods;
2698 // struct _objc_method_description_list *class_methods;
2699 // }
2700 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002701 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002702 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2703 MethodDescriptionListPtrTy,
2704 MethodDescriptionListPtrTy,
2705 NULL);
2706 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2707
2708 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2709 CGM.getModule().addTypeName("struct._objc_protocol_list",
2710 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002711 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002712 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2713
2714 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002715 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002716 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002717
2718 // Class description structures
2719
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002720 // struct _objc_ivar {
2721 // char *ivar_name;
2722 // char *ivar_type;
2723 // int ivar_offset;
2724 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002725 IvarTy = llvm::StructType::get(Int8PtrTy,
2726 Int8PtrTy,
2727 IntTy,
2728 NULL);
2729 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2730
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002731 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002732 IvarListTy = llvm::OpaqueType::get();
2733 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2734 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2735
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002736 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002737 MethodListTy = llvm::OpaqueType::get();
2738 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2739 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2740
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002741 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002742 ClassExtensionTy =
2743 llvm::StructType::get(IntTy,
2744 Int8PtrTy,
2745 PropertyListPtrTy,
2746 NULL);
2747 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2748 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2749
2750 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2751
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002752 // struct _objc_class {
2753 // Class isa;
2754 // Class super_class;
2755 // char *name;
2756 // long version;
2757 // long info;
2758 // long instance_size;
2759 // struct _objc_ivar_list *ivars;
2760 // struct _objc_method_list *methods;
2761 // struct _objc_cache *cache;
2762 // struct _objc_protocol_list *protocols;
2763 // char *ivar_layout;
2764 // struct _objc_class_ext *ext;
2765 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002766 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2767 llvm::PointerType::getUnqual(ClassTyHolder),
2768 Int8PtrTy,
2769 LongTy,
2770 LongTy,
2771 LongTy,
2772 IvarListPtrTy,
2773 MethodListPtrTy,
2774 CachePtrTy,
2775 ProtocolListPtrTy,
2776 Int8PtrTy,
2777 ClassExtensionPtrTy,
2778 NULL);
2779 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2780
2781 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2782 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2783 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2784
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002785 // struct _objc_category {
2786 // char *category_name;
2787 // char *class_name;
2788 // struct _objc_method_list *instance_method;
2789 // struct _objc_method_list *class_method;
2790 // uint32_t size; // sizeof(struct _objc_category)
2791 // struct _objc_property_list *instance_properties;// category's @property
2792 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002793 CategoryTy = llvm::StructType::get(Int8PtrTy,
2794 Int8PtrTy,
2795 MethodListPtrTy,
2796 MethodListPtrTy,
2797 ProtocolListPtrTy,
2798 IntTy,
2799 PropertyListPtrTy,
2800 NULL);
2801 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2802
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002803 // Global metadata structures
2804
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002805 // struct _objc_symtab {
2806 // long sel_ref_cnt;
2807 // SEL *refs;
2808 // short cls_def_cnt;
2809 // short cat_def_cnt;
2810 // char *defs[cls_def_cnt + cat_def_cnt];
2811 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002812 SymtabTy = llvm::StructType::get(LongTy,
2813 SelectorPtrTy,
2814 ShortTy,
2815 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002816 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002817 NULL);
2818 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2819 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2820
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002821 // struct _objc_module {
2822 // long version;
2823 // long size; // sizeof(struct _objc_module)
2824 // char *name;
2825 // struct _objc_symtab* symtab;
2826 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002827 ModuleTy =
2828 llvm::StructType::get(LongTy,
2829 LongTy,
2830 Int8PtrTy,
2831 SymtabPtrTy,
2832 NULL);
2833 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002834
Daniel Dunbar49f66022008-09-24 03:38:44 +00002835 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002836
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002837 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002838 std::vector<const llvm::Type*> Params;
2839 Params.push_back(ObjectPtrTy);
2840 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002841 MessageSendFn =
2842 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2843 Params,
2844 true),
2845 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002846
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002847 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002848 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002849 Params.push_back(ObjectPtrTy);
2850 Params.push_back(SelectorPtrTy);
2851 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002852 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2853 Params,
2854 true),
2855 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002856
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002857 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00002858 Params.clear();
2859 Params.push_back(ObjectPtrTy);
2860 Params.push_back(SelectorPtrTy);
2861 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002862 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00002863 MessageSendFpretFn =
2864 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
2865 Params,
2866 true),
2867 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002868
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002869 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002870 Params.clear();
2871 Params.push_back(SuperPtrTy);
2872 Params.push_back(SelectorPtrTy);
2873 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002874 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2875 Params,
2876 true),
2877 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002878
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002879 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
2880 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002881 Params.clear();
2882 Params.push_back(Int8PtrTy);
2883 Params.push_back(SuperPtrTy);
2884 Params.push_back(SelectorPtrTy);
2885 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002886 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2887 Params,
2888 true),
2889 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002890
2891 // There is no objc_msgSendSuper_fpret? How can that work?
2892 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002893
Anders Carlsson124526b2008-09-09 10:10:21 +00002894 // FIXME: This is the size of the setjmp buffer and should be
2895 // target specific. 18 is what's used on 32-bit X86.
2896 uint64_t SetJmpBufferSize = 18;
2897
2898 // Exceptions
2899 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00002900 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00002901
2902 ExceptionDataTy =
2903 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
2904 SetJmpBufferSize),
2905 StackPtrTy, NULL);
2906 CGM.getModule().addTypeName("struct._objc_exception_data",
2907 ExceptionDataTy);
2908
2909 Params.clear();
2910 Params.push_back(ObjectPtrTy);
2911 ExceptionThrowFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002912 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2913 Params,
2914 false),
2915 "objc_exception_throw");
Anders Carlsson124526b2008-09-09 10:10:21 +00002916
2917 Params.clear();
2918 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
2919 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002920 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2921 Params,
2922 false),
2923 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00002924 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002925 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2926 Params,
2927 false),
2928 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00002929 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002930 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2931 Params,
2932 false),
2933 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00002934
2935 Params.clear();
2936 Params.push_back(ClassPtrTy);
2937 Params.push_back(ObjectPtrTy);
2938 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002939 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2940 Params,
2941 false),
2942 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00002943
2944 // synchronized APIs
2945 // void objc_sync_enter (id)
2946 Params.clear();
2947 Params.push_back(ObjectPtrTy);
2948 SyncEnterFn =
2949 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2950 Params,
2951 false),
2952 "objc_sync_enter");
2953 // void objc_sync_exit (id)
2954 SyncExitFn =
2955 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2956 Params,
2957 false),
2958 "objc_sync_exit");
2959
Anders Carlsson124526b2008-09-09 10:10:21 +00002960
2961 Params.clear();
2962 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
2963 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002964 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2965 Params,
2966 false),
2967 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002968
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002969}
2970
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002971ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002972: ObjCCommonTypesHelper(cgm)
2973{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002974 // struct _method_list_t {
2975 // uint32_t entsize; // sizeof(struct _objc_method)
2976 // uint32_t method_count;
2977 // struct _objc_method method_list[method_count];
2978 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002979 MethodListnfABITy = llvm::StructType::get(IntTy,
2980 IntTy,
2981 llvm::ArrayType::get(MethodTy, 0),
2982 NULL);
2983 CGM.getModule().addTypeName("struct.__method_list_t",
2984 MethodListnfABITy);
2985 // struct method_list_t *
2986 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002987
2988 // struct _protocol_t {
2989 // id isa; // NULL
2990 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002991 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002992 // const struct method_list_t * const instance_methods;
2993 // const struct method_list_t * const class_methods;
2994 // const struct method_list_t *optionalInstanceMethods;
2995 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002996 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002997 // const uint32_t size; // sizeof(struct _protocol_t)
2998 // const uint32_t flags; // = 0
2999 // }
3000
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003001 // Holder for struct _protocol_list_t *
3002 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3003
3004 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3005 Int8PtrTy,
3006 llvm::PointerType::getUnqual(
3007 ProtocolListTyHolder),
3008 MethodListnfABIPtrTy,
3009 MethodListnfABIPtrTy,
3010 MethodListnfABIPtrTy,
3011 MethodListnfABIPtrTy,
3012 PropertyListPtrTy,
3013 IntTy,
3014 IntTy,
3015 NULL);
3016 CGM.getModule().addTypeName("struct._protocol_t",
3017 ProtocolnfABITy);
3018
Fariborz Jahanianda320092009-01-29 19:24:30 +00003019 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003020 // long protocol_count; // Note, this is 32/64 bit
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003021 // struct _protocol_t[protocol_count];
3022 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003023 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3024 llvm::ArrayType::get(
3025 ProtocolnfABITy, 0),
3026 NULL);
3027 CGM.getModule().addTypeName("struct._objc_protocol_list",
3028 ProtocolListnfABITy);
3029
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003030 // struct _objc_protocol_list*
3031 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003032
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003033 // FIXME! Is this doing the right thing?
3034 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3035 ProtocolListnfABIPtrTy);
3036
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003037 // struct _ivar_t {
3038 // unsigned long int *offset; // pointer to ivar offset location
3039 // char *name;
3040 // char *type;
3041 // uint32_t alignment;
3042 // uint32_t size;
3043 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003044 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3045 Int8PtrTy,
3046 Int8PtrTy,
3047 IntTy,
3048 IntTy,
3049 NULL);
3050 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3051
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003052 // struct _ivar_list_t {
3053 // uint32 entsize; // sizeof(struct _ivar_t)
3054 // uint32 count;
3055 // struct _iver_t list[count];
3056 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003057 IvarListnfABITy = llvm::StructType::get(IntTy,
3058 IntTy,
3059 llvm::ArrayType::get(
3060 IvarnfABITy, 0),
3061 NULL);
3062 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3063
3064 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003065
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003066 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003067 // uint32_t const flags;
3068 // uint32_t const instanceStart;
3069 // uint32_t const instanceSize;
3070 // uint32_t const reserved; // only when building for 64bit targets
3071 // const uint8_t * const ivarLayout;
3072 // const char *const name;
3073 // const struct _method_list_t * const baseMethods;
3074 // const struct _objc_protocol_list *const baseProtocols;
3075 // const struct _ivar_list_t *const ivars;
3076 // const uint8_t * const weakIvarLayout;
3077 // const struct _prop_list_t * const properties;
3078 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003079
3080 // FIXME. Add 'reserved' field in 64bit abi mode!
3081 ClassRonfABITy = llvm::StructType::get(IntTy,
3082 IntTy,
3083 IntTy,
3084 Int8PtrTy,
3085 Int8PtrTy,
3086 MethodListnfABIPtrTy,
3087 ProtocolListnfABIPtrTy,
3088 IvarListnfABIPtrTy,
3089 Int8PtrTy,
3090 PropertyListPtrTy,
3091 NULL);
3092 CGM.getModule().addTypeName("struct._class_ro_t",
3093 ClassRonfABITy);
3094
3095 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3096 std::vector<const llvm::Type*> Params;
3097 Params.push_back(ObjectPtrTy);
3098 Params.push_back(SelectorPtrTy);
3099 ImpnfABITy = llvm::PointerType::getUnqual(
3100 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3101
3102 // struct _class_t {
3103 // struct _class_t *isa;
3104 // struct _class_t * const superclass;
3105 // void *cache;
3106 // IMP *vtable;
3107 // struct class_ro_t *ro;
3108 // }
3109
3110 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3111 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3112 llvm::PointerType::getUnqual(ClassTyHolder),
3113 CachePtrTy,
3114 llvm::PointerType::getUnqual(ImpnfABITy),
3115 llvm::PointerType::getUnqual(
3116 ClassRonfABITy),
3117 NULL);
3118 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3119
3120 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3121 ClassnfABITy);
3122
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003123 // LLVM for struct _class_t *
3124 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3125
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003126 // struct _category_t {
3127 // const char * const name;
3128 // struct _class_t *const cls;
3129 // const struct _method_list_t * const instance_methods;
3130 // const struct _method_list_t * const class_methods;
3131 // const struct _protocol_list_t * const protocols;
3132 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003133 // }
3134 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003135 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003136 MethodListnfABIPtrTy,
3137 MethodListnfABIPtrTy,
3138 ProtocolListnfABIPtrTy,
3139 PropertyListPtrTy,
3140 NULL);
3141 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
3142
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003143}
3144
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003145llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3146 FinishNonFragileABIModule();
3147
3148 return NULL;
3149}
3150
3151void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3152 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003153
3154 // Build list of all implemented classe addresses in array
3155 // L_OBJC_LABEL_CLASS_$.
3156 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
3157 // list of 'nonlazy' implementations (defined as those with a +load{}
3158 // method!!).
3159 unsigned NumClasses = DefinedClasses.size();
3160 if (NumClasses) {
3161 std::vector<llvm::Constant*> Symbols(NumClasses);
3162 for (unsigned i=0; i<NumClasses; i++)
3163 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3164 ObjCTypes.Int8PtrTy);
3165 llvm::Constant* Init =
3166 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3167 NumClasses),
3168 Symbols);
3169
3170 llvm::GlobalVariable *GV =
3171 new llvm::GlobalVariable(Init->getType(), false,
3172 llvm::GlobalValue::InternalLinkage,
3173 Init,
3174 "\01L_OBJC_LABEL_CLASS_$",
3175 &CGM.getModule());
3176 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
3177 UsedGlobals.push_back(GV);
3178 }
3179
3180 // Build list of all implemented category addresses in array
3181 // L_OBJC_LABEL_CATEGORY_$.
3182 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
3183 // list of 'nonlazy' category implementations (defined as those with a +load{}
3184 // method!!).
3185 unsigned NumCategory = DefinedCategories.size();
3186 if (NumCategory) {
3187 std::vector<llvm::Constant*> Symbols(NumCategory);
3188 for (unsigned i=0; i<NumCategory; i++)
3189 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
3190 ObjCTypes.Int8PtrTy);
3191 llvm::Constant* Init =
3192 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3193 NumCategory),
3194 Symbols);
3195
3196 llvm::GlobalVariable *GV =
3197 new llvm::GlobalVariable(Init->getType(), false,
3198 llvm::GlobalValue::InternalLinkage,
3199 Init,
3200 "\01L_OBJC_LABEL_CATEGORY_$",
3201 &CGM.getModule());
3202 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
3203 UsedGlobals.push_back(GV);
3204 }
3205
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00003206 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
3207 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
3208 std::vector<llvm::Constant*> Values(2);
3209 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3210 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3211 llvm::Constant* Init = llvm::ConstantArray::get(
3212 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
3213 Values);
3214 llvm::GlobalVariable *IMGV =
3215 new llvm::GlobalVariable(Init->getType(), false,
3216 llvm::GlobalValue::InternalLinkage,
3217 Init,
3218 "\01L_OBJC_IMAGE_INFO",
3219 &CGM.getModule());
3220 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
3221 UsedGlobals.push_back(IMGV);
3222
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003223 std::vector<llvm::Constant*> Used;
3224 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3225 e = UsedGlobals.end(); i != e; ++i) {
3226 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3227 }
3228
3229 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3230 llvm::GlobalValue *GV =
3231 new llvm::GlobalVariable(AT, false,
3232 llvm::GlobalValue::AppendingLinkage,
3233 llvm::ConstantArray::get(AT, Used),
3234 "llvm.used",
3235 &CGM.getModule());
3236
3237 GV->setSection("llvm.metadata");
3238
3239}
3240
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003241// Metadata flags
3242enum MetaDataDlags {
3243 CLS = 0x0,
3244 CLS_META = 0x1,
3245 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003246 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003247 CLS_EXCEPTION = 0x20
3248};
3249/// BuildClassRoTInitializer - generate meta-data for:
3250/// struct _class_ro_t {
3251/// uint32_t const flags;
3252/// uint32_t const instanceStart;
3253/// uint32_t const instanceSize;
3254/// uint32_t const reserved; // only when building for 64bit targets
3255/// const uint8_t * const ivarLayout;
3256/// const char *const name;
3257/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00003258/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003259/// const struct _ivar_list_t *const ivars;
3260/// const uint8_t * const weakIvarLayout;
3261/// const struct _prop_list_t * const properties;
3262/// }
3263///
3264llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
3265 unsigned flags,
3266 unsigned InstanceStart,
3267 unsigned InstanceSize,
3268 const ObjCImplementationDecl *ID) {
3269 std::string ClassName = ID->getNameAsString();
3270 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
3271 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
3272 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
3273 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
3274 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianda320092009-01-29 19:24:30 +00003275 // FIXME. ivarLayout is currently null!
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003276 Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3277 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003278 // const struct _method_list_t * const baseMethods;
3279 std::vector<llvm::Constant*> Methods;
3280 std::string MethodListName("\01l_OBJC_$_");
3281 if (flags & CLS_META) {
3282 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
3283 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
3284 e = ID->classmeth_end(); i != e; ++i) {
3285 // Class methods should always be defined.
3286 Methods.push_back(GetMethodConstant(*i));
3287 }
3288 } else {
3289 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
3290 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
3291 e = ID->instmeth_end(); i != e; ++i) {
3292 // Instance methods should always be defined.
3293 Methods.push_back(GetMethodConstant(*i));
3294 }
Fariborz Jahanian939abce2009-01-28 22:46:49 +00003295 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
3296 e = ID->propimpl_end(); i != e; ++i) {
3297 ObjCPropertyImplDecl *PID = *i;
3298
3299 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
3300 ObjCPropertyDecl *PD = PID->getPropertyDecl();
3301
3302 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3303 if (llvm::Constant *C = GetMethodConstant(MD))
3304 Methods.push_back(C);
3305 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3306 if (llvm::Constant *C = GetMethodConstant(MD))
3307 Methods.push_back(C);
3308 }
3309 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003310 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003311 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003312 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003313
3314 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3315 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
3316 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
3317 + OID->getNameAsString(),
3318 OID->protocol_begin(),
3319 OID->protocol_end());
3320
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003321 if (flags & CLS_META)
3322 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3323 else
3324 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003325 // FIXME. weakIvarLayout is currently null.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003326 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003327 if (flags & CLS_META)
3328 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3329 else
3330 Values[ 9] =
3331 EmitPropertyList(
3332 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
3333 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003334 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
3335 Values);
3336 llvm::GlobalVariable *CLASS_RO_GV =
3337 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
3338 llvm::GlobalValue::InternalLinkage,
3339 Init,
3340 (flags & CLS_META) ?
3341 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
3342 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
3343 &CGM.getModule());
Fariborz Jahanian014858b2009-01-30 23:15:42 +00003344 CLASS_RO_GV->setAlignment(GetPointerAlign());
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003345 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003346 UsedGlobals.push_back(CLASS_RO_GV);
3347 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003348
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003349}
3350
3351/// BuildClassMetaData - This routine defines that to-level meta-data
3352/// for the given ClassName for:
3353/// struct _class_t {
3354/// struct _class_t *isa;
3355/// struct _class_t * const superclass;
3356/// void *cache;
3357/// IMP *vtable;
3358/// struct class_ro_t *ro;
3359/// }
3360///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003361llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
3362 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003363 llvm::Constant *IsAGV,
3364 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003365 llvm::Constant *ClassRoGV,
3366 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003367 std::vector<llvm::Constant*> Values(5);
3368 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003369 Values[1] = SuperClassGV
3370 ? SuperClassGV
3371 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003372 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
3373 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
3374 Values[4] = ClassRoGV; // &CLASS_RO_GV
3375 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
3376 Values);
3377 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName);
3378 if (GV)
3379 GV->setInitializer(Init);
3380 else
3381 GV =
3382 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3383 llvm::GlobalValue::ExternalLinkage,
3384 Init,
3385 ClassName,
3386 &CGM.getModule());
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00003387 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian014858b2009-01-30 23:15:42 +00003388 GV->setAlignment(GetPointerAlign());
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003389 if (HiddenVisibility)
3390 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003391 UsedGlobals.push_back(GV);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003392 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003393}
3394
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003395void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
3396 std::string ClassName = ID->getNameAsString();
3397 if (!ObjCEmptyCacheVar) {
3398 ObjCEmptyCacheVar = new llvm::GlobalVariable(
3399 ObjCTypes.CachePtrTy,
3400 false,
3401 llvm::GlobalValue::ExternalLinkage,
3402 0,
3403 "\01_objc_empty_cache",
3404 &CGM.getModule());
3405 UsedGlobals.push_back(ObjCEmptyCacheVar);
3406
3407 ObjCEmptyVtableVar = new llvm::GlobalVariable(
3408 llvm::PointerType::getUnqual(
3409 ObjCTypes.ImpnfABITy),
3410 false,
3411 llvm::GlobalValue::ExternalLinkage,
3412 0,
3413 "\01_objc_empty_vtable",
3414 &CGM.getModule());
3415 UsedGlobals.push_back(ObjCEmptyVtableVar);
3416 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003417 assert(ID->getClassInterface() &&
3418 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003419 uint32_t InstanceStart =
3420 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
3421 uint32_t InstanceSize = InstanceStart;
3422 uint32_t flags = CLS_META;
3423 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
3424 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003425
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003426 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003427
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003428 bool classIsHidden = IsClassHidden(ID->getClassInterface());
3429 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003430 flags |= OBJC2_CLS_HIDDEN;
3431 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003432 // class is root
3433 flags |= CLS_ROOT;
3434 std::string SuperClassName = ObjCClassName + ClassName;
3435 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3436 if (!SuperClassGV)
3437 SuperClassGV =
3438 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3439 llvm::GlobalValue::ExternalLinkage,
3440 0,
3441 SuperClassName,
3442 &CGM.getModule());
3443 UsedGlobals.push_back(SuperClassGV);
3444 std::string IsAClassName = ObjCMetaClassName + ClassName;
3445 IsAGV = CGM.getModule().getGlobalVariable(IsAClassName);
3446 if (!IsAGV)
3447 IsAGV =
3448 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3449 llvm::GlobalValue::ExternalLinkage,
3450 0,
3451 IsAClassName,
3452 &CGM.getModule());
3453 UsedGlobals.push_back(IsAGV);
3454 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003455 // Has a root. Current class is not a root.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003456 std::string RootClassName =
3457 ID->getClassInterface()->getSuperClass()->getNameAsString();
3458 std::string SuperClassName = ObjCMetaClassName + RootClassName;
3459 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3460 if (!SuperClassGV)
3461 SuperClassGV =
3462 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3463 llvm::GlobalValue::ExternalLinkage,
3464 0,
3465 SuperClassName,
3466 &CGM.getModule());
3467 UsedGlobals.push_back(SuperClassGV);
3468 IsAGV = SuperClassGV;
3469 }
3470 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
3471 InstanceStart,
3472 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003473 std::string TClassName = ObjCMetaClassName + ClassName;
3474 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003475 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
3476 classIsHidden);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003477
3478 // Metadata for the class
3479 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003480 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003481 flags |= OBJC2_CLS_HIDDEN;
3482 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003483 flags |= CLS_ROOT;
3484 SuperClassGV = 0;
3485 }
3486 else {
3487 // Has a root. Current class is not a root.
3488 std::string RootClassName =
3489 ID->getClassInterface()->getSuperClass()->getNameAsString();
3490 std::string SuperClassName = ObjCClassName + RootClassName;
3491 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3492 if (!SuperClassGV)
3493 SuperClassGV =
3494 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3495 llvm::GlobalValue::ExternalLinkage,
3496 0,
3497 SuperClassName,
3498 &CGM.getModule());
3499 UsedGlobals.push_back(SuperClassGV);
3500
3501 }
3502
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003503 InstanceStart = InstanceSize = 0;
3504 if (ObjCInterfaceDecl *OID =
3505 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
3506 // FIXME. Share this with the one in EmitIvarList.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003507 const llvm::Type *InterfaceTy =
3508 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
3509 const llvm::StructLayout *Layout =
3510 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003511
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003512 RecordDecl::field_iterator firstField, lastField;
3513 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003514
3515 for (RecordDecl::field_iterator e = RD->field_end(),
3516 ifield = firstField; ifield != e; ++ifield)
3517 lastField = ifield;
3518
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003519 if (lastField != RD->field_end()) {
3520 FieldDecl *Field = *lastField;
3521 const llvm::Type *FieldTy =
3522 CGM.getTypes().ConvertTypeForMem(Field->getType());
3523 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3524 InstanceSize = Layout->getElementOffset(
3525 CGM.getTypes().getLLVMFieldNo(Field)) +
3526 Size;
3527 if (firstField == RD->field_end())
3528 InstanceStart = InstanceSize;
3529 else
3530 InstanceStart = Layout->getElementOffset(CGM.getTypes().
3531 getLLVMFieldNo(*firstField));
3532 }
3533 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003534 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003535 InstanceStart,
3536 InstanceSize,
3537 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003538
3539 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003540 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003541 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
3542 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003543 DefinedClasses.push_back(ClassMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003544}
3545
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00003546/// GenerateProtocolRef - This routine is called to generate code for
3547/// a protocol reference expression; as in:
3548/// @code
3549/// @protocol(Proto1);
3550/// @endcode
3551/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
3552/// which will hold address of the protocol meta-data.
3553///
3554llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
3555 const ObjCProtocolDecl *PD) {
3556
3557 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
3558 ObjCTypes.ExternalProtocolPtrTy);
3559
3560 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
3561 ProtocolName += PD->getNameAsCString();
3562
3563 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
3564 if (PTGV)
3565 return Builder.CreateLoad(PTGV, false, "tmp");
3566 PTGV = new llvm::GlobalVariable(
3567 Init->getType(), false,
3568 llvm::GlobalValue::WeakLinkage,
3569 Init,
3570 ProtocolName,
3571 &CGM.getModule());
3572 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
3573 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3574 UsedGlobals.push_back(PTGV);
3575 return Builder.CreateLoad(PTGV, false, "tmp");
3576}
3577
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003578/// GenerateCategory - Build metadata for a category implementation.
3579/// struct _category_t {
3580/// const char * const name;
3581/// struct _class_t *const cls;
3582/// const struct _method_list_t * const instance_methods;
3583/// const struct _method_list_t * const class_methods;
3584/// const struct _protocol_list_t * const protocols;
3585/// const struct _prop_list_t * const properties;
3586/// }
3587///
3588void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
3589{
3590 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003591 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
3592 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003593 "_$_" + OCD->getNameAsString());
3594 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString());
3595
3596 std::vector<llvm::Constant*> Values(6);
3597 Values[0] = GetClassName(OCD->getIdentifier());
3598 // meta-class entry symbol
3599 llvm::GlobalVariable *ClassGV =
3600 CGM.getModule().getGlobalVariable(ExtClassName);
3601 if (!ClassGV)
3602 ClassGV =
3603 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3604 llvm::GlobalValue::ExternalLinkage,
3605 0,
3606 ExtClassName,
3607 &CGM.getModule());
3608 UsedGlobals.push_back(ClassGV);
3609 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003610 std::vector<llvm::Constant*> Methods;
3611 std::string MethodListName(Prefix);
3612 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
3613 "_$_" + OCD->getNameAsString();
3614
3615 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
3616 e = OCD->instmeth_end(); i != e; ++i) {
3617 // Instance methods should always be defined.
3618 Methods.push_back(GetMethodConstant(*i));
3619 }
3620
3621 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003622 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003623 Methods);
3624
3625 MethodListName = Prefix;
3626 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
3627 OCD->getNameAsString();
3628 Methods.clear();
3629 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
3630 e = OCD->classmeth_end(); i != e; ++i) {
3631 // Class methods should always be defined.
3632 Methods.push_back(GetMethodConstant(*i));
3633 }
3634
3635 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003636 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003637 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003638 const ObjCCategoryDecl *Category =
3639 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanianeb732132009-01-29 23:23:06 +00003640 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
3641 + Interface->getNameAsString() + "_$_"
3642 + Category->getNameAsString(),
3643 Category->protocol_begin(),
3644 Category->protocol_end());
3645
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003646 std::string ExtName(Interface->getNameAsString() + "_$_" +
3647 OCD->getNameAsString());
3648 Values[5] =
3649 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
3650 OCD, Category, ObjCTypes);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003651 llvm::Constant *Init =
3652 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
3653 Values);
3654 llvm::GlobalVariable *GCATV
3655 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
3656 false,
3657 llvm::GlobalValue::InternalLinkage,
3658 Init,
3659 ExtCatName,
3660 &CGM.getModule());
Fariborz Jahanian014858b2009-01-30 23:15:42 +00003661 GCATV->setAlignment(GetPointerAlign());
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003662 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003663 UsedGlobals.push_back(GCATV);
3664 DefinedCategories.push_back(GCATV);
3665}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003666
3667/// GetMethodConstant - Return a struct objc_method constant for the
3668/// given method if it has been defined. The result is null if the
3669/// method has not been defined. The return value has type MethodPtrTy.
3670llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
3671 const ObjCMethodDecl *MD) {
3672 // FIXME: Use DenseMap::lookup
3673 llvm::Function *Fn = MethodDefinitions[MD];
3674 if (!Fn)
3675 return 0;
3676
3677 std::vector<llvm::Constant*> Method(3);
3678 Method[0] =
3679 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
3680 ObjCTypes.SelectorPtrTy);
3681 Method[1] = GetMethodVarType(MD);
3682 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
3683 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
3684}
3685
3686/// EmitMethodList - Build meta-data for method declarations
3687/// struct _method_list_t {
3688/// uint32_t entsize; // sizeof(struct _objc_method)
3689/// uint32_t method_count;
3690/// struct _objc_method method_list[method_count];
3691/// }
3692///
3693llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
3694 const std::string &Name,
3695 const char *Section,
3696 const ConstantVector &Methods) {
3697 // Return null for empty list.
3698 if (Methods.empty())
3699 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
3700
3701 std::vector<llvm::Constant*> Values(3);
3702 // sizeof(struct _objc_method)
3703 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
3704 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3705 // method_count
3706 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
3707 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
3708 Methods.size());
3709 Values[2] = llvm::ConstantArray::get(AT, Methods);
3710 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3711
3712 llvm::GlobalVariable *GV =
3713 new llvm::GlobalVariable(Init->getType(), false,
3714 llvm::GlobalValue::InternalLinkage,
3715 Init,
3716 Name,
3717 &CGM.getModule());
Fariborz Jahanian014858b2009-01-30 23:15:42 +00003718 GV->setAlignment(GetPointerAlign());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003719 GV->setSection(Section);
3720 UsedGlobals.push_back(GV);
3721 return llvm::ConstantExpr::getBitCast(GV,
3722 ObjCTypes.MethodListnfABIPtrTy);
3723}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003724
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003725llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
3726 const ObjCImplementationDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003727 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003728 unsigned long int Offset) {
3729
3730 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003731 + Ivar->getNameAsString());
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003732 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
3733
3734 llvm::GlobalVariable *IvarOffsetGV =
3735 CGM.getModule().getGlobalVariable(ExternalName);
3736 if (IvarOffsetGV) {
3737 // ivar offset symbol already built due to user code referencing it.
3738 IvarOffsetGV->setInitializer(Init);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003739 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003740 return IvarOffsetGV;
3741 }
3742
3743 IvarOffsetGV =
3744 new llvm::GlobalVariable(Init->getType(),
3745 false,
3746 llvm::GlobalValue::ExternalLinkage,
3747 Init,
3748 ExternalName,
3749 &CGM.getModule());
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003750 // @private and @package have hidden visibility.
3751 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public ||
3752 Ivar->getAccessControl() == ObjCIvarDecl::Protected);
3753 if (!globalVisibility)
3754 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003755 else
3756 if (const ObjCInterfaceDecl *OID = ID->getClassInterface())
3757 if (IsClassHidden(OID))
3758 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3759
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003760 IvarOffsetGV->setSection("__DATA, __objc_const");
3761 UsedGlobals.push_back(IvarOffsetGV);
3762
3763 return llvm::ConstantExpr::getBitCast(
3764 IvarOffsetGV,
3765 llvm::PointerType::getUnqual(ObjCTypes.LongTy));
3766}
3767
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003768/// EmitIvarList - Emit the ivar list for the given
3769/// implementation. If ForClass is true the list of class ivars
3770/// (i.e. metaclass ivars) is emitted, otherwise the list of
3771/// interface ivars will be emitted. The return value has type
3772/// IvarListnfABIPtrTy.
3773/// struct _ivar_t {
3774/// unsigned long int *offset; // pointer to ivar offset location
3775/// char *name;
3776/// char *type;
3777/// uint32_t alignment;
3778/// uint32_t size;
3779/// }
3780/// struct _ivar_list_t {
3781/// uint32 entsize; // sizeof(struct _ivar_t)
3782/// uint32 count;
3783/// struct _iver_t list[count];
3784/// }
3785///
3786llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
3787 const ObjCImplementationDecl *ID) {
3788
3789 std::vector<llvm::Constant*> Ivars, Ivar(5);
3790
3791 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3792 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
3793
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003794 // FIXME. Consolidate this with similar code in GenerateClass.
3795 const llvm::Type *InterfaceTy =
3796 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(
3797 const_cast<ObjCInterfaceDecl*>(OID)));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003798 const llvm::StructLayout *Layout =
3799 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003800
3801 RecordDecl::field_iterator i,p;
3802 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003803 ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin();
3804
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003805 for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) {
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003806 FieldDecl *Field = *i;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003807 unsigned long offset = Layout->getElementOffset(CGM.getTypes().
3808 getLLVMFieldNo(Field));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003809 const ObjCIvarDecl *ivarDecl = *I++;
3810 Ivar[0] = EmitIvarOffsetVar(ID, ivarDecl, offset);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003811 if (Field->getIdentifier())
3812 Ivar[1] = GetMethodVarName(Field->getIdentifier());
3813 else
3814 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3815 std::string TypeStr;
3816 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3817 Ivar[2] = GetMethodVarType(TypeStr);
3818 const llvm::Type *FieldTy =
3819 CGM.getTypes().ConvertTypeForMem(Field->getType());
3820 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3821 unsigned Align = CGM.getContext().getPreferredTypeAlign(
3822 Field->getType().getTypePtr()) >> 3;
3823 Align = llvm::Log2_32(Align);
3824 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Fariborz Jahanian07236ba2009-01-27 22:27:56 +00003825 // NOTE. Size of a bitfield does not match gcc's, because of the way
3826 // bitfields are treated special in each. But I am told that 'size'
3827 // for bitfield ivars is ignored by the runtime so it does not matter.
3828 // (even if it matters, some day, there is enough info. to get the bitfield
3829 // right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003830 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3831 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
3832 }
3833 // Return null for empty list.
3834 if (Ivars.empty())
3835 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3836 std::vector<llvm::Constant*> Values(3);
3837 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
3838 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3839 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
3840 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
3841 Ivars.size());
3842 Values[2] = llvm::ConstantArray::get(AT, Ivars);
3843 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3844 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
3845 llvm::GlobalVariable *GV =
3846 new llvm::GlobalVariable(Init->getType(), false,
3847 llvm::GlobalValue::InternalLinkage,
3848 Init,
3849 Prefix + OID->getNameAsString(),
3850 &CGM.getModule());
Fariborz Jahanianfdbe11d2009-01-30 23:51:52 +00003851 GV->setAlignment(GetPointerAlign());
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003852 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003853
3854 UsedGlobals.push_back(GV);
3855 return llvm::ConstantExpr::getBitCast(GV,
3856 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003857}
3858
3859llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
3860 const ObjCProtocolDecl *PD) {
3861 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
3862
3863 if (!Entry) {
3864 // We use the initializer as a marker of whether this is a forward
3865 // reference or not. At module finalization we add the empty
3866 // contents for protocols which were referenced but never defined.
3867 Entry =
3868 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
3869 llvm::GlobalValue::ExternalLinkage,
3870 0,
3871 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
3872 &CGM.getModule());
3873 Entry->setSection("__DATA,__datacoal_nt,coalesced");
3874 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003875 }
3876
3877 return Entry;
3878}
3879
3880/// GetOrEmitProtocol - Generate the protocol meta-data:
3881/// @code
3882/// struct _protocol_t {
3883/// id isa; // NULL
3884/// const char * const protocol_name;
3885/// const struct _protocol_list_t * protocol_list; // super protocols
3886/// const struct method_list_t * const instance_methods;
3887/// const struct method_list_t * const class_methods;
3888/// const struct method_list_t *optionalInstanceMethods;
3889/// const struct method_list_t *optionalClassMethods;
3890/// const struct _prop_list_t * properties;
3891/// const uint32_t size; // sizeof(struct _protocol_t)
3892/// const uint32_t flags; // = 0
3893/// }
3894/// @endcode
3895///
3896
3897llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
3898 const ObjCProtocolDecl *PD) {
3899 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
3900
3901 // Early exit if a defining object has already been generated.
3902 if (Entry && Entry->hasInitializer())
3903 return Entry;
3904
3905 const char *ProtocolName = PD->getNameAsCString();
3906
3907 // Construct method lists.
3908 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
3909 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
3910 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
3911 e = PD->instmeth_end(); i != e; ++i) {
3912 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003913 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003914 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
3915 OptInstanceMethods.push_back(C);
3916 } else {
3917 InstanceMethods.push_back(C);
3918 }
3919 }
3920
3921 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
3922 e = PD->classmeth_end(); i != e; ++i) {
3923 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003924 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003925 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
3926 OptClassMethods.push_back(C);
3927 } else {
3928 ClassMethods.push_back(C);
3929 }
3930 }
3931
3932 std::vector<llvm::Constant*> Values(10);
3933 // isa is NULL
3934 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
3935 Values[1] = GetClassName(PD->getIdentifier());
3936 Values[2] = EmitProtocolList(
3937 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
3938 PD->protocol_begin(),
3939 PD->protocol_end());
3940
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003941 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00003942 + PD->getNameAsString(),
3943 "__DATA, __objc_const",
3944 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003945 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00003946 + PD->getNameAsString(),
3947 "__DATA, __objc_const",
3948 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003949 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00003950 + PD->getNameAsString(),
3951 "__DATA, __objc_const",
3952 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003953 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00003954 + PD->getNameAsString(),
3955 "__DATA, __objc_const",
3956 OptClassMethods);
3957 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
3958 0, PD, ObjCTypes);
3959 uint32_t Size =
3960 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
3961 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3962 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
3963 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
3964 Values);
3965
3966 if (Entry) {
3967 // Already created, fix the linkage and update the initializer.
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00003968 Entry->setLinkage(llvm::GlobalValue::WeakLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003969 Entry->setInitializer(Init);
3970 } else {
3971 Entry =
3972 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00003973 llvm::GlobalValue::WeakLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00003974 Init,
3975 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
3976 &CGM.getModule());
Fariborz Jahanian014858b2009-01-30 23:15:42 +00003977 Entry->setAlignment(GetPointerAlign());
Fariborz Jahanianda320092009-01-29 19:24:30 +00003978 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00003979 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00003980 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
3981
3982 // Use this protocol meta-data to build protocol list table in section
3983 // __DATA, __objc_protolist
3984 llvm::Type *ptype = llvm::PointerType::getUnqual(ObjCTypes.ProtocolnfABITy);
3985 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
3986 ptype, false,
3987 llvm::GlobalValue::WeakLinkage,
3988 Entry,
3989 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
3990 +ProtocolName,
3991 &CGM.getModule());
Fariborz Jahanian014858b2009-01-30 23:15:42 +00003992 PTGV->setAlignment(GetPointerAlign());
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00003993 PTGV->setSection("__DATA, __objc_protolist");
3994 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3995 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003996 return Entry;
3997}
3998
3999/// EmitProtocolList - Generate protocol list meta-data:
4000/// @code
4001/// struct _protocol_list_t {
4002/// long protocol_count; // Note, this is 32/64 bit
4003/// struct _protocol_t[protocol_count];
4004/// }
4005/// @endcode
4006///
4007llvm::Constant *
4008CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4009 ObjCProtocolDecl::protocol_iterator begin,
4010 ObjCProtocolDecl::protocol_iterator end) {
4011 std::vector<llvm::Constant*> ProtocolRefs;
4012
4013 for (; begin != end; ++begin)
4014 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4015
4016 // Just return null for empty protocol lists
4017 if (ProtocolRefs.empty())
4018 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4019
4020 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4021 if (GV)
4022 return GV;
4023 // This list is null terminated.
4024 ProtocolRefs.push_back(llvm::Constant::getNullValue(
4025 ObjCTypes.ProtocolListnfABIPtrTy));
4026
4027 std::vector<llvm::Constant*> Values(2);
4028 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4029 Values[1] =
4030 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolListnfABIPtrTy,
4031 ProtocolRefs.size()),
4032 ProtocolRefs);
4033
4034 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4035 GV = new llvm::GlobalVariable(Init->getType(), false,
4036 llvm::GlobalValue::InternalLinkage,
4037 Init,
4038 Name,
4039 &CGM.getModule());
4040 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian014858b2009-01-30 23:15:42 +00004041 GV->setAlignment(GetPointerAlign());
Fariborz Jahanianda320092009-01-29 19:24:30 +00004042 UsedGlobals.push_back(GV);
4043 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
4044}
4045
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004046/// GetMethodDescriptionConstant - This routine build following meta-data:
4047/// struct _objc_method {
4048/// SEL _cmd;
4049/// char *method_type;
4050/// char *_imp;
4051/// }
4052
4053llvm::Constant *
4054CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4055 std::vector<llvm::Constant*> Desc(3);
4056 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4057 ObjCTypes.SelectorPtrTy);
4058 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004059 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004060 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4061 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4062}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004063/* *** */
4064
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004065CodeGen::CGObjCRuntime *
4066CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00004067 return new CGObjCMac(CGM);
4068}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004069
4070CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004071CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004072 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004073}