blob: a577bd03cb261d52f3d9ea8b99fd99b1ae1def0f [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 Jahanianee0af742009-01-21 22:04:16 +0000366public:
367 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
368 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000369
370 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000371
372 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
373 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000374
375 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
376
377 /// GetOrEmitProtocol - Get the protocol object for the given
378 /// declaration, emitting it if necessary. The return value has type
379 /// ProtocolPtrTy.
380 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
381
382 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
383 /// object for the given declaration, emitting it if needed. These
384 /// forward references will be filled in with empty bodies if no
385 /// definition is seen. The return value has type ProtocolPtrTy.
386 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000387};
388
389class CGObjCMac : public CGObjCCommonMac {
390private:
391 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000392 /// EmitImageInfo - Emit the image info marker used to encode some module
393 /// level information.
394 void EmitImageInfo();
395
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000396 /// EmitModuleInfo - Another marker encoding module level
397 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000398 void EmitModuleInfo();
399
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000400 /// EmitModuleSymols - Emit module symbols, the list of defined
401 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000402 llvm::Constant *EmitModuleSymbols();
403
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000404 /// FinishModule - Write out global data structures at the end of
405 /// processing a translation unit.
406 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000407
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000408 /// EmitClassExtension - Generate the class extension structure used
409 /// to store the weak ivar layout and properties. The return value
410 /// has type ClassExtensionPtrTy.
411 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
412
413 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
414 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000415 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000416 const ObjCInterfaceDecl *ID);
417
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000418 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000419 QualType ResultType,
420 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000421 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000422 QualType Arg0Ty,
423 bool IsSuper,
424 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000425
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000426 /// EmitIvarList - Emit the ivar list for the given
427 /// implementation. If ForClass is true the list of class ivars
428 /// (i.e. metaclass ivars) is emitted, otherwise the list of
429 /// interface ivars will be emitted. The return value has type
430 /// IvarListPtrTy.
431 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000432 bool ForClass);
433
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000434 /// EmitMetaClass - Emit a forward reference to the class structure
435 /// for the metaclass of the given interface. The return value has
436 /// type ClassPtrTy.
437 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
438
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000439 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000440 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000441 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
442 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000443 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000444 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000445
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000446 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000447
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000448 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000449
450 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000451 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000452 llvm::Constant *EmitMethodList(const std::string &Name,
453 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000454 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000455
456 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000457 /// method declarations.
458 /// - TypeName: The name for the type containing the methods.
459 /// - IsProtocol: True iff these methods are for a protocol.
460 /// - ClassMethds: True iff these are class methods.
461 /// - Required: When true, only "required" methods are
462 /// listed. Similarly, when false only "optional" methods are
463 /// listed. For classes this should always be true.
464 /// - begin, end: The method list to output.
465 ///
466 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000467 llvm::Constant *EmitMethodDescList(const std::string &Name,
468 const char *Section,
469 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000470
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000471 /// GetOrEmitProtocol - Get the protocol object for the given
472 /// declaration, emitting it if necessary. The return value has type
473 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000474 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000475
476 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
477 /// object for the given declaration, emitting it if needed. These
478 /// forward references will be filled in with empty bodies if no
479 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000480 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000481
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000482 /// EmitProtocolExtension - Generate the protocol extension
483 /// structure used to store optional instance and class methods, and
484 /// protocol properties. The return value has type
485 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000486 llvm::Constant *
487 EmitProtocolExtension(const ObjCProtocolDecl *PD,
488 const ConstantVector &OptInstanceMethods,
489 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000490
491 /// EmitProtocolList - Generate the list of referenced
492 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +0000493 llvm::Constant *EmitProtocolList(const std::string &Name,
494 ObjCProtocolDecl::protocol_iterator begin,
495 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000496
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000497 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
498 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000499 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000500
Fariborz Jahanianda320092009-01-29 19:24:30 +0000501 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000502 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000503
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000504 virtual llvm::Function *ModuleInitFunction();
505
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000506 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000507 QualType ResultType,
508 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000509 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000510 bool IsClassMessage,
511 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000512
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000513 virtual CodeGen::RValue
514 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000515 QualType ResultType,
516 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000517 const ObjCInterfaceDecl *Class,
518 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000519 bool IsClassMessage,
520 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000521
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000522 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000523 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000524
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000525 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000526
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000527 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000528
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000529 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000530
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000531 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000532 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000533
Daniel Dunbar49f66022008-09-24 03:38:44 +0000534 virtual llvm::Function *GetPropertyGetFunction();
535 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000536 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000537
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000538 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
539 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000540 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
541 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000542 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000543 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000544 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
545 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000546 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
547 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000548 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
549 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000550 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
551 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +0000552
553 virtual llvm::Value *EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
554 llvm::Value *BaseValue,
555 const ObjCIvarDecl *Ivar,
556 const FieldDecl *Field,
557 unsigned CVRQualifiers);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000558};
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000559
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000560class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000561private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000562 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000563 llvm::GlobalVariable* ObjCEmptyCacheVar;
564 llvm::GlobalVariable* ObjCEmptyVtableVar;
565
566 /// FinishNonFragileABIModule - Write out global data structures at the end of
567 /// processing a translation unit.
568 void FinishNonFragileABIModule();
569
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000570 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
571 unsigned InstanceStart,
572 unsigned InstanceSize,
573 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +0000574 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
575 llvm::Constant *IsAGV,
576 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +0000577 llvm::Constant *ClassRoGV,
578 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000579
580 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
581
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +0000582 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
583
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000584 /// EmitMethodList - Emit the method list for the given
585 /// implementation. The return value has type MethodListnfABITy.
586 llvm::Constant *EmitMethodList(const std::string &Name,
587 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +0000588 const ConstantVector &Methods);
589 /// EmitIvarList - Emit the ivar list for the given
590 /// implementation. If ForClass is true the list of class ivars
591 /// (i.e. metaclass ivars) is emitted, otherwise the list of
592 /// interface ivars will be emitted. The return value has type
593 /// IvarListnfABIPtrTy.
594 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000595
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000596 llvm::Constant *EmitIvarOffsetVar(const ObjCImplementationDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +0000597 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000598 unsigned long int offset);
599
Fariborz Jahanianda320092009-01-29 19:24:30 +0000600 /// GetOrEmitProtocol - Get the protocol object for the given
601 /// declaration, emitting it if necessary. The return value has type
602 /// ProtocolPtrTy.
603 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
604
605 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
606 /// object for the given declaration, emitting it if needed. These
607 /// forward references will be filled in with empty bodies if no
608 /// definition is seen. The return value has type ProtocolPtrTy.
609 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
610
611 /// EmitProtocolList - Generate the list of referenced
612 /// protocols. The return value has type ProtocolListPtrTy.
613 llvm::Constant *EmitProtocolList(const std::string &Name,
614 ObjCProtocolDecl::protocol_iterator begin,
615 ObjCProtocolDecl::protocol_iterator end);
616
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000617public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000618 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000619 // FIXME. All stubs for now!
620 virtual llvm::Function *ModuleInitFunction();
621
622 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
623 QualType ResultType,
624 Selector Sel,
625 llvm::Value *Receiver,
626 bool IsClassMessage,
627 const CallArgList &CallArgs)
628 {return RValue::get(0);}
629
630 virtual CodeGen::RValue
631 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
632 QualType ResultType,
633 Selector Sel,
634 const ObjCInterfaceDecl *Class,
635 llvm::Value *Receiver,
636 bool IsClassMessage,
637 const CallArgList &CallArgs){ return RValue::get(0);}
638
639 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
640 const ObjCInterfaceDecl *ID){ return 0; }
641
642 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
643 { return 0; }
644
Fariborz Jahanianeb062d92009-01-26 18:32:24 +0000645 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000646
647 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000648 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +0000649 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000650
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000651 virtual llvm::Function *GetPropertyGetFunction(){ return 0; }
652 virtual llvm::Function *GetPropertySetFunction()
653 { return 0; }
654 virtual llvm::Function *EnumerationMutationFunction()
655 { return 0; }
656
657 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
658 const Stmt &S)
659 { return; }
660 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
661 const ObjCAtThrowStmt &S)
662 { return; }
663 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
664 llvm::Value *AddrWeakObj)
665 { return 0; }
666 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
667 llvm::Value *src, llvm::Value *dst)
668 { return; }
669 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
670 llvm::Value *src, llvm::Value *dest)
671 { return; }
672 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
673 llvm::Value *src, llvm::Value *dest)
674 { return; }
675 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
676 llvm::Value *src, llvm::Value *dest)
677 { return; }
Fariborz Jahanian0bb20362009-02-02 20:02:29 +0000678 virtual llvm::Value *EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
679 llvm::Value *BaseValue,
680 const ObjCIvarDecl *Ivar,
681 const FieldDecl *Field,
682 unsigned CVRQualifiers)
683 { return 0; }
684
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000685};
686
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000687} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000688
689/* *** Helper Functions *** */
690
691/// getConstantGEP() - Help routine to construct simple GEPs.
692static llvm::Constant *getConstantGEP(llvm::Constant *C,
693 unsigned idx0,
694 unsigned idx1) {
695 llvm::Value *Idxs[] = {
696 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
697 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
698 };
699 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
700}
701
702/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000703
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000704CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
705 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000706{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000707 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000708 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000709}
710
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000711/// GetClass - Return a reference to the class for the given interface
712/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000713llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000714 const ObjCInterfaceDecl *ID) {
715 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000716}
717
718/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000719llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000720 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000721}
722
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000723/// Generate a constant CFString object.
724/*
725 struct __builtin_CFString {
726 const int *isa; // point to __CFConstantStringClassReference
727 int flags;
728 const char *str;
729 long length;
730 };
731*/
732
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000733llvm::Constant *CGObjCCommonMac::GenerateConstantString(
734 const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000735 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000736}
737
738/// Generates a message send where the super is the receiver. This is
739/// a message send to self with special delivery semantics indicating
740/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000741CodeGen::RValue
742CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000743 QualType ResultType,
744 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000745 const ObjCInterfaceDecl *Class,
746 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000747 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000748 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000749 // Create and init a super structure; this is a (receiver, class)
750 // pair we will pass to objc_msgSendSuper.
751 llvm::Value *ObjCSuper =
752 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
753 llvm::Value *ReceiverAsObject =
754 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
755 CGF.Builder.CreateStore(ReceiverAsObject,
756 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000757
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000758 // If this is a class message the metaclass is passed as the target.
759 llvm::Value *Target;
760 if (IsClassMessage) {
761 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
762 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
763 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
764 Target = Super;
765 } else {
766 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
767 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000768 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
769 // and ObjCTypes types.
770 const llvm::Type *ClassTy =
771 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000772 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000773 CGF.Builder.CreateStore(Target,
774 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
775
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000776 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000777 ObjCSuper, ObjCTypes.SuperPtrCTy,
778 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000779}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000780
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000781/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000782CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000783 QualType ResultType,
784 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000785 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000786 bool IsClassMessage,
787 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000788 llvm::Value *Arg0 =
789 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000790 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000791 Arg0, CGF.getContext().getObjCIdType(),
792 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000793}
794
795CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000796 QualType ResultType,
797 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000798 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000799 QualType Arg0Ty,
800 bool IsSuper,
801 const CallArgList &CallArgs) {
802 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000803 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
804 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
805 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000806 CGF.getContext().getObjCSelType()));
807 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000808
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000809 CodeGenTypes &Types = CGM.getTypes();
810 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
811 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbar5669e572008-10-17 03:24:53 +0000812
813 llvm::Constant *Fn;
Daniel Dunbar88b53962009-02-02 22:03:45 +0000814 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000815 Fn = ObjCTypes.getSendStretFn(IsSuper);
816 } else if (ResultType->isFloatingType()) {
817 // FIXME: Sadly, this is wrong. This actually depends on the
818 // architecture. This happens to be right for x86-32 though.
819 Fn = ObjCTypes.getSendFpretFn(IsSuper);
820 } else {
821 Fn = ObjCTypes.getSendFn(IsSuper);
822 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000823 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +0000824 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000825}
826
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000827llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000828 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +0000829 // FIXME: I don't understand why gcc generates this, or where it is
830 // resolved. Investigate. Its also wasteful to look this up over and
831 // over.
832 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
833
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000834 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
835 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000836}
837
Fariborz Jahanianda320092009-01-29 19:24:30 +0000838void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000839 // FIXME: We shouldn't need this, the protocol decl should contain
840 // enough information to tell us whether this was a declaration or a
841 // definition.
842 DefinedProtocols.insert(PD->getIdentifier());
843
844 // If we have generated a forward reference to this protocol, emit
845 // it now. Otherwise do nothing, the protocol objects are lazily
846 // emitted.
847 if (Protocols.count(PD->getIdentifier()))
848 GetOrEmitProtocol(PD);
849}
850
Fariborz Jahanianda320092009-01-29 19:24:30 +0000851llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000852 if (DefinedProtocols.count(PD->getIdentifier()))
853 return GetOrEmitProtocol(PD);
854 return GetOrEmitProtocolRef(PD);
855}
856
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000857/*
858 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
859 struct _objc_protocol {
860 struct _objc_protocol_extension *isa;
861 char *protocol_name;
862 struct _objc_protocol_list *protocol_list;
863 struct _objc__method_prototype_list *instance_methods;
864 struct _objc__method_prototype_list *class_methods
865 };
866
867 See EmitProtocolExtension().
868*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000869llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
870 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
871
872 // Early exit if a defining object has already been generated.
873 if (Entry && Entry->hasInitializer())
874 return Entry;
875
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000876 // FIXME: I don't understand why gcc generates this, or where it is
877 // resolved. Investigate. Its also wasteful to look this up over and
878 // over.
879 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
880
Chris Lattner8ec03f52008-11-24 03:54:41 +0000881 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000882
883 // Construct method lists.
884 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
885 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
886 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
887 e = PD->instmeth_end(); i != e; ++i) {
888 ObjCMethodDecl *MD = *i;
889 llvm::Constant *C = GetMethodDescriptionConstant(MD);
890 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
891 OptInstanceMethods.push_back(C);
892 } else {
893 InstanceMethods.push_back(C);
894 }
895 }
896
897 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
898 e = PD->classmeth_end(); i != e; ++i) {
899 ObjCMethodDecl *MD = *i;
900 llvm::Constant *C = GetMethodDescriptionConstant(MD);
901 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
902 OptClassMethods.push_back(C);
903 } else {
904 ClassMethods.push_back(C);
905 }
906 }
907
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000908 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000909 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000910 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000911 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000912 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +0000913 PD->protocol_begin(),
914 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000915 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000916 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
917 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000918 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
919 InstanceMethods);
920 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000921 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
922 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000923 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
924 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000925 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
926 Values);
927
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000928 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000929 // Already created, fix the linkage and update the initializer.
930 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000931 Entry->setInitializer(Init);
932 } else {
933 Entry =
934 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
935 llvm::GlobalValue::InternalLinkage,
936 Init,
937 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
938 &CGM.getModule());
939 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
940 UsedGlobals.push_back(Entry);
941 // FIXME: Is this necessary? Why only for protocol?
942 Entry->setAlignment(4);
943 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000944
945 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000946}
947
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000948llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000949 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
950
951 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000952 // We use the initializer as a marker of whether this is a forward
953 // reference or not. At module finalization we add the empty
954 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000955 Entry =
956 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000957 llvm::GlobalValue::ExternalLinkage,
958 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000959 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000960 &CGM.getModule());
961 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
962 UsedGlobals.push_back(Entry);
963 // FIXME: Is this necessary? Why only for protocol?
964 Entry->setAlignment(4);
965 }
966
967 return Entry;
968}
969
970/*
971 struct _objc_protocol_extension {
972 uint32_t size;
973 struct objc_method_description_list *optional_instance_methods;
974 struct objc_method_description_list *optional_class_methods;
975 struct objc_property_list *instance_properties;
976 };
977*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000978llvm::Constant *
979CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
980 const ConstantVector &OptInstanceMethods,
981 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000982 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +0000983 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000984 std::vector<llvm::Constant*> Values(4);
985 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000986 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000987 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
988 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000989 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
990 OptInstanceMethods);
991 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000992 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
993 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000994 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
995 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000996 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
997 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000998 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000999
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001000 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001001 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1002 Values[3]->isNullValue())
1003 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1004
1005 llvm::Constant *Init =
1006 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
1007 llvm::GlobalVariable *GV =
1008 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
1009 llvm::GlobalValue::InternalLinkage,
1010 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001011 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001012 &CGM.getModule());
1013 // No special section, but goes in llvm.used
1014 UsedGlobals.push_back(GV);
1015
1016 return GV;
1017}
1018
1019/*
1020 struct objc_protocol_list {
1021 struct objc_protocol_list *next;
1022 long count;
1023 Protocol *list[];
1024 };
1025*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001026llvm::Constant *
1027CGObjCMac::EmitProtocolList(const std::string &Name,
1028 ObjCProtocolDecl::protocol_iterator begin,
1029 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001030 std::vector<llvm::Constant*> ProtocolRefs;
1031
Daniel Dunbardbc933702008-08-21 21:57:41 +00001032 for (; begin != end; ++begin)
1033 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001034
1035 // Just return null for empty protocol lists
1036 if (ProtocolRefs.empty())
1037 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1038
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001039 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001040 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1041
1042 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001043 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001044 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1045 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1046 Values[2] =
1047 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1048 ProtocolRefs.size()),
1049 ProtocolRefs);
1050
1051 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1052 llvm::GlobalVariable *GV =
1053 new llvm::GlobalVariable(Init->getType(), false,
1054 llvm::GlobalValue::InternalLinkage,
1055 Init,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001056 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001057 &CGM.getModule());
1058 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
1059 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1060}
1061
1062/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001063 struct _objc_property {
1064 const char * const name;
1065 const char * const attributes;
1066 };
1067
1068 struct _objc_property_list {
1069 uint32_t entsize; // sizeof (struct _objc_property)
1070 uint32_t prop_count;
1071 struct _objc_property[prop_count];
1072 };
1073*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001074llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1075 const Decl *Container,
1076 const ObjCContainerDecl *OCD,
1077 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001078 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +00001079 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1080 E = OCD->prop_end(); I != E; ++I) {
1081 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001082 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001083 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001084 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1085 Prop));
1086 }
1087
1088 // Return null for empty list.
1089 if (Properties.empty())
1090 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1091
1092 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001093 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001094 std::vector<llvm::Constant*> Values(3);
1095 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1096 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1097 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1098 Properties.size());
1099 Values[2] = llvm::ConstantArray::get(AT, Properties);
1100 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1101
1102 llvm::GlobalVariable *GV =
1103 new llvm::GlobalVariable(Init->getType(), false,
1104 llvm::GlobalValue::InternalLinkage,
1105 Init,
1106 Name,
1107 &CGM.getModule());
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001108 if (ObjCABI == 2)
1109 GV->setSection("__DATA, __objc_const");
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001110 // No special section on property lists?
1111 UsedGlobals.push_back(GV);
1112 return llvm::ConstantExpr::getBitCast(GV,
1113 ObjCTypes.PropertyListPtrTy);
1114
1115}
1116
1117/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001118 struct objc_method_description_list {
1119 int count;
1120 struct objc_method_description list[];
1121 };
1122*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001123llvm::Constant *
1124CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1125 std::vector<llvm::Constant*> Desc(2);
1126 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1127 ObjCTypes.SelectorPtrTy);
1128 Desc[1] = GetMethodVarType(MD);
1129 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1130 Desc);
1131}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001132
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001133llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1134 const char *Section,
1135 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001136 // Return null for empty list.
1137 if (Methods.empty())
1138 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1139
1140 std::vector<llvm::Constant*> Values(2);
1141 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1142 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1143 Methods.size());
1144 Values[1] = llvm::ConstantArray::get(AT, Methods);
1145 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1146
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001147 llvm::GlobalVariable *GV =
1148 new llvm::GlobalVariable(Init->getType(), false,
1149 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001150 Init, Name, &CGM.getModule());
1151 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001152 UsedGlobals.push_back(GV);
1153 return llvm::ConstantExpr::getBitCast(GV,
1154 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001155}
1156
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001157/*
1158 struct _objc_category {
1159 char *category_name;
1160 char *class_name;
1161 struct _objc_method_list *instance_methods;
1162 struct _objc_method_list *class_methods;
1163 struct _objc_protocol_list *protocols;
1164 uint32_t size; // <rdar://4585769>
1165 struct _objc_property_list *instance_properties;
1166 };
1167 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001168void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001169 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001170
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001171 // FIXME: This is poor design, the OCD should have a pointer to the
1172 // category decl. Additionally, note that Category can be null for
1173 // the @implementation w/o an @interface case. Sema should just
1174 // create one for us as it does for @implementation so everyone else
1175 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001176 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001177 const ObjCCategoryDecl *Category =
1178 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001179 std::string ExtName(Interface->getNameAsString() + "_" +
1180 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001181
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001182 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1183 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1184 e = OCD->instmeth_end(); i != e; ++i) {
1185 // Instance methods should always be defined.
1186 InstanceMethods.push_back(GetMethodConstant(*i));
1187 }
1188 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1189 e = OCD->classmeth_end(); i != e; ++i) {
1190 // Class methods should always be defined.
1191 ClassMethods.push_back(GetMethodConstant(*i));
1192 }
1193
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001194 std::vector<llvm::Constant*> Values(7);
1195 Values[0] = GetClassName(OCD->getIdentifier());
1196 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001197 Values[2] =
1198 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1199 ExtName,
1200 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001201 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001202 Values[3] =
1203 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1204 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001205 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001206 if (Category) {
1207 Values[4] =
1208 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1209 Category->protocol_begin(),
1210 Category->protocol_end());
1211 } else {
1212 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1213 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001214 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001215
1216 // If there is no category @interface then there can be no properties.
1217 if (Category) {
1218 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001219 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001220 } else {
1221 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1222 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001223
1224 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1225 Values);
1226
1227 llvm::GlobalVariable *GV =
1228 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1229 llvm::GlobalValue::InternalLinkage,
1230 Init,
1231 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1232 &CGM.getModule());
1233 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1234 UsedGlobals.push_back(GV);
1235 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001236}
1237
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001238// FIXME: Get from somewhere?
1239enum ClassFlags {
1240 eClassFlags_Factory = 0x00001,
1241 eClassFlags_Meta = 0x00002,
1242 // <rdr://5142207>
1243 eClassFlags_HasCXXStructors = 0x02000,
1244 eClassFlags_Hidden = 0x20000,
1245 eClassFlags_ABI2_Hidden = 0x00010,
1246 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1247};
1248
1249// <rdr://5142207&4705298&4843145>
1250static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1251 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1252 // FIXME: Support -fvisibility
1253 switch (attr->getVisibility()) {
1254 default:
1255 assert(0 && "Unknown visibility");
1256 return false;
1257 case VisibilityAttr::DefaultVisibility:
1258 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1259 return false;
1260 case VisibilityAttr::HiddenVisibility:
1261 return true;
1262 }
1263 } else {
1264 return false; // FIXME: Support -fvisibility
1265 }
1266}
1267
1268/*
1269 struct _objc_class {
1270 Class isa;
1271 Class super_class;
1272 const char *name;
1273 long version;
1274 long info;
1275 long instance_size;
1276 struct _objc_ivar_list *ivars;
1277 struct _objc_method_list *methods;
1278 struct _objc_cache *cache;
1279 struct _objc_protocol_list *protocols;
1280 // Objective-C 1.0 extensions (<rdr://4585769>)
1281 const char *ivar_layout;
1282 struct _objc_class_ext *ext;
1283 };
1284
1285 See EmitClassExtension();
1286 */
1287void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001288 DefinedSymbols.insert(ID->getIdentifier());
1289
Chris Lattner8ec03f52008-11-24 03:54:41 +00001290 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001291 // FIXME: Gross
1292 ObjCInterfaceDecl *Interface =
1293 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001294 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001295 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001296 Interface->protocol_begin(),
1297 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001298 const llvm::Type *InterfaceTy =
1299 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
1300 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001301 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001302
1303 // FIXME: Set CXX-structors flag.
1304 if (IsClassHidden(ID->getClassInterface()))
1305 Flags |= eClassFlags_Hidden;
1306
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001307 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1308 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1309 e = ID->instmeth_end(); i != e; ++i) {
1310 // Instance methods should always be defined.
1311 InstanceMethods.push_back(GetMethodConstant(*i));
1312 }
1313 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1314 e = ID->classmeth_end(); i != e; ++i) {
1315 // Class methods should always be defined.
1316 ClassMethods.push_back(GetMethodConstant(*i));
1317 }
1318
1319 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1320 e = ID->propimpl_end(); i != e; ++i) {
1321 ObjCPropertyImplDecl *PID = *i;
1322
1323 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1324 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1325
1326 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1327 if (llvm::Constant *C = GetMethodConstant(MD))
1328 InstanceMethods.push_back(C);
1329 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1330 if (llvm::Constant *C = GetMethodConstant(MD))
1331 InstanceMethods.push_back(C);
1332 }
1333 }
1334
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001335 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001336 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001337 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001338 // Record a reference to the super class.
1339 LazySymbols.insert(Super->getIdentifier());
1340
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001341 Values[ 1] =
1342 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1343 ObjCTypes.ClassPtrTy);
1344 } else {
1345 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1346 }
1347 Values[ 2] = GetClassName(ID->getIdentifier());
1348 // Version is always 0.
1349 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1350 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1351 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001352 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001353 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001354 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001355 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001356 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001357 // cache is always NULL.
1358 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1359 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001360 // FIXME: Set ivar_layout
1361 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001362 Values[11] = EmitClassExtension(ID);
1363 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1364 Values);
1365
1366 llvm::GlobalVariable *GV =
1367 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1368 llvm::GlobalValue::InternalLinkage,
1369 Init,
1370 std::string("\01L_OBJC_CLASS_")+ClassName,
1371 &CGM.getModule());
1372 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1373 UsedGlobals.push_back(GV);
1374 // FIXME: Why?
1375 GV->setAlignment(32);
1376 DefinedClasses.push_back(GV);
1377}
1378
1379llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1380 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001381 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001382 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001383 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001384 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001385
1386 if (IsClassHidden(ID->getClassInterface()))
1387 Flags |= eClassFlags_Hidden;
1388
1389 std::vector<llvm::Constant*> Values(12);
1390 // The isa for the metaclass is the root of the hierarchy.
1391 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1392 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1393 Root = Super;
1394 Values[ 0] =
1395 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1396 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001397 // The super class for the metaclass is emitted as the name of the
1398 // super class. The runtime fixes this up to point to the
1399 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001400 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1401 Values[ 1] =
1402 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1403 ObjCTypes.ClassPtrTy);
1404 } else {
1405 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1406 }
1407 Values[ 2] = GetClassName(ID->getIdentifier());
1408 // Version is always 0.
1409 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1410 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1411 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001412 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001413 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001414 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001415 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001416 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001417 // cache is always NULL.
1418 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1419 Values[ 9] = Protocols;
1420 // ivar_layout for metaclass is always NULL.
1421 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1422 // The class extension is always unused for metaclasses.
1423 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1424 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1425 Values);
1426
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001427 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001428 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001429
1430 // Check for a forward reference.
1431 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1432 if (GV) {
1433 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1434 "Forward metaclass reference has incorrect type.");
1435 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1436 GV->setInitializer(Init);
1437 } else {
1438 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1439 llvm::GlobalValue::InternalLinkage,
1440 Init, Name,
1441 &CGM.getModule());
1442 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001443 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1444 UsedGlobals.push_back(GV);
1445 // FIXME: Why?
1446 GV->setAlignment(32);
1447
1448 return GV;
1449}
1450
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001451llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001452 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001453
1454 // FIXME: Should we look these up somewhere other than the
1455 // module. Its a bit silly since we only generate these while
1456 // processing an implementation, so exactly one pointer would work
1457 // if know when we entered/exitted an implementation block.
1458
1459 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001460 // Previously, metaclass with internal linkage may have been defined.
1461 // pass 'true' as 2nd argument so it is returned.
1462 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001463 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1464 "Forward metaclass reference has incorrect type.");
1465 return GV;
1466 } else {
1467 // Generate as an external reference to keep a consistent
1468 // module. This will be patched up when we emit the metaclass.
1469 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1470 llvm::GlobalValue::ExternalLinkage,
1471 0,
1472 Name,
1473 &CGM.getModule());
1474 }
1475}
1476
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001477/*
1478 struct objc_class_ext {
1479 uint32_t size;
1480 const char *weak_ivar_layout;
1481 struct _objc_property_list *properties;
1482 };
1483*/
1484llvm::Constant *
1485CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1486 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001487 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001488
1489 std::vector<llvm::Constant*> Values(3);
1490 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001491 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001492 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001493 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001494 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001495
1496 // Return null if no extension bits are used.
1497 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1498 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1499
1500 llvm::Constant *Init =
1501 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1502 llvm::GlobalVariable *GV =
1503 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1504 llvm::GlobalValue::InternalLinkage,
1505 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001506 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001507 &CGM.getModule());
1508 // No special section, but goes in llvm.used
1509 UsedGlobals.push_back(GV);
1510
1511 return GV;
1512}
1513
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001514/// countInheritedIvars - count number of ivars in class and its super class(s)
1515///
1516static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1517 int count = 0;
1518 if (!OI)
1519 return 0;
1520 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1521 if (SuperClass)
1522 count += countInheritedIvars(SuperClass);
1523 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1524 E = OI->ivar_end(); I != E; ++I)
1525 ++count;
1526 return count;
1527}
1528
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001529/*
1530 struct objc_ivar {
1531 char *ivar_name;
1532 char *ivar_type;
1533 int ivar_offset;
1534 };
1535
1536 struct objc_ivar_list {
1537 int ivar_count;
1538 struct objc_ivar list[count];
1539 };
1540 */
1541llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001542 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001543 std::vector<llvm::Constant*> Ivars, Ivar(3);
1544
1545 // When emitting the root class GCC emits ivar entries for the
1546 // actual class structure. It is not clear if we need to follow this
1547 // behavior; for now lets try and get away with not doing it. If so,
1548 // the cleanest solution would be to make up an ObjCInterfaceDecl
1549 // for the class.
1550 if (ForClass)
1551 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001552
1553 ObjCInterfaceDecl *OID =
1554 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
1555 const llvm::Type *InterfaceTy =
1556 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001557 const llvm::StructLayout *Layout =
1558 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001559
1560 RecordDecl::field_iterator ifield, pfield;
1561 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001562 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1563 FieldDecl *Field = *ifield;
1564 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1565 getLLVMFieldNo(Field));
1566 if (Field->getIdentifier())
1567 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1568 else
1569 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001570 std::string TypeStr;
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001571 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001572 Ivar[1] = GetMethodVarType(TypeStr);
1573 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001574 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001575 }
1576
1577 // Return null for empty list.
1578 if (Ivars.empty())
1579 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1580
1581 std::vector<llvm::Constant*> Values(2);
1582 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1583 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1584 Ivars.size());
1585 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1586 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1587
1588 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1589 "\01L_OBJC_INSTANCE_VARIABLES_");
1590 llvm::GlobalVariable *GV =
1591 new llvm::GlobalVariable(Init->getType(), false,
1592 llvm::GlobalValue::InternalLinkage,
1593 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001594 Prefix + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001595 &CGM.getModule());
1596 if (ForClass) {
1597 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1598 // FIXME: Why is this only here?
1599 GV->setAlignment(32);
1600 } else {
1601 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1602 }
1603 UsedGlobals.push_back(GV);
1604 return llvm::ConstantExpr::getBitCast(GV,
1605 ObjCTypes.IvarListPtrTy);
1606}
1607
1608/*
1609 struct objc_method {
1610 SEL method_name;
1611 char *method_types;
1612 void *method;
1613 };
1614
1615 struct objc_method_list {
1616 struct objc_method_list *obsolete;
1617 int count;
1618 struct objc_method methods_list[count];
1619 };
1620*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001621
1622/// GetMethodConstant - Return a struct objc_method constant for the
1623/// given method if it has been defined. The result is null if the
1624/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001625llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001626 // FIXME: Use DenseMap::lookup
1627 llvm::Function *Fn = MethodDefinitions[MD];
1628 if (!Fn)
1629 return 0;
1630
1631 std::vector<llvm::Constant*> Method(3);
1632 Method[0] =
1633 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1634 ObjCTypes.SelectorPtrTy);
1635 Method[1] = GetMethodVarType(MD);
1636 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1637 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1638}
1639
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001640llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1641 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001642 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001643 // Return null for empty list.
1644 if (Methods.empty())
1645 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1646
1647 std::vector<llvm::Constant*> Values(3);
1648 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1649 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1650 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1651 Methods.size());
1652 Values[2] = llvm::ConstantArray::get(AT, Methods);
1653 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1654
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001655 llvm::GlobalVariable *GV =
1656 new llvm::GlobalVariable(Init->getType(), false,
1657 llvm::GlobalValue::InternalLinkage,
1658 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001659 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001660 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001661 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001662 UsedGlobals.push_back(GV);
1663 return llvm::ConstantExpr::getBitCast(GV,
1664 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001665}
1666
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001667llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001668 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001669 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001670 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001671
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001672 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001673 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001674 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001675 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001676 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001677 llvm::GlobalValue::InternalLinkage,
1678 Name,
1679 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001680 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001681
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001682 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001683}
1684
1685llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001686 // Abuse this interface function as a place to finalize.
1687 FinishModule();
1688
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001689 return NULL;
1690}
1691
Daniel Dunbar49f66022008-09-24 03:38:44 +00001692llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1693 return ObjCTypes.GetPropertyFn;
1694}
1695
1696llvm::Function *CGObjCMac::GetPropertySetFunction() {
1697 return ObjCTypes.SetPropertyFn;
1698}
1699
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001700llvm::Function *CGObjCMac::EnumerationMutationFunction()
1701{
1702 return ObjCTypes.EnumerationMutationFn;
1703}
1704
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001705/*
1706
1707Objective-C setjmp-longjmp (sjlj) Exception Handling
1708--
1709
1710The basic framework for a @try-catch-finally is as follows:
1711{
1712 objc_exception_data d;
1713 id _rethrow = null;
1714
1715 objc_exception_try_enter(&d);
1716 if (!setjmp(d.jmp_buf)) {
1717 ... try body ...
1718 } else {
1719 // exception path
1720 id _caught = objc_exception_extract(&d);
1721
1722 // enter new try scope for handlers
1723 if (!setjmp(d.jmp_buf)) {
1724 ... match exception and execute catch blocks ...
1725
1726 // fell off end, rethrow.
1727 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001728 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001729 } else {
1730 // exception in catch block
1731 _rethrow = objc_exception_extract(&d);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001732 ... jump-through-finally_no_exit to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001733 }
1734 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001735 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001736
1737finally:
1738 // match either the initial try_enter or the catch try_enter,
1739 // depending on the path followed.
1740 objc_exception_try_exit(&d);
1741finally_no_exit:
1742 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001743 ... dispatch to finally destination ...
1744
1745finally_rethrow:
1746 objc_exception_throw(_rethrow);
1747
1748finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001749}
1750
1751This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001752uses _rethrow to determine if objc_exception_try_exit should be called
1753and if the object should be rethrown. This breaks in the face of
1754throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001755
1756We specialize this framework for a few particular circumstances:
1757
1758 - If there are no catch blocks, then we avoid emitting the second
1759 exception handling context.
1760
1761 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1762 e)) we avoid emitting the code to rethrow an uncaught exception.
1763
1764 - FIXME: If there is no @finally block we can do a few more
1765 simplifications.
1766
1767Rethrows and Jumps-Through-Finally
1768--
1769
1770Support for implicit rethrows and jumping through the finally block is
1771handled by storing the current exception-handling context in
1772ObjCEHStack.
1773
Daniel Dunbar898d5082008-09-30 01:06:03 +00001774In order to implement proper @finally semantics, we support one basic
1775mechanism for jumping through the finally block to an arbitrary
1776destination. Constructs which generate exits from a @try or @catch
1777block use this mechanism to implement the proper semantics by chaining
1778jumps, as necessary.
1779
1780This mechanism works like the one used for indirect goto: we
1781arbitrarily assign an ID to each destination and store the ID for the
1782destination in a variable prior to entering the finally block. At the
1783end of the finally block we simply create a switch to the proper
1784destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001785
1786Code gen for @synchronized(expr) stmt;
1787Effectively generating code for:
1788objc_sync_enter(expr);
1789@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001790*/
1791
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001792void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1793 const Stmt &S) {
1794 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001795 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001796 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
1797 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1798 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1799 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001800 llvm::Value *DestCode =
1801 CGF.CreateTempAlloca(llvm::Type::Int32Ty, "finally.dst");
1802
1803 // Generate jump code. Done here so we can directly add things to
1804 // the switch instruction.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001805 llvm::BasicBlock *FinallyJump = CGF.createBasicBlock("finally.jump");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001806 llvm::SwitchInst *FinallySwitch =
1807 llvm::SwitchInst::Create(new llvm::LoadInst(DestCode, "", FinallyJump),
1808 FinallyEnd, 10, FinallyJump);
1809
1810 // Push an EH context entry, used for handling rethrows and jumps
1811 // through finally.
1812 CodeGenFunction::ObjCEHEntry EHEntry(FinallyBlock, FinallyNoExit,
1813 FinallySwitch, DestCode);
1814 CGF.ObjCEHStack.push_back(&EHEntry);
1815
1816 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00001817 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1818 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001819 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1820 "_rethrow");
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001821 if (!isTry) {
1822 // For @synchronized, call objc_sync_enter(sync.expr)
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001823 llvm::Value *Arg = CGF.EmitScalarExpr(
1824 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1825 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1826 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001827 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001828
1829 // Enter a new try block and call setjmp.
1830 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1831 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1832 "jmpbufarray");
1833 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1834 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1835 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001836
Daniel Dunbar55e87422008-11-11 02:29:29 +00001837 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1838 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001839 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001840 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001841
1842 // Emit the @try block.
1843 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001844 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1845 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001846 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001847
1848 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001849 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001850
1851 // Retrieve the exception object. We may emit multiple blocks but
1852 // nothing can cross this so the value is already in SSA form.
1853 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1854 ExceptionData,
1855 "caught");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001856 EHEntry.Exception = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001857 if (!isTry)
1858 {
1859 CGF.Builder.CreateStore(Caught, RethrowPtr);
1860 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
1861 }
1862 else if (const ObjCAtCatchStmt* CatchStmt =
1863 cast<ObjCAtTryStmt>(S).getCatchStmts())
1864 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00001865 // Enter a new exception try block (in case a @catch block throws
1866 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00001867 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001868
Anders Carlsson80f25672008-09-09 17:59:25 +00001869 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1870 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001871 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00001872
Daniel Dunbar55e87422008-11-11 02:29:29 +00001873 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1874 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001875 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001876
1877 CGF.EmitBlock(CatchBlock);
1878
Daniel Dunbar55e40722008-09-27 07:03:52 +00001879 // Handle catch list. As a special case we check if everything is
1880 // matched and avoid generating code for falling off the end if
1881 // so.
1882 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00001883 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00001884 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00001885
Anders Carlssondde0a942008-09-11 09:15:33 +00001886 const DeclStmt *CatchParam =
1887 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001888 const VarDecl *VD = 0;
1889 const PointerType *PT = 0;
1890
Anders Carlsson80f25672008-09-09 17:59:25 +00001891 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00001892 if (!CatchParam) {
1893 AllMatched = true;
1894 } else {
Ted Kremenekde3b8fb2008-10-06 20:58:56 +00001895 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001896 PT = VD->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001897
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001898 // catch(id e) always matches.
1899 // FIXME: For the time being we also match id<X>; this should
1900 // be rejected by Sema instead.
1901 if ((PT && CGF.getContext().isObjCIdType(PT->getPointeeType())) ||
1902 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00001903 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00001904 }
1905
Daniel Dunbar55e40722008-09-27 07:03:52 +00001906 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00001907 if (CatchParam) {
1908 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001909 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar129271a2008-09-27 07:36:24 +00001910 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001911 }
Anders Carlsson1452f552008-09-11 08:21:54 +00001912
Anders Carlssondde0a942008-09-11 09:15:33 +00001913 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001914 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001915 break;
1916 }
1917
Daniel Dunbar129271a2008-09-27 07:36:24 +00001918 assert(PT && "Unexpected non-pointer type in @catch");
1919 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00001920 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001921 assert(ObjCType && "Catch parameter must have Objective-C type!");
1922
1923 // Check if the @catch block matches the exception object.
1924 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
1925
Anders Carlsson80f25672008-09-09 17:59:25 +00001926 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
1927 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00001928
Daniel Dunbar55e87422008-11-11 02:29:29 +00001929 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00001930
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001931 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001932 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001933
1934 // Emit the @catch block.
1935 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001936 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001937 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001938
1939 llvm::Value *Tmp =
1940 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
1941 "tmp");
1942 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001943
1944 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001945 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001946
1947 CGF.EmitBlock(NextCatchBlock);
1948 }
1949
Daniel Dunbar55e40722008-09-27 07:03:52 +00001950 if (!AllMatched) {
1951 // None of the handlers caught the exception, so store it to be
1952 // rethrown at the end of the @finally block.
1953 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001954 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001955 }
1956
1957 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001958 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001959 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1960 ExceptionData),
1961 RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001962 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001963 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00001964 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001965 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Anders Carlsson80f25672008-09-09 17:59:25 +00001966 }
1967
Daniel Dunbar898d5082008-09-30 01:06:03 +00001968 // Pop the exception-handling stack entry. It is important to do
1969 // this now, because the code in the @finally block is not in this
1970 // context.
1971 CGF.ObjCEHStack.pop_back();
1972
Anders Carlsson80f25672008-09-09 17:59:25 +00001973 // Emit the @finally block.
1974 CGF.EmitBlock(FinallyBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001975 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00001976
1977 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001978 if (isTry) {
1979 if (const ObjCAtFinallyStmt* FinallyStmt =
1980 cast<ObjCAtTryStmt>(S).getFinallyStmt())
1981 CGF.EmitStmt(FinallyStmt->getFinallyBody());
1982 }
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001983 else {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001984 // For @synchronized objc_sync_exit(expr); As finally's sole statement.
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001985 // For @synchronized, call objc_sync_enter(sync.expr)
1986 llvm::Value *Arg = CGF.EmitScalarExpr(
1987 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1988 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1989 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg);
1990 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001991
Daniel Dunbar898d5082008-09-30 01:06:03 +00001992 CGF.EmitBlock(FinallyJump);
1993
1994 CGF.EmitBlock(FinallyRethrow);
1995 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
1996 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001997 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00001998
1999 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002000}
2001
2002void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002003 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002004 llvm::Value *ExceptionAsObject;
2005
2006 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2007 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2008 ExceptionAsObject =
2009 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2010 } else {
Daniel Dunbar898d5082008-09-30 01:06:03 +00002011 assert((!CGF.ObjCEHStack.empty() && CGF.ObjCEHStack.back()->Exception) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002012 "Unexpected rethrow outside @catch block.");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002013 ExceptionAsObject = CGF.ObjCEHStack.back()->Exception;
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002014 }
2015
2016 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002017 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002018
2019 // Clear the insertion point to indicate we are in unreachable code.
2020 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002021}
2022
Daniel Dunbar898d5082008-09-30 01:06:03 +00002023void CodeGenFunction::EmitJumpThroughFinally(ObjCEHEntry *E,
2024 llvm::BasicBlock *Dst,
2025 bool ExecuteTryExit) {
Daniel Dunbara448fb22008-11-11 23:11:34 +00002026 if (!HaveInsertPoint())
Daniel Dunbar898d5082008-09-30 01:06:03 +00002027 return;
2028
2029 // Find the destination code for this block. We always use 0 for the
2030 // fallthrough block (default destination).
2031 llvm::SwitchInst *SI = E->FinallySwitch;
2032 llvm::ConstantInt *ID;
2033 if (Dst == SI->getDefaultDest()) {
2034 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
2035 } else {
2036 ID = SI->findCaseDest(Dst);
2037 if (!ID) {
2038 // No code found, get a new unique one by just using the number
2039 // of switch successors.
2040 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, SI->getNumSuccessors());
2041 SI->addCase(ID, Dst);
2042 }
2043 }
2044
2045 // Set the destination code and branch.
2046 Builder.CreateStore(ID, E->DestCode);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002047 EmitBranch(ExecuteTryExit ? E->FinallyBlock : E->FinallyNoExit);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002048}
2049
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002050/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002051/// object: objc_read_weak (id *src)
2052///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002053llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002054 llvm::Value *AddrWeakObj)
2055{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002056 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002057 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002058 AddrWeakObj, "weakread");
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002059 return read_weak;
2060}
2061
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002062/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2063/// objc_assign_weak (id src, id *dst)
2064///
2065void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2066 llvm::Value *src, llvm::Value *dst)
2067{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002068 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2069 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002070 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2071 src, dst, "weakassign");
2072 return;
2073}
2074
Fariborz Jahanian58626502008-11-19 00:59:10 +00002075/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2076/// objc_assign_global (id src, id *dst)
2077///
2078void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2079 llvm::Value *src, llvm::Value *dst)
2080{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002081 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2082 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002083 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2084 src, dst, "globalassign");
2085 return;
2086}
2087
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002088/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2089/// objc_assign_ivar (id src, id *dst)
2090///
2091void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2092 llvm::Value *src, llvm::Value *dst)
2093{
2094 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2095 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2096 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2097 src, dst, "assignivar");
2098 return;
2099}
2100
Fariborz Jahanian58626502008-11-19 00:59:10 +00002101/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2102/// objc_assign_strongCast (id src, id *dst)
2103///
2104void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2105 llvm::Value *src, llvm::Value *dst)
2106{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002107 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2108 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002109 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2110 src, dst, "weakassign");
2111 return;
2112}
2113
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002114/// EmitObjCValueForIvar - Code Gen for ivar reference.
2115///
2116llvm::Value *CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2117 llvm::Value *BaseValue,
2118 const ObjCIvarDecl *Ivar,
2119 const FieldDecl *Field,
2120 unsigned CVRQualifiers) {
2121 // TODO: Add a special case for isa (index 0)
2122 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
2123 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
2124 return V;
2125}
2126
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002127/* *** Private Interface *** */
2128
2129/// EmitImageInfo - Emit the image info marker used to encode some module
2130/// level information.
2131///
2132/// See: <rdr://4810609&4810587&4810587>
2133/// struct IMAGE_INFO {
2134/// unsigned version;
2135/// unsigned flags;
2136/// };
2137enum ImageInfoFlags {
2138 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2139 eImageInfo_GarbageCollected = (1 << 1),
2140 eImageInfo_GCOnly = (1 << 2)
2141};
2142
2143void CGObjCMac::EmitImageInfo() {
2144 unsigned version = 0; // Version is unused?
2145 unsigned flags = 0;
2146
2147 // FIXME: Fix and continue?
2148 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2149 flags |= eImageInfo_GarbageCollected;
2150 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2151 flags |= eImageInfo_GCOnly;
2152
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002153 // Emitted as int[2];
2154 llvm::Constant *values[2] = {
2155 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2156 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2157 };
2158 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002159 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002160 new llvm::GlobalVariable(AT, true,
2161 llvm::GlobalValue::InternalLinkage,
2162 llvm::ConstantArray::get(AT, values, 2),
2163 "\01L_OBJC_IMAGE_INFO",
2164 &CGM.getModule());
2165
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002166 if (ObjCABI == 1) {
2167 GV->setSection("__OBJC, __image_info,regular");
2168 } else {
2169 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2170 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002171
2172 UsedGlobals.push_back(GV);
2173}
2174
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002175
2176// struct objc_module {
2177// unsigned long version;
2178// unsigned long size;
2179// const char *name;
2180// Symtab symtab;
2181// };
2182
2183// FIXME: Get from somewhere
2184static const int ModuleVersion = 7;
2185
2186void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002187 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002188
2189 std::vector<llvm::Constant*> Values(4);
2190 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2191 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002192 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002193 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002194 Values[3] = EmitModuleSymbols();
2195
2196 llvm::GlobalVariable *GV =
2197 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2198 llvm::GlobalValue::InternalLinkage,
2199 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2200 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002201 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002202 &CGM.getModule());
2203 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2204 UsedGlobals.push_back(GV);
2205}
2206
2207llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002208 unsigned NumClasses = DefinedClasses.size();
2209 unsigned NumCategories = DefinedCategories.size();
2210
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002211 // Return null if no symbols were defined.
2212 if (!NumClasses && !NumCategories)
2213 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2214
2215 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002216 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2217 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2218 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2219 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2220
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002221 // The runtime expects exactly the list of defined classes followed
2222 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002223 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002224 for (unsigned i=0; i<NumClasses; i++)
2225 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2226 ObjCTypes.Int8PtrTy);
2227 for (unsigned i=0; i<NumCategories; i++)
2228 Symbols[NumClasses + i] =
2229 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2230 ObjCTypes.Int8PtrTy);
2231
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002232 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002233 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002234 NumClasses + NumCategories),
2235 Symbols);
2236
2237 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2238
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002239 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002240 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002241 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002242 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002243 "\01L_OBJC_SYMBOLS",
2244 &CGM.getModule());
2245 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2246 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002247 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2248}
2249
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002250llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002251 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002252 LazySymbols.insert(ID->getIdentifier());
2253
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002254 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2255
2256 if (!Entry) {
2257 llvm::Constant *Casted =
2258 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2259 ObjCTypes.ClassPtrTy);
2260 Entry =
2261 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2262 llvm::GlobalValue::InternalLinkage,
2263 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2264 &CGM.getModule());
2265 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2266 UsedGlobals.push_back(Entry);
2267 }
2268
2269 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002270}
2271
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002272llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002273 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2274
2275 if (!Entry) {
2276 llvm::Constant *Casted =
2277 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2278 ObjCTypes.SelectorPtrTy);
2279 Entry =
2280 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2281 llvm::GlobalValue::InternalLinkage,
2282 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2283 &CGM.getModule());
2284 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2285 UsedGlobals.push_back(Entry);
2286 }
2287
2288 return Builder.CreateLoad(Entry, false, "tmp");
2289}
2290
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002291llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002292 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002293
2294 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002295 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002296 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002297 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002298 llvm::GlobalValue::InternalLinkage,
2299 C, "\01L_OBJC_CLASS_NAME_",
2300 &CGM.getModule());
2301 Entry->setSection("__TEXT,__cstring,cstring_literals");
2302 UsedGlobals.push_back(Entry);
2303 }
2304
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002305 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002306}
2307
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002308llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002309 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2310
2311 if (!Entry) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00002312 // FIXME: Avoid std::string copying.
2313 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002314 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002315 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002316 llvm::GlobalValue::InternalLinkage,
2317 C, "\01L_OBJC_METH_VAR_NAME_",
2318 &CGM.getModule());
2319 Entry->setSection("__TEXT,__cstring,cstring_literals");
2320 UsedGlobals.push_back(Entry);
2321 }
2322
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002323 return getConstantGEP(Entry, 0, 0);
2324}
2325
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002326// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002327llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002328 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2329}
2330
2331// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002332llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002333 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2334}
2335
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002336llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002337 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002338
2339 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002340 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002341 Entry =
2342 new llvm::GlobalVariable(C->getType(), false,
2343 llvm::GlobalValue::InternalLinkage,
2344 C, "\01L_OBJC_METH_VAR_TYPE_",
2345 &CGM.getModule());
2346 Entry->setSection("__TEXT,__cstring,cstring_literals");
2347 UsedGlobals.push_back(Entry);
2348 }
2349
2350 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002351}
2352
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002353// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002354llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002355 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002356 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2357 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002358 return GetMethodVarType(TypeStr);
2359}
2360
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002361// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002362llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002363 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2364
2365 if (!Entry) {
2366 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2367 Entry =
2368 new llvm::GlobalVariable(C->getType(), false,
2369 llvm::GlobalValue::InternalLinkage,
2370 C, "\01L_OBJC_PROP_NAME_ATTR_",
2371 &CGM.getModule());
2372 Entry->setSection("__TEXT,__cstring,cstring_literals");
2373 UsedGlobals.push_back(Entry);
2374 }
2375
2376 return getConstantGEP(Entry, 0, 0);
2377}
2378
2379// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002380// FIXME: This Decl should be more precise.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002381llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002382 const Decl *Container) {
2383 std::string TypeStr;
2384 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002385 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2386}
2387
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002388void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2389 const ObjCContainerDecl *CD,
2390 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002391 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002392 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002393 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002394 assert (CD && "Missing container decl in GetNameForMethod");
2395 NameOut += CD->getNameAsString();
Fariborz Jahanian52847332009-01-26 23:49:05 +00002396 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
2397 // Right now! there is not enough info. to do this.
Chris Lattner077bf5e2008-11-24 03:33:13 +00002398 NameOut += ' ';
2399 NameOut += D->getSelector().getAsString();
2400 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002401}
2402
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002403/// GetFirstIvarInRecord - This routine returns the record for the
2404/// implementation of the fiven class OID. It also returns field
2405/// corresponding to the first ivar in the class in FIV. It also
2406/// returns the one before the first ivar.
2407///
2408const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord(
2409 const ObjCInterfaceDecl *OID,
2410 RecordDecl::field_iterator &FIV,
2411 RecordDecl::field_iterator &PIV) {
2412 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
2413 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
2414 RecordDecl::field_iterator ifield = RD->field_begin();
2415 RecordDecl::field_iterator pfield = RD->field_end();
2416 while (countSuperClassIvars-- > 0) {
2417 pfield = ifield;
2418 ++ifield;
2419 }
2420 FIV = ifield;
2421 PIV = pfield;
2422 return RD;
2423}
2424
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002425void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002426 EmitModuleInfo();
2427
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002428 // Emit the dummy bodies for any protocols which were referenced but
2429 // never defined.
2430 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2431 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2432 if (i->second->hasInitializer())
2433 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002434
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002435 std::vector<llvm::Constant*> Values(5);
2436 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2437 Values[1] = GetClassName(i->first);
2438 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2439 Values[3] = Values[4] =
2440 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2441 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2442 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2443 Values));
2444 }
2445
2446 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002447 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002448 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002449 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002450 }
2451
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002452 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002453 llvm::GlobalValue *GV =
2454 new llvm::GlobalVariable(AT, false,
2455 llvm::GlobalValue::AppendingLinkage,
2456 llvm::ConstantArray::get(AT, Used),
2457 "llvm.used",
2458 &CGM.getModule());
2459
2460 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002461
2462 // Add assembler directives to add lazy undefined symbol references
2463 // for classes which are referenced but not defined. This is
2464 // important for correct linker interaction.
2465
2466 // FIXME: Uh, this isn't particularly portable.
2467 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00002468
2469 if (!CGM.getModule().getModuleInlineAsm().empty())
2470 s << "\n";
2471
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002472 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2473 e = LazySymbols.end(); i != e; ++i) {
2474 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2475 }
2476 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2477 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002478 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002479 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2480 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00002481
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002482 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002483}
2484
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002485CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002486 : CGObjCCommonMac(cgm),
2487 ObjCTypes(cgm)
2488{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002489 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002490 ObjCABI = 2;
2491}
2492
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002493/* *** */
2494
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002495ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2496: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002497{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002498 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2499 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002500
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002501 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002502 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002503 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002504 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2505
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002506 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002507 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002508 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002509
2510 // FIXME: It would be nice to unify this with the opaque type, so
2511 // that the IR comes out a bit cleaner.
2512 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2513 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002514
2515 // I'm not sure I like this. The implicit coordination is a bit
2516 // gross. We should solve this in a reasonable fashion because this
2517 // is a pretty common task (match some runtime data structure with
2518 // an LLVM data structure).
2519
2520 // FIXME: This is leaked.
2521 // FIXME: Merge with rewriter code?
2522
2523 // struct _objc_super {
2524 // id self;
2525 // Class cls;
2526 // }
2527 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2528 SourceLocation(),
2529 &Ctx.Idents.get("_objc_super"));
2530 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2531 Ctx.getObjCIdType(), 0, false));
2532 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2533 Ctx.getObjCClassType(), 0, false));
2534 RD->completeDefinition(Ctx);
2535
2536 SuperCTy = Ctx.getTagDeclType(RD);
2537 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2538
2539 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002540 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2541
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002542 // struct _prop_t {
2543 // char *name;
2544 // char *attributes;
2545 // }
2546 PropertyTy = llvm::StructType::get(Int8PtrTy,
2547 Int8PtrTy,
2548 NULL);
2549 CGM.getModule().addTypeName("struct._prop_t",
2550 PropertyTy);
2551
2552 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002553 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002554 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002555 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002556 // }
2557 PropertyListTy = llvm::StructType::get(IntTy,
2558 IntTy,
2559 llvm::ArrayType::get(PropertyTy, 0),
2560 NULL);
2561 CGM.getModule().addTypeName("struct._prop_list_t",
2562 PropertyListTy);
2563 // struct _prop_list_t *
2564 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2565
2566 // struct _objc_method {
2567 // SEL _cmd;
2568 // char *method_type;
2569 // char *_imp;
2570 // }
2571 MethodTy = llvm::StructType::get(SelectorPtrTy,
2572 Int8PtrTy,
2573 Int8PtrTy,
2574 NULL);
2575 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002576
2577 // struct _objc_cache *
2578 CacheTy = llvm::OpaqueType::get();
2579 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2580 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002581
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002582 // Property manipulation functions.
2583
2584 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
2585 std::vector<const llvm::Type*> Params;
2586 Params.push_back(ObjectPtrTy);
2587 Params.push_back(SelectorPtrTy);
2588 Params.push_back(LongTy);
2589 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2590 GetPropertyFn =
2591 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2592 Params,
2593 false),
2594 "objc_getProperty");
2595
2596 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2597 Params.clear();
2598 Params.push_back(ObjectPtrTy);
2599 Params.push_back(SelectorPtrTy);
2600 Params.push_back(LongTy);
2601 Params.push_back(ObjectPtrTy);
2602 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2603 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2604 SetPropertyFn =
2605 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2606 Params,
2607 false),
2608 "objc_setProperty");
2609 // Enumeration mutation.
2610
2611 Params.clear();
2612 Params.push_back(ObjectPtrTy);
2613 EnumerationMutationFn =
2614 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2615 Params,
2616 false),
2617 "objc_enumerationMutation");
2618
2619 // gc's API
2620 // id objc_read_weak (id *)
2621 Params.clear();
2622 Params.push_back(PtrObjectPtrTy);
2623 GcReadWeakFn =
2624 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2625 Params,
2626 false),
2627 "objc_read_weak");
2628 // id objc_assign_weak (id, id *)
2629 Params.clear();
2630 Params.push_back(ObjectPtrTy);
2631 Params.push_back(PtrObjectPtrTy);
2632 GcAssignWeakFn =
2633 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2634 Params,
2635 false),
2636 "objc_assign_weak");
2637 GcAssignGlobalFn =
2638 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2639 Params,
2640 false),
2641 "objc_assign_global");
2642 GcAssignIvarFn =
2643 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2644 Params,
2645 false),
2646 "objc_assign_ivar");
2647 GcAssignStrongCastFn =
2648 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2649 Params,
2650 false),
2651 "objc_assign_strongCast");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002652}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002653
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002654ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2655 : ObjCCommonTypesHelper(cgm)
2656{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002657 // struct _objc_method_description {
2658 // SEL name;
2659 // char *types;
2660 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002661 MethodDescriptionTy =
2662 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002663 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002664 NULL);
2665 CGM.getModule().addTypeName("struct._objc_method_description",
2666 MethodDescriptionTy);
2667
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002668 // struct _objc_method_description_list {
2669 // int count;
2670 // struct _objc_method_description[1];
2671 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002672 MethodDescriptionListTy =
2673 llvm::StructType::get(IntTy,
2674 llvm::ArrayType::get(MethodDescriptionTy, 0),
2675 NULL);
2676 CGM.getModule().addTypeName("struct._objc_method_description_list",
2677 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002678
2679 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002680 MethodDescriptionListPtrTy =
2681 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2682
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002683 // Protocol description structures
2684
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002685 // struct _objc_protocol_extension {
2686 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2687 // struct _objc_method_description_list *optional_instance_methods;
2688 // struct _objc_method_description_list *optional_class_methods;
2689 // struct _objc_property_list *instance_properties;
2690 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002691 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002692 llvm::StructType::get(IntTy,
2693 MethodDescriptionListPtrTy,
2694 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002695 PropertyListPtrTy,
2696 NULL);
2697 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2698 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002699
2700 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002701 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2702
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002703 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002704
2705 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2706 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2707
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002708 const llvm::Type *T =
2709 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2710 LongTy,
2711 llvm::ArrayType::get(ProtocolTyHolder, 0),
2712 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002713 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2714
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002715 // struct _objc_protocol {
2716 // struct _objc_protocol_extension *isa;
2717 // char *protocol_name;
2718 // struct _objc_protocol **_objc_protocol_list;
2719 // struct _objc_method_description_list *instance_methods;
2720 // struct _objc_method_description_list *class_methods;
2721 // }
2722 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002723 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002724 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2725 MethodDescriptionListPtrTy,
2726 MethodDescriptionListPtrTy,
2727 NULL);
2728 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2729
2730 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2731 CGM.getModule().addTypeName("struct._objc_protocol_list",
2732 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002733 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002734 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2735
2736 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002737 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002738 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002739
2740 // Class description structures
2741
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002742 // struct _objc_ivar {
2743 // char *ivar_name;
2744 // char *ivar_type;
2745 // int ivar_offset;
2746 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002747 IvarTy = llvm::StructType::get(Int8PtrTy,
2748 Int8PtrTy,
2749 IntTy,
2750 NULL);
2751 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2752
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002753 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002754 IvarListTy = llvm::OpaqueType::get();
2755 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2756 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2757
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002758 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002759 MethodListTy = llvm::OpaqueType::get();
2760 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2761 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2762
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002763 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002764 ClassExtensionTy =
2765 llvm::StructType::get(IntTy,
2766 Int8PtrTy,
2767 PropertyListPtrTy,
2768 NULL);
2769 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2770 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2771
2772 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2773
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002774 // struct _objc_class {
2775 // Class isa;
2776 // Class super_class;
2777 // char *name;
2778 // long version;
2779 // long info;
2780 // long instance_size;
2781 // struct _objc_ivar_list *ivars;
2782 // struct _objc_method_list *methods;
2783 // struct _objc_cache *cache;
2784 // struct _objc_protocol_list *protocols;
2785 // char *ivar_layout;
2786 // struct _objc_class_ext *ext;
2787 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002788 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2789 llvm::PointerType::getUnqual(ClassTyHolder),
2790 Int8PtrTy,
2791 LongTy,
2792 LongTy,
2793 LongTy,
2794 IvarListPtrTy,
2795 MethodListPtrTy,
2796 CachePtrTy,
2797 ProtocolListPtrTy,
2798 Int8PtrTy,
2799 ClassExtensionPtrTy,
2800 NULL);
2801 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2802
2803 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2804 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2805 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2806
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002807 // struct _objc_category {
2808 // char *category_name;
2809 // char *class_name;
2810 // struct _objc_method_list *instance_method;
2811 // struct _objc_method_list *class_method;
2812 // uint32_t size; // sizeof(struct _objc_category)
2813 // struct _objc_property_list *instance_properties;// category's @property
2814 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002815 CategoryTy = llvm::StructType::get(Int8PtrTy,
2816 Int8PtrTy,
2817 MethodListPtrTy,
2818 MethodListPtrTy,
2819 ProtocolListPtrTy,
2820 IntTy,
2821 PropertyListPtrTy,
2822 NULL);
2823 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2824
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002825 // Global metadata structures
2826
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002827 // struct _objc_symtab {
2828 // long sel_ref_cnt;
2829 // SEL *refs;
2830 // short cls_def_cnt;
2831 // short cat_def_cnt;
2832 // char *defs[cls_def_cnt + cat_def_cnt];
2833 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002834 SymtabTy = llvm::StructType::get(LongTy,
2835 SelectorPtrTy,
2836 ShortTy,
2837 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002838 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002839 NULL);
2840 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2841 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2842
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002843 // struct _objc_module {
2844 // long version;
2845 // long size; // sizeof(struct _objc_module)
2846 // char *name;
2847 // struct _objc_symtab* symtab;
2848 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002849 ModuleTy =
2850 llvm::StructType::get(LongTy,
2851 LongTy,
2852 Int8PtrTy,
2853 SymtabPtrTy,
2854 NULL);
2855 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002856
Daniel Dunbar49f66022008-09-24 03:38:44 +00002857 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002858
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002859 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002860 std::vector<const llvm::Type*> Params;
2861 Params.push_back(ObjectPtrTy);
2862 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002863 MessageSendFn =
2864 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2865 Params,
2866 true),
2867 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002868
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002869 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002870 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002871 Params.push_back(ObjectPtrTy);
2872 Params.push_back(SelectorPtrTy);
2873 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002874 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2875 Params,
2876 true),
2877 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002878
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002879 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00002880 Params.clear();
2881 Params.push_back(ObjectPtrTy);
2882 Params.push_back(SelectorPtrTy);
2883 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002884 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00002885 MessageSendFpretFn =
2886 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
2887 Params,
2888 true),
2889 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002890
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002891 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002892 Params.clear();
2893 Params.push_back(SuperPtrTy);
2894 Params.push_back(SelectorPtrTy);
2895 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002896 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2897 Params,
2898 true),
2899 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002900
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002901 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
2902 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002903 Params.clear();
2904 Params.push_back(Int8PtrTy);
2905 Params.push_back(SuperPtrTy);
2906 Params.push_back(SelectorPtrTy);
2907 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002908 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2909 Params,
2910 true),
2911 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002912
2913 // There is no objc_msgSendSuper_fpret? How can that work?
2914 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002915
Anders Carlsson124526b2008-09-09 10:10:21 +00002916 // FIXME: This is the size of the setjmp buffer and should be
2917 // target specific. 18 is what's used on 32-bit X86.
2918 uint64_t SetJmpBufferSize = 18;
2919
2920 // Exceptions
2921 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00002922 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00002923
2924 ExceptionDataTy =
2925 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
2926 SetJmpBufferSize),
2927 StackPtrTy, NULL);
2928 CGM.getModule().addTypeName("struct._objc_exception_data",
2929 ExceptionDataTy);
2930
2931 Params.clear();
2932 Params.push_back(ObjectPtrTy);
2933 ExceptionThrowFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002934 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2935 Params,
2936 false),
2937 "objc_exception_throw");
Anders Carlsson124526b2008-09-09 10:10:21 +00002938
2939 Params.clear();
2940 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
2941 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002942 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2943 Params,
2944 false),
2945 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00002946 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002947 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2948 Params,
2949 false),
2950 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00002951 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002952 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2953 Params,
2954 false),
2955 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00002956
2957 Params.clear();
2958 Params.push_back(ClassPtrTy);
2959 Params.push_back(ObjectPtrTy);
2960 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002961 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2962 Params,
2963 false),
2964 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00002965
2966 // synchronized APIs
2967 // void objc_sync_enter (id)
2968 Params.clear();
2969 Params.push_back(ObjectPtrTy);
2970 SyncEnterFn =
2971 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2972 Params,
2973 false),
2974 "objc_sync_enter");
2975 // void objc_sync_exit (id)
2976 SyncExitFn =
2977 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2978 Params,
2979 false),
2980 "objc_sync_exit");
2981
Anders Carlsson124526b2008-09-09 10:10:21 +00002982
2983 Params.clear();
2984 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
2985 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002986 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2987 Params,
2988 false),
2989 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002990
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002991}
2992
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002993ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002994: ObjCCommonTypesHelper(cgm)
2995{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002996 // struct _method_list_t {
2997 // uint32_t entsize; // sizeof(struct _objc_method)
2998 // uint32_t method_count;
2999 // struct _objc_method method_list[method_count];
3000 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003001 MethodListnfABITy = llvm::StructType::get(IntTy,
3002 IntTy,
3003 llvm::ArrayType::get(MethodTy, 0),
3004 NULL);
3005 CGM.getModule().addTypeName("struct.__method_list_t",
3006 MethodListnfABITy);
3007 // struct method_list_t *
3008 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003009
3010 // struct _protocol_t {
3011 // id isa; // NULL
3012 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003013 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003014 // const struct method_list_t * const instance_methods;
3015 // const struct method_list_t * const class_methods;
3016 // const struct method_list_t *optionalInstanceMethods;
3017 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003018 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003019 // const uint32_t size; // sizeof(struct _protocol_t)
3020 // const uint32_t flags; // = 0
3021 // }
3022
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003023 // Holder for struct _protocol_list_t *
3024 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3025
3026 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3027 Int8PtrTy,
3028 llvm::PointerType::getUnqual(
3029 ProtocolListTyHolder),
3030 MethodListnfABIPtrTy,
3031 MethodListnfABIPtrTy,
3032 MethodListnfABIPtrTy,
3033 MethodListnfABIPtrTy,
3034 PropertyListPtrTy,
3035 IntTy,
3036 IntTy,
3037 NULL);
3038 CGM.getModule().addTypeName("struct._protocol_t",
3039 ProtocolnfABITy);
3040
Fariborz Jahanianda320092009-01-29 19:24:30 +00003041 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003042 // long protocol_count; // Note, this is 32/64 bit
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003043 // struct _protocol_t[protocol_count];
3044 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003045 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3046 llvm::ArrayType::get(
3047 ProtocolnfABITy, 0),
3048 NULL);
3049 CGM.getModule().addTypeName("struct._objc_protocol_list",
3050 ProtocolListnfABITy);
3051
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003052 // struct _objc_protocol_list*
3053 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003054
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003055 // FIXME! Is this doing the right thing?
3056 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3057 ProtocolListnfABIPtrTy);
3058
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003059 // struct _ivar_t {
3060 // unsigned long int *offset; // pointer to ivar offset location
3061 // char *name;
3062 // char *type;
3063 // uint32_t alignment;
3064 // uint32_t size;
3065 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003066 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3067 Int8PtrTy,
3068 Int8PtrTy,
3069 IntTy,
3070 IntTy,
3071 NULL);
3072 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3073
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003074 // struct _ivar_list_t {
3075 // uint32 entsize; // sizeof(struct _ivar_t)
3076 // uint32 count;
3077 // struct _iver_t list[count];
3078 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003079 IvarListnfABITy = llvm::StructType::get(IntTy,
3080 IntTy,
3081 llvm::ArrayType::get(
3082 IvarnfABITy, 0),
3083 NULL);
3084 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3085
3086 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003087
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003088 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003089 // uint32_t const flags;
3090 // uint32_t const instanceStart;
3091 // uint32_t const instanceSize;
3092 // uint32_t const reserved; // only when building for 64bit targets
3093 // const uint8_t * const ivarLayout;
3094 // const char *const name;
3095 // const struct _method_list_t * const baseMethods;
3096 // const struct _objc_protocol_list *const baseProtocols;
3097 // const struct _ivar_list_t *const ivars;
3098 // const uint8_t * const weakIvarLayout;
3099 // const struct _prop_list_t * const properties;
3100 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003101
3102 // FIXME. Add 'reserved' field in 64bit abi mode!
3103 ClassRonfABITy = llvm::StructType::get(IntTy,
3104 IntTy,
3105 IntTy,
3106 Int8PtrTy,
3107 Int8PtrTy,
3108 MethodListnfABIPtrTy,
3109 ProtocolListnfABIPtrTy,
3110 IvarListnfABIPtrTy,
3111 Int8PtrTy,
3112 PropertyListPtrTy,
3113 NULL);
3114 CGM.getModule().addTypeName("struct._class_ro_t",
3115 ClassRonfABITy);
3116
3117 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3118 std::vector<const llvm::Type*> Params;
3119 Params.push_back(ObjectPtrTy);
3120 Params.push_back(SelectorPtrTy);
3121 ImpnfABITy = llvm::PointerType::getUnqual(
3122 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3123
3124 // struct _class_t {
3125 // struct _class_t *isa;
3126 // struct _class_t * const superclass;
3127 // void *cache;
3128 // IMP *vtable;
3129 // struct class_ro_t *ro;
3130 // }
3131
3132 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3133 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3134 llvm::PointerType::getUnqual(ClassTyHolder),
3135 CachePtrTy,
3136 llvm::PointerType::getUnqual(ImpnfABITy),
3137 llvm::PointerType::getUnqual(
3138 ClassRonfABITy),
3139 NULL);
3140 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3141
3142 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3143 ClassnfABITy);
3144
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003145 // LLVM for struct _class_t *
3146 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3147
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003148 // struct _category_t {
3149 // const char * const name;
3150 // struct _class_t *const cls;
3151 // const struct _method_list_t * const instance_methods;
3152 // const struct _method_list_t * const class_methods;
3153 // const struct _protocol_list_t * const protocols;
3154 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003155 // }
3156 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003157 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003158 MethodListnfABIPtrTy,
3159 MethodListnfABIPtrTy,
3160 ProtocolListnfABIPtrTy,
3161 PropertyListPtrTy,
3162 NULL);
3163 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
3164
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003165}
3166
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003167llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3168 FinishNonFragileABIModule();
3169
3170 return NULL;
3171}
3172
3173void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3174 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003175
3176 // Build list of all implemented classe addresses in array
3177 // L_OBJC_LABEL_CLASS_$.
3178 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
3179 // list of 'nonlazy' implementations (defined as those with a +load{}
3180 // method!!).
3181 unsigned NumClasses = DefinedClasses.size();
3182 if (NumClasses) {
3183 std::vector<llvm::Constant*> Symbols(NumClasses);
3184 for (unsigned i=0; i<NumClasses; i++)
3185 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3186 ObjCTypes.Int8PtrTy);
3187 llvm::Constant* Init =
3188 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3189 NumClasses),
3190 Symbols);
3191
3192 llvm::GlobalVariable *GV =
3193 new llvm::GlobalVariable(Init->getType(), false,
3194 llvm::GlobalValue::InternalLinkage,
3195 Init,
3196 "\01L_OBJC_LABEL_CLASS_$",
3197 &CGM.getModule());
3198 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
3199 UsedGlobals.push_back(GV);
3200 }
3201
3202 // Build list of all implemented category addresses in array
3203 // L_OBJC_LABEL_CATEGORY_$.
3204 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
3205 // list of 'nonlazy' category implementations (defined as those with a +load{}
3206 // method!!).
3207 unsigned NumCategory = DefinedCategories.size();
3208 if (NumCategory) {
3209 std::vector<llvm::Constant*> Symbols(NumCategory);
3210 for (unsigned i=0; i<NumCategory; i++)
3211 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
3212 ObjCTypes.Int8PtrTy);
3213 llvm::Constant* Init =
3214 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3215 NumCategory),
3216 Symbols);
3217
3218 llvm::GlobalVariable *GV =
3219 new llvm::GlobalVariable(Init->getType(), false,
3220 llvm::GlobalValue::InternalLinkage,
3221 Init,
3222 "\01L_OBJC_LABEL_CATEGORY_$",
3223 &CGM.getModule());
3224 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
3225 UsedGlobals.push_back(GV);
3226 }
3227
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00003228 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
3229 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
3230 std::vector<llvm::Constant*> Values(2);
3231 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3232 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3233 llvm::Constant* Init = llvm::ConstantArray::get(
3234 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
3235 Values);
3236 llvm::GlobalVariable *IMGV =
3237 new llvm::GlobalVariable(Init->getType(), false,
3238 llvm::GlobalValue::InternalLinkage,
3239 Init,
3240 "\01L_OBJC_IMAGE_INFO",
3241 &CGM.getModule());
3242 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
3243 UsedGlobals.push_back(IMGV);
3244
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003245 std::vector<llvm::Constant*> Used;
3246 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3247 e = UsedGlobals.end(); i != e; ++i) {
3248 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3249 }
3250
3251 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3252 llvm::GlobalValue *GV =
3253 new llvm::GlobalVariable(AT, false,
3254 llvm::GlobalValue::AppendingLinkage,
3255 llvm::ConstantArray::get(AT, Used),
3256 "llvm.used",
3257 &CGM.getModule());
3258
3259 GV->setSection("llvm.metadata");
3260
3261}
3262
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003263// Metadata flags
3264enum MetaDataDlags {
3265 CLS = 0x0,
3266 CLS_META = 0x1,
3267 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003268 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003269 CLS_EXCEPTION = 0x20
3270};
3271/// BuildClassRoTInitializer - generate meta-data for:
3272/// struct _class_ro_t {
3273/// uint32_t const flags;
3274/// uint32_t const instanceStart;
3275/// uint32_t const instanceSize;
3276/// uint32_t const reserved; // only when building for 64bit targets
3277/// const uint8_t * const ivarLayout;
3278/// const char *const name;
3279/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00003280/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003281/// const struct _ivar_list_t *const ivars;
3282/// const uint8_t * const weakIvarLayout;
3283/// const struct _prop_list_t * const properties;
3284/// }
3285///
3286llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
3287 unsigned flags,
3288 unsigned InstanceStart,
3289 unsigned InstanceSize,
3290 const ObjCImplementationDecl *ID) {
3291 std::string ClassName = ID->getNameAsString();
3292 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
3293 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
3294 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
3295 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
3296 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianda320092009-01-29 19:24:30 +00003297 // FIXME. ivarLayout is currently null!
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003298 Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3299 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003300 // const struct _method_list_t * const baseMethods;
3301 std::vector<llvm::Constant*> Methods;
3302 std::string MethodListName("\01l_OBJC_$_");
3303 if (flags & CLS_META) {
3304 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
3305 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
3306 e = ID->classmeth_end(); i != e; ++i) {
3307 // Class methods should always be defined.
3308 Methods.push_back(GetMethodConstant(*i));
3309 }
3310 } else {
3311 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
3312 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
3313 e = ID->instmeth_end(); i != e; ++i) {
3314 // Instance methods should always be defined.
3315 Methods.push_back(GetMethodConstant(*i));
3316 }
Fariborz Jahanian939abce2009-01-28 22:46:49 +00003317 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
3318 e = ID->propimpl_end(); i != e; ++i) {
3319 ObjCPropertyImplDecl *PID = *i;
3320
3321 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
3322 ObjCPropertyDecl *PD = PID->getPropertyDecl();
3323
3324 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3325 if (llvm::Constant *C = GetMethodConstant(MD))
3326 Methods.push_back(C);
3327 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3328 if (llvm::Constant *C = GetMethodConstant(MD))
3329 Methods.push_back(C);
3330 }
3331 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003332 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003333 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003334 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003335
3336 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3337 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
3338 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
3339 + OID->getNameAsString(),
3340 OID->protocol_begin(),
3341 OID->protocol_end());
3342
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003343 if (flags & CLS_META)
3344 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3345 else
3346 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003347 // FIXME. weakIvarLayout is currently null.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003348 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003349 if (flags & CLS_META)
3350 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3351 else
3352 Values[ 9] =
3353 EmitPropertyList(
3354 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
3355 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003356 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
3357 Values);
3358 llvm::GlobalVariable *CLASS_RO_GV =
3359 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
3360 llvm::GlobalValue::InternalLinkage,
3361 Init,
3362 (flags & CLS_META) ?
3363 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
3364 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
3365 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003366 CLASS_RO_GV->setAlignment(
3367 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003368 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003369 UsedGlobals.push_back(CLASS_RO_GV);
3370 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003371
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003372}
3373
3374/// BuildClassMetaData - This routine defines that to-level meta-data
3375/// for the given ClassName for:
3376/// struct _class_t {
3377/// struct _class_t *isa;
3378/// struct _class_t * const superclass;
3379/// void *cache;
3380/// IMP *vtable;
3381/// struct class_ro_t *ro;
3382/// }
3383///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003384llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
3385 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003386 llvm::Constant *IsAGV,
3387 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003388 llvm::Constant *ClassRoGV,
3389 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003390 std::vector<llvm::Constant*> Values(5);
3391 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003392 Values[1] = SuperClassGV
3393 ? SuperClassGV
3394 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003395 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
3396 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
3397 Values[4] = ClassRoGV; // &CLASS_RO_GV
3398 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
3399 Values);
3400 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName);
3401 if (GV)
3402 GV->setInitializer(Init);
3403 else
3404 GV =
3405 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3406 llvm::GlobalValue::ExternalLinkage,
3407 Init,
3408 ClassName,
3409 &CGM.getModule());
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00003410 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003411 GV->setAlignment(
3412 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003413 if (HiddenVisibility)
3414 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003415 UsedGlobals.push_back(GV);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003416 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003417}
3418
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003419void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
3420 std::string ClassName = ID->getNameAsString();
3421 if (!ObjCEmptyCacheVar) {
3422 ObjCEmptyCacheVar = new llvm::GlobalVariable(
3423 ObjCTypes.CachePtrTy,
3424 false,
3425 llvm::GlobalValue::ExternalLinkage,
3426 0,
3427 "\01_objc_empty_cache",
3428 &CGM.getModule());
3429 UsedGlobals.push_back(ObjCEmptyCacheVar);
3430
3431 ObjCEmptyVtableVar = new llvm::GlobalVariable(
3432 llvm::PointerType::getUnqual(
3433 ObjCTypes.ImpnfABITy),
3434 false,
3435 llvm::GlobalValue::ExternalLinkage,
3436 0,
3437 "\01_objc_empty_vtable",
3438 &CGM.getModule());
3439 UsedGlobals.push_back(ObjCEmptyVtableVar);
3440 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003441 assert(ID->getClassInterface() &&
3442 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003443 uint32_t InstanceStart =
3444 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
3445 uint32_t InstanceSize = InstanceStart;
3446 uint32_t flags = CLS_META;
3447 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
3448 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003449
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003450 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003451
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003452 bool classIsHidden = IsClassHidden(ID->getClassInterface());
3453 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003454 flags |= OBJC2_CLS_HIDDEN;
3455 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003456 // class is root
3457 flags |= CLS_ROOT;
3458 std::string SuperClassName = ObjCClassName + ClassName;
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 std::string IsAClassName = ObjCMetaClassName + ClassName;
3469 IsAGV = CGM.getModule().getGlobalVariable(IsAClassName);
3470 if (!IsAGV)
3471 IsAGV =
3472 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3473 llvm::GlobalValue::ExternalLinkage,
3474 0,
3475 IsAClassName,
3476 &CGM.getModule());
3477 UsedGlobals.push_back(IsAGV);
3478 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003479 // Has a root. Current class is not a root.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003480 std::string RootClassName =
3481 ID->getClassInterface()->getSuperClass()->getNameAsString();
3482 std::string SuperClassName = ObjCMetaClassName + RootClassName;
3483 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3484 if (!SuperClassGV)
3485 SuperClassGV =
3486 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3487 llvm::GlobalValue::ExternalLinkage,
3488 0,
3489 SuperClassName,
3490 &CGM.getModule());
3491 UsedGlobals.push_back(SuperClassGV);
3492 IsAGV = SuperClassGV;
3493 }
3494 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
3495 InstanceStart,
3496 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003497 std::string TClassName = ObjCMetaClassName + ClassName;
3498 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003499 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
3500 classIsHidden);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003501
3502 // Metadata for the class
3503 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003504 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003505 flags |= OBJC2_CLS_HIDDEN;
3506 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003507 flags |= CLS_ROOT;
3508 SuperClassGV = 0;
3509 }
3510 else {
3511 // Has a root. Current class is not a root.
3512 std::string RootClassName =
3513 ID->getClassInterface()->getSuperClass()->getNameAsString();
3514 std::string SuperClassName = ObjCClassName + RootClassName;
3515 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3516 if (!SuperClassGV)
3517 SuperClassGV =
3518 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3519 llvm::GlobalValue::ExternalLinkage,
3520 0,
3521 SuperClassName,
3522 &CGM.getModule());
3523 UsedGlobals.push_back(SuperClassGV);
3524
3525 }
3526
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003527 InstanceStart = InstanceSize = 0;
3528 if (ObjCInterfaceDecl *OID =
3529 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
3530 // FIXME. Share this with the one in EmitIvarList.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003531 const llvm::Type *InterfaceTy =
3532 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
3533 const llvm::StructLayout *Layout =
3534 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003535
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003536 RecordDecl::field_iterator firstField, lastField;
3537 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003538
3539 for (RecordDecl::field_iterator e = RD->field_end(),
3540 ifield = firstField; ifield != e; ++ifield)
3541 lastField = ifield;
3542
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003543 if (lastField != RD->field_end()) {
3544 FieldDecl *Field = *lastField;
3545 const llvm::Type *FieldTy =
3546 CGM.getTypes().ConvertTypeForMem(Field->getType());
3547 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3548 InstanceSize = Layout->getElementOffset(
3549 CGM.getTypes().getLLVMFieldNo(Field)) +
3550 Size;
3551 if (firstField == RD->field_end())
3552 InstanceStart = InstanceSize;
3553 else
3554 InstanceStart = Layout->getElementOffset(CGM.getTypes().
3555 getLLVMFieldNo(*firstField));
3556 }
3557 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003558 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003559 InstanceStart,
3560 InstanceSize,
3561 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003562
3563 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003564 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003565 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
3566 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003567 DefinedClasses.push_back(ClassMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003568}
3569
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00003570/// GenerateProtocolRef - This routine is called to generate code for
3571/// a protocol reference expression; as in:
3572/// @code
3573/// @protocol(Proto1);
3574/// @endcode
3575/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
3576/// which will hold address of the protocol meta-data.
3577///
3578llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
3579 const ObjCProtocolDecl *PD) {
3580
3581 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
3582 ObjCTypes.ExternalProtocolPtrTy);
3583
3584 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
3585 ProtocolName += PD->getNameAsCString();
3586
3587 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
3588 if (PTGV)
3589 return Builder.CreateLoad(PTGV, false, "tmp");
3590 PTGV = new llvm::GlobalVariable(
3591 Init->getType(), false,
3592 llvm::GlobalValue::WeakLinkage,
3593 Init,
3594 ProtocolName,
3595 &CGM.getModule());
3596 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
3597 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3598 UsedGlobals.push_back(PTGV);
3599 return Builder.CreateLoad(PTGV, false, "tmp");
3600}
3601
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003602/// GenerateCategory - Build metadata for a category implementation.
3603/// struct _category_t {
3604/// const char * const name;
3605/// struct _class_t *const cls;
3606/// const struct _method_list_t * const instance_methods;
3607/// const struct _method_list_t * const class_methods;
3608/// const struct _protocol_list_t * const protocols;
3609/// const struct _prop_list_t * const properties;
3610/// }
3611///
3612void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
3613{
3614 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003615 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
3616 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003617 "_$_" + OCD->getNameAsString());
3618 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString());
3619
3620 std::vector<llvm::Constant*> Values(6);
3621 Values[0] = GetClassName(OCD->getIdentifier());
3622 // meta-class entry symbol
3623 llvm::GlobalVariable *ClassGV =
3624 CGM.getModule().getGlobalVariable(ExtClassName);
3625 if (!ClassGV)
3626 ClassGV =
3627 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3628 llvm::GlobalValue::ExternalLinkage,
3629 0,
3630 ExtClassName,
3631 &CGM.getModule());
3632 UsedGlobals.push_back(ClassGV);
3633 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003634 std::vector<llvm::Constant*> Methods;
3635 std::string MethodListName(Prefix);
3636 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
3637 "_$_" + OCD->getNameAsString();
3638
3639 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
3640 e = OCD->instmeth_end(); i != e; ++i) {
3641 // Instance methods should always be defined.
3642 Methods.push_back(GetMethodConstant(*i));
3643 }
3644
3645 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003646 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003647 Methods);
3648
3649 MethodListName = Prefix;
3650 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
3651 OCD->getNameAsString();
3652 Methods.clear();
3653 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
3654 e = OCD->classmeth_end(); i != e; ++i) {
3655 // Class methods should always be defined.
3656 Methods.push_back(GetMethodConstant(*i));
3657 }
3658
3659 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003660 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003661 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003662 const ObjCCategoryDecl *Category =
3663 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanianeb732132009-01-29 23:23:06 +00003664 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
3665 + Interface->getNameAsString() + "_$_"
3666 + Category->getNameAsString(),
3667 Category->protocol_begin(),
3668 Category->protocol_end());
3669
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003670 std::string ExtName(Interface->getNameAsString() + "_$_" +
3671 OCD->getNameAsString());
3672 Values[5] =
3673 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
3674 OCD, Category, ObjCTypes);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003675 llvm::Constant *Init =
3676 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
3677 Values);
3678 llvm::GlobalVariable *GCATV
3679 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
3680 false,
3681 llvm::GlobalValue::InternalLinkage,
3682 Init,
3683 ExtCatName,
3684 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003685 GCATV->setAlignment(
3686 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003687 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003688 UsedGlobals.push_back(GCATV);
3689 DefinedCategories.push_back(GCATV);
3690}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003691
3692/// GetMethodConstant - Return a struct objc_method constant for the
3693/// given method if it has been defined. The result is null if the
3694/// method has not been defined. The return value has type MethodPtrTy.
3695llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
3696 const ObjCMethodDecl *MD) {
3697 // FIXME: Use DenseMap::lookup
3698 llvm::Function *Fn = MethodDefinitions[MD];
3699 if (!Fn)
3700 return 0;
3701
3702 std::vector<llvm::Constant*> Method(3);
3703 Method[0] =
3704 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
3705 ObjCTypes.SelectorPtrTy);
3706 Method[1] = GetMethodVarType(MD);
3707 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
3708 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
3709}
3710
3711/// EmitMethodList - Build meta-data for method declarations
3712/// struct _method_list_t {
3713/// uint32_t entsize; // sizeof(struct _objc_method)
3714/// uint32_t method_count;
3715/// struct _objc_method method_list[method_count];
3716/// }
3717///
3718llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
3719 const std::string &Name,
3720 const char *Section,
3721 const ConstantVector &Methods) {
3722 // Return null for empty list.
3723 if (Methods.empty())
3724 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
3725
3726 std::vector<llvm::Constant*> Values(3);
3727 // sizeof(struct _objc_method)
3728 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
3729 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3730 // method_count
3731 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
3732 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
3733 Methods.size());
3734 Values[2] = llvm::ConstantArray::get(AT, Methods);
3735 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3736
3737 llvm::GlobalVariable *GV =
3738 new llvm::GlobalVariable(Init->getType(), false,
3739 llvm::GlobalValue::InternalLinkage,
3740 Init,
3741 Name,
3742 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003743 GV->setAlignment(
3744 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003745 GV->setSection(Section);
3746 UsedGlobals.push_back(GV);
3747 return llvm::ConstantExpr::getBitCast(GV,
3748 ObjCTypes.MethodListnfABIPtrTy);
3749}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003750
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003751llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
3752 const ObjCImplementationDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003753 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003754 unsigned long int Offset) {
3755
3756 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003757 + Ivar->getNameAsString());
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003758 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
3759
3760 llvm::GlobalVariable *IvarOffsetGV =
3761 CGM.getModule().getGlobalVariable(ExternalName);
3762 if (IvarOffsetGV) {
3763 // ivar offset symbol already built due to user code referencing it.
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003764 IvarOffsetGV->setAlignment(
3765 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.Int8PtrTy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003766 IvarOffsetGV->setInitializer(Init);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003767 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003768 return IvarOffsetGV;
3769 }
3770
3771 IvarOffsetGV =
3772 new llvm::GlobalVariable(Init->getType(),
3773 false,
3774 llvm::GlobalValue::ExternalLinkage,
3775 Init,
3776 ExternalName,
3777 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003778 IvarOffsetGV->setAlignment(
3779 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.Int8PtrTy));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003780 // @private and @package have hidden visibility.
3781 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public ||
3782 Ivar->getAccessControl() == ObjCIvarDecl::Protected);
3783 if (!globalVisibility)
3784 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003785 else
3786 if (const ObjCInterfaceDecl *OID = ID->getClassInterface())
3787 if (IsClassHidden(OID))
3788 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3789
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003790 IvarOffsetGV->setSection("__DATA, __objc_const");
3791 UsedGlobals.push_back(IvarOffsetGV);
3792
3793 return llvm::ConstantExpr::getBitCast(
3794 IvarOffsetGV,
3795 llvm::PointerType::getUnqual(ObjCTypes.LongTy));
3796}
3797
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003798/// EmitIvarList - Emit the ivar list for the given
3799/// implementation. If ForClass is true the list of class ivars
3800/// (i.e. metaclass ivars) is emitted, otherwise the list of
3801/// interface ivars will be emitted. The return value has type
3802/// IvarListnfABIPtrTy.
3803/// struct _ivar_t {
3804/// unsigned long int *offset; // pointer to ivar offset location
3805/// char *name;
3806/// char *type;
3807/// uint32_t alignment;
3808/// uint32_t size;
3809/// }
3810/// struct _ivar_list_t {
3811/// uint32 entsize; // sizeof(struct _ivar_t)
3812/// uint32 count;
3813/// struct _iver_t list[count];
3814/// }
3815///
3816llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
3817 const ObjCImplementationDecl *ID) {
3818
3819 std::vector<llvm::Constant*> Ivars, Ivar(5);
3820
3821 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3822 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
3823
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003824 // FIXME. Consolidate this with similar code in GenerateClass.
3825 const llvm::Type *InterfaceTy =
3826 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(
3827 const_cast<ObjCInterfaceDecl*>(OID)));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003828 const llvm::StructLayout *Layout =
3829 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003830
3831 RecordDecl::field_iterator i,p;
3832 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003833 ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin();
3834
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003835 for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) {
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003836 FieldDecl *Field = *i;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003837 unsigned long offset = Layout->getElementOffset(CGM.getTypes().
3838 getLLVMFieldNo(Field));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003839 const ObjCIvarDecl *ivarDecl = *I++;
3840 Ivar[0] = EmitIvarOffsetVar(ID, ivarDecl, offset);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003841 if (Field->getIdentifier())
3842 Ivar[1] = GetMethodVarName(Field->getIdentifier());
3843 else
3844 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3845 std::string TypeStr;
3846 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3847 Ivar[2] = GetMethodVarType(TypeStr);
3848 const llvm::Type *FieldTy =
3849 CGM.getTypes().ConvertTypeForMem(Field->getType());
3850 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3851 unsigned Align = CGM.getContext().getPreferredTypeAlign(
3852 Field->getType().getTypePtr()) >> 3;
3853 Align = llvm::Log2_32(Align);
3854 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Fariborz Jahanian07236ba2009-01-27 22:27:56 +00003855 // NOTE. Size of a bitfield does not match gcc's, because of the way
3856 // bitfields are treated special in each. But I am told that 'size'
3857 // for bitfield ivars is ignored by the runtime so it does not matter.
3858 // (even if it matters, some day, there is enough info. to get the bitfield
3859 // right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003860 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3861 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
3862 }
3863 // Return null for empty list.
3864 if (Ivars.empty())
3865 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3866 std::vector<llvm::Constant*> Values(3);
3867 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
3868 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3869 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
3870 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
3871 Ivars.size());
3872 Values[2] = llvm::ConstantArray::get(AT, Ivars);
3873 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3874 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
3875 llvm::GlobalVariable *GV =
3876 new llvm::GlobalVariable(Init->getType(), false,
3877 llvm::GlobalValue::InternalLinkage,
3878 Init,
3879 Prefix + OID->getNameAsString(),
3880 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003881 GV->setAlignment(
3882 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003883 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003884
3885 UsedGlobals.push_back(GV);
3886 return llvm::ConstantExpr::getBitCast(GV,
3887 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003888}
3889
3890llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
3891 const ObjCProtocolDecl *PD) {
3892 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
3893
3894 if (!Entry) {
3895 // We use the initializer as a marker of whether this is a forward
3896 // reference or not. At module finalization we add the empty
3897 // contents for protocols which were referenced but never defined.
3898 Entry =
3899 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
3900 llvm::GlobalValue::ExternalLinkage,
3901 0,
3902 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
3903 &CGM.getModule());
3904 Entry->setSection("__DATA,__datacoal_nt,coalesced");
3905 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003906 }
3907
3908 return Entry;
3909}
3910
3911/// GetOrEmitProtocol - Generate the protocol meta-data:
3912/// @code
3913/// struct _protocol_t {
3914/// id isa; // NULL
3915/// const char * const protocol_name;
3916/// const struct _protocol_list_t * protocol_list; // super protocols
3917/// const struct method_list_t * const instance_methods;
3918/// const struct method_list_t * const class_methods;
3919/// const struct method_list_t *optionalInstanceMethods;
3920/// const struct method_list_t *optionalClassMethods;
3921/// const struct _prop_list_t * properties;
3922/// const uint32_t size; // sizeof(struct _protocol_t)
3923/// const uint32_t flags; // = 0
3924/// }
3925/// @endcode
3926///
3927
3928llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
3929 const ObjCProtocolDecl *PD) {
3930 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
3931
3932 // Early exit if a defining object has already been generated.
3933 if (Entry && Entry->hasInitializer())
3934 return Entry;
3935
3936 const char *ProtocolName = PD->getNameAsCString();
3937
3938 // Construct method lists.
3939 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
3940 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
3941 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
3942 e = PD->instmeth_end(); i != e; ++i) {
3943 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003944 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003945 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
3946 OptInstanceMethods.push_back(C);
3947 } else {
3948 InstanceMethods.push_back(C);
3949 }
3950 }
3951
3952 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
3953 e = PD->classmeth_end(); i != e; ++i) {
3954 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003955 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003956 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
3957 OptClassMethods.push_back(C);
3958 } else {
3959 ClassMethods.push_back(C);
3960 }
3961 }
3962
3963 std::vector<llvm::Constant*> Values(10);
3964 // isa is NULL
3965 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
3966 Values[1] = GetClassName(PD->getIdentifier());
3967 Values[2] = EmitProtocolList(
3968 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
3969 PD->protocol_begin(),
3970 PD->protocol_end());
3971
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003972 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00003973 + PD->getNameAsString(),
3974 "__DATA, __objc_const",
3975 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003976 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00003977 + PD->getNameAsString(),
3978 "__DATA, __objc_const",
3979 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003980 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00003981 + PD->getNameAsString(),
3982 "__DATA, __objc_const",
3983 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003984 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00003985 + PD->getNameAsString(),
3986 "__DATA, __objc_const",
3987 OptClassMethods);
3988 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
3989 0, PD, ObjCTypes);
3990 uint32_t Size =
3991 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
3992 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3993 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
3994 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
3995 Values);
3996
3997 if (Entry) {
3998 // Already created, fix the linkage and update the initializer.
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00003999 Entry->setLinkage(llvm::GlobalValue::WeakLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004000 Entry->setInitializer(Init);
4001 } else {
4002 Entry =
4003 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004004 llvm::GlobalValue::WeakLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004005 Init,
4006 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4007 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004008 Entry->setAlignment(
4009 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004010 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004011 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004012 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4013
4014 // Use this protocol meta-data to build protocol list table in section
4015 // __DATA, __objc_protolist
4016 llvm::Type *ptype = llvm::PointerType::getUnqual(ObjCTypes.ProtocolnfABITy);
4017 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
4018 ptype, false,
4019 llvm::GlobalValue::WeakLinkage,
4020 Entry,
4021 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4022 +ProtocolName,
4023 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004024 PTGV->setAlignment(
4025 CGM.getTargetData().getPrefTypeAlignment(ptype));
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004026 PTGV->setSection("__DATA, __objc_protolist");
4027 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4028 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004029 return Entry;
4030}
4031
4032/// EmitProtocolList - Generate protocol list meta-data:
4033/// @code
4034/// struct _protocol_list_t {
4035/// long protocol_count; // Note, this is 32/64 bit
4036/// struct _protocol_t[protocol_count];
4037/// }
4038/// @endcode
4039///
4040llvm::Constant *
4041CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4042 ObjCProtocolDecl::protocol_iterator begin,
4043 ObjCProtocolDecl::protocol_iterator end) {
4044 std::vector<llvm::Constant*> ProtocolRefs;
4045
4046 for (; begin != end; ++begin)
4047 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4048
4049 // Just return null for empty protocol lists
4050 if (ProtocolRefs.empty())
4051 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4052
4053 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4054 if (GV)
4055 return GV;
4056 // This list is null terminated.
4057 ProtocolRefs.push_back(llvm::Constant::getNullValue(
4058 ObjCTypes.ProtocolListnfABIPtrTy));
4059
4060 std::vector<llvm::Constant*> Values(2);
4061 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4062 Values[1] =
4063 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolListnfABIPtrTy,
4064 ProtocolRefs.size()),
4065 ProtocolRefs);
4066
4067 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4068 GV = new llvm::GlobalVariable(Init->getType(), false,
4069 llvm::GlobalValue::InternalLinkage,
4070 Init,
4071 Name,
4072 &CGM.getModule());
4073 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004074 GV->setAlignment(
4075 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004076 UsedGlobals.push_back(GV);
4077 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
4078}
4079
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004080/// GetMethodDescriptionConstant - This routine build following meta-data:
4081/// struct _objc_method {
4082/// SEL _cmd;
4083/// char *method_type;
4084/// char *_imp;
4085/// }
4086
4087llvm::Constant *
4088CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4089 std::vector<llvm::Constant*> Desc(3);
4090 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4091 ObjCTypes.SelectorPtrTy);
4092 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004093 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004094 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4095 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4096}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004097/* *** */
4098
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004099CodeGen::CGObjCRuntime *
4100CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00004101 return new CGObjCMac(CGM);
4102}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004103
4104CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004105CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004106 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004107}