blob: 7e42d56159239564c480189c9774e2d2ddc0e0be [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 Jahanian2e4672b2009-02-03 23:49:23 +0000218private:
219 llvm::Function *MessageSendFixupFn, *MessageSendFpretFixupFn,
220 *MessageSendStretFixupFn, *MessageSendIdFixupFn,
221 *MessageSendIdStretFixupFn, *MessageSendSuper2FixupFn,
222 *MessageSendSuper2StretFixupFn;
223
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000224public:
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000225 // MethodListnfABITy - LLVM for struct _method_list_t
226 const llvm::StructType *MethodListnfABITy;
227
228 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
229 const llvm::Type *MethodListnfABIPtrTy;
230
231 // ProtocolnfABITy = LLVM for struct _protocol_t
232 const llvm::StructType *ProtocolnfABITy;
233
234 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
235 const llvm::StructType *ProtocolListnfABITy;
236
237 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
238 const llvm::Type *ProtocolListnfABIPtrTy;
239
240 // ClassnfABITy - LLVM for struct _class_t
241 const llvm::StructType *ClassnfABITy;
242
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000243 // ClassnfABIPtrTy - LLVM for struct _class_t*
244 const llvm::Type *ClassnfABIPtrTy;
245
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000246 // IvarnfABITy - LLVM for struct _ivar_t
247 const llvm::StructType *IvarnfABITy;
248
249 // IvarListnfABITy - LLVM for struct _ivar_list_t
250 const llvm::StructType *IvarListnfABITy;
251
252 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
253 const llvm::Type *IvarListnfABIPtrTy;
254
255 // ClassRonfABITy - LLVM for struct _class_ro_t
256 const llvm::StructType *ClassRonfABITy;
257
258 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
259 const llvm::Type *ImpnfABITy;
260
261 // CategorynfABITy - LLVM for struct _category_t
262 const llvm::StructType *CategorynfABITy;
263
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000264 // New types for nonfragile abi messaging.
265
266 // MessageRefTy - LLVM for:
267 // struct _message_ref_t {
268 // IMP messenger;
269 // SEL name;
270 // };
271 const llvm::StructType *MessageRefTy;
272
273 // MessageRefPtrTy - LLVM for struct _message_ref_t*
274 const llvm::Type *MessageRefPtrTy;
275
276 // SuperMessageRefTy - LLVM for:
277 // struct _super_message_ref_t {
278 // SUPER_IMP messenger;
279 // SEL name;
280 // };
281 const llvm::StructType *SuperMessageRefTy;
282
283 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
284 const llvm::Type *SuperMessageRefPtrTy;
285
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000286 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
287 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000288};
289
290class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
291protected:
292 CodeGen::CodeGenModule &CGM;
293 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000294 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000295
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000296 /// LazySymbols - Symbols to generate a lazy reference for. See
297 /// DefinedSymbols and FinishModule().
298 std::set<IdentifierInfo*> LazySymbols;
299
300 /// DefinedSymbols - External symbols which are defined by this
301 /// module. The symbols in this list and LazySymbols are used to add
302 /// special linker symbols which ensure that Objective-C modules are
303 /// linked properly.
304 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000305
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000306 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000307 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000308
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000309 /// MethodVarNames - uniqued method variable names.
310 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000311
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000312 /// MethodVarTypes - uniqued method type signatures. We have to use
313 /// a StringMap here because have no other unique reference.
314 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000315
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000316 /// MethodDefinitions - map of methods which have been defined in
317 /// this translation unit.
318 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000319
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000320 /// PropertyNames - uniqued method variable names.
321 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000322
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000323 /// ClassReferences - uniqued class references.
324 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000325
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000326 /// SelectorReferences - uniqued selector references.
327 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000328
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000329 /// Protocols - Protocols for which an objc_protocol structure has
330 /// been emitted. Forward declarations are handled by creating an
331 /// empty structure whose initializer is filled in when/if defined.
332 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000333
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000334 /// DefinedProtocols - Protocols which have actually been
335 /// defined. We should not need this, see FIXME in GenerateProtocol.
336 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000337
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000338 /// DefinedClasses - List of defined classes.
339 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000340
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000341 /// DefinedCategories - List of defined categories.
342 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000343
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000344 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000345 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000346 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000347
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000348 /// GetNameForMethod - Return a name for the given method.
349 /// \param[out] NameOut - The return value.
350 void GetNameForMethod(const ObjCMethodDecl *OMD,
351 const ObjCContainerDecl *CD,
352 std::string &NameOut);
353
354 /// GetMethodVarName - Return a unique constant for the given
355 /// selector's name. The return value has type char *.
356 llvm::Constant *GetMethodVarName(Selector Sel);
357 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
358 llvm::Constant *GetMethodVarName(const std::string &Name);
359
360 /// GetMethodVarType - Return a unique constant for the given
361 /// selector's name. The return value has type char *.
362
363 // FIXME: This is a horrible name.
364 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
365 llvm::Constant *GetMethodVarType(const std::string &Name);
366
367 /// GetPropertyName - Return a unique constant for the given
368 /// name. The return value has type char *.
369 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
370
371 // FIXME: This can be dropped once string functions are unified.
372 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
373 const Decl *Container);
374
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000375 /// GetClassName - Return a unique constant for the given selector's
376 /// name. The return value has type char *.
377 llvm::Constant *GetClassName(IdentifierInfo *Ident);
378
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000379 const RecordDecl *GetFirstIvarInRecord(const ObjCInterfaceDecl *OID,
380 RecordDecl::field_iterator &FIV,
381 RecordDecl::field_iterator &PIV);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000382 /// EmitPropertyList - Emit the given property list. The return
383 /// value has type PropertyListPtrTy.
384 llvm::Constant *EmitPropertyList(const std::string &Name,
385 const Decl *Container,
386 const ObjCContainerDecl *OCD,
387 const ObjCCommonTypesHelper &ObjCTypes);
388
Fariborz Jahanianda320092009-01-29 19:24:30 +0000389 /// GetProtocolRef - Return a reference to the internal protocol
390 /// description, creating an empty one if it has not been
391 /// defined. The return value has type ProtocolPtrTy.
392 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
393
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000394public:
395 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
396 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000397
398 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000399
400 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
401 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000402
403 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
404
405 /// GetOrEmitProtocol - Get the protocol object for the given
406 /// declaration, emitting it if necessary. The return value has type
407 /// ProtocolPtrTy.
408 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
409
410 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
411 /// object for the given declaration, emitting it if needed. These
412 /// forward references will be filled in with empty bodies if no
413 /// definition is seen. The return value has type ProtocolPtrTy.
414 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000415};
416
417class CGObjCMac : public CGObjCCommonMac {
418private:
419 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000420 /// EmitImageInfo - Emit the image info marker used to encode some module
421 /// level information.
422 void EmitImageInfo();
423
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000424 /// EmitModuleInfo - Another marker encoding module level
425 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000426 void EmitModuleInfo();
427
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000428 /// EmitModuleSymols - Emit module symbols, the list of defined
429 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000430 llvm::Constant *EmitModuleSymbols();
431
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000432 /// FinishModule - Write out global data structures at the end of
433 /// processing a translation unit.
434 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000435
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000436 /// EmitClassExtension - Generate the class extension structure used
437 /// to store the weak ivar layout and properties. The return value
438 /// has type ClassExtensionPtrTy.
439 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
440
441 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
442 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000443 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000444 const ObjCInterfaceDecl *ID);
445
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000446 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000447 QualType ResultType,
448 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000449 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000450 QualType Arg0Ty,
451 bool IsSuper,
452 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000453
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000454 /// EmitIvarList - Emit the ivar list for the given
455 /// implementation. If ForClass is true the list of class ivars
456 /// (i.e. metaclass ivars) is emitted, otherwise the list of
457 /// interface ivars will be emitted. The return value has type
458 /// IvarListPtrTy.
459 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000460 bool ForClass);
461
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000462 /// EmitMetaClass - Emit a forward reference to the class structure
463 /// for the metaclass of the given interface. The return value has
464 /// type ClassPtrTy.
465 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
466
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000467 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000468 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000469 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
470 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000471 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000472 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000473
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000474 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000475
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000476 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000477
478 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000479 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000480 llvm::Constant *EmitMethodList(const std::string &Name,
481 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000482 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000483
484 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000485 /// method declarations.
486 /// - TypeName: The name for the type containing the methods.
487 /// - IsProtocol: True iff these methods are for a protocol.
488 /// - ClassMethds: True iff these are class methods.
489 /// - Required: When true, only "required" methods are
490 /// listed. Similarly, when false only "optional" methods are
491 /// listed. For classes this should always be true.
492 /// - begin, end: The method list to output.
493 ///
494 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000495 llvm::Constant *EmitMethodDescList(const std::string &Name,
496 const char *Section,
497 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000498
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000499 /// GetOrEmitProtocol - Get the protocol object for the given
500 /// declaration, emitting it if necessary. The return value has type
501 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000502 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000503
504 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
505 /// object for the given declaration, emitting it if needed. These
506 /// forward references will be filled in with empty bodies if no
507 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000508 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000509
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000510 /// EmitProtocolExtension - Generate the protocol extension
511 /// structure used to store optional instance and class methods, and
512 /// protocol properties. The return value has type
513 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000514 llvm::Constant *
515 EmitProtocolExtension(const ObjCProtocolDecl *PD,
516 const ConstantVector &OptInstanceMethods,
517 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000518
519 /// EmitProtocolList - Generate the list of referenced
520 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +0000521 llvm::Constant *EmitProtocolList(const std::string &Name,
522 ObjCProtocolDecl::protocol_iterator begin,
523 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000524
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000525 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
526 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000527 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000528
Fariborz Jahanianda320092009-01-29 19:24:30 +0000529 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000530 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000531
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000532 virtual llvm::Function *ModuleInitFunction();
533
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000534 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000535 QualType ResultType,
536 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000537 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000538 bool IsClassMessage,
539 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000540
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000541 virtual CodeGen::RValue
542 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000543 QualType ResultType,
544 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000545 const ObjCInterfaceDecl *Class,
546 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000547 bool IsClassMessage,
548 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000549
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000550 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000551 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000552
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000553 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000554
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000555 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000556
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000557 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000558
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000559 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000560 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000561
Daniel Dunbar49f66022008-09-24 03:38:44 +0000562 virtual llvm::Function *GetPropertyGetFunction();
563 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000564 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000565
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000566 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
567 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000568 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
569 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000570 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000571 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000572 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
573 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000574 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
575 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000576 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
577 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000578 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
579 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +0000580
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000581 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
582 QualType ObjectTy,
583 llvm::Value *BaseValue,
584 const ObjCIvarDecl *Ivar,
585 const FieldDecl *Field,
586 unsigned CVRQualifiers);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000587};
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000588
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000589class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000590private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000591 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000592 llvm::GlobalVariable* ObjCEmptyCacheVar;
593 llvm::GlobalVariable* ObjCEmptyVtableVar;
594
595 /// FinishNonFragileABIModule - Write out global data structures at the end of
596 /// processing a translation unit.
597 void FinishNonFragileABIModule();
598
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000599 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
600 unsigned InstanceStart,
601 unsigned InstanceSize,
602 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +0000603 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
604 llvm::Constant *IsAGV,
605 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +0000606 llvm::Constant *ClassRoGV,
607 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000608
609 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
610
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +0000611 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
612
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000613 /// EmitMethodList - Emit the method list for the given
614 /// implementation. The return value has type MethodListnfABITy.
615 llvm::Constant *EmitMethodList(const std::string &Name,
616 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +0000617 const ConstantVector &Methods);
618 /// EmitIvarList - Emit the ivar list for the given
619 /// implementation. If ForClass is true the list of class ivars
620 /// (i.e. metaclass ivars) is emitted, otherwise the list of
621 /// interface ivars will be emitted. The return value has type
622 /// IvarListnfABIPtrTy.
623 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000624
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000625 llvm::Constant *EmitIvarOffsetVar(const ObjCImplementationDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +0000626 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000627 unsigned long int offset);
628
Fariborz Jahanianda320092009-01-29 19:24:30 +0000629 /// GetOrEmitProtocol - Get the protocol object for the given
630 /// declaration, emitting it if necessary. The return value has type
631 /// ProtocolPtrTy.
632 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
633
634 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
635 /// object for the given declaration, emitting it if needed. These
636 /// forward references will be filled in with empty bodies if no
637 /// definition is seen. The return value has type ProtocolPtrTy.
638 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
639
640 /// EmitProtocolList - Generate the list of referenced
641 /// protocols. The return value has type ProtocolListPtrTy.
642 llvm::Constant *EmitProtocolList(const std::string &Name,
643 ObjCProtocolDecl::protocol_iterator begin,
644 ObjCProtocolDecl::protocol_iterator end);
645
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000646public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000647 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000648 // FIXME. All stubs for now!
649 virtual llvm::Function *ModuleInitFunction();
650
651 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
652 QualType ResultType,
653 Selector Sel,
654 llvm::Value *Receiver,
655 bool IsClassMessage,
656 const CallArgList &CallArgs)
657 {return RValue::get(0);}
658
659 virtual CodeGen::RValue
660 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
661 QualType ResultType,
662 Selector Sel,
663 const ObjCInterfaceDecl *Class,
664 llvm::Value *Receiver,
665 bool IsClassMessage,
666 const CallArgList &CallArgs){ return RValue::get(0);}
667
668 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
669 const ObjCInterfaceDecl *ID){ return 0; }
670
671 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
672 { return 0; }
673
Fariborz Jahanianeb062d92009-01-26 18:32:24 +0000674 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000675
676 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000677 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +0000678 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000679
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000680 virtual llvm::Function *GetPropertyGetFunction(){ return 0; }
681 virtual llvm::Function *GetPropertySetFunction()
682 { return 0; }
683 virtual llvm::Function *EnumerationMutationFunction()
684 { return 0; }
685
686 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
687 const Stmt &S)
688 { return; }
689 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
690 const ObjCAtThrowStmt &S)
691 { return; }
692 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
693 llvm::Value *AddrWeakObj)
694 { return 0; }
695 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
696 llvm::Value *src, llvm::Value *dst)
697 { return; }
698 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
699 llvm::Value *src, llvm::Value *dest)
700 { return; }
701 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
702 llvm::Value *src, llvm::Value *dest)
703 { return; }
704 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
705 llvm::Value *src, llvm::Value *dest)
706 { return; }
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000707 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
708 QualType ObjectTy,
709 llvm::Value *BaseValue,
710 const ObjCIvarDecl *Ivar,
711 const FieldDecl *Field,
712 unsigned CVRQualifiers);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000713};
714
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000715} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000716
717/* *** Helper Functions *** */
718
719/// getConstantGEP() - Help routine to construct simple GEPs.
720static llvm::Constant *getConstantGEP(llvm::Constant *C,
721 unsigned idx0,
722 unsigned idx1) {
723 llvm::Value *Idxs[] = {
724 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
725 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
726 };
727 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
728}
729
730/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000731
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000732CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
733 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000734{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000735 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000736 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000737}
738
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000739/// GetClass - Return a reference to the class for the given interface
740/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000741llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000742 const ObjCInterfaceDecl *ID) {
743 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000744}
745
746/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000747llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000748 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000749}
750
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000751/// Generate a constant CFString object.
752/*
753 struct __builtin_CFString {
754 const int *isa; // point to __CFConstantStringClassReference
755 int flags;
756 const char *str;
757 long length;
758 };
759*/
760
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000761llvm::Constant *CGObjCCommonMac::GenerateConstantString(
762 const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000763 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000764}
765
766/// Generates a message send where the super is the receiver. This is
767/// a message send to self with special delivery semantics indicating
768/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000769CodeGen::RValue
770CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000771 QualType ResultType,
772 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000773 const ObjCInterfaceDecl *Class,
774 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000775 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000776 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000777 // Create and init a super structure; this is a (receiver, class)
778 // pair we will pass to objc_msgSendSuper.
779 llvm::Value *ObjCSuper =
780 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
781 llvm::Value *ReceiverAsObject =
782 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
783 CGF.Builder.CreateStore(ReceiverAsObject,
784 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000785
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000786 // If this is a class message the metaclass is passed as the target.
787 llvm::Value *Target;
788 if (IsClassMessage) {
789 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
790 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
791 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
792 Target = Super;
793 } else {
794 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
795 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000796 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
797 // and ObjCTypes types.
798 const llvm::Type *ClassTy =
799 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000800 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000801 CGF.Builder.CreateStore(Target,
802 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
803
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000804 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000805 ObjCSuper, ObjCTypes.SuperPtrCTy,
806 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000807}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000808
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000809/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000810CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000811 QualType ResultType,
812 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000813 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000814 bool IsClassMessage,
815 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000816 llvm::Value *Arg0 =
817 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000818 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000819 Arg0, CGF.getContext().getObjCIdType(),
820 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000821}
822
823CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000824 QualType ResultType,
825 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000826 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000827 QualType Arg0Ty,
828 bool IsSuper,
829 const CallArgList &CallArgs) {
830 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000831 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
832 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
833 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000834 CGF.getContext().getObjCSelType()));
835 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000836
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000837 CodeGenTypes &Types = CGM.getTypes();
838 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
839 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbar5669e572008-10-17 03:24:53 +0000840
841 llvm::Constant *Fn;
Daniel Dunbar88b53962009-02-02 22:03:45 +0000842 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000843 Fn = ObjCTypes.getSendStretFn(IsSuper);
844 } else if (ResultType->isFloatingType()) {
845 // FIXME: Sadly, this is wrong. This actually depends on the
846 // architecture. This happens to be right for x86-32 though.
847 Fn = ObjCTypes.getSendFpretFn(IsSuper);
848 } else {
849 Fn = ObjCTypes.getSendFn(IsSuper);
850 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000851 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +0000852 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000853}
854
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000855llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000856 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +0000857 // FIXME: I don't understand why gcc generates this, or where it is
858 // resolved. Investigate. Its also wasteful to look this up over and
859 // over.
860 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
861
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000862 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
863 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000864}
865
Fariborz Jahanianda320092009-01-29 19:24:30 +0000866void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000867 // FIXME: We shouldn't need this, the protocol decl should contain
868 // enough information to tell us whether this was a declaration or a
869 // definition.
870 DefinedProtocols.insert(PD->getIdentifier());
871
872 // If we have generated a forward reference to this protocol, emit
873 // it now. Otherwise do nothing, the protocol objects are lazily
874 // emitted.
875 if (Protocols.count(PD->getIdentifier()))
876 GetOrEmitProtocol(PD);
877}
878
Fariborz Jahanianda320092009-01-29 19:24:30 +0000879llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000880 if (DefinedProtocols.count(PD->getIdentifier()))
881 return GetOrEmitProtocol(PD);
882 return GetOrEmitProtocolRef(PD);
883}
884
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000885/*
886 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
887 struct _objc_protocol {
888 struct _objc_protocol_extension *isa;
889 char *protocol_name;
890 struct _objc_protocol_list *protocol_list;
891 struct _objc__method_prototype_list *instance_methods;
892 struct _objc__method_prototype_list *class_methods
893 };
894
895 See EmitProtocolExtension().
896*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000897llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
898 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
899
900 // Early exit if a defining object has already been generated.
901 if (Entry && Entry->hasInitializer())
902 return Entry;
903
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000904 // FIXME: I don't understand why gcc generates this, or where it is
905 // resolved. Investigate. Its also wasteful to look this up over and
906 // over.
907 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
908
Chris Lattner8ec03f52008-11-24 03:54:41 +0000909 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000910
911 // Construct method lists.
912 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
913 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
914 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
915 e = PD->instmeth_end(); i != e; ++i) {
916 ObjCMethodDecl *MD = *i;
917 llvm::Constant *C = GetMethodDescriptionConstant(MD);
918 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
919 OptInstanceMethods.push_back(C);
920 } else {
921 InstanceMethods.push_back(C);
922 }
923 }
924
925 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
926 e = PD->classmeth_end(); i != e; ++i) {
927 ObjCMethodDecl *MD = *i;
928 llvm::Constant *C = GetMethodDescriptionConstant(MD);
929 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
930 OptClassMethods.push_back(C);
931 } else {
932 ClassMethods.push_back(C);
933 }
934 }
935
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000936 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000937 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000938 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000939 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000940 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +0000941 PD->protocol_begin(),
942 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000943 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000944 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
945 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000946 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
947 InstanceMethods);
948 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000949 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
950 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000951 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
952 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000953 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
954 Values);
955
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000956 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000957 // Already created, fix the linkage and update the initializer.
958 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000959 Entry->setInitializer(Init);
960 } else {
961 Entry =
962 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
963 llvm::GlobalValue::InternalLinkage,
964 Init,
965 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
966 &CGM.getModule());
967 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
968 UsedGlobals.push_back(Entry);
969 // FIXME: Is this necessary? Why only for protocol?
970 Entry->setAlignment(4);
971 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000972
973 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000974}
975
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000976llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000977 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
978
979 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000980 // We use the initializer as a marker of whether this is a forward
981 // reference or not. At module finalization we add the empty
982 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000983 Entry =
984 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000985 llvm::GlobalValue::ExternalLinkage,
986 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000987 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000988 &CGM.getModule());
989 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
990 UsedGlobals.push_back(Entry);
991 // FIXME: Is this necessary? Why only for protocol?
992 Entry->setAlignment(4);
993 }
994
995 return Entry;
996}
997
998/*
999 struct _objc_protocol_extension {
1000 uint32_t size;
1001 struct objc_method_description_list *optional_instance_methods;
1002 struct objc_method_description_list *optional_class_methods;
1003 struct objc_property_list *instance_properties;
1004 };
1005*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001006llvm::Constant *
1007CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1008 const ConstantVector &OptInstanceMethods,
1009 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001010 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001011 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001012 std::vector<llvm::Constant*> Values(4);
1013 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001014 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001015 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1016 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001017 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1018 OptInstanceMethods);
1019 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001020 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1021 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001022 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1023 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001024 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1025 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001026 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001027
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001028 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001029 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1030 Values[3]->isNullValue())
1031 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1032
1033 llvm::Constant *Init =
1034 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
1035 llvm::GlobalVariable *GV =
1036 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
1037 llvm::GlobalValue::InternalLinkage,
1038 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001039 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001040 &CGM.getModule());
1041 // No special section, but goes in llvm.used
1042 UsedGlobals.push_back(GV);
1043
1044 return GV;
1045}
1046
1047/*
1048 struct objc_protocol_list {
1049 struct objc_protocol_list *next;
1050 long count;
1051 Protocol *list[];
1052 };
1053*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001054llvm::Constant *
1055CGObjCMac::EmitProtocolList(const std::string &Name,
1056 ObjCProtocolDecl::protocol_iterator begin,
1057 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001058 std::vector<llvm::Constant*> ProtocolRefs;
1059
Daniel Dunbardbc933702008-08-21 21:57:41 +00001060 for (; begin != end; ++begin)
1061 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001062
1063 // Just return null for empty protocol lists
1064 if (ProtocolRefs.empty())
1065 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1066
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001067 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001068 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1069
1070 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001071 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001072 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1073 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1074 Values[2] =
1075 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1076 ProtocolRefs.size()),
1077 ProtocolRefs);
1078
1079 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1080 llvm::GlobalVariable *GV =
1081 new llvm::GlobalVariable(Init->getType(), false,
1082 llvm::GlobalValue::InternalLinkage,
1083 Init,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001084 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001085 &CGM.getModule());
1086 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
1087 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1088}
1089
1090/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001091 struct _objc_property {
1092 const char * const name;
1093 const char * const attributes;
1094 };
1095
1096 struct _objc_property_list {
1097 uint32_t entsize; // sizeof (struct _objc_property)
1098 uint32_t prop_count;
1099 struct _objc_property[prop_count];
1100 };
1101*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001102llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1103 const Decl *Container,
1104 const ObjCContainerDecl *OCD,
1105 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001106 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +00001107 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1108 E = OCD->prop_end(); I != E; ++I) {
1109 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001110 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001111 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001112 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1113 Prop));
1114 }
1115
1116 // Return null for empty list.
1117 if (Properties.empty())
1118 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1119
1120 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001121 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001122 std::vector<llvm::Constant*> Values(3);
1123 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1124 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1125 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1126 Properties.size());
1127 Values[2] = llvm::ConstantArray::get(AT, Properties);
1128 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1129
1130 llvm::GlobalVariable *GV =
1131 new llvm::GlobalVariable(Init->getType(), false,
1132 llvm::GlobalValue::InternalLinkage,
1133 Init,
1134 Name,
1135 &CGM.getModule());
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001136 if (ObjCABI == 2)
1137 GV->setSection("__DATA, __objc_const");
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001138 // No special section on property lists?
1139 UsedGlobals.push_back(GV);
1140 return llvm::ConstantExpr::getBitCast(GV,
1141 ObjCTypes.PropertyListPtrTy);
1142
1143}
1144
1145/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001146 struct objc_method_description_list {
1147 int count;
1148 struct objc_method_description list[];
1149 };
1150*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001151llvm::Constant *
1152CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1153 std::vector<llvm::Constant*> Desc(2);
1154 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1155 ObjCTypes.SelectorPtrTy);
1156 Desc[1] = GetMethodVarType(MD);
1157 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1158 Desc);
1159}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001160
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001161llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1162 const char *Section,
1163 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001164 // Return null for empty list.
1165 if (Methods.empty())
1166 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1167
1168 std::vector<llvm::Constant*> Values(2);
1169 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1170 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1171 Methods.size());
1172 Values[1] = llvm::ConstantArray::get(AT, Methods);
1173 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1174
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001175 llvm::GlobalVariable *GV =
1176 new llvm::GlobalVariable(Init->getType(), false,
1177 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001178 Init, Name, &CGM.getModule());
1179 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001180 UsedGlobals.push_back(GV);
1181 return llvm::ConstantExpr::getBitCast(GV,
1182 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001183}
1184
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001185/*
1186 struct _objc_category {
1187 char *category_name;
1188 char *class_name;
1189 struct _objc_method_list *instance_methods;
1190 struct _objc_method_list *class_methods;
1191 struct _objc_protocol_list *protocols;
1192 uint32_t size; // <rdar://4585769>
1193 struct _objc_property_list *instance_properties;
1194 };
1195 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001196void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001197 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001198
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001199 // FIXME: This is poor design, the OCD should have a pointer to the
1200 // category decl. Additionally, note that Category can be null for
1201 // the @implementation w/o an @interface case. Sema should just
1202 // create one for us as it does for @implementation so everyone else
1203 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001204 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001205 const ObjCCategoryDecl *Category =
1206 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001207 std::string ExtName(Interface->getNameAsString() + "_" +
1208 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001209
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001210 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1211 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1212 e = OCD->instmeth_end(); i != e; ++i) {
1213 // Instance methods should always be defined.
1214 InstanceMethods.push_back(GetMethodConstant(*i));
1215 }
1216 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1217 e = OCD->classmeth_end(); i != e; ++i) {
1218 // Class methods should always be defined.
1219 ClassMethods.push_back(GetMethodConstant(*i));
1220 }
1221
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001222 std::vector<llvm::Constant*> Values(7);
1223 Values[0] = GetClassName(OCD->getIdentifier());
1224 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001225 Values[2] =
1226 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1227 ExtName,
1228 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001229 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001230 Values[3] =
1231 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1232 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001233 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001234 if (Category) {
1235 Values[4] =
1236 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1237 Category->protocol_begin(),
1238 Category->protocol_end());
1239 } else {
1240 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1241 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001242 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001243
1244 // If there is no category @interface then there can be no properties.
1245 if (Category) {
1246 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001247 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001248 } else {
1249 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1250 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001251
1252 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1253 Values);
1254
1255 llvm::GlobalVariable *GV =
1256 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1257 llvm::GlobalValue::InternalLinkage,
1258 Init,
1259 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1260 &CGM.getModule());
1261 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1262 UsedGlobals.push_back(GV);
1263 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001264}
1265
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001266// FIXME: Get from somewhere?
1267enum ClassFlags {
1268 eClassFlags_Factory = 0x00001,
1269 eClassFlags_Meta = 0x00002,
1270 // <rdr://5142207>
1271 eClassFlags_HasCXXStructors = 0x02000,
1272 eClassFlags_Hidden = 0x20000,
1273 eClassFlags_ABI2_Hidden = 0x00010,
1274 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1275};
1276
1277// <rdr://5142207&4705298&4843145>
1278static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1279 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1280 // FIXME: Support -fvisibility
1281 switch (attr->getVisibility()) {
1282 default:
1283 assert(0 && "Unknown visibility");
1284 return false;
1285 case VisibilityAttr::DefaultVisibility:
1286 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1287 return false;
1288 case VisibilityAttr::HiddenVisibility:
1289 return true;
1290 }
1291 } else {
1292 return false; // FIXME: Support -fvisibility
1293 }
1294}
1295
1296/*
1297 struct _objc_class {
1298 Class isa;
1299 Class super_class;
1300 const char *name;
1301 long version;
1302 long info;
1303 long instance_size;
1304 struct _objc_ivar_list *ivars;
1305 struct _objc_method_list *methods;
1306 struct _objc_cache *cache;
1307 struct _objc_protocol_list *protocols;
1308 // Objective-C 1.0 extensions (<rdr://4585769>)
1309 const char *ivar_layout;
1310 struct _objc_class_ext *ext;
1311 };
1312
1313 See EmitClassExtension();
1314 */
1315void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001316 DefinedSymbols.insert(ID->getIdentifier());
1317
Chris Lattner8ec03f52008-11-24 03:54:41 +00001318 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001319 // FIXME: Gross
1320 ObjCInterfaceDecl *Interface =
1321 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001322 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001323 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001324 Interface->protocol_begin(),
1325 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001326 const llvm::Type *InterfaceTy =
1327 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
1328 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001329 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001330
1331 // FIXME: Set CXX-structors flag.
1332 if (IsClassHidden(ID->getClassInterface()))
1333 Flags |= eClassFlags_Hidden;
1334
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001335 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1336 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1337 e = ID->instmeth_end(); i != e; ++i) {
1338 // Instance methods should always be defined.
1339 InstanceMethods.push_back(GetMethodConstant(*i));
1340 }
1341 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1342 e = ID->classmeth_end(); i != e; ++i) {
1343 // Class methods should always be defined.
1344 ClassMethods.push_back(GetMethodConstant(*i));
1345 }
1346
1347 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1348 e = ID->propimpl_end(); i != e; ++i) {
1349 ObjCPropertyImplDecl *PID = *i;
1350
1351 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1352 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1353
1354 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1355 if (llvm::Constant *C = GetMethodConstant(MD))
1356 InstanceMethods.push_back(C);
1357 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1358 if (llvm::Constant *C = GetMethodConstant(MD))
1359 InstanceMethods.push_back(C);
1360 }
1361 }
1362
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001363 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001364 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001365 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001366 // Record a reference to the super class.
1367 LazySymbols.insert(Super->getIdentifier());
1368
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001369 Values[ 1] =
1370 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1371 ObjCTypes.ClassPtrTy);
1372 } else {
1373 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1374 }
1375 Values[ 2] = GetClassName(ID->getIdentifier());
1376 // Version is always 0.
1377 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1378 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1379 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001380 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001381 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001382 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001383 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001384 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001385 // cache is always NULL.
1386 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1387 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001388 // FIXME: Set ivar_layout
1389 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001390 Values[11] = EmitClassExtension(ID);
1391 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1392 Values);
1393
1394 llvm::GlobalVariable *GV =
1395 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1396 llvm::GlobalValue::InternalLinkage,
1397 Init,
1398 std::string("\01L_OBJC_CLASS_")+ClassName,
1399 &CGM.getModule());
1400 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1401 UsedGlobals.push_back(GV);
1402 // FIXME: Why?
1403 GV->setAlignment(32);
1404 DefinedClasses.push_back(GV);
1405}
1406
1407llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1408 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001409 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001410 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001411 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001412 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001413
1414 if (IsClassHidden(ID->getClassInterface()))
1415 Flags |= eClassFlags_Hidden;
1416
1417 std::vector<llvm::Constant*> Values(12);
1418 // The isa for the metaclass is the root of the hierarchy.
1419 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1420 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1421 Root = Super;
1422 Values[ 0] =
1423 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1424 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001425 // The super class for the metaclass is emitted as the name of the
1426 // super class. The runtime fixes this up to point to the
1427 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001428 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1429 Values[ 1] =
1430 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1431 ObjCTypes.ClassPtrTy);
1432 } else {
1433 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1434 }
1435 Values[ 2] = GetClassName(ID->getIdentifier());
1436 // Version is always 0.
1437 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1438 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1439 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001440 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001441 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001442 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001443 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001444 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001445 // cache is always NULL.
1446 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1447 Values[ 9] = Protocols;
1448 // ivar_layout for metaclass is always NULL.
1449 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1450 // The class extension is always unused for metaclasses.
1451 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1452 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1453 Values);
1454
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001455 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001456 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001457
1458 // Check for a forward reference.
1459 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1460 if (GV) {
1461 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1462 "Forward metaclass reference has incorrect type.");
1463 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1464 GV->setInitializer(Init);
1465 } else {
1466 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1467 llvm::GlobalValue::InternalLinkage,
1468 Init, Name,
1469 &CGM.getModule());
1470 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001471 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1472 UsedGlobals.push_back(GV);
1473 // FIXME: Why?
1474 GV->setAlignment(32);
1475
1476 return GV;
1477}
1478
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001479llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001480 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001481
1482 // FIXME: Should we look these up somewhere other than the
1483 // module. Its a bit silly since we only generate these while
1484 // processing an implementation, so exactly one pointer would work
1485 // if know when we entered/exitted an implementation block.
1486
1487 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001488 // Previously, metaclass with internal linkage may have been defined.
1489 // pass 'true' as 2nd argument so it is returned.
1490 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001491 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1492 "Forward metaclass reference has incorrect type.");
1493 return GV;
1494 } else {
1495 // Generate as an external reference to keep a consistent
1496 // module. This will be patched up when we emit the metaclass.
1497 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1498 llvm::GlobalValue::ExternalLinkage,
1499 0,
1500 Name,
1501 &CGM.getModule());
1502 }
1503}
1504
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001505/*
1506 struct objc_class_ext {
1507 uint32_t size;
1508 const char *weak_ivar_layout;
1509 struct _objc_property_list *properties;
1510 };
1511*/
1512llvm::Constant *
1513CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1514 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001515 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001516
1517 std::vector<llvm::Constant*> Values(3);
1518 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001519 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001520 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001521 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001522 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001523
1524 // Return null if no extension bits are used.
1525 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1526 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1527
1528 llvm::Constant *Init =
1529 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1530 llvm::GlobalVariable *GV =
1531 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1532 llvm::GlobalValue::InternalLinkage,
1533 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001534 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001535 &CGM.getModule());
1536 // No special section, but goes in llvm.used
1537 UsedGlobals.push_back(GV);
1538
1539 return GV;
1540}
1541
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001542/// countInheritedIvars - count number of ivars in class and its super class(s)
1543///
1544static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1545 int count = 0;
1546 if (!OI)
1547 return 0;
1548 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1549 if (SuperClass)
1550 count += countInheritedIvars(SuperClass);
1551 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1552 E = OI->ivar_end(); I != E; ++I)
1553 ++count;
1554 return count;
1555}
1556
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001557/*
1558 struct objc_ivar {
1559 char *ivar_name;
1560 char *ivar_type;
1561 int ivar_offset;
1562 };
1563
1564 struct objc_ivar_list {
1565 int ivar_count;
1566 struct objc_ivar list[count];
1567 };
1568 */
1569llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001570 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001571 std::vector<llvm::Constant*> Ivars, Ivar(3);
1572
1573 // When emitting the root class GCC emits ivar entries for the
1574 // actual class structure. It is not clear if we need to follow this
1575 // behavior; for now lets try and get away with not doing it. If so,
1576 // the cleanest solution would be to make up an ObjCInterfaceDecl
1577 // for the class.
1578 if (ForClass)
1579 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001580
1581 ObjCInterfaceDecl *OID =
1582 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
1583 const llvm::Type *InterfaceTy =
1584 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001585 const llvm::StructLayout *Layout =
1586 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001587
1588 RecordDecl::field_iterator ifield, pfield;
1589 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001590 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1591 FieldDecl *Field = *ifield;
1592 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1593 getLLVMFieldNo(Field));
1594 if (Field->getIdentifier())
1595 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1596 else
1597 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001598 std::string TypeStr;
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001599 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001600 Ivar[1] = GetMethodVarType(TypeStr);
1601 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001602 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001603 }
1604
1605 // Return null for empty list.
1606 if (Ivars.empty())
1607 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1608
1609 std::vector<llvm::Constant*> Values(2);
1610 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1611 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1612 Ivars.size());
1613 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1614 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1615
1616 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1617 "\01L_OBJC_INSTANCE_VARIABLES_");
1618 llvm::GlobalVariable *GV =
1619 new llvm::GlobalVariable(Init->getType(), false,
1620 llvm::GlobalValue::InternalLinkage,
1621 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001622 Prefix + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001623 &CGM.getModule());
1624 if (ForClass) {
1625 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1626 // FIXME: Why is this only here?
1627 GV->setAlignment(32);
1628 } else {
1629 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1630 }
1631 UsedGlobals.push_back(GV);
1632 return llvm::ConstantExpr::getBitCast(GV,
1633 ObjCTypes.IvarListPtrTy);
1634}
1635
1636/*
1637 struct objc_method {
1638 SEL method_name;
1639 char *method_types;
1640 void *method;
1641 };
1642
1643 struct objc_method_list {
1644 struct objc_method_list *obsolete;
1645 int count;
1646 struct objc_method methods_list[count];
1647 };
1648*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001649
1650/// GetMethodConstant - Return a struct objc_method constant for the
1651/// given method if it has been defined. The result is null if the
1652/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001653llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001654 // FIXME: Use DenseMap::lookup
1655 llvm::Function *Fn = MethodDefinitions[MD];
1656 if (!Fn)
1657 return 0;
1658
1659 std::vector<llvm::Constant*> Method(3);
1660 Method[0] =
1661 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1662 ObjCTypes.SelectorPtrTy);
1663 Method[1] = GetMethodVarType(MD);
1664 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1665 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1666}
1667
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001668llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1669 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001670 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001671 // Return null for empty list.
1672 if (Methods.empty())
1673 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1674
1675 std::vector<llvm::Constant*> Values(3);
1676 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1677 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1678 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1679 Methods.size());
1680 Values[2] = llvm::ConstantArray::get(AT, Methods);
1681 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1682
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001683 llvm::GlobalVariable *GV =
1684 new llvm::GlobalVariable(Init->getType(), false,
1685 llvm::GlobalValue::InternalLinkage,
1686 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001687 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001688 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001689 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001690 UsedGlobals.push_back(GV);
1691 return llvm::ConstantExpr::getBitCast(GV,
1692 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001693}
1694
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001695llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001696 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001697 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001698 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001699
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001700 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001701 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001702 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001703 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001704 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001705 llvm::GlobalValue::InternalLinkage,
1706 Name,
1707 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001708 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001709
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001710 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001711}
1712
1713llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001714 // Abuse this interface function as a place to finalize.
1715 FinishModule();
1716
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001717 return NULL;
1718}
1719
Daniel Dunbar49f66022008-09-24 03:38:44 +00001720llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1721 return ObjCTypes.GetPropertyFn;
1722}
1723
1724llvm::Function *CGObjCMac::GetPropertySetFunction() {
1725 return ObjCTypes.SetPropertyFn;
1726}
1727
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001728llvm::Function *CGObjCMac::EnumerationMutationFunction()
1729{
1730 return ObjCTypes.EnumerationMutationFn;
1731}
1732
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001733/*
1734
1735Objective-C setjmp-longjmp (sjlj) Exception Handling
1736--
1737
1738The basic framework for a @try-catch-finally is as follows:
1739{
1740 objc_exception_data d;
1741 id _rethrow = null;
1742
1743 objc_exception_try_enter(&d);
1744 if (!setjmp(d.jmp_buf)) {
1745 ... try body ...
1746 } else {
1747 // exception path
1748 id _caught = objc_exception_extract(&d);
1749
1750 // enter new try scope for handlers
1751 if (!setjmp(d.jmp_buf)) {
1752 ... match exception and execute catch blocks ...
1753
1754 // fell off end, rethrow.
1755 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001756 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001757 } else {
1758 // exception in catch block
1759 _rethrow = objc_exception_extract(&d);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001760 ... jump-through-finally_no_exit to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001761 }
1762 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001763 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001764
1765finally:
1766 // match either the initial try_enter or the catch try_enter,
1767 // depending on the path followed.
1768 objc_exception_try_exit(&d);
1769finally_no_exit:
1770 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001771 ... dispatch to finally destination ...
1772
1773finally_rethrow:
1774 objc_exception_throw(_rethrow);
1775
1776finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001777}
1778
1779This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001780uses _rethrow to determine if objc_exception_try_exit should be called
1781and if the object should be rethrown. This breaks in the face of
1782throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001783
1784We specialize this framework for a few particular circumstances:
1785
1786 - If there are no catch blocks, then we avoid emitting the second
1787 exception handling context.
1788
1789 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1790 e)) we avoid emitting the code to rethrow an uncaught exception.
1791
1792 - FIXME: If there is no @finally block we can do a few more
1793 simplifications.
1794
1795Rethrows and Jumps-Through-Finally
1796--
1797
1798Support for implicit rethrows and jumping through the finally block is
1799handled by storing the current exception-handling context in
1800ObjCEHStack.
1801
Daniel Dunbar898d5082008-09-30 01:06:03 +00001802In order to implement proper @finally semantics, we support one basic
1803mechanism for jumping through the finally block to an arbitrary
1804destination. Constructs which generate exits from a @try or @catch
1805block use this mechanism to implement the proper semantics by chaining
1806jumps, as necessary.
1807
1808This mechanism works like the one used for indirect goto: we
1809arbitrarily assign an ID to each destination and store the ID for the
1810destination in a variable prior to entering the finally block. At the
1811end of the finally block we simply create a switch to the proper
1812destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001813
1814Code gen for @synchronized(expr) stmt;
1815Effectively generating code for:
1816objc_sync_enter(expr);
1817@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001818*/
1819
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001820void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1821 const Stmt &S) {
1822 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001823 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001824 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
1825 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1826 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1827 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001828 llvm::Value *DestCode =
1829 CGF.CreateTempAlloca(llvm::Type::Int32Ty, "finally.dst");
1830
1831 // Generate jump code. Done here so we can directly add things to
1832 // the switch instruction.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001833 llvm::BasicBlock *FinallyJump = CGF.createBasicBlock("finally.jump");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001834 llvm::SwitchInst *FinallySwitch =
1835 llvm::SwitchInst::Create(new llvm::LoadInst(DestCode, "", FinallyJump),
1836 FinallyEnd, 10, FinallyJump);
1837
1838 // Push an EH context entry, used for handling rethrows and jumps
1839 // through finally.
1840 CodeGenFunction::ObjCEHEntry EHEntry(FinallyBlock, FinallyNoExit,
1841 FinallySwitch, DestCode);
1842 CGF.ObjCEHStack.push_back(&EHEntry);
1843
1844 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00001845 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1846 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001847 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1848 "_rethrow");
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001849 if (!isTry) {
1850 // For @synchronized, call objc_sync_enter(sync.expr)
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001851 llvm::Value *Arg = CGF.EmitScalarExpr(
1852 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1853 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1854 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001855 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001856
1857 // Enter a new try block and call setjmp.
1858 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1859 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1860 "jmpbufarray");
1861 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1862 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1863 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001864
Daniel Dunbar55e87422008-11-11 02:29:29 +00001865 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1866 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001867 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001868 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001869
1870 // Emit the @try block.
1871 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001872 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1873 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001874 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001875
1876 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001877 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001878
1879 // Retrieve the exception object. We may emit multiple blocks but
1880 // nothing can cross this so the value is already in SSA form.
1881 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1882 ExceptionData,
1883 "caught");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001884 EHEntry.Exception = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001885 if (!isTry)
1886 {
1887 CGF.Builder.CreateStore(Caught, RethrowPtr);
1888 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
1889 }
1890 else if (const ObjCAtCatchStmt* CatchStmt =
1891 cast<ObjCAtTryStmt>(S).getCatchStmts())
1892 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00001893 // Enter a new exception try block (in case a @catch block throws
1894 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00001895 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001896
Anders Carlsson80f25672008-09-09 17:59:25 +00001897 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1898 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001899 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00001900
Daniel Dunbar55e87422008-11-11 02:29:29 +00001901 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1902 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001903 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001904
1905 CGF.EmitBlock(CatchBlock);
1906
Daniel Dunbar55e40722008-09-27 07:03:52 +00001907 // Handle catch list. As a special case we check if everything is
1908 // matched and avoid generating code for falling off the end if
1909 // so.
1910 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00001911 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00001912 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00001913
Anders Carlssondde0a942008-09-11 09:15:33 +00001914 const DeclStmt *CatchParam =
1915 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001916 const VarDecl *VD = 0;
1917 const PointerType *PT = 0;
1918
Anders Carlsson80f25672008-09-09 17:59:25 +00001919 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00001920 if (!CatchParam) {
1921 AllMatched = true;
1922 } else {
Ted Kremenekde3b8fb2008-10-06 20:58:56 +00001923 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001924 PT = VD->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001925
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001926 // catch(id e) always matches.
1927 // FIXME: For the time being we also match id<X>; this should
1928 // be rejected by Sema instead.
1929 if ((PT && CGF.getContext().isObjCIdType(PT->getPointeeType())) ||
1930 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00001931 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00001932 }
1933
Daniel Dunbar55e40722008-09-27 07:03:52 +00001934 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00001935 if (CatchParam) {
1936 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001937 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar129271a2008-09-27 07:36:24 +00001938 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001939 }
Anders Carlsson1452f552008-09-11 08:21:54 +00001940
Anders Carlssondde0a942008-09-11 09:15:33 +00001941 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001942 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001943 break;
1944 }
1945
Daniel Dunbar129271a2008-09-27 07:36:24 +00001946 assert(PT && "Unexpected non-pointer type in @catch");
1947 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00001948 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001949 assert(ObjCType && "Catch parameter must have Objective-C type!");
1950
1951 // Check if the @catch block matches the exception object.
1952 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
1953
Anders Carlsson80f25672008-09-09 17:59:25 +00001954 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
1955 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00001956
Daniel Dunbar55e87422008-11-11 02:29:29 +00001957 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00001958
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001959 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001960 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001961
1962 // Emit the @catch block.
1963 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001964 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001965 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001966
1967 llvm::Value *Tmp =
1968 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
1969 "tmp");
1970 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001971
1972 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001973 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001974
1975 CGF.EmitBlock(NextCatchBlock);
1976 }
1977
Daniel Dunbar55e40722008-09-27 07:03:52 +00001978 if (!AllMatched) {
1979 // None of the handlers caught the exception, so store it to be
1980 // rethrown at the end of the @finally block.
1981 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001982 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001983 }
1984
1985 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001986 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001987 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1988 ExceptionData),
1989 RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001990 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001991 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00001992 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001993 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Anders Carlsson80f25672008-09-09 17:59:25 +00001994 }
1995
Daniel Dunbar898d5082008-09-30 01:06:03 +00001996 // Pop the exception-handling stack entry. It is important to do
1997 // this now, because the code in the @finally block is not in this
1998 // context.
1999 CGF.ObjCEHStack.pop_back();
2000
Anders Carlsson80f25672008-09-09 17:59:25 +00002001 // Emit the @finally block.
2002 CGF.EmitBlock(FinallyBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002003 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002004
2005 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002006 if (isTry) {
2007 if (const ObjCAtFinallyStmt* FinallyStmt =
2008 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2009 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2010 }
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002011 else {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002012 // For @synchronized objc_sync_exit(expr); As finally's sole statement.
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002013 // For @synchronized, call objc_sync_enter(sync.expr)
2014 llvm::Value *Arg = CGF.EmitScalarExpr(
2015 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2016 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
2017 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg);
2018 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002019
Daniel Dunbar898d5082008-09-30 01:06:03 +00002020 CGF.EmitBlock(FinallyJump);
2021
2022 CGF.EmitBlock(FinallyRethrow);
2023 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
2024 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002025 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002026
2027 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002028}
2029
2030void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002031 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002032 llvm::Value *ExceptionAsObject;
2033
2034 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2035 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2036 ExceptionAsObject =
2037 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2038 } else {
Daniel Dunbar898d5082008-09-30 01:06:03 +00002039 assert((!CGF.ObjCEHStack.empty() && CGF.ObjCEHStack.back()->Exception) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002040 "Unexpected rethrow outside @catch block.");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002041 ExceptionAsObject = CGF.ObjCEHStack.back()->Exception;
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002042 }
2043
2044 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002045 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002046
2047 // Clear the insertion point to indicate we are in unreachable code.
2048 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002049}
2050
Daniel Dunbar898d5082008-09-30 01:06:03 +00002051void CodeGenFunction::EmitJumpThroughFinally(ObjCEHEntry *E,
2052 llvm::BasicBlock *Dst,
2053 bool ExecuteTryExit) {
Daniel Dunbara448fb22008-11-11 23:11:34 +00002054 if (!HaveInsertPoint())
Daniel Dunbar898d5082008-09-30 01:06:03 +00002055 return;
2056
2057 // Find the destination code for this block. We always use 0 for the
2058 // fallthrough block (default destination).
2059 llvm::SwitchInst *SI = E->FinallySwitch;
2060 llvm::ConstantInt *ID;
2061 if (Dst == SI->getDefaultDest()) {
2062 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
2063 } else {
2064 ID = SI->findCaseDest(Dst);
2065 if (!ID) {
2066 // No code found, get a new unique one by just using the number
2067 // of switch successors.
2068 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, SI->getNumSuccessors());
2069 SI->addCase(ID, Dst);
2070 }
2071 }
2072
2073 // Set the destination code and branch.
2074 Builder.CreateStore(ID, E->DestCode);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002075 EmitBranch(ExecuteTryExit ? E->FinallyBlock : E->FinallyNoExit);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002076}
2077
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002078/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002079/// object: objc_read_weak (id *src)
2080///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002081llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002082 llvm::Value *AddrWeakObj)
2083{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002084 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002085 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002086 AddrWeakObj, "weakread");
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002087 return read_weak;
2088}
2089
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002090/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2091/// objc_assign_weak (id src, id *dst)
2092///
2093void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2094 llvm::Value *src, llvm::Value *dst)
2095{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002096 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2097 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002098 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2099 src, dst, "weakassign");
2100 return;
2101}
2102
Fariborz Jahanian58626502008-11-19 00:59:10 +00002103/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2104/// objc_assign_global (id src, id *dst)
2105///
2106void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2107 llvm::Value *src, llvm::Value *dst)
2108{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002109 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2110 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002111 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2112 src, dst, "globalassign");
2113 return;
2114}
2115
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002116/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2117/// objc_assign_ivar (id src, id *dst)
2118///
2119void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2120 llvm::Value *src, llvm::Value *dst)
2121{
2122 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2123 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2124 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2125 src, dst, "assignivar");
2126 return;
2127}
2128
Fariborz Jahanian58626502008-11-19 00:59:10 +00002129/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2130/// objc_assign_strongCast (id src, id *dst)
2131///
2132void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2133 llvm::Value *src, llvm::Value *dst)
2134{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002135 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2136 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002137 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2138 src, dst, "weakassign");
2139 return;
2140}
2141
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002142/// EmitObjCValueForIvar - Code Gen for ivar reference.
2143///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002144LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2145 QualType ObjectTy,
2146 llvm::Value *BaseValue,
2147 const ObjCIvarDecl *Ivar,
2148 const FieldDecl *Field,
2149 unsigned CVRQualifiers) {
2150 if (Ivar->isBitField())
2151 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
2152 CVRQualifiers);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002153 // TODO: Add a special case for isa (index 0)
2154 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
2155 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002156 LValue LV = LValue::MakeAddr(V,
2157 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
2158 LValue::SetObjCIvar(LV, true);
2159 return LV;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002160}
2161
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002162/* *** Private Interface *** */
2163
2164/// EmitImageInfo - Emit the image info marker used to encode some module
2165/// level information.
2166///
2167/// See: <rdr://4810609&4810587&4810587>
2168/// struct IMAGE_INFO {
2169/// unsigned version;
2170/// unsigned flags;
2171/// };
2172enum ImageInfoFlags {
2173 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2174 eImageInfo_GarbageCollected = (1 << 1),
2175 eImageInfo_GCOnly = (1 << 2)
2176};
2177
2178void CGObjCMac::EmitImageInfo() {
2179 unsigned version = 0; // Version is unused?
2180 unsigned flags = 0;
2181
2182 // FIXME: Fix and continue?
2183 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2184 flags |= eImageInfo_GarbageCollected;
2185 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2186 flags |= eImageInfo_GCOnly;
2187
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002188 // Emitted as int[2];
2189 llvm::Constant *values[2] = {
2190 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2191 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2192 };
2193 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002194 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002195 new llvm::GlobalVariable(AT, true,
2196 llvm::GlobalValue::InternalLinkage,
2197 llvm::ConstantArray::get(AT, values, 2),
2198 "\01L_OBJC_IMAGE_INFO",
2199 &CGM.getModule());
2200
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002201 if (ObjCABI == 1) {
2202 GV->setSection("__OBJC, __image_info,regular");
2203 } else {
2204 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2205 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002206
2207 UsedGlobals.push_back(GV);
2208}
2209
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002210
2211// struct objc_module {
2212// unsigned long version;
2213// unsigned long size;
2214// const char *name;
2215// Symtab symtab;
2216// };
2217
2218// FIXME: Get from somewhere
2219static const int ModuleVersion = 7;
2220
2221void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002222 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002223
2224 std::vector<llvm::Constant*> Values(4);
2225 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2226 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002227 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002228 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002229 Values[3] = EmitModuleSymbols();
2230
2231 llvm::GlobalVariable *GV =
2232 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2233 llvm::GlobalValue::InternalLinkage,
2234 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2235 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002236 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002237 &CGM.getModule());
2238 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2239 UsedGlobals.push_back(GV);
2240}
2241
2242llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002243 unsigned NumClasses = DefinedClasses.size();
2244 unsigned NumCategories = DefinedCategories.size();
2245
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002246 // Return null if no symbols were defined.
2247 if (!NumClasses && !NumCategories)
2248 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2249
2250 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002251 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2252 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2253 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2254 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2255
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002256 // The runtime expects exactly the list of defined classes followed
2257 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002258 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002259 for (unsigned i=0; i<NumClasses; i++)
2260 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2261 ObjCTypes.Int8PtrTy);
2262 for (unsigned i=0; i<NumCategories; i++)
2263 Symbols[NumClasses + i] =
2264 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2265 ObjCTypes.Int8PtrTy);
2266
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002267 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002268 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002269 NumClasses + NumCategories),
2270 Symbols);
2271
2272 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2273
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002274 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002275 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002276 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002277 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002278 "\01L_OBJC_SYMBOLS",
2279 &CGM.getModule());
2280 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2281 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002282 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2283}
2284
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002285llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002286 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002287 LazySymbols.insert(ID->getIdentifier());
2288
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002289 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2290
2291 if (!Entry) {
2292 llvm::Constant *Casted =
2293 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2294 ObjCTypes.ClassPtrTy);
2295 Entry =
2296 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2297 llvm::GlobalValue::InternalLinkage,
2298 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2299 &CGM.getModule());
2300 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2301 UsedGlobals.push_back(Entry);
2302 }
2303
2304 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002305}
2306
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002307llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002308 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2309
2310 if (!Entry) {
2311 llvm::Constant *Casted =
2312 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2313 ObjCTypes.SelectorPtrTy);
2314 Entry =
2315 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2316 llvm::GlobalValue::InternalLinkage,
2317 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2318 &CGM.getModule());
2319 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2320 UsedGlobals.push_back(Entry);
2321 }
2322
2323 return Builder.CreateLoad(Entry, false, "tmp");
2324}
2325
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002326llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002327 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002328
2329 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002330 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002331 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002332 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002333 llvm::GlobalValue::InternalLinkage,
2334 C, "\01L_OBJC_CLASS_NAME_",
2335 &CGM.getModule());
2336 Entry->setSection("__TEXT,__cstring,cstring_literals");
2337 UsedGlobals.push_back(Entry);
2338 }
2339
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002340 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002341}
2342
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002343llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002344 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2345
2346 if (!Entry) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00002347 // FIXME: Avoid std::string copying.
2348 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002349 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002350 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002351 llvm::GlobalValue::InternalLinkage,
2352 C, "\01L_OBJC_METH_VAR_NAME_",
2353 &CGM.getModule());
2354 Entry->setSection("__TEXT,__cstring,cstring_literals");
2355 UsedGlobals.push_back(Entry);
2356 }
2357
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002358 return getConstantGEP(Entry, 0, 0);
2359}
2360
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002361// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002362llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002363 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2364}
2365
2366// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002367llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002368 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2369}
2370
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002371llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002372 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002373
2374 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002375 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002376 Entry =
2377 new llvm::GlobalVariable(C->getType(), false,
2378 llvm::GlobalValue::InternalLinkage,
2379 C, "\01L_OBJC_METH_VAR_TYPE_",
2380 &CGM.getModule());
2381 Entry->setSection("__TEXT,__cstring,cstring_literals");
2382 UsedGlobals.push_back(Entry);
2383 }
2384
2385 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002386}
2387
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002388// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002389llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002390 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002391 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2392 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002393 return GetMethodVarType(TypeStr);
2394}
2395
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002396// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002397llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002398 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2399
2400 if (!Entry) {
2401 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2402 Entry =
2403 new llvm::GlobalVariable(C->getType(), false,
2404 llvm::GlobalValue::InternalLinkage,
2405 C, "\01L_OBJC_PROP_NAME_ATTR_",
2406 &CGM.getModule());
2407 Entry->setSection("__TEXT,__cstring,cstring_literals");
2408 UsedGlobals.push_back(Entry);
2409 }
2410
2411 return getConstantGEP(Entry, 0, 0);
2412}
2413
2414// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002415// FIXME: This Decl should be more precise.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002416llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002417 const Decl *Container) {
2418 std::string TypeStr;
2419 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002420 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2421}
2422
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002423void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2424 const ObjCContainerDecl *CD,
2425 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002426 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002427 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002428 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002429 assert (CD && "Missing container decl in GetNameForMethod");
2430 NameOut += CD->getNameAsString();
Fariborz Jahanian52847332009-01-26 23:49:05 +00002431 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
2432 // Right now! there is not enough info. to do this.
Chris Lattner077bf5e2008-11-24 03:33:13 +00002433 NameOut += ' ';
2434 NameOut += D->getSelector().getAsString();
2435 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002436}
2437
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002438/// GetFirstIvarInRecord - This routine returns the record for the
2439/// implementation of the fiven class OID. It also returns field
2440/// corresponding to the first ivar in the class in FIV. It also
2441/// returns the one before the first ivar.
2442///
2443const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord(
2444 const ObjCInterfaceDecl *OID,
2445 RecordDecl::field_iterator &FIV,
2446 RecordDecl::field_iterator &PIV) {
2447 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
2448 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
2449 RecordDecl::field_iterator ifield = RD->field_begin();
2450 RecordDecl::field_iterator pfield = RD->field_end();
2451 while (countSuperClassIvars-- > 0) {
2452 pfield = ifield;
2453 ++ifield;
2454 }
2455 FIV = ifield;
2456 PIV = pfield;
2457 return RD;
2458}
2459
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002460void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002461 EmitModuleInfo();
2462
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002463 // Emit the dummy bodies for any protocols which were referenced but
2464 // never defined.
2465 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2466 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2467 if (i->second->hasInitializer())
2468 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002469
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002470 std::vector<llvm::Constant*> Values(5);
2471 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2472 Values[1] = GetClassName(i->first);
2473 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2474 Values[3] = Values[4] =
2475 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2476 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2477 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2478 Values));
2479 }
2480
2481 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002482 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002483 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002484 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002485 }
2486
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002487 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002488 llvm::GlobalValue *GV =
2489 new llvm::GlobalVariable(AT, false,
2490 llvm::GlobalValue::AppendingLinkage,
2491 llvm::ConstantArray::get(AT, Used),
2492 "llvm.used",
2493 &CGM.getModule());
2494
2495 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002496
2497 // Add assembler directives to add lazy undefined symbol references
2498 // for classes which are referenced but not defined. This is
2499 // important for correct linker interaction.
2500
2501 // FIXME: Uh, this isn't particularly portable.
2502 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00002503
2504 if (!CGM.getModule().getModuleInlineAsm().empty())
2505 s << "\n";
2506
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002507 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2508 e = LazySymbols.end(); i != e; ++i) {
2509 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2510 }
2511 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2512 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002513 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002514 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2515 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00002516
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002517 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002518}
2519
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002520CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002521 : CGObjCCommonMac(cgm),
2522 ObjCTypes(cgm)
2523{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002524 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002525 ObjCABI = 2;
2526}
2527
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002528/* *** */
2529
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002530ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2531: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002532{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002533 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2534 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002535
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002536 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002537 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002538 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002539 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2540
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002541 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002542 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002543 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002544
2545 // FIXME: It would be nice to unify this with the opaque type, so
2546 // that the IR comes out a bit cleaner.
2547 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2548 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002549
2550 // I'm not sure I like this. The implicit coordination is a bit
2551 // gross. We should solve this in a reasonable fashion because this
2552 // is a pretty common task (match some runtime data structure with
2553 // an LLVM data structure).
2554
2555 // FIXME: This is leaked.
2556 // FIXME: Merge with rewriter code?
2557
2558 // struct _objc_super {
2559 // id self;
2560 // Class cls;
2561 // }
2562 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2563 SourceLocation(),
2564 &Ctx.Idents.get("_objc_super"));
2565 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2566 Ctx.getObjCIdType(), 0, false));
2567 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2568 Ctx.getObjCClassType(), 0, false));
2569 RD->completeDefinition(Ctx);
2570
2571 SuperCTy = Ctx.getTagDeclType(RD);
2572 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2573
2574 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002575 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2576
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002577 // struct _prop_t {
2578 // char *name;
2579 // char *attributes;
2580 // }
2581 PropertyTy = llvm::StructType::get(Int8PtrTy,
2582 Int8PtrTy,
2583 NULL);
2584 CGM.getModule().addTypeName("struct._prop_t",
2585 PropertyTy);
2586
2587 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002588 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002589 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002590 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002591 // }
2592 PropertyListTy = llvm::StructType::get(IntTy,
2593 IntTy,
2594 llvm::ArrayType::get(PropertyTy, 0),
2595 NULL);
2596 CGM.getModule().addTypeName("struct._prop_list_t",
2597 PropertyListTy);
2598 // struct _prop_list_t *
2599 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2600
2601 // struct _objc_method {
2602 // SEL _cmd;
2603 // char *method_type;
2604 // char *_imp;
2605 // }
2606 MethodTy = llvm::StructType::get(SelectorPtrTy,
2607 Int8PtrTy,
2608 Int8PtrTy,
2609 NULL);
2610 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002611
2612 // struct _objc_cache *
2613 CacheTy = llvm::OpaqueType::get();
2614 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2615 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002616
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002617 // Property manipulation functions.
2618
2619 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
2620 std::vector<const llvm::Type*> Params;
2621 Params.push_back(ObjectPtrTy);
2622 Params.push_back(SelectorPtrTy);
2623 Params.push_back(LongTy);
2624 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2625 GetPropertyFn =
2626 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2627 Params,
2628 false),
2629 "objc_getProperty");
2630
2631 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2632 Params.clear();
2633 Params.push_back(ObjectPtrTy);
2634 Params.push_back(SelectorPtrTy);
2635 Params.push_back(LongTy);
2636 Params.push_back(ObjectPtrTy);
2637 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2638 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2639 SetPropertyFn =
2640 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2641 Params,
2642 false),
2643 "objc_setProperty");
2644 // Enumeration mutation.
2645
2646 Params.clear();
2647 Params.push_back(ObjectPtrTy);
2648 EnumerationMutationFn =
2649 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2650 Params,
2651 false),
2652 "objc_enumerationMutation");
2653
2654 // gc's API
2655 // id objc_read_weak (id *)
2656 Params.clear();
2657 Params.push_back(PtrObjectPtrTy);
2658 GcReadWeakFn =
2659 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2660 Params,
2661 false),
2662 "objc_read_weak");
2663 // id objc_assign_weak (id, id *)
2664 Params.clear();
2665 Params.push_back(ObjectPtrTy);
2666 Params.push_back(PtrObjectPtrTy);
2667 GcAssignWeakFn =
2668 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2669 Params,
2670 false),
2671 "objc_assign_weak");
2672 GcAssignGlobalFn =
2673 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2674 Params,
2675 false),
2676 "objc_assign_global");
2677 GcAssignIvarFn =
2678 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2679 Params,
2680 false),
2681 "objc_assign_ivar");
2682 GcAssignStrongCastFn =
2683 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2684 Params,
2685 false),
2686 "objc_assign_strongCast");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002687}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002688
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002689ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2690 : ObjCCommonTypesHelper(cgm)
2691{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002692 // struct _objc_method_description {
2693 // SEL name;
2694 // char *types;
2695 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002696 MethodDescriptionTy =
2697 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002698 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002699 NULL);
2700 CGM.getModule().addTypeName("struct._objc_method_description",
2701 MethodDescriptionTy);
2702
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002703 // struct _objc_method_description_list {
2704 // int count;
2705 // struct _objc_method_description[1];
2706 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002707 MethodDescriptionListTy =
2708 llvm::StructType::get(IntTy,
2709 llvm::ArrayType::get(MethodDescriptionTy, 0),
2710 NULL);
2711 CGM.getModule().addTypeName("struct._objc_method_description_list",
2712 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002713
2714 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002715 MethodDescriptionListPtrTy =
2716 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2717
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002718 // Protocol description structures
2719
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002720 // struct _objc_protocol_extension {
2721 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2722 // struct _objc_method_description_list *optional_instance_methods;
2723 // struct _objc_method_description_list *optional_class_methods;
2724 // struct _objc_property_list *instance_properties;
2725 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002726 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002727 llvm::StructType::get(IntTy,
2728 MethodDescriptionListPtrTy,
2729 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002730 PropertyListPtrTy,
2731 NULL);
2732 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2733 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002734
2735 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002736 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2737
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002738 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002739
2740 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2741 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2742
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002743 const llvm::Type *T =
2744 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2745 LongTy,
2746 llvm::ArrayType::get(ProtocolTyHolder, 0),
2747 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002748 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2749
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002750 // struct _objc_protocol {
2751 // struct _objc_protocol_extension *isa;
2752 // char *protocol_name;
2753 // struct _objc_protocol **_objc_protocol_list;
2754 // struct _objc_method_description_list *instance_methods;
2755 // struct _objc_method_description_list *class_methods;
2756 // }
2757 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002758 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002759 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2760 MethodDescriptionListPtrTy,
2761 MethodDescriptionListPtrTy,
2762 NULL);
2763 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2764
2765 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2766 CGM.getModule().addTypeName("struct._objc_protocol_list",
2767 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002768 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002769 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2770
2771 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002772 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002773 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002774
2775 // Class description structures
2776
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002777 // struct _objc_ivar {
2778 // char *ivar_name;
2779 // char *ivar_type;
2780 // int ivar_offset;
2781 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002782 IvarTy = llvm::StructType::get(Int8PtrTy,
2783 Int8PtrTy,
2784 IntTy,
2785 NULL);
2786 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2787
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002788 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002789 IvarListTy = llvm::OpaqueType::get();
2790 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2791 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2792
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002793 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002794 MethodListTy = llvm::OpaqueType::get();
2795 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2796 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2797
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002798 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002799 ClassExtensionTy =
2800 llvm::StructType::get(IntTy,
2801 Int8PtrTy,
2802 PropertyListPtrTy,
2803 NULL);
2804 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2805 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2806
2807 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2808
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002809 // struct _objc_class {
2810 // Class isa;
2811 // Class super_class;
2812 // char *name;
2813 // long version;
2814 // long info;
2815 // long instance_size;
2816 // struct _objc_ivar_list *ivars;
2817 // struct _objc_method_list *methods;
2818 // struct _objc_cache *cache;
2819 // struct _objc_protocol_list *protocols;
2820 // char *ivar_layout;
2821 // struct _objc_class_ext *ext;
2822 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002823 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2824 llvm::PointerType::getUnqual(ClassTyHolder),
2825 Int8PtrTy,
2826 LongTy,
2827 LongTy,
2828 LongTy,
2829 IvarListPtrTy,
2830 MethodListPtrTy,
2831 CachePtrTy,
2832 ProtocolListPtrTy,
2833 Int8PtrTy,
2834 ClassExtensionPtrTy,
2835 NULL);
2836 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2837
2838 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2839 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2840 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2841
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002842 // struct _objc_category {
2843 // char *category_name;
2844 // char *class_name;
2845 // struct _objc_method_list *instance_method;
2846 // struct _objc_method_list *class_method;
2847 // uint32_t size; // sizeof(struct _objc_category)
2848 // struct _objc_property_list *instance_properties;// category's @property
2849 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002850 CategoryTy = llvm::StructType::get(Int8PtrTy,
2851 Int8PtrTy,
2852 MethodListPtrTy,
2853 MethodListPtrTy,
2854 ProtocolListPtrTy,
2855 IntTy,
2856 PropertyListPtrTy,
2857 NULL);
2858 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2859
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002860 // Global metadata structures
2861
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002862 // struct _objc_symtab {
2863 // long sel_ref_cnt;
2864 // SEL *refs;
2865 // short cls_def_cnt;
2866 // short cat_def_cnt;
2867 // char *defs[cls_def_cnt + cat_def_cnt];
2868 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002869 SymtabTy = llvm::StructType::get(LongTy,
2870 SelectorPtrTy,
2871 ShortTy,
2872 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002873 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002874 NULL);
2875 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2876 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2877
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002878 // struct _objc_module {
2879 // long version;
2880 // long size; // sizeof(struct _objc_module)
2881 // char *name;
2882 // struct _objc_symtab* symtab;
2883 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002884 ModuleTy =
2885 llvm::StructType::get(LongTy,
2886 LongTy,
2887 Int8PtrTy,
2888 SymtabPtrTy,
2889 NULL);
2890 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002891
Daniel Dunbar49f66022008-09-24 03:38:44 +00002892 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002893
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002894 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002895 std::vector<const llvm::Type*> Params;
2896 Params.push_back(ObjectPtrTy);
2897 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002898 MessageSendFn =
2899 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2900 Params,
2901 true),
2902 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002903
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002904 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002905 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002906 Params.push_back(ObjectPtrTy);
2907 Params.push_back(SelectorPtrTy);
2908 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002909 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2910 Params,
2911 true),
2912 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002913
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002914 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00002915 Params.clear();
2916 Params.push_back(ObjectPtrTy);
2917 Params.push_back(SelectorPtrTy);
2918 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002919 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00002920 MessageSendFpretFn =
2921 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
2922 Params,
2923 true),
2924 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002925
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002926 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002927 Params.clear();
2928 Params.push_back(SuperPtrTy);
2929 Params.push_back(SelectorPtrTy);
2930 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002931 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2932 Params,
2933 true),
2934 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002935
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002936 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
2937 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002938 Params.clear();
2939 Params.push_back(Int8PtrTy);
2940 Params.push_back(SuperPtrTy);
2941 Params.push_back(SelectorPtrTy);
2942 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002943 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2944 Params,
2945 true),
2946 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002947
2948 // There is no objc_msgSendSuper_fpret? How can that work?
2949 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002950
Anders Carlsson124526b2008-09-09 10:10:21 +00002951 // FIXME: This is the size of the setjmp buffer and should be
2952 // target specific. 18 is what's used on 32-bit X86.
2953 uint64_t SetJmpBufferSize = 18;
2954
2955 // Exceptions
2956 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00002957 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00002958
2959 ExceptionDataTy =
2960 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
2961 SetJmpBufferSize),
2962 StackPtrTy, NULL);
2963 CGM.getModule().addTypeName("struct._objc_exception_data",
2964 ExceptionDataTy);
2965
2966 Params.clear();
2967 Params.push_back(ObjectPtrTy);
2968 ExceptionThrowFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002969 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2970 Params,
2971 false),
2972 "objc_exception_throw");
Anders Carlsson124526b2008-09-09 10:10:21 +00002973
2974 Params.clear();
2975 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
2976 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002977 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2978 Params,
2979 false),
2980 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00002981 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002982 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2983 Params,
2984 false),
2985 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00002986 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002987 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2988 Params,
2989 false),
2990 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00002991
2992 Params.clear();
2993 Params.push_back(ClassPtrTy);
2994 Params.push_back(ObjectPtrTy);
2995 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002996 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2997 Params,
2998 false),
2999 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00003000
3001 // synchronized APIs
3002 // void objc_sync_enter (id)
3003 Params.clear();
3004 Params.push_back(ObjectPtrTy);
3005 SyncEnterFn =
3006 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3007 Params,
3008 false),
3009 "objc_sync_enter");
3010 // void objc_sync_exit (id)
3011 SyncExitFn =
3012 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3013 Params,
3014 false),
3015 "objc_sync_exit");
3016
Anders Carlsson124526b2008-09-09 10:10:21 +00003017
3018 Params.clear();
3019 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
3020 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003021 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3022 Params,
3023 false),
3024 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003025
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003026}
3027
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003028ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003029: ObjCCommonTypesHelper(cgm)
3030{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003031 // struct _method_list_t {
3032 // uint32_t entsize; // sizeof(struct _objc_method)
3033 // uint32_t method_count;
3034 // struct _objc_method method_list[method_count];
3035 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003036 MethodListnfABITy = llvm::StructType::get(IntTy,
3037 IntTy,
3038 llvm::ArrayType::get(MethodTy, 0),
3039 NULL);
3040 CGM.getModule().addTypeName("struct.__method_list_t",
3041 MethodListnfABITy);
3042 // struct method_list_t *
3043 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003044
3045 // struct _protocol_t {
3046 // id isa; // NULL
3047 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003048 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003049 // const struct method_list_t * const instance_methods;
3050 // const struct method_list_t * const class_methods;
3051 // const struct method_list_t *optionalInstanceMethods;
3052 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003053 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003054 // const uint32_t size; // sizeof(struct _protocol_t)
3055 // const uint32_t flags; // = 0
3056 // }
3057
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003058 // Holder for struct _protocol_list_t *
3059 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3060
3061 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3062 Int8PtrTy,
3063 llvm::PointerType::getUnqual(
3064 ProtocolListTyHolder),
3065 MethodListnfABIPtrTy,
3066 MethodListnfABIPtrTy,
3067 MethodListnfABIPtrTy,
3068 MethodListnfABIPtrTy,
3069 PropertyListPtrTy,
3070 IntTy,
3071 IntTy,
3072 NULL);
3073 CGM.getModule().addTypeName("struct._protocol_t",
3074 ProtocolnfABITy);
3075
Fariborz Jahanianda320092009-01-29 19:24:30 +00003076 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003077 // long protocol_count; // Note, this is 32/64 bit
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003078 // struct _protocol_t[protocol_count];
3079 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003080 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3081 llvm::ArrayType::get(
3082 ProtocolnfABITy, 0),
3083 NULL);
3084 CGM.getModule().addTypeName("struct._objc_protocol_list",
3085 ProtocolListnfABITy);
3086
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003087 // struct _objc_protocol_list*
3088 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003089
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003090 // FIXME! Is this doing the right thing?
3091 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3092 ProtocolListnfABIPtrTy);
3093
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003094 // struct _ivar_t {
3095 // unsigned long int *offset; // pointer to ivar offset location
3096 // char *name;
3097 // char *type;
3098 // uint32_t alignment;
3099 // uint32_t size;
3100 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003101 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3102 Int8PtrTy,
3103 Int8PtrTy,
3104 IntTy,
3105 IntTy,
3106 NULL);
3107 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3108
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003109 // struct _ivar_list_t {
3110 // uint32 entsize; // sizeof(struct _ivar_t)
3111 // uint32 count;
3112 // struct _iver_t list[count];
3113 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003114 IvarListnfABITy = llvm::StructType::get(IntTy,
3115 IntTy,
3116 llvm::ArrayType::get(
3117 IvarnfABITy, 0),
3118 NULL);
3119 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3120
3121 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003122
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003123 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003124 // uint32_t const flags;
3125 // uint32_t const instanceStart;
3126 // uint32_t const instanceSize;
3127 // uint32_t const reserved; // only when building for 64bit targets
3128 // const uint8_t * const ivarLayout;
3129 // const char *const name;
3130 // const struct _method_list_t * const baseMethods;
3131 // const struct _objc_protocol_list *const baseProtocols;
3132 // const struct _ivar_list_t *const ivars;
3133 // const uint8_t * const weakIvarLayout;
3134 // const struct _prop_list_t * const properties;
3135 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003136
3137 // FIXME. Add 'reserved' field in 64bit abi mode!
3138 ClassRonfABITy = llvm::StructType::get(IntTy,
3139 IntTy,
3140 IntTy,
3141 Int8PtrTy,
3142 Int8PtrTy,
3143 MethodListnfABIPtrTy,
3144 ProtocolListnfABIPtrTy,
3145 IvarListnfABIPtrTy,
3146 Int8PtrTy,
3147 PropertyListPtrTy,
3148 NULL);
3149 CGM.getModule().addTypeName("struct._class_ro_t",
3150 ClassRonfABITy);
3151
3152 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3153 std::vector<const llvm::Type*> Params;
3154 Params.push_back(ObjectPtrTy);
3155 Params.push_back(SelectorPtrTy);
3156 ImpnfABITy = llvm::PointerType::getUnqual(
3157 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3158
3159 // struct _class_t {
3160 // struct _class_t *isa;
3161 // struct _class_t * const superclass;
3162 // void *cache;
3163 // IMP *vtable;
3164 // struct class_ro_t *ro;
3165 // }
3166
3167 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3168 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3169 llvm::PointerType::getUnqual(ClassTyHolder),
3170 CachePtrTy,
3171 llvm::PointerType::getUnqual(ImpnfABITy),
3172 llvm::PointerType::getUnqual(
3173 ClassRonfABITy),
3174 NULL);
3175 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3176
3177 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3178 ClassnfABITy);
3179
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003180 // LLVM for struct _class_t *
3181 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3182
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003183 // struct _category_t {
3184 // const char * const name;
3185 // struct _class_t *const cls;
3186 // const struct _method_list_t * const instance_methods;
3187 // const struct _method_list_t * const class_methods;
3188 // const struct _protocol_list_t * const protocols;
3189 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003190 // }
3191 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003192 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003193 MethodListnfABIPtrTy,
3194 MethodListnfABIPtrTy,
3195 ProtocolListnfABIPtrTy,
3196 PropertyListPtrTy,
3197 NULL);
3198 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003199
3200 // New types for nonfragile abi messaging.
3201
3202 // MessageRefTy - LLVM for:
3203 // struct _message_ref_t {
3204 // IMP messenger;
3205 // SEL name;
3206 // };
3207 MessageRefTy = llvm::StructType::get(ImpnfABITy,
3208 SelectorPtrTy,
3209 NULL);
3210 CGM.getModule().addTypeName("struct._message_ref_t", MessageRefTy);
3211
3212 // MessageRefPtrTy - LLVM for struct _message_ref_t*
3213 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
3214
3215 // SuperMessageRefTy - LLVM for:
3216 // struct _super_message_ref_t {
3217 // SUPER_IMP messenger;
3218 // SEL name;
3219 // };
3220 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
3221 SelectorPtrTy,
3222 NULL);
3223 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
3224
3225 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
3226 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
3227
3228 // id objc_msgSend_fixup (id, struct message_ref_t*, ...)
3229 Params.clear();
3230 Params.push_back(ObjectPtrTy);
3231 Params.push_back(MessageRefPtrTy);
3232 MessageSendFixupFn =
3233 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3234 Params,
3235 true),
3236 "objc_msgSend_fixup");
3237
3238 // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...)
3239 MessageSendFpretFixupFn =
3240 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3241 Params,
3242 true),
3243 "objc_msgSend_fpret_fixup");
3244
3245 // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...)
3246 MessageSendStretFixupFn =
3247 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3248 Params,
3249 true),
3250 "objc_msgSend_stret_fixup");
3251
3252 // id objc_msgSendId_fixup (id, struct message_ref_t*, ...)
3253 MessageSendIdFixupFn =
3254 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3255 Params,
3256 true),
3257 "objc_msgSendId_fixup");
3258
3259
3260 // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...)
3261 MessageSendIdStretFixupFn =
3262 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3263 Params,
3264 true),
3265 "MessageSendIdStretFixupFn");
3266
3267 // id objc_msgSendSuper2_fixup (struct objc_super *,
3268 // struct _super_message_ref_t*, ...)
3269 Params.clear();
3270 Params.push_back(SuperPtrTy);
3271 Params.push_back(SuperMessageRefPtrTy);
3272 MessageSendSuper2FixupFn =
3273 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3274 Params,
3275 true),
3276 "objc_msgSendSuper2_fixup");
3277
3278
3279 // id objc_msgSendSuper2_stret_fixup (struct objc_super *,
3280 // struct _super_message_ref_t*, ...)
3281 MessageSendSuper2StretFixupFn =
3282 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3283 Params,
3284 true),
3285 "objc_msgSendSuper2_stret_fixup");
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003286
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003287}
3288
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003289llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3290 FinishNonFragileABIModule();
3291
3292 return NULL;
3293}
3294
3295void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3296 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003297
3298 // Build list of all implemented classe addresses in array
3299 // L_OBJC_LABEL_CLASS_$.
3300 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
3301 // list of 'nonlazy' implementations (defined as those with a +load{}
3302 // method!!).
3303 unsigned NumClasses = DefinedClasses.size();
3304 if (NumClasses) {
3305 std::vector<llvm::Constant*> Symbols(NumClasses);
3306 for (unsigned i=0; i<NumClasses; i++)
3307 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3308 ObjCTypes.Int8PtrTy);
3309 llvm::Constant* Init =
3310 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3311 NumClasses),
3312 Symbols);
3313
3314 llvm::GlobalVariable *GV =
3315 new llvm::GlobalVariable(Init->getType(), false,
3316 llvm::GlobalValue::InternalLinkage,
3317 Init,
3318 "\01L_OBJC_LABEL_CLASS_$",
3319 &CGM.getModule());
3320 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
3321 UsedGlobals.push_back(GV);
3322 }
3323
3324 // Build list of all implemented category addresses in array
3325 // L_OBJC_LABEL_CATEGORY_$.
3326 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
3327 // list of 'nonlazy' category implementations (defined as those with a +load{}
3328 // method!!).
3329 unsigned NumCategory = DefinedCategories.size();
3330 if (NumCategory) {
3331 std::vector<llvm::Constant*> Symbols(NumCategory);
3332 for (unsigned i=0; i<NumCategory; i++)
3333 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
3334 ObjCTypes.Int8PtrTy);
3335 llvm::Constant* Init =
3336 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3337 NumCategory),
3338 Symbols);
3339
3340 llvm::GlobalVariable *GV =
3341 new llvm::GlobalVariable(Init->getType(), false,
3342 llvm::GlobalValue::InternalLinkage,
3343 Init,
3344 "\01L_OBJC_LABEL_CATEGORY_$",
3345 &CGM.getModule());
3346 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
3347 UsedGlobals.push_back(GV);
3348 }
3349
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00003350 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
3351 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
3352 std::vector<llvm::Constant*> Values(2);
3353 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3354 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3355 llvm::Constant* Init = llvm::ConstantArray::get(
3356 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
3357 Values);
3358 llvm::GlobalVariable *IMGV =
3359 new llvm::GlobalVariable(Init->getType(), false,
3360 llvm::GlobalValue::InternalLinkage,
3361 Init,
3362 "\01L_OBJC_IMAGE_INFO",
3363 &CGM.getModule());
3364 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
3365 UsedGlobals.push_back(IMGV);
3366
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003367 std::vector<llvm::Constant*> Used;
3368 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3369 e = UsedGlobals.end(); i != e; ++i) {
3370 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3371 }
3372
3373 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3374 llvm::GlobalValue *GV =
3375 new llvm::GlobalVariable(AT, false,
3376 llvm::GlobalValue::AppendingLinkage,
3377 llvm::ConstantArray::get(AT, Used),
3378 "llvm.used",
3379 &CGM.getModule());
3380
3381 GV->setSection("llvm.metadata");
3382
3383}
3384
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003385// Metadata flags
3386enum MetaDataDlags {
3387 CLS = 0x0,
3388 CLS_META = 0x1,
3389 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003390 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003391 CLS_EXCEPTION = 0x20
3392};
3393/// BuildClassRoTInitializer - generate meta-data for:
3394/// struct _class_ro_t {
3395/// uint32_t const flags;
3396/// uint32_t const instanceStart;
3397/// uint32_t const instanceSize;
3398/// uint32_t const reserved; // only when building for 64bit targets
3399/// const uint8_t * const ivarLayout;
3400/// const char *const name;
3401/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00003402/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003403/// const struct _ivar_list_t *const ivars;
3404/// const uint8_t * const weakIvarLayout;
3405/// const struct _prop_list_t * const properties;
3406/// }
3407///
3408llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
3409 unsigned flags,
3410 unsigned InstanceStart,
3411 unsigned InstanceSize,
3412 const ObjCImplementationDecl *ID) {
3413 std::string ClassName = ID->getNameAsString();
3414 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
3415 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
3416 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
3417 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
3418 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianda320092009-01-29 19:24:30 +00003419 // FIXME. ivarLayout is currently null!
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003420 Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3421 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003422 // const struct _method_list_t * const baseMethods;
3423 std::vector<llvm::Constant*> Methods;
3424 std::string MethodListName("\01l_OBJC_$_");
3425 if (flags & CLS_META) {
3426 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
3427 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
3428 e = ID->classmeth_end(); i != e; ++i) {
3429 // Class methods should always be defined.
3430 Methods.push_back(GetMethodConstant(*i));
3431 }
3432 } else {
3433 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
3434 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
3435 e = ID->instmeth_end(); i != e; ++i) {
3436 // Instance methods should always be defined.
3437 Methods.push_back(GetMethodConstant(*i));
3438 }
Fariborz Jahanian939abce2009-01-28 22:46:49 +00003439 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
3440 e = ID->propimpl_end(); i != e; ++i) {
3441 ObjCPropertyImplDecl *PID = *i;
3442
3443 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
3444 ObjCPropertyDecl *PD = PID->getPropertyDecl();
3445
3446 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3447 if (llvm::Constant *C = GetMethodConstant(MD))
3448 Methods.push_back(C);
3449 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3450 if (llvm::Constant *C = GetMethodConstant(MD))
3451 Methods.push_back(C);
3452 }
3453 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003454 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003455 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003456 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003457
3458 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3459 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
3460 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
3461 + OID->getNameAsString(),
3462 OID->protocol_begin(),
3463 OID->protocol_end());
3464
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003465 if (flags & CLS_META)
3466 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3467 else
3468 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003469 // FIXME. weakIvarLayout is currently null.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003470 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003471 if (flags & CLS_META)
3472 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3473 else
3474 Values[ 9] =
3475 EmitPropertyList(
3476 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
3477 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003478 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
3479 Values);
3480 llvm::GlobalVariable *CLASS_RO_GV =
3481 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
3482 llvm::GlobalValue::InternalLinkage,
3483 Init,
3484 (flags & CLS_META) ?
3485 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
3486 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
3487 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003488 CLASS_RO_GV->setAlignment(
3489 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003490 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003491 UsedGlobals.push_back(CLASS_RO_GV);
3492 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003493
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003494}
3495
3496/// BuildClassMetaData - This routine defines that to-level meta-data
3497/// for the given ClassName for:
3498/// struct _class_t {
3499/// struct _class_t *isa;
3500/// struct _class_t * const superclass;
3501/// void *cache;
3502/// IMP *vtable;
3503/// struct class_ro_t *ro;
3504/// }
3505///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003506llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
3507 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003508 llvm::Constant *IsAGV,
3509 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003510 llvm::Constant *ClassRoGV,
3511 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003512 std::vector<llvm::Constant*> Values(5);
3513 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003514 Values[1] = SuperClassGV
3515 ? SuperClassGV
3516 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003517 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
3518 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
3519 Values[4] = ClassRoGV; // &CLASS_RO_GV
3520 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
3521 Values);
3522 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName);
3523 if (GV)
3524 GV->setInitializer(Init);
3525 else
3526 GV =
3527 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3528 llvm::GlobalValue::ExternalLinkage,
3529 Init,
3530 ClassName,
3531 &CGM.getModule());
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00003532 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003533 GV->setAlignment(
3534 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003535 if (HiddenVisibility)
3536 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003537 UsedGlobals.push_back(GV);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003538 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003539}
3540
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003541void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
3542 std::string ClassName = ID->getNameAsString();
3543 if (!ObjCEmptyCacheVar) {
3544 ObjCEmptyCacheVar = new llvm::GlobalVariable(
3545 ObjCTypes.CachePtrTy,
3546 false,
3547 llvm::GlobalValue::ExternalLinkage,
3548 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00003549 "\01__objc_empty_cache",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003550 &CGM.getModule());
3551 UsedGlobals.push_back(ObjCEmptyCacheVar);
3552
3553 ObjCEmptyVtableVar = new llvm::GlobalVariable(
3554 llvm::PointerType::getUnqual(
3555 ObjCTypes.ImpnfABITy),
3556 false,
3557 llvm::GlobalValue::ExternalLinkage,
3558 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00003559 "\01__objc_empty_vtable",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003560 &CGM.getModule());
3561 UsedGlobals.push_back(ObjCEmptyVtableVar);
3562 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003563 assert(ID->getClassInterface() &&
3564 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003565 uint32_t InstanceStart =
3566 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
3567 uint32_t InstanceSize = InstanceStart;
3568 uint32_t flags = CLS_META;
3569 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
3570 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003571
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003572 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003573
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003574 bool classIsHidden = IsClassHidden(ID->getClassInterface());
3575 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003576 flags |= OBJC2_CLS_HIDDEN;
3577 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003578 // class is root
3579 flags |= CLS_ROOT;
3580 std::string SuperClassName = ObjCClassName + ClassName;
3581 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3582 if (!SuperClassGV)
3583 SuperClassGV =
3584 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3585 llvm::GlobalValue::ExternalLinkage,
3586 0,
3587 SuperClassName,
3588 &CGM.getModule());
3589 UsedGlobals.push_back(SuperClassGV);
3590 std::string IsAClassName = ObjCMetaClassName + ClassName;
3591 IsAGV = CGM.getModule().getGlobalVariable(IsAClassName);
3592 if (!IsAGV)
3593 IsAGV =
3594 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3595 llvm::GlobalValue::ExternalLinkage,
3596 0,
3597 IsAClassName,
3598 &CGM.getModule());
3599 UsedGlobals.push_back(IsAGV);
3600 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003601 // Has a root. Current class is not a root.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003602 std::string RootClassName =
3603 ID->getClassInterface()->getSuperClass()->getNameAsString();
3604 std::string SuperClassName = ObjCMetaClassName + RootClassName;
3605 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3606 if (!SuperClassGV)
3607 SuperClassGV =
3608 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3609 llvm::GlobalValue::ExternalLinkage,
3610 0,
3611 SuperClassName,
3612 &CGM.getModule());
3613 UsedGlobals.push_back(SuperClassGV);
3614 IsAGV = SuperClassGV;
3615 }
3616 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
3617 InstanceStart,
3618 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003619 std::string TClassName = ObjCMetaClassName + ClassName;
3620 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003621 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
3622 classIsHidden);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003623
3624 // Metadata for the class
3625 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003626 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003627 flags |= OBJC2_CLS_HIDDEN;
3628 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003629 flags |= CLS_ROOT;
3630 SuperClassGV = 0;
3631 }
3632 else {
3633 // Has a root. Current class is not a root.
3634 std::string RootClassName =
3635 ID->getClassInterface()->getSuperClass()->getNameAsString();
3636 std::string SuperClassName = ObjCClassName + RootClassName;
3637 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3638 if (!SuperClassGV)
3639 SuperClassGV =
3640 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3641 llvm::GlobalValue::ExternalLinkage,
3642 0,
3643 SuperClassName,
3644 &CGM.getModule());
3645 UsedGlobals.push_back(SuperClassGV);
3646
3647 }
3648
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003649 InstanceStart = InstanceSize = 0;
3650 if (ObjCInterfaceDecl *OID =
3651 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
3652 // FIXME. Share this with the one in EmitIvarList.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003653 const llvm::Type *InterfaceTy =
3654 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
3655 const llvm::StructLayout *Layout =
3656 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003657
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003658 RecordDecl::field_iterator firstField, lastField;
3659 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003660
3661 for (RecordDecl::field_iterator e = RD->field_end(),
3662 ifield = firstField; ifield != e; ++ifield)
3663 lastField = ifield;
3664
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003665 if (lastField != RD->field_end()) {
3666 FieldDecl *Field = *lastField;
3667 const llvm::Type *FieldTy =
3668 CGM.getTypes().ConvertTypeForMem(Field->getType());
3669 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3670 InstanceSize = Layout->getElementOffset(
3671 CGM.getTypes().getLLVMFieldNo(Field)) +
3672 Size;
3673 if (firstField == RD->field_end())
3674 InstanceStart = InstanceSize;
3675 else
3676 InstanceStart = Layout->getElementOffset(CGM.getTypes().
3677 getLLVMFieldNo(*firstField));
3678 }
3679 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003680 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003681 InstanceStart,
3682 InstanceSize,
3683 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003684
3685 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003686 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003687 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
3688 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003689 DefinedClasses.push_back(ClassMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003690}
3691
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00003692/// GenerateProtocolRef - This routine is called to generate code for
3693/// a protocol reference expression; as in:
3694/// @code
3695/// @protocol(Proto1);
3696/// @endcode
3697/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
3698/// which will hold address of the protocol meta-data.
3699///
3700llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
3701 const ObjCProtocolDecl *PD) {
3702
3703 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
3704 ObjCTypes.ExternalProtocolPtrTy);
3705
3706 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
3707 ProtocolName += PD->getNameAsCString();
3708
3709 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
3710 if (PTGV)
3711 return Builder.CreateLoad(PTGV, false, "tmp");
3712 PTGV = new llvm::GlobalVariable(
3713 Init->getType(), false,
3714 llvm::GlobalValue::WeakLinkage,
3715 Init,
3716 ProtocolName,
3717 &CGM.getModule());
3718 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
3719 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3720 UsedGlobals.push_back(PTGV);
3721 return Builder.CreateLoad(PTGV, false, "tmp");
3722}
3723
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003724/// GenerateCategory - Build metadata for a category implementation.
3725/// struct _category_t {
3726/// const char * const name;
3727/// struct _class_t *const cls;
3728/// const struct _method_list_t * const instance_methods;
3729/// const struct _method_list_t * const class_methods;
3730/// const struct _protocol_list_t * const protocols;
3731/// const struct _prop_list_t * const properties;
3732/// }
3733///
3734void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
3735{
3736 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003737 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
3738 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003739 "_$_" + OCD->getNameAsString());
3740 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString());
3741
3742 std::vector<llvm::Constant*> Values(6);
3743 Values[0] = GetClassName(OCD->getIdentifier());
3744 // meta-class entry symbol
3745 llvm::GlobalVariable *ClassGV =
3746 CGM.getModule().getGlobalVariable(ExtClassName);
3747 if (!ClassGV)
3748 ClassGV =
3749 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3750 llvm::GlobalValue::ExternalLinkage,
3751 0,
3752 ExtClassName,
3753 &CGM.getModule());
3754 UsedGlobals.push_back(ClassGV);
3755 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003756 std::vector<llvm::Constant*> Methods;
3757 std::string MethodListName(Prefix);
3758 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
3759 "_$_" + OCD->getNameAsString();
3760
3761 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
3762 e = OCD->instmeth_end(); i != e; ++i) {
3763 // Instance methods should always be defined.
3764 Methods.push_back(GetMethodConstant(*i));
3765 }
3766
3767 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003768 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003769 Methods);
3770
3771 MethodListName = Prefix;
3772 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
3773 OCD->getNameAsString();
3774 Methods.clear();
3775 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
3776 e = OCD->classmeth_end(); i != e; ++i) {
3777 // Class methods should always be defined.
3778 Methods.push_back(GetMethodConstant(*i));
3779 }
3780
3781 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003782 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003783 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003784 const ObjCCategoryDecl *Category =
3785 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanianeb732132009-01-29 23:23:06 +00003786 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
3787 + Interface->getNameAsString() + "_$_"
3788 + Category->getNameAsString(),
3789 Category->protocol_begin(),
3790 Category->protocol_end());
3791
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003792 std::string ExtName(Interface->getNameAsString() + "_$_" +
3793 OCD->getNameAsString());
3794 Values[5] =
3795 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
3796 OCD, Category, ObjCTypes);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003797 llvm::Constant *Init =
3798 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
3799 Values);
3800 llvm::GlobalVariable *GCATV
3801 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
3802 false,
3803 llvm::GlobalValue::InternalLinkage,
3804 Init,
3805 ExtCatName,
3806 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003807 GCATV->setAlignment(
3808 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003809 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003810 UsedGlobals.push_back(GCATV);
3811 DefinedCategories.push_back(GCATV);
3812}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003813
3814/// GetMethodConstant - Return a struct objc_method constant for the
3815/// given method if it has been defined. The result is null if the
3816/// method has not been defined. The return value has type MethodPtrTy.
3817llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
3818 const ObjCMethodDecl *MD) {
3819 // FIXME: Use DenseMap::lookup
3820 llvm::Function *Fn = MethodDefinitions[MD];
3821 if (!Fn)
3822 return 0;
3823
3824 std::vector<llvm::Constant*> Method(3);
3825 Method[0] =
3826 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
3827 ObjCTypes.SelectorPtrTy);
3828 Method[1] = GetMethodVarType(MD);
3829 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
3830 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
3831}
3832
3833/// EmitMethodList - Build meta-data for method declarations
3834/// struct _method_list_t {
3835/// uint32_t entsize; // sizeof(struct _objc_method)
3836/// uint32_t method_count;
3837/// struct _objc_method method_list[method_count];
3838/// }
3839///
3840llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
3841 const std::string &Name,
3842 const char *Section,
3843 const ConstantVector &Methods) {
3844 // Return null for empty list.
3845 if (Methods.empty())
3846 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
3847
3848 std::vector<llvm::Constant*> Values(3);
3849 // sizeof(struct _objc_method)
3850 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
3851 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3852 // method_count
3853 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
3854 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
3855 Methods.size());
3856 Values[2] = llvm::ConstantArray::get(AT, Methods);
3857 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3858
3859 llvm::GlobalVariable *GV =
3860 new llvm::GlobalVariable(Init->getType(), false,
3861 llvm::GlobalValue::InternalLinkage,
3862 Init,
3863 Name,
3864 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003865 GV->setAlignment(
3866 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003867 GV->setSection(Section);
3868 UsedGlobals.push_back(GV);
3869 return llvm::ConstantExpr::getBitCast(GV,
3870 ObjCTypes.MethodListnfABIPtrTy);
3871}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003872
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003873llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
3874 const ObjCImplementationDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003875 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003876 unsigned long int Offset) {
3877
3878 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003879 + Ivar->getNameAsString());
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003880 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.LongTy, Offset);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003881
3882 llvm::GlobalVariable *IvarOffsetGV =
3883 CGM.getModule().getGlobalVariable(ExternalName);
3884 if (IvarOffsetGV) {
3885 // ivar offset symbol already built due to user code referencing it.
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003886 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003887 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003888 IvarOffsetGV->setInitializer(Init);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003889 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003890 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003891 return IvarOffsetGV;
3892 }
3893
3894 IvarOffsetGV =
3895 new llvm::GlobalVariable(Init->getType(),
3896 false,
3897 llvm::GlobalValue::ExternalLinkage,
3898 Init,
3899 ExternalName,
3900 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003901 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003902 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003903 // @private and @package have hidden visibility.
3904 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public ||
3905 Ivar->getAccessControl() == ObjCIvarDecl::Protected);
3906 if (!globalVisibility)
3907 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003908 else
3909 if (const ObjCInterfaceDecl *OID = ID->getClassInterface())
3910 if (IsClassHidden(OID))
3911 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3912
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003913 IvarOffsetGV->setSection("__DATA, __objc_const");
3914 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003915 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003916}
3917
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003918/// EmitIvarList - Emit the ivar list for the given
3919/// implementation. If ForClass is true the list of class ivars
3920/// (i.e. metaclass ivars) is emitted, otherwise the list of
3921/// interface ivars will be emitted. The return value has type
3922/// IvarListnfABIPtrTy.
3923/// struct _ivar_t {
3924/// unsigned long int *offset; // pointer to ivar offset location
3925/// char *name;
3926/// char *type;
3927/// uint32_t alignment;
3928/// uint32_t size;
3929/// }
3930/// struct _ivar_list_t {
3931/// uint32 entsize; // sizeof(struct _ivar_t)
3932/// uint32 count;
3933/// struct _iver_t list[count];
3934/// }
3935///
3936llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
3937 const ObjCImplementationDecl *ID) {
3938
3939 std::vector<llvm::Constant*> Ivars, Ivar(5);
3940
3941 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3942 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
3943
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003944 // FIXME. Consolidate this with similar code in GenerateClass.
3945 const llvm::Type *InterfaceTy =
3946 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(
3947 const_cast<ObjCInterfaceDecl*>(OID)));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003948 const llvm::StructLayout *Layout =
3949 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003950
3951 RecordDecl::field_iterator i,p;
3952 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003953 ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin();
3954
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003955 for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) {
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003956 FieldDecl *Field = *i;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003957 unsigned long offset = Layout->getElementOffset(CGM.getTypes().
3958 getLLVMFieldNo(Field));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003959 const ObjCIvarDecl *ivarDecl = *I++;
3960 Ivar[0] = EmitIvarOffsetVar(ID, ivarDecl, offset);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003961 if (Field->getIdentifier())
3962 Ivar[1] = GetMethodVarName(Field->getIdentifier());
3963 else
3964 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3965 std::string TypeStr;
3966 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3967 Ivar[2] = GetMethodVarType(TypeStr);
3968 const llvm::Type *FieldTy =
3969 CGM.getTypes().ConvertTypeForMem(Field->getType());
3970 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3971 unsigned Align = CGM.getContext().getPreferredTypeAlign(
3972 Field->getType().getTypePtr()) >> 3;
3973 Align = llvm::Log2_32(Align);
3974 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Fariborz Jahanian07236ba2009-01-27 22:27:56 +00003975 // NOTE. Size of a bitfield does not match gcc's, because of the way
3976 // bitfields are treated special in each. But I am told that 'size'
3977 // for bitfield ivars is ignored by the runtime so it does not matter.
3978 // (even if it matters, some day, there is enough info. to get the bitfield
3979 // right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003980 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3981 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
3982 }
3983 // Return null for empty list.
3984 if (Ivars.empty())
3985 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3986 std::vector<llvm::Constant*> Values(3);
3987 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
3988 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3989 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
3990 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
3991 Ivars.size());
3992 Values[2] = llvm::ConstantArray::get(AT, Ivars);
3993 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3994 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
3995 llvm::GlobalVariable *GV =
3996 new llvm::GlobalVariable(Init->getType(), false,
3997 llvm::GlobalValue::InternalLinkage,
3998 Init,
3999 Prefix + OID->getNameAsString(),
4000 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004001 GV->setAlignment(
4002 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004003 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004004
4005 UsedGlobals.push_back(GV);
4006 return llvm::ConstantExpr::getBitCast(GV,
4007 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004008}
4009
4010llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4011 const ObjCProtocolDecl *PD) {
4012 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4013
4014 if (!Entry) {
4015 // We use the initializer as a marker of whether this is a forward
4016 // reference or not. At module finalization we add the empty
4017 // contents for protocols which were referenced but never defined.
4018 Entry =
4019 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4020 llvm::GlobalValue::ExternalLinkage,
4021 0,
4022 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4023 &CGM.getModule());
4024 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4025 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004026 }
4027
4028 return Entry;
4029}
4030
4031/// GetOrEmitProtocol - Generate the protocol meta-data:
4032/// @code
4033/// struct _protocol_t {
4034/// id isa; // NULL
4035/// const char * const protocol_name;
4036/// const struct _protocol_list_t * protocol_list; // super protocols
4037/// const struct method_list_t * const instance_methods;
4038/// const struct method_list_t * const class_methods;
4039/// const struct method_list_t *optionalInstanceMethods;
4040/// const struct method_list_t *optionalClassMethods;
4041/// const struct _prop_list_t * properties;
4042/// const uint32_t size; // sizeof(struct _protocol_t)
4043/// const uint32_t flags; // = 0
4044/// }
4045/// @endcode
4046///
4047
4048llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4049 const ObjCProtocolDecl *PD) {
4050 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4051
4052 // Early exit if a defining object has already been generated.
4053 if (Entry && Entry->hasInitializer())
4054 return Entry;
4055
4056 const char *ProtocolName = PD->getNameAsCString();
4057
4058 // Construct method lists.
4059 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4060 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
4061 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
4062 e = PD->instmeth_end(); i != e; ++i) {
4063 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004064 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004065 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4066 OptInstanceMethods.push_back(C);
4067 } else {
4068 InstanceMethods.push_back(C);
4069 }
4070 }
4071
4072 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
4073 e = PD->classmeth_end(); i != e; ++i) {
4074 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004075 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004076 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4077 OptClassMethods.push_back(C);
4078 } else {
4079 ClassMethods.push_back(C);
4080 }
4081 }
4082
4083 std::vector<llvm::Constant*> Values(10);
4084 // isa is NULL
4085 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4086 Values[1] = GetClassName(PD->getIdentifier());
4087 Values[2] = EmitProtocolList(
4088 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4089 PD->protocol_begin(),
4090 PD->protocol_end());
4091
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004092 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004093 + PD->getNameAsString(),
4094 "__DATA, __objc_const",
4095 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004096 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004097 + PD->getNameAsString(),
4098 "__DATA, __objc_const",
4099 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004100 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004101 + PD->getNameAsString(),
4102 "__DATA, __objc_const",
4103 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004104 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004105 + PD->getNameAsString(),
4106 "__DATA, __objc_const",
4107 OptClassMethods);
4108 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4109 0, PD, ObjCTypes);
4110 uint32_t Size =
4111 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
4112 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4113 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4114 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4115 Values);
4116
4117 if (Entry) {
4118 // Already created, fix the linkage and update the initializer.
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004119 Entry->setLinkage(llvm::GlobalValue::WeakLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004120 Entry->setInitializer(Init);
4121 } else {
4122 Entry =
4123 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004124 llvm::GlobalValue::WeakLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004125 Init,
4126 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4127 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004128 Entry->setAlignment(
4129 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004130 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004131 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004132 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4133
4134 // Use this protocol meta-data to build protocol list table in section
4135 // __DATA, __objc_protolist
4136 llvm::Type *ptype = llvm::PointerType::getUnqual(ObjCTypes.ProtocolnfABITy);
4137 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
4138 ptype, false,
4139 llvm::GlobalValue::WeakLinkage,
4140 Entry,
4141 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4142 +ProtocolName,
4143 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004144 PTGV->setAlignment(
4145 CGM.getTargetData().getPrefTypeAlignment(ptype));
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004146 PTGV->setSection("__DATA, __objc_protolist");
4147 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4148 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004149 return Entry;
4150}
4151
4152/// EmitProtocolList - Generate protocol list meta-data:
4153/// @code
4154/// struct _protocol_list_t {
4155/// long protocol_count; // Note, this is 32/64 bit
4156/// struct _protocol_t[protocol_count];
4157/// }
4158/// @endcode
4159///
4160llvm::Constant *
4161CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4162 ObjCProtocolDecl::protocol_iterator begin,
4163 ObjCProtocolDecl::protocol_iterator end) {
4164 std::vector<llvm::Constant*> ProtocolRefs;
4165
4166 for (; begin != end; ++begin)
4167 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4168
4169 // Just return null for empty protocol lists
4170 if (ProtocolRefs.empty())
4171 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4172
4173 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4174 if (GV)
4175 return GV;
4176 // This list is null terminated.
4177 ProtocolRefs.push_back(llvm::Constant::getNullValue(
4178 ObjCTypes.ProtocolListnfABIPtrTy));
4179
4180 std::vector<llvm::Constant*> Values(2);
4181 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4182 Values[1] =
4183 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolListnfABIPtrTy,
4184 ProtocolRefs.size()),
4185 ProtocolRefs);
4186
4187 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4188 GV = new llvm::GlobalVariable(Init->getType(), false,
4189 llvm::GlobalValue::InternalLinkage,
4190 Init,
4191 Name,
4192 &CGM.getModule());
4193 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004194 GV->setAlignment(
4195 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004196 UsedGlobals.push_back(GV);
4197 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
4198}
4199
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004200/// GetMethodDescriptionConstant - This routine build following meta-data:
4201/// struct _objc_method {
4202/// SEL _cmd;
4203/// char *method_type;
4204/// char *_imp;
4205/// }
4206
4207llvm::Constant *
4208CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4209 std::vector<llvm::Constant*> Desc(3);
4210 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4211 ObjCTypes.SelectorPtrTy);
4212 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004213 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004214 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4215 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4216}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004217
4218/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4219/// This code gen. amounts to generating code for:
4220/// @code
4221/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4222/// @encode
4223///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004224LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004225 CodeGen::CodeGenFunction &CGF,
4226 QualType ObjectTy,
4227 llvm::Value *BaseValue,
4228 const ObjCIvarDecl *Ivar,
4229 const FieldDecl *Field,
4230 unsigned CVRQualifiers) {
4231 assert(ObjectTy->isObjCInterfaceType() &&
4232 "CGObjCNonFragileABIMac::EmitObjCValueForIvar");
4233 NamedDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
4234 // NOTE. This name must match one in EmitIvarOffsetVar.
4235 // FIXME. Consolidate into one naming routine.
4236 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
4237 + Ivar->getNameAsString());
4238
4239 llvm::GlobalVariable *IvarOffsetGV =
4240 CGM.getModule().getGlobalVariable(ExternalName);
4241 if (!IvarOffsetGV)
4242 IvarOffsetGV =
4243 new llvm::GlobalVariable(ObjCTypes.LongTy,
4244 false,
4245 llvm::GlobalValue::ExternalLinkage,
4246 0,
4247 ExternalName,
4248 &CGM.getModule());
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004249
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004250 // (char *) BaseValue
4251 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue,
4252 ObjCTypes.Int8PtrTy);
4253 llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV);
4254 // (char*)BaseValue + Offset_symbol
4255 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
4256 // (type *)((char*)BaseValue + Offset_symbol)
4257 const llvm::Type *IvarTy =
4258 CGM.getTypes().ConvertType(Ivar->getType());
4259 llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy);
4260 V = CGF.Builder.CreateBitCast(V, ptrIvarTy);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004261
4262 if (Ivar->isBitField())
4263 return CGF.EmitLValueForBitfield(V, const_cast<FieldDecl *>(Field),
4264 CVRQualifiers);
4265
4266 LValue LV = LValue::MakeAddr(V,
4267 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
4268 LValue::SetObjCIvar(LV, true);
4269 return LV;
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004270}
4271
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004272/* *** */
4273
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004274CodeGen::CGObjCRuntime *
4275CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00004276 return new CGObjCMac(CGM);
4277}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004278
4279CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004280CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004281 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004282}