blob: 37c40eb95907834cae2b7dc8044423514a68798c [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,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000644 ObjCProtocolDecl::protocol_iterator end);
645
646 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
647 QualType ResultType,
648 Selector Sel,
649 llvm::Value *Arg0,
650 QualType Arg0Ty,
651 bool IsSuper,
652 const CallArgList &CallArgs);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000653
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000654public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000655 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000656 // FIXME. All stubs for now!
657 virtual llvm::Function *ModuleInitFunction();
658
659 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
660 QualType ResultType,
661 Selector Sel,
662 llvm::Value *Receiver,
663 bool IsClassMessage,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000664 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000665
666 virtual CodeGen::RValue
667 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
668 QualType ResultType,
669 Selector Sel,
670 const ObjCInterfaceDecl *Class,
671 llvm::Value *Receiver,
672 bool IsClassMessage,
673 const CallArgList &CallArgs){ return RValue::get(0);}
674
675 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
676 const ObjCInterfaceDecl *ID){ return 0; }
677
678 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
679 { return 0; }
680
Fariborz Jahanianeb062d92009-01-26 18:32:24 +0000681 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000682
683 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000684 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +0000685 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000686
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000687 virtual llvm::Function *GetPropertyGetFunction(){ return 0; }
688 virtual llvm::Function *GetPropertySetFunction()
689 { return 0; }
690 virtual llvm::Function *EnumerationMutationFunction()
691 { return 0; }
692
693 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
694 const Stmt &S)
695 { return; }
696 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
697 const ObjCAtThrowStmt &S)
698 { return; }
699 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
700 llvm::Value *AddrWeakObj)
701 { return 0; }
702 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
703 llvm::Value *src, llvm::Value *dst)
704 { return; }
705 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
706 llvm::Value *src, llvm::Value *dest)
707 { return; }
708 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
709 llvm::Value *src, llvm::Value *dest)
710 { return; }
711 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
712 llvm::Value *src, llvm::Value *dest)
713 { return; }
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000714 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
715 QualType ObjectTy,
716 llvm::Value *BaseValue,
717 const ObjCIvarDecl *Ivar,
718 const FieldDecl *Field,
719 unsigned CVRQualifiers);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000720};
721
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000722} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000723
724/* *** Helper Functions *** */
725
726/// getConstantGEP() - Help routine to construct simple GEPs.
727static llvm::Constant *getConstantGEP(llvm::Constant *C,
728 unsigned idx0,
729 unsigned idx1) {
730 llvm::Value *Idxs[] = {
731 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
732 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
733 };
734 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
735}
736
737/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000738
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000739CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
740 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000741{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000742 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000743 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000744}
745
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000746/// GetClass - Return a reference to the class for the given interface
747/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000748llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000749 const ObjCInterfaceDecl *ID) {
750 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000751}
752
753/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000754llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000755 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000756}
757
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000758/// Generate a constant CFString object.
759/*
760 struct __builtin_CFString {
761 const int *isa; // point to __CFConstantStringClassReference
762 int flags;
763 const char *str;
764 long length;
765 };
766*/
767
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000768llvm::Constant *CGObjCCommonMac::GenerateConstantString(
769 const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000770 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000771}
772
773/// Generates a message send where the super is the receiver. This is
774/// a message send to self with special delivery semantics indicating
775/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000776CodeGen::RValue
777CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000778 QualType ResultType,
779 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000780 const ObjCInterfaceDecl *Class,
781 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000782 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000783 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000784 // Create and init a super structure; this is a (receiver, class)
785 // pair we will pass to objc_msgSendSuper.
786 llvm::Value *ObjCSuper =
787 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
788 llvm::Value *ReceiverAsObject =
789 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
790 CGF.Builder.CreateStore(ReceiverAsObject,
791 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000792
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000793 // If this is a class message the metaclass is passed as the target.
794 llvm::Value *Target;
795 if (IsClassMessage) {
796 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
797 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
798 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
799 Target = Super;
800 } else {
801 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
802 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000803 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
804 // and ObjCTypes types.
805 const llvm::Type *ClassTy =
806 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000807 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000808 CGF.Builder.CreateStore(Target,
809 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
810
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000811 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000812 ObjCSuper, ObjCTypes.SuperPtrCTy,
813 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000814}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000815
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000816/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000817CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000818 QualType ResultType,
819 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000820 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000821 bool IsClassMessage,
822 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000823 llvm::Value *Arg0 =
824 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000825 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000826 Arg0, CGF.getContext().getObjCIdType(),
827 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000828}
829
830CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000831 QualType ResultType,
832 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000833 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000834 QualType Arg0Ty,
835 bool IsSuper,
836 const CallArgList &CallArgs) {
837 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000838 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
839 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
840 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000841 CGF.getContext().getObjCSelType()));
842 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000843
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000844 CodeGenTypes &Types = CGM.getTypes();
845 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
846 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbar5669e572008-10-17 03:24:53 +0000847
848 llvm::Constant *Fn;
Daniel Dunbar88b53962009-02-02 22:03:45 +0000849 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000850 Fn = ObjCTypes.getSendStretFn(IsSuper);
851 } else if (ResultType->isFloatingType()) {
852 // FIXME: Sadly, this is wrong. This actually depends on the
853 // architecture. This happens to be right for x86-32 though.
854 Fn = ObjCTypes.getSendFpretFn(IsSuper);
855 } else {
856 Fn = ObjCTypes.getSendFn(IsSuper);
857 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000858 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +0000859 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000860}
861
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000862llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000863 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +0000864 // FIXME: I don't understand why gcc generates this, or where it is
865 // resolved. Investigate. Its also wasteful to look this up over and
866 // over.
867 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
868
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000869 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
870 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000871}
872
Fariborz Jahanianda320092009-01-29 19:24:30 +0000873void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000874 // FIXME: We shouldn't need this, the protocol decl should contain
875 // enough information to tell us whether this was a declaration or a
876 // definition.
877 DefinedProtocols.insert(PD->getIdentifier());
878
879 // If we have generated a forward reference to this protocol, emit
880 // it now. Otherwise do nothing, the protocol objects are lazily
881 // emitted.
882 if (Protocols.count(PD->getIdentifier()))
883 GetOrEmitProtocol(PD);
884}
885
Fariborz Jahanianda320092009-01-29 19:24:30 +0000886llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000887 if (DefinedProtocols.count(PD->getIdentifier()))
888 return GetOrEmitProtocol(PD);
889 return GetOrEmitProtocolRef(PD);
890}
891
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000892/*
893 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
894 struct _objc_protocol {
895 struct _objc_protocol_extension *isa;
896 char *protocol_name;
897 struct _objc_protocol_list *protocol_list;
898 struct _objc__method_prototype_list *instance_methods;
899 struct _objc__method_prototype_list *class_methods
900 };
901
902 See EmitProtocolExtension().
903*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000904llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
905 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
906
907 // Early exit if a defining object has already been generated.
908 if (Entry && Entry->hasInitializer())
909 return Entry;
910
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000911 // FIXME: I don't understand why gcc generates this, or where it is
912 // resolved. Investigate. Its also wasteful to look this up over and
913 // over.
914 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
915
Chris Lattner8ec03f52008-11-24 03:54:41 +0000916 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000917
918 // Construct method lists.
919 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
920 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
921 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
922 e = PD->instmeth_end(); i != e; ++i) {
923 ObjCMethodDecl *MD = *i;
924 llvm::Constant *C = GetMethodDescriptionConstant(MD);
925 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
926 OptInstanceMethods.push_back(C);
927 } else {
928 InstanceMethods.push_back(C);
929 }
930 }
931
932 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
933 e = PD->classmeth_end(); i != e; ++i) {
934 ObjCMethodDecl *MD = *i;
935 llvm::Constant *C = GetMethodDescriptionConstant(MD);
936 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
937 OptClassMethods.push_back(C);
938 } else {
939 ClassMethods.push_back(C);
940 }
941 }
942
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000943 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000944 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000945 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000946 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000947 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +0000948 PD->protocol_begin(),
949 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000950 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000951 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
952 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000953 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
954 InstanceMethods);
955 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000956 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
957 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000958 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
959 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000960 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
961 Values);
962
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000963 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000964 // Already created, fix the linkage and update the initializer.
965 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000966 Entry->setInitializer(Init);
967 } else {
968 Entry =
969 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
970 llvm::GlobalValue::InternalLinkage,
971 Init,
972 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
973 &CGM.getModule());
974 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
975 UsedGlobals.push_back(Entry);
976 // FIXME: Is this necessary? Why only for protocol?
977 Entry->setAlignment(4);
978 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000979
980 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000981}
982
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000983llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000984 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
985
986 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000987 // We use the initializer as a marker of whether this is a forward
988 // reference or not. At module finalization we add the empty
989 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000990 Entry =
991 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000992 llvm::GlobalValue::ExternalLinkage,
993 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000994 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000995 &CGM.getModule());
996 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
997 UsedGlobals.push_back(Entry);
998 // FIXME: Is this necessary? Why only for protocol?
999 Entry->setAlignment(4);
1000 }
1001
1002 return Entry;
1003}
1004
1005/*
1006 struct _objc_protocol_extension {
1007 uint32_t size;
1008 struct objc_method_description_list *optional_instance_methods;
1009 struct objc_method_description_list *optional_class_methods;
1010 struct objc_property_list *instance_properties;
1011 };
1012*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001013llvm::Constant *
1014CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1015 const ConstantVector &OptInstanceMethods,
1016 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001017 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001018 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001019 std::vector<llvm::Constant*> Values(4);
1020 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001021 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001022 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1023 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001024 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1025 OptInstanceMethods);
1026 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001027 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1028 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001029 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1030 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001031 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1032 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001033 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001034
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001035 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001036 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1037 Values[3]->isNullValue())
1038 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1039
1040 llvm::Constant *Init =
1041 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
1042 llvm::GlobalVariable *GV =
1043 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
1044 llvm::GlobalValue::InternalLinkage,
1045 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001046 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001047 &CGM.getModule());
1048 // No special section, but goes in llvm.used
1049 UsedGlobals.push_back(GV);
1050
1051 return GV;
1052}
1053
1054/*
1055 struct objc_protocol_list {
1056 struct objc_protocol_list *next;
1057 long count;
1058 Protocol *list[];
1059 };
1060*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001061llvm::Constant *
1062CGObjCMac::EmitProtocolList(const std::string &Name,
1063 ObjCProtocolDecl::protocol_iterator begin,
1064 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001065 std::vector<llvm::Constant*> ProtocolRefs;
1066
Daniel Dunbardbc933702008-08-21 21:57:41 +00001067 for (; begin != end; ++begin)
1068 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001069
1070 // Just return null for empty protocol lists
1071 if (ProtocolRefs.empty())
1072 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1073
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001074 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001075 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1076
1077 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001078 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001079 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1080 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1081 Values[2] =
1082 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1083 ProtocolRefs.size()),
1084 ProtocolRefs);
1085
1086 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1087 llvm::GlobalVariable *GV =
1088 new llvm::GlobalVariable(Init->getType(), false,
1089 llvm::GlobalValue::InternalLinkage,
1090 Init,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001091 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001092 &CGM.getModule());
1093 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
1094 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1095}
1096
1097/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001098 struct _objc_property {
1099 const char * const name;
1100 const char * const attributes;
1101 };
1102
1103 struct _objc_property_list {
1104 uint32_t entsize; // sizeof (struct _objc_property)
1105 uint32_t prop_count;
1106 struct _objc_property[prop_count];
1107 };
1108*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001109llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1110 const Decl *Container,
1111 const ObjCContainerDecl *OCD,
1112 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001113 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +00001114 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1115 E = OCD->prop_end(); I != E; ++I) {
1116 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001117 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001118 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001119 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1120 Prop));
1121 }
1122
1123 // Return null for empty list.
1124 if (Properties.empty())
1125 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1126
1127 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001128 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001129 std::vector<llvm::Constant*> Values(3);
1130 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1131 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1132 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1133 Properties.size());
1134 Values[2] = llvm::ConstantArray::get(AT, Properties);
1135 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1136
1137 llvm::GlobalVariable *GV =
1138 new llvm::GlobalVariable(Init->getType(), false,
1139 llvm::GlobalValue::InternalLinkage,
1140 Init,
1141 Name,
1142 &CGM.getModule());
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001143 if (ObjCABI == 2)
1144 GV->setSection("__DATA, __objc_const");
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001145 // No special section on property lists?
1146 UsedGlobals.push_back(GV);
1147 return llvm::ConstantExpr::getBitCast(GV,
1148 ObjCTypes.PropertyListPtrTy);
1149
1150}
1151
1152/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001153 struct objc_method_description_list {
1154 int count;
1155 struct objc_method_description list[];
1156 };
1157*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001158llvm::Constant *
1159CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1160 std::vector<llvm::Constant*> Desc(2);
1161 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1162 ObjCTypes.SelectorPtrTy);
1163 Desc[1] = GetMethodVarType(MD);
1164 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1165 Desc);
1166}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001167
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001168llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1169 const char *Section,
1170 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001171 // Return null for empty list.
1172 if (Methods.empty())
1173 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1174
1175 std::vector<llvm::Constant*> Values(2);
1176 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1177 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1178 Methods.size());
1179 Values[1] = llvm::ConstantArray::get(AT, Methods);
1180 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1181
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001182 llvm::GlobalVariable *GV =
1183 new llvm::GlobalVariable(Init->getType(), false,
1184 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001185 Init, Name, &CGM.getModule());
1186 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001187 UsedGlobals.push_back(GV);
1188 return llvm::ConstantExpr::getBitCast(GV,
1189 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001190}
1191
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001192/*
1193 struct _objc_category {
1194 char *category_name;
1195 char *class_name;
1196 struct _objc_method_list *instance_methods;
1197 struct _objc_method_list *class_methods;
1198 struct _objc_protocol_list *protocols;
1199 uint32_t size; // <rdar://4585769>
1200 struct _objc_property_list *instance_properties;
1201 };
1202 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001203void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001204 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001205
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001206 // FIXME: This is poor design, the OCD should have a pointer to the
1207 // category decl. Additionally, note that Category can be null for
1208 // the @implementation w/o an @interface case. Sema should just
1209 // create one for us as it does for @implementation so everyone else
1210 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001211 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001212 const ObjCCategoryDecl *Category =
1213 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001214 std::string ExtName(Interface->getNameAsString() + "_" +
1215 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001216
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001217 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1218 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1219 e = OCD->instmeth_end(); i != e; ++i) {
1220 // Instance methods should always be defined.
1221 InstanceMethods.push_back(GetMethodConstant(*i));
1222 }
1223 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1224 e = OCD->classmeth_end(); i != e; ++i) {
1225 // Class methods should always be defined.
1226 ClassMethods.push_back(GetMethodConstant(*i));
1227 }
1228
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001229 std::vector<llvm::Constant*> Values(7);
1230 Values[0] = GetClassName(OCD->getIdentifier());
1231 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001232 Values[2] =
1233 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1234 ExtName,
1235 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001236 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001237 Values[3] =
1238 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1239 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001240 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001241 if (Category) {
1242 Values[4] =
1243 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1244 Category->protocol_begin(),
1245 Category->protocol_end());
1246 } else {
1247 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1248 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001249 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001250
1251 // If there is no category @interface then there can be no properties.
1252 if (Category) {
1253 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001254 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001255 } else {
1256 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1257 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001258
1259 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1260 Values);
1261
1262 llvm::GlobalVariable *GV =
1263 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1264 llvm::GlobalValue::InternalLinkage,
1265 Init,
1266 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1267 &CGM.getModule());
1268 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1269 UsedGlobals.push_back(GV);
1270 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001271}
1272
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001273// FIXME: Get from somewhere?
1274enum ClassFlags {
1275 eClassFlags_Factory = 0x00001,
1276 eClassFlags_Meta = 0x00002,
1277 // <rdr://5142207>
1278 eClassFlags_HasCXXStructors = 0x02000,
1279 eClassFlags_Hidden = 0x20000,
1280 eClassFlags_ABI2_Hidden = 0x00010,
1281 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1282};
1283
1284// <rdr://5142207&4705298&4843145>
1285static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1286 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1287 // FIXME: Support -fvisibility
1288 switch (attr->getVisibility()) {
1289 default:
1290 assert(0 && "Unknown visibility");
1291 return false;
1292 case VisibilityAttr::DefaultVisibility:
1293 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1294 return false;
1295 case VisibilityAttr::HiddenVisibility:
1296 return true;
1297 }
1298 } else {
1299 return false; // FIXME: Support -fvisibility
1300 }
1301}
1302
1303/*
1304 struct _objc_class {
1305 Class isa;
1306 Class super_class;
1307 const char *name;
1308 long version;
1309 long info;
1310 long instance_size;
1311 struct _objc_ivar_list *ivars;
1312 struct _objc_method_list *methods;
1313 struct _objc_cache *cache;
1314 struct _objc_protocol_list *protocols;
1315 // Objective-C 1.0 extensions (<rdr://4585769>)
1316 const char *ivar_layout;
1317 struct _objc_class_ext *ext;
1318 };
1319
1320 See EmitClassExtension();
1321 */
1322void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001323 DefinedSymbols.insert(ID->getIdentifier());
1324
Chris Lattner8ec03f52008-11-24 03:54:41 +00001325 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001326 // FIXME: Gross
1327 ObjCInterfaceDecl *Interface =
1328 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001329 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001330 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001331 Interface->protocol_begin(),
1332 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001333 const llvm::Type *InterfaceTy =
1334 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
1335 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001336 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001337
1338 // FIXME: Set CXX-structors flag.
1339 if (IsClassHidden(ID->getClassInterface()))
1340 Flags |= eClassFlags_Hidden;
1341
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001342 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1343 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1344 e = ID->instmeth_end(); i != e; ++i) {
1345 // Instance methods should always be defined.
1346 InstanceMethods.push_back(GetMethodConstant(*i));
1347 }
1348 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1349 e = ID->classmeth_end(); i != e; ++i) {
1350 // Class methods should always be defined.
1351 ClassMethods.push_back(GetMethodConstant(*i));
1352 }
1353
1354 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1355 e = ID->propimpl_end(); i != e; ++i) {
1356 ObjCPropertyImplDecl *PID = *i;
1357
1358 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1359 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1360
1361 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1362 if (llvm::Constant *C = GetMethodConstant(MD))
1363 InstanceMethods.push_back(C);
1364 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1365 if (llvm::Constant *C = GetMethodConstant(MD))
1366 InstanceMethods.push_back(C);
1367 }
1368 }
1369
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001370 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001371 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001372 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001373 // Record a reference to the super class.
1374 LazySymbols.insert(Super->getIdentifier());
1375
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001376 Values[ 1] =
1377 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1378 ObjCTypes.ClassPtrTy);
1379 } else {
1380 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1381 }
1382 Values[ 2] = GetClassName(ID->getIdentifier());
1383 // Version is always 0.
1384 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1385 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1386 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001387 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001388 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001389 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001390 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001391 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001392 // cache is always NULL.
1393 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1394 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001395 // FIXME: Set ivar_layout
1396 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001397 Values[11] = EmitClassExtension(ID);
1398 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1399 Values);
1400
1401 llvm::GlobalVariable *GV =
1402 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1403 llvm::GlobalValue::InternalLinkage,
1404 Init,
1405 std::string("\01L_OBJC_CLASS_")+ClassName,
1406 &CGM.getModule());
1407 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1408 UsedGlobals.push_back(GV);
1409 // FIXME: Why?
1410 GV->setAlignment(32);
1411 DefinedClasses.push_back(GV);
1412}
1413
1414llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1415 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001416 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001417 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001418 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001419 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001420
1421 if (IsClassHidden(ID->getClassInterface()))
1422 Flags |= eClassFlags_Hidden;
1423
1424 std::vector<llvm::Constant*> Values(12);
1425 // The isa for the metaclass is the root of the hierarchy.
1426 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1427 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1428 Root = Super;
1429 Values[ 0] =
1430 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1431 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001432 // The super class for the metaclass is emitted as the name of the
1433 // super class. The runtime fixes this up to point to the
1434 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001435 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1436 Values[ 1] =
1437 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1438 ObjCTypes.ClassPtrTy);
1439 } else {
1440 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1441 }
1442 Values[ 2] = GetClassName(ID->getIdentifier());
1443 // Version is always 0.
1444 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1445 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1446 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001447 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001448 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001449 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001450 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001451 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001452 // cache is always NULL.
1453 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1454 Values[ 9] = Protocols;
1455 // ivar_layout for metaclass is always NULL.
1456 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1457 // The class extension is always unused for metaclasses.
1458 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1459 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1460 Values);
1461
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001462 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001463 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001464
1465 // Check for a forward reference.
1466 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1467 if (GV) {
1468 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1469 "Forward metaclass reference has incorrect type.");
1470 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1471 GV->setInitializer(Init);
1472 } else {
1473 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1474 llvm::GlobalValue::InternalLinkage,
1475 Init, Name,
1476 &CGM.getModule());
1477 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001478 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1479 UsedGlobals.push_back(GV);
1480 // FIXME: Why?
1481 GV->setAlignment(32);
1482
1483 return GV;
1484}
1485
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001486llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001487 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001488
1489 // FIXME: Should we look these up somewhere other than the
1490 // module. Its a bit silly since we only generate these while
1491 // processing an implementation, so exactly one pointer would work
1492 // if know when we entered/exitted an implementation block.
1493
1494 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001495 // Previously, metaclass with internal linkage may have been defined.
1496 // pass 'true' as 2nd argument so it is returned.
1497 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001498 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1499 "Forward metaclass reference has incorrect type.");
1500 return GV;
1501 } else {
1502 // Generate as an external reference to keep a consistent
1503 // module. This will be patched up when we emit the metaclass.
1504 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1505 llvm::GlobalValue::ExternalLinkage,
1506 0,
1507 Name,
1508 &CGM.getModule());
1509 }
1510}
1511
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001512/*
1513 struct objc_class_ext {
1514 uint32_t size;
1515 const char *weak_ivar_layout;
1516 struct _objc_property_list *properties;
1517 };
1518*/
1519llvm::Constant *
1520CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1521 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001522 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001523
1524 std::vector<llvm::Constant*> Values(3);
1525 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001526 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001527 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001528 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001529 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001530
1531 // Return null if no extension bits are used.
1532 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1533 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1534
1535 llvm::Constant *Init =
1536 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1537 llvm::GlobalVariable *GV =
1538 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1539 llvm::GlobalValue::InternalLinkage,
1540 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001541 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001542 &CGM.getModule());
1543 // No special section, but goes in llvm.used
1544 UsedGlobals.push_back(GV);
1545
1546 return GV;
1547}
1548
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001549/// countInheritedIvars - count number of ivars in class and its super class(s)
1550///
1551static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1552 int count = 0;
1553 if (!OI)
1554 return 0;
1555 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1556 if (SuperClass)
1557 count += countInheritedIvars(SuperClass);
1558 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1559 E = OI->ivar_end(); I != E; ++I)
1560 ++count;
1561 return count;
1562}
1563
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001564/*
1565 struct objc_ivar {
1566 char *ivar_name;
1567 char *ivar_type;
1568 int ivar_offset;
1569 };
1570
1571 struct objc_ivar_list {
1572 int ivar_count;
1573 struct objc_ivar list[count];
1574 };
1575 */
1576llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001577 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001578 std::vector<llvm::Constant*> Ivars, Ivar(3);
1579
1580 // When emitting the root class GCC emits ivar entries for the
1581 // actual class structure. It is not clear if we need to follow this
1582 // behavior; for now lets try and get away with not doing it. If so,
1583 // the cleanest solution would be to make up an ObjCInterfaceDecl
1584 // for the class.
1585 if (ForClass)
1586 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001587
1588 ObjCInterfaceDecl *OID =
1589 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
1590 const llvm::Type *InterfaceTy =
1591 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001592 const llvm::StructLayout *Layout =
1593 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001594
1595 RecordDecl::field_iterator ifield, pfield;
1596 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001597 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1598 FieldDecl *Field = *ifield;
1599 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1600 getLLVMFieldNo(Field));
1601 if (Field->getIdentifier())
1602 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1603 else
1604 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001605 std::string TypeStr;
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001606 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001607 Ivar[1] = GetMethodVarType(TypeStr);
1608 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001609 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001610 }
1611
1612 // Return null for empty list.
1613 if (Ivars.empty())
1614 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1615
1616 std::vector<llvm::Constant*> Values(2);
1617 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1618 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1619 Ivars.size());
1620 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1621 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1622
1623 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1624 "\01L_OBJC_INSTANCE_VARIABLES_");
1625 llvm::GlobalVariable *GV =
1626 new llvm::GlobalVariable(Init->getType(), false,
1627 llvm::GlobalValue::InternalLinkage,
1628 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001629 Prefix + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001630 &CGM.getModule());
1631 if (ForClass) {
1632 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1633 // FIXME: Why is this only here?
1634 GV->setAlignment(32);
1635 } else {
1636 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1637 }
1638 UsedGlobals.push_back(GV);
1639 return llvm::ConstantExpr::getBitCast(GV,
1640 ObjCTypes.IvarListPtrTy);
1641}
1642
1643/*
1644 struct objc_method {
1645 SEL method_name;
1646 char *method_types;
1647 void *method;
1648 };
1649
1650 struct objc_method_list {
1651 struct objc_method_list *obsolete;
1652 int count;
1653 struct objc_method methods_list[count];
1654 };
1655*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001656
1657/// GetMethodConstant - Return a struct objc_method constant for the
1658/// given method if it has been defined. The result is null if the
1659/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001660llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001661 // FIXME: Use DenseMap::lookup
1662 llvm::Function *Fn = MethodDefinitions[MD];
1663 if (!Fn)
1664 return 0;
1665
1666 std::vector<llvm::Constant*> Method(3);
1667 Method[0] =
1668 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1669 ObjCTypes.SelectorPtrTy);
1670 Method[1] = GetMethodVarType(MD);
1671 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1672 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1673}
1674
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001675llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1676 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001677 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001678 // Return null for empty list.
1679 if (Methods.empty())
1680 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1681
1682 std::vector<llvm::Constant*> Values(3);
1683 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1684 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1685 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1686 Methods.size());
1687 Values[2] = llvm::ConstantArray::get(AT, Methods);
1688 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1689
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001690 llvm::GlobalVariable *GV =
1691 new llvm::GlobalVariable(Init->getType(), false,
1692 llvm::GlobalValue::InternalLinkage,
1693 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001694 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001695 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001696 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001697 UsedGlobals.push_back(GV);
1698 return llvm::ConstantExpr::getBitCast(GV,
1699 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001700}
1701
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001702llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001703 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001704 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001705 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001706
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001707 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001708 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001709 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001710 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001711 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001712 llvm::GlobalValue::InternalLinkage,
1713 Name,
1714 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001715 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001716
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001717 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001718}
1719
1720llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001721 // Abuse this interface function as a place to finalize.
1722 FinishModule();
1723
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001724 return NULL;
1725}
1726
Daniel Dunbar49f66022008-09-24 03:38:44 +00001727llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1728 return ObjCTypes.GetPropertyFn;
1729}
1730
1731llvm::Function *CGObjCMac::GetPropertySetFunction() {
1732 return ObjCTypes.SetPropertyFn;
1733}
1734
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001735llvm::Function *CGObjCMac::EnumerationMutationFunction()
1736{
1737 return ObjCTypes.EnumerationMutationFn;
1738}
1739
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001740/*
1741
1742Objective-C setjmp-longjmp (sjlj) Exception Handling
1743--
1744
1745The basic framework for a @try-catch-finally is as follows:
1746{
1747 objc_exception_data d;
1748 id _rethrow = null;
1749
1750 objc_exception_try_enter(&d);
1751 if (!setjmp(d.jmp_buf)) {
1752 ... try body ...
1753 } else {
1754 // exception path
1755 id _caught = objc_exception_extract(&d);
1756
1757 // enter new try scope for handlers
1758 if (!setjmp(d.jmp_buf)) {
1759 ... match exception and execute catch blocks ...
1760
1761 // fell off end, rethrow.
1762 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001763 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001764 } else {
1765 // exception in catch block
1766 _rethrow = objc_exception_extract(&d);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001767 ... jump-through-finally_no_exit to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001768 }
1769 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001770 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001771
1772finally:
1773 // match either the initial try_enter or the catch try_enter,
1774 // depending on the path followed.
1775 objc_exception_try_exit(&d);
1776finally_no_exit:
1777 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001778 ... dispatch to finally destination ...
1779
1780finally_rethrow:
1781 objc_exception_throw(_rethrow);
1782
1783finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001784}
1785
1786This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001787uses _rethrow to determine if objc_exception_try_exit should be called
1788and if the object should be rethrown. This breaks in the face of
1789throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001790
1791We specialize this framework for a few particular circumstances:
1792
1793 - If there are no catch blocks, then we avoid emitting the second
1794 exception handling context.
1795
1796 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1797 e)) we avoid emitting the code to rethrow an uncaught exception.
1798
1799 - FIXME: If there is no @finally block we can do a few more
1800 simplifications.
1801
1802Rethrows and Jumps-Through-Finally
1803--
1804
1805Support for implicit rethrows and jumping through the finally block is
1806handled by storing the current exception-handling context in
1807ObjCEHStack.
1808
Daniel Dunbar898d5082008-09-30 01:06:03 +00001809In order to implement proper @finally semantics, we support one basic
1810mechanism for jumping through the finally block to an arbitrary
1811destination. Constructs which generate exits from a @try or @catch
1812block use this mechanism to implement the proper semantics by chaining
1813jumps, as necessary.
1814
1815This mechanism works like the one used for indirect goto: we
1816arbitrarily assign an ID to each destination and store the ID for the
1817destination in a variable prior to entering the finally block. At the
1818end of the finally block we simply create a switch to the proper
1819destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001820
1821Code gen for @synchronized(expr) stmt;
1822Effectively generating code for:
1823objc_sync_enter(expr);
1824@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001825*/
1826
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001827void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1828 const Stmt &S) {
1829 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001830 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001831 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
1832 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1833 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1834 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001835 llvm::Value *DestCode =
1836 CGF.CreateTempAlloca(llvm::Type::Int32Ty, "finally.dst");
1837
1838 // Generate jump code. Done here so we can directly add things to
1839 // the switch instruction.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001840 llvm::BasicBlock *FinallyJump = CGF.createBasicBlock("finally.jump");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001841 llvm::SwitchInst *FinallySwitch =
1842 llvm::SwitchInst::Create(new llvm::LoadInst(DestCode, "", FinallyJump),
1843 FinallyEnd, 10, FinallyJump);
1844
1845 // Push an EH context entry, used for handling rethrows and jumps
1846 // through finally.
1847 CodeGenFunction::ObjCEHEntry EHEntry(FinallyBlock, FinallyNoExit,
1848 FinallySwitch, DestCode);
1849 CGF.ObjCEHStack.push_back(&EHEntry);
1850
1851 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00001852 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1853 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001854 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1855 "_rethrow");
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001856 if (!isTry) {
1857 // For @synchronized, call objc_sync_enter(sync.expr)
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001858 llvm::Value *Arg = CGF.EmitScalarExpr(
1859 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1860 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1861 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001862 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001863
1864 // Enter a new try block and call setjmp.
1865 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1866 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1867 "jmpbufarray");
1868 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1869 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1870 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001871
Daniel Dunbar55e87422008-11-11 02:29:29 +00001872 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1873 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001874 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001875 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001876
1877 // Emit the @try block.
1878 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001879 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1880 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001881 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001882
1883 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001884 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001885
1886 // Retrieve the exception object. We may emit multiple blocks but
1887 // nothing can cross this so the value is already in SSA form.
1888 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1889 ExceptionData,
1890 "caught");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001891 EHEntry.Exception = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001892 if (!isTry)
1893 {
1894 CGF.Builder.CreateStore(Caught, RethrowPtr);
1895 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
1896 }
1897 else if (const ObjCAtCatchStmt* CatchStmt =
1898 cast<ObjCAtTryStmt>(S).getCatchStmts())
1899 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00001900 // Enter a new exception try block (in case a @catch block throws
1901 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00001902 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001903
Anders Carlsson80f25672008-09-09 17:59:25 +00001904 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1905 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001906 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00001907
Daniel Dunbar55e87422008-11-11 02:29:29 +00001908 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1909 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001910 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001911
1912 CGF.EmitBlock(CatchBlock);
1913
Daniel Dunbar55e40722008-09-27 07:03:52 +00001914 // Handle catch list. As a special case we check if everything is
1915 // matched and avoid generating code for falling off the end if
1916 // so.
1917 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00001918 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00001919 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00001920
Anders Carlssondde0a942008-09-11 09:15:33 +00001921 const DeclStmt *CatchParam =
1922 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001923 const VarDecl *VD = 0;
1924 const PointerType *PT = 0;
1925
Anders Carlsson80f25672008-09-09 17:59:25 +00001926 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00001927 if (!CatchParam) {
1928 AllMatched = true;
1929 } else {
Ted Kremenekde3b8fb2008-10-06 20:58:56 +00001930 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001931 PT = VD->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001932
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001933 // catch(id e) always matches.
1934 // FIXME: For the time being we also match id<X>; this should
1935 // be rejected by Sema instead.
1936 if ((PT && CGF.getContext().isObjCIdType(PT->getPointeeType())) ||
1937 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00001938 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00001939 }
1940
Daniel Dunbar55e40722008-09-27 07:03:52 +00001941 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00001942 if (CatchParam) {
1943 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001944 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar129271a2008-09-27 07:36:24 +00001945 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001946 }
Anders Carlsson1452f552008-09-11 08:21:54 +00001947
Anders Carlssondde0a942008-09-11 09:15:33 +00001948 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001949 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001950 break;
1951 }
1952
Daniel Dunbar129271a2008-09-27 07:36:24 +00001953 assert(PT && "Unexpected non-pointer type in @catch");
1954 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00001955 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001956 assert(ObjCType && "Catch parameter must have Objective-C type!");
1957
1958 // Check if the @catch block matches the exception object.
1959 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
1960
Anders Carlsson80f25672008-09-09 17:59:25 +00001961 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
1962 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00001963
Daniel Dunbar55e87422008-11-11 02:29:29 +00001964 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00001965
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001966 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001967 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001968
1969 // Emit the @catch block.
1970 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001971 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001972 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001973
1974 llvm::Value *Tmp =
1975 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
1976 "tmp");
1977 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001978
1979 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001980 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001981
1982 CGF.EmitBlock(NextCatchBlock);
1983 }
1984
Daniel Dunbar55e40722008-09-27 07:03:52 +00001985 if (!AllMatched) {
1986 // None of the handlers caught the exception, so store it to be
1987 // rethrown at the end of the @finally block.
1988 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001989 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001990 }
1991
1992 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001993 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001994 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1995 ExceptionData),
1996 RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001997 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001998 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00001999 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002000 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Anders Carlsson80f25672008-09-09 17:59:25 +00002001 }
2002
Daniel Dunbar898d5082008-09-30 01:06:03 +00002003 // Pop the exception-handling stack entry. It is important to do
2004 // this now, because the code in the @finally block is not in this
2005 // context.
2006 CGF.ObjCEHStack.pop_back();
2007
Anders Carlsson80f25672008-09-09 17:59:25 +00002008 // Emit the @finally block.
2009 CGF.EmitBlock(FinallyBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002010 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002011
2012 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002013 if (isTry) {
2014 if (const ObjCAtFinallyStmt* FinallyStmt =
2015 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2016 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2017 }
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002018 else {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002019 // For @synchronized objc_sync_exit(expr); As finally's sole statement.
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002020 // For @synchronized, call objc_sync_enter(sync.expr)
2021 llvm::Value *Arg = CGF.EmitScalarExpr(
2022 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2023 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
2024 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg);
2025 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002026
Daniel Dunbar898d5082008-09-30 01:06:03 +00002027 CGF.EmitBlock(FinallyJump);
2028
2029 CGF.EmitBlock(FinallyRethrow);
2030 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
2031 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002032 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002033
2034 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002035}
2036
2037void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002038 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002039 llvm::Value *ExceptionAsObject;
2040
2041 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2042 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2043 ExceptionAsObject =
2044 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2045 } else {
Daniel Dunbar898d5082008-09-30 01:06:03 +00002046 assert((!CGF.ObjCEHStack.empty() && CGF.ObjCEHStack.back()->Exception) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002047 "Unexpected rethrow outside @catch block.");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002048 ExceptionAsObject = CGF.ObjCEHStack.back()->Exception;
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002049 }
2050
2051 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002052 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002053
2054 // Clear the insertion point to indicate we are in unreachable code.
2055 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002056}
2057
Daniel Dunbar898d5082008-09-30 01:06:03 +00002058void CodeGenFunction::EmitJumpThroughFinally(ObjCEHEntry *E,
2059 llvm::BasicBlock *Dst,
2060 bool ExecuteTryExit) {
Daniel Dunbara448fb22008-11-11 23:11:34 +00002061 if (!HaveInsertPoint())
Daniel Dunbar898d5082008-09-30 01:06:03 +00002062 return;
2063
2064 // Find the destination code for this block. We always use 0 for the
2065 // fallthrough block (default destination).
2066 llvm::SwitchInst *SI = E->FinallySwitch;
2067 llvm::ConstantInt *ID;
2068 if (Dst == SI->getDefaultDest()) {
2069 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
2070 } else {
2071 ID = SI->findCaseDest(Dst);
2072 if (!ID) {
2073 // No code found, get a new unique one by just using the number
2074 // of switch successors.
2075 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, SI->getNumSuccessors());
2076 SI->addCase(ID, Dst);
2077 }
2078 }
2079
2080 // Set the destination code and branch.
2081 Builder.CreateStore(ID, E->DestCode);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002082 EmitBranch(ExecuteTryExit ? E->FinallyBlock : E->FinallyNoExit);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002083}
2084
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002085/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002086/// object: objc_read_weak (id *src)
2087///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002088llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002089 llvm::Value *AddrWeakObj)
2090{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002091 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002092 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002093 AddrWeakObj, "weakread");
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002094 return read_weak;
2095}
2096
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002097/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2098/// objc_assign_weak (id src, id *dst)
2099///
2100void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2101 llvm::Value *src, llvm::Value *dst)
2102{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002103 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2104 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002105 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2106 src, dst, "weakassign");
2107 return;
2108}
2109
Fariborz Jahanian58626502008-11-19 00:59:10 +00002110/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2111/// objc_assign_global (id src, id *dst)
2112///
2113void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2114 llvm::Value *src, llvm::Value *dst)
2115{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002116 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2117 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002118 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2119 src, dst, "globalassign");
2120 return;
2121}
2122
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002123/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2124/// objc_assign_ivar (id src, id *dst)
2125///
2126void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2127 llvm::Value *src, llvm::Value *dst)
2128{
2129 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2130 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2131 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2132 src, dst, "assignivar");
2133 return;
2134}
2135
Fariborz Jahanian58626502008-11-19 00:59:10 +00002136/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2137/// objc_assign_strongCast (id src, id *dst)
2138///
2139void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2140 llvm::Value *src, llvm::Value *dst)
2141{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002142 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2143 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002144 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2145 src, dst, "weakassign");
2146 return;
2147}
2148
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002149/// EmitObjCValueForIvar - Code Gen for ivar reference.
2150///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002151LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2152 QualType ObjectTy,
2153 llvm::Value *BaseValue,
2154 const ObjCIvarDecl *Ivar,
2155 const FieldDecl *Field,
2156 unsigned CVRQualifiers) {
2157 if (Ivar->isBitField())
2158 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
2159 CVRQualifiers);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002160 // TODO: Add a special case for isa (index 0)
2161 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
2162 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002163 LValue LV = LValue::MakeAddr(V,
2164 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
2165 LValue::SetObjCIvar(LV, true);
2166 return LV;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002167}
2168
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002169/* *** Private Interface *** */
2170
2171/// EmitImageInfo - Emit the image info marker used to encode some module
2172/// level information.
2173///
2174/// See: <rdr://4810609&4810587&4810587>
2175/// struct IMAGE_INFO {
2176/// unsigned version;
2177/// unsigned flags;
2178/// };
2179enum ImageInfoFlags {
2180 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2181 eImageInfo_GarbageCollected = (1 << 1),
2182 eImageInfo_GCOnly = (1 << 2)
2183};
2184
2185void CGObjCMac::EmitImageInfo() {
2186 unsigned version = 0; // Version is unused?
2187 unsigned flags = 0;
2188
2189 // FIXME: Fix and continue?
2190 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2191 flags |= eImageInfo_GarbageCollected;
2192 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2193 flags |= eImageInfo_GCOnly;
2194
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002195 // Emitted as int[2];
2196 llvm::Constant *values[2] = {
2197 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2198 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2199 };
2200 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002201 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002202 new llvm::GlobalVariable(AT, true,
2203 llvm::GlobalValue::InternalLinkage,
2204 llvm::ConstantArray::get(AT, values, 2),
2205 "\01L_OBJC_IMAGE_INFO",
2206 &CGM.getModule());
2207
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002208 if (ObjCABI == 1) {
2209 GV->setSection("__OBJC, __image_info,regular");
2210 } else {
2211 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2212 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002213
2214 UsedGlobals.push_back(GV);
2215}
2216
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002217
2218// struct objc_module {
2219// unsigned long version;
2220// unsigned long size;
2221// const char *name;
2222// Symtab symtab;
2223// };
2224
2225// FIXME: Get from somewhere
2226static const int ModuleVersion = 7;
2227
2228void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002229 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002230
2231 std::vector<llvm::Constant*> Values(4);
2232 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2233 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002234 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002235 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002236 Values[3] = EmitModuleSymbols();
2237
2238 llvm::GlobalVariable *GV =
2239 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2240 llvm::GlobalValue::InternalLinkage,
2241 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2242 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002243 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002244 &CGM.getModule());
2245 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2246 UsedGlobals.push_back(GV);
2247}
2248
2249llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002250 unsigned NumClasses = DefinedClasses.size();
2251 unsigned NumCategories = DefinedCategories.size();
2252
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002253 // Return null if no symbols were defined.
2254 if (!NumClasses && !NumCategories)
2255 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2256
2257 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002258 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2259 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2260 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2261 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2262
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002263 // The runtime expects exactly the list of defined classes followed
2264 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002265 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002266 for (unsigned i=0; i<NumClasses; i++)
2267 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2268 ObjCTypes.Int8PtrTy);
2269 for (unsigned i=0; i<NumCategories; i++)
2270 Symbols[NumClasses + i] =
2271 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2272 ObjCTypes.Int8PtrTy);
2273
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002274 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002275 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002276 NumClasses + NumCategories),
2277 Symbols);
2278
2279 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2280
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002281 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002282 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002283 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002284 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002285 "\01L_OBJC_SYMBOLS",
2286 &CGM.getModule());
2287 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2288 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002289 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2290}
2291
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002292llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002293 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002294 LazySymbols.insert(ID->getIdentifier());
2295
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002296 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2297
2298 if (!Entry) {
2299 llvm::Constant *Casted =
2300 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2301 ObjCTypes.ClassPtrTy);
2302 Entry =
2303 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2304 llvm::GlobalValue::InternalLinkage,
2305 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2306 &CGM.getModule());
2307 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2308 UsedGlobals.push_back(Entry);
2309 }
2310
2311 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002312}
2313
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002314llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002315 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2316
2317 if (!Entry) {
2318 llvm::Constant *Casted =
2319 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2320 ObjCTypes.SelectorPtrTy);
2321 Entry =
2322 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2323 llvm::GlobalValue::InternalLinkage,
2324 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2325 &CGM.getModule());
2326 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2327 UsedGlobals.push_back(Entry);
2328 }
2329
2330 return Builder.CreateLoad(Entry, false, "tmp");
2331}
2332
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002333llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002334 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002335
2336 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002337 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002338 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002339 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002340 llvm::GlobalValue::InternalLinkage,
2341 C, "\01L_OBJC_CLASS_NAME_",
2342 &CGM.getModule());
2343 Entry->setSection("__TEXT,__cstring,cstring_literals");
2344 UsedGlobals.push_back(Entry);
2345 }
2346
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002347 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002348}
2349
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002350llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002351 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2352
2353 if (!Entry) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00002354 // FIXME: Avoid std::string copying.
2355 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002356 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002357 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002358 llvm::GlobalValue::InternalLinkage,
2359 C, "\01L_OBJC_METH_VAR_NAME_",
2360 &CGM.getModule());
2361 Entry->setSection("__TEXT,__cstring,cstring_literals");
2362 UsedGlobals.push_back(Entry);
2363 }
2364
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002365 return getConstantGEP(Entry, 0, 0);
2366}
2367
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002368// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002369llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002370 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2371}
2372
2373// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002374llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002375 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2376}
2377
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002378llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002379 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002380
2381 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002382 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002383 Entry =
2384 new llvm::GlobalVariable(C->getType(), false,
2385 llvm::GlobalValue::InternalLinkage,
2386 C, "\01L_OBJC_METH_VAR_TYPE_",
2387 &CGM.getModule());
2388 Entry->setSection("__TEXT,__cstring,cstring_literals");
2389 UsedGlobals.push_back(Entry);
2390 }
2391
2392 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002393}
2394
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002395// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002396llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002397 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002398 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2399 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002400 return GetMethodVarType(TypeStr);
2401}
2402
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002403// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002404llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002405 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2406
2407 if (!Entry) {
2408 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2409 Entry =
2410 new llvm::GlobalVariable(C->getType(), false,
2411 llvm::GlobalValue::InternalLinkage,
2412 C, "\01L_OBJC_PROP_NAME_ATTR_",
2413 &CGM.getModule());
2414 Entry->setSection("__TEXT,__cstring,cstring_literals");
2415 UsedGlobals.push_back(Entry);
2416 }
2417
2418 return getConstantGEP(Entry, 0, 0);
2419}
2420
2421// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002422// FIXME: This Decl should be more precise.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002423llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002424 const Decl *Container) {
2425 std::string TypeStr;
2426 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002427 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2428}
2429
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002430void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2431 const ObjCContainerDecl *CD,
2432 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002433 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002434 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002435 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002436 assert (CD && "Missing container decl in GetNameForMethod");
2437 NameOut += CD->getNameAsString();
Fariborz Jahanian52847332009-01-26 23:49:05 +00002438 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
2439 // Right now! there is not enough info. to do this.
Chris Lattner077bf5e2008-11-24 03:33:13 +00002440 NameOut += ' ';
2441 NameOut += D->getSelector().getAsString();
2442 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002443}
2444
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002445/// GetFirstIvarInRecord - This routine returns the record for the
2446/// implementation of the fiven class OID. It also returns field
2447/// corresponding to the first ivar in the class in FIV. It also
2448/// returns the one before the first ivar.
2449///
2450const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord(
2451 const ObjCInterfaceDecl *OID,
2452 RecordDecl::field_iterator &FIV,
2453 RecordDecl::field_iterator &PIV) {
2454 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
2455 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
2456 RecordDecl::field_iterator ifield = RD->field_begin();
2457 RecordDecl::field_iterator pfield = RD->field_end();
2458 while (countSuperClassIvars-- > 0) {
2459 pfield = ifield;
2460 ++ifield;
2461 }
2462 FIV = ifield;
2463 PIV = pfield;
2464 return RD;
2465}
2466
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002467void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002468 EmitModuleInfo();
2469
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002470 // Emit the dummy bodies for any protocols which were referenced but
2471 // never defined.
2472 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2473 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2474 if (i->second->hasInitializer())
2475 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002476
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002477 std::vector<llvm::Constant*> Values(5);
2478 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2479 Values[1] = GetClassName(i->first);
2480 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2481 Values[3] = Values[4] =
2482 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2483 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2484 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2485 Values));
2486 }
2487
2488 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002489 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002490 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002491 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002492 }
2493
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002494 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002495 llvm::GlobalValue *GV =
2496 new llvm::GlobalVariable(AT, false,
2497 llvm::GlobalValue::AppendingLinkage,
2498 llvm::ConstantArray::get(AT, Used),
2499 "llvm.used",
2500 &CGM.getModule());
2501
2502 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002503
2504 // Add assembler directives to add lazy undefined symbol references
2505 // for classes which are referenced but not defined. This is
2506 // important for correct linker interaction.
2507
2508 // FIXME: Uh, this isn't particularly portable.
2509 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00002510
2511 if (!CGM.getModule().getModuleInlineAsm().empty())
2512 s << "\n";
2513
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002514 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2515 e = LazySymbols.end(); i != e; ++i) {
2516 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2517 }
2518 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2519 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002520 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002521 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2522 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00002523
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002524 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002525}
2526
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002527CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002528 : CGObjCCommonMac(cgm),
2529 ObjCTypes(cgm)
2530{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002531 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002532 ObjCABI = 2;
2533}
2534
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002535/* *** */
2536
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002537ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2538: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002539{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002540 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2541 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002542
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002543 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002544 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002545 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002546 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2547
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002548 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002549 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002550 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002551
2552 // FIXME: It would be nice to unify this with the opaque type, so
2553 // that the IR comes out a bit cleaner.
2554 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2555 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002556
2557 // I'm not sure I like this. The implicit coordination is a bit
2558 // gross. We should solve this in a reasonable fashion because this
2559 // is a pretty common task (match some runtime data structure with
2560 // an LLVM data structure).
2561
2562 // FIXME: This is leaked.
2563 // FIXME: Merge with rewriter code?
2564
2565 // struct _objc_super {
2566 // id self;
2567 // Class cls;
2568 // }
2569 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2570 SourceLocation(),
2571 &Ctx.Idents.get("_objc_super"));
2572 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2573 Ctx.getObjCIdType(), 0, false));
2574 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2575 Ctx.getObjCClassType(), 0, false));
2576 RD->completeDefinition(Ctx);
2577
2578 SuperCTy = Ctx.getTagDeclType(RD);
2579 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2580
2581 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002582 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2583
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002584 // struct _prop_t {
2585 // char *name;
2586 // char *attributes;
2587 // }
2588 PropertyTy = llvm::StructType::get(Int8PtrTy,
2589 Int8PtrTy,
2590 NULL);
2591 CGM.getModule().addTypeName("struct._prop_t",
2592 PropertyTy);
2593
2594 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002595 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002596 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002597 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002598 // }
2599 PropertyListTy = llvm::StructType::get(IntTy,
2600 IntTy,
2601 llvm::ArrayType::get(PropertyTy, 0),
2602 NULL);
2603 CGM.getModule().addTypeName("struct._prop_list_t",
2604 PropertyListTy);
2605 // struct _prop_list_t *
2606 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2607
2608 // struct _objc_method {
2609 // SEL _cmd;
2610 // char *method_type;
2611 // char *_imp;
2612 // }
2613 MethodTy = llvm::StructType::get(SelectorPtrTy,
2614 Int8PtrTy,
2615 Int8PtrTy,
2616 NULL);
2617 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002618
2619 // struct _objc_cache *
2620 CacheTy = llvm::OpaqueType::get();
2621 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2622 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002623
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002624 // Property manipulation functions.
2625
2626 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
2627 std::vector<const llvm::Type*> Params;
2628 Params.push_back(ObjectPtrTy);
2629 Params.push_back(SelectorPtrTy);
2630 Params.push_back(LongTy);
2631 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2632 GetPropertyFn =
2633 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2634 Params,
2635 false),
2636 "objc_getProperty");
2637
2638 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2639 Params.clear();
2640 Params.push_back(ObjectPtrTy);
2641 Params.push_back(SelectorPtrTy);
2642 Params.push_back(LongTy);
2643 Params.push_back(ObjectPtrTy);
2644 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2645 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2646 SetPropertyFn =
2647 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2648 Params,
2649 false),
2650 "objc_setProperty");
2651 // Enumeration mutation.
2652
2653 Params.clear();
2654 Params.push_back(ObjectPtrTy);
2655 EnumerationMutationFn =
2656 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2657 Params,
2658 false),
2659 "objc_enumerationMutation");
2660
2661 // gc's API
2662 // id objc_read_weak (id *)
2663 Params.clear();
2664 Params.push_back(PtrObjectPtrTy);
2665 GcReadWeakFn =
2666 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2667 Params,
2668 false),
2669 "objc_read_weak");
2670 // id objc_assign_weak (id, id *)
2671 Params.clear();
2672 Params.push_back(ObjectPtrTy);
2673 Params.push_back(PtrObjectPtrTy);
2674 GcAssignWeakFn =
2675 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2676 Params,
2677 false),
2678 "objc_assign_weak");
2679 GcAssignGlobalFn =
2680 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2681 Params,
2682 false),
2683 "objc_assign_global");
2684 GcAssignIvarFn =
2685 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2686 Params,
2687 false),
2688 "objc_assign_ivar");
2689 GcAssignStrongCastFn =
2690 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2691 Params,
2692 false),
2693 "objc_assign_strongCast");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002694}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002695
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002696ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2697 : ObjCCommonTypesHelper(cgm)
2698{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002699 // struct _objc_method_description {
2700 // SEL name;
2701 // char *types;
2702 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002703 MethodDescriptionTy =
2704 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002705 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002706 NULL);
2707 CGM.getModule().addTypeName("struct._objc_method_description",
2708 MethodDescriptionTy);
2709
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002710 // struct _objc_method_description_list {
2711 // int count;
2712 // struct _objc_method_description[1];
2713 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002714 MethodDescriptionListTy =
2715 llvm::StructType::get(IntTy,
2716 llvm::ArrayType::get(MethodDescriptionTy, 0),
2717 NULL);
2718 CGM.getModule().addTypeName("struct._objc_method_description_list",
2719 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002720
2721 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002722 MethodDescriptionListPtrTy =
2723 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2724
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002725 // Protocol description structures
2726
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002727 // struct _objc_protocol_extension {
2728 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2729 // struct _objc_method_description_list *optional_instance_methods;
2730 // struct _objc_method_description_list *optional_class_methods;
2731 // struct _objc_property_list *instance_properties;
2732 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002733 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002734 llvm::StructType::get(IntTy,
2735 MethodDescriptionListPtrTy,
2736 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002737 PropertyListPtrTy,
2738 NULL);
2739 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2740 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002741
2742 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002743 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2744
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002745 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002746
2747 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2748 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2749
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002750 const llvm::Type *T =
2751 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2752 LongTy,
2753 llvm::ArrayType::get(ProtocolTyHolder, 0),
2754 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002755 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2756
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002757 // struct _objc_protocol {
2758 // struct _objc_protocol_extension *isa;
2759 // char *protocol_name;
2760 // struct _objc_protocol **_objc_protocol_list;
2761 // struct _objc_method_description_list *instance_methods;
2762 // struct _objc_method_description_list *class_methods;
2763 // }
2764 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002765 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002766 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2767 MethodDescriptionListPtrTy,
2768 MethodDescriptionListPtrTy,
2769 NULL);
2770 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2771
2772 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2773 CGM.getModule().addTypeName("struct._objc_protocol_list",
2774 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002775 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002776 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2777
2778 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002779 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002780 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002781
2782 // Class description structures
2783
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002784 // struct _objc_ivar {
2785 // char *ivar_name;
2786 // char *ivar_type;
2787 // int ivar_offset;
2788 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002789 IvarTy = llvm::StructType::get(Int8PtrTy,
2790 Int8PtrTy,
2791 IntTy,
2792 NULL);
2793 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2794
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002795 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002796 IvarListTy = llvm::OpaqueType::get();
2797 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2798 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2799
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002800 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002801 MethodListTy = llvm::OpaqueType::get();
2802 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2803 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2804
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002805 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002806 ClassExtensionTy =
2807 llvm::StructType::get(IntTy,
2808 Int8PtrTy,
2809 PropertyListPtrTy,
2810 NULL);
2811 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2812 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2813
2814 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2815
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002816 // struct _objc_class {
2817 // Class isa;
2818 // Class super_class;
2819 // char *name;
2820 // long version;
2821 // long info;
2822 // long instance_size;
2823 // struct _objc_ivar_list *ivars;
2824 // struct _objc_method_list *methods;
2825 // struct _objc_cache *cache;
2826 // struct _objc_protocol_list *protocols;
2827 // char *ivar_layout;
2828 // struct _objc_class_ext *ext;
2829 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002830 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2831 llvm::PointerType::getUnqual(ClassTyHolder),
2832 Int8PtrTy,
2833 LongTy,
2834 LongTy,
2835 LongTy,
2836 IvarListPtrTy,
2837 MethodListPtrTy,
2838 CachePtrTy,
2839 ProtocolListPtrTy,
2840 Int8PtrTy,
2841 ClassExtensionPtrTy,
2842 NULL);
2843 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2844
2845 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2846 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2847 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2848
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002849 // struct _objc_category {
2850 // char *category_name;
2851 // char *class_name;
2852 // struct _objc_method_list *instance_method;
2853 // struct _objc_method_list *class_method;
2854 // uint32_t size; // sizeof(struct _objc_category)
2855 // struct _objc_property_list *instance_properties;// category's @property
2856 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002857 CategoryTy = llvm::StructType::get(Int8PtrTy,
2858 Int8PtrTy,
2859 MethodListPtrTy,
2860 MethodListPtrTy,
2861 ProtocolListPtrTy,
2862 IntTy,
2863 PropertyListPtrTy,
2864 NULL);
2865 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2866
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002867 // Global metadata structures
2868
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002869 // struct _objc_symtab {
2870 // long sel_ref_cnt;
2871 // SEL *refs;
2872 // short cls_def_cnt;
2873 // short cat_def_cnt;
2874 // char *defs[cls_def_cnt + cat_def_cnt];
2875 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002876 SymtabTy = llvm::StructType::get(LongTy,
2877 SelectorPtrTy,
2878 ShortTy,
2879 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002880 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002881 NULL);
2882 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2883 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2884
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002885 // struct _objc_module {
2886 // long version;
2887 // long size; // sizeof(struct _objc_module)
2888 // char *name;
2889 // struct _objc_symtab* symtab;
2890 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002891 ModuleTy =
2892 llvm::StructType::get(LongTy,
2893 LongTy,
2894 Int8PtrTy,
2895 SymtabPtrTy,
2896 NULL);
2897 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002898
Daniel Dunbar49f66022008-09-24 03:38:44 +00002899 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002900
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002901 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002902 std::vector<const llvm::Type*> Params;
2903 Params.push_back(ObjectPtrTy);
2904 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002905 MessageSendFn =
2906 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2907 Params,
2908 true),
2909 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002910
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002911 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002912 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002913 Params.push_back(ObjectPtrTy);
2914 Params.push_back(SelectorPtrTy);
2915 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002916 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2917 Params,
2918 true),
2919 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002920
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002921 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00002922 Params.clear();
2923 Params.push_back(ObjectPtrTy);
2924 Params.push_back(SelectorPtrTy);
2925 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002926 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00002927 MessageSendFpretFn =
2928 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
2929 Params,
2930 true),
2931 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002932
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002933 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002934 Params.clear();
2935 Params.push_back(SuperPtrTy);
2936 Params.push_back(SelectorPtrTy);
2937 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002938 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2939 Params,
2940 true),
2941 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002942
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002943 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
2944 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002945 Params.clear();
2946 Params.push_back(Int8PtrTy);
2947 Params.push_back(SuperPtrTy);
2948 Params.push_back(SelectorPtrTy);
2949 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002950 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2951 Params,
2952 true),
2953 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002954
2955 // There is no objc_msgSendSuper_fpret? How can that work?
2956 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002957
Anders Carlsson124526b2008-09-09 10:10:21 +00002958 // FIXME: This is the size of the setjmp buffer and should be
2959 // target specific. 18 is what's used on 32-bit X86.
2960 uint64_t SetJmpBufferSize = 18;
2961
2962 // Exceptions
2963 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00002964 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00002965
2966 ExceptionDataTy =
2967 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
2968 SetJmpBufferSize),
2969 StackPtrTy, NULL);
2970 CGM.getModule().addTypeName("struct._objc_exception_data",
2971 ExceptionDataTy);
2972
2973 Params.clear();
2974 Params.push_back(ObjectPtrTy);
2975 ExceptionThrowFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002976 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2977 Params,
2978 false),
2979 "objc_exception_throw");
Anders Carlsson124526b2008-09-09 10:10:21 +00002980
2981 Params.clear();
2982 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
2983 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002984 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2985 Params,
2986 false),
2987 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00002988 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002989 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2990 Params,
2991 false),
2992 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00002993 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002994 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2995 Params,
2996 false),
2997 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00002998
2999 Params.clear();
3000 Params.push_back(ClassPtrTy);
3001 Params.push_back(ObjectPtrTy);
3002 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003003 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3004 Params,
3005 false),
3006 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00003007
3008 // synchronized APIs
3009 // void objc_sync_enter (id)
3010 Params.clear();
3011 Params.push_back(ObjectPtrTy);
3012 SyncEnterFn =
3013 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3014 Params,
3015 false),
3016 "objc_sync_enter");
3017 // void objc_sync_exit (id)
3018 SyncExitFn =
3019 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3020 Params,
3021 false),
3022 "objc_sync_exit");
3023
Anders Carlsson124526b2008-09-09 10:10:21 +00003024
3025 Params.clear();
3026 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
3027 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003028 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3029 Params,
3030 false),
3031 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003032
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003033}
3034
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003035ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003036: ObjCCommonTypesHelper(cgm)
3037{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003038 // struct _method_list_t {
3039 // uint32_t entsize; // sizeof(struct _objc_method)
3040 // uint32_t method_count;
3041 // struct _objc_method method_list[method_count];
3042 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003043 MethodListnfABITy = llvm::StructType::get(IntTy,
3044 IntTy,
3045 llvm::ArrayType::get(MethodTy, 0),
3046 NULL);
3047 CGM.getModule().addTypeName("struct.__method_list_t",
3048 MethodListnfABITy);
3049 // struct method_list_t *
3050 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003051
3052 // struct _protocol_t {
3053 // id isa; // NULL
3054 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003055 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003056 // const struct method_list_t * const instance_methods;
3057 // const struct method_list_t * const class_methods;
3058 // const struct method_list_t *optionalInstanceMethods;
3059 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003060 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003061 // const uint32_t size; // sizeof(struct _protocol_t)
3062 // const uint32_t flags; // = 0
3063 // }
3064
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003065 // Holder for struct _protocol_list_t *
3066 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3067
3068 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3069 Int8PtrTy,
3070 llvm::PointerType::getUnqual(
3071 ProtocolListTyHolder),
3072 MethodListnfABIPtrTy,
3073 MethodListnfABIPtrTy,
3074 MethodListnfABIPtrTy,
3075 MethodListnfABIPtrTy,
3076 PropertyListPtrTy,
3077 IntTy,
3078 IntTy,
3079 NULL);
3080 CGM.getModule().addTypeName("struct._protocol_t",
3081 ProtocolnfABITy);
3082
Fariborz Jahanianda320092009-01-29 19:24:30 +00003083 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003084 // long protocol_count; // Note, this is 32/64 bit
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003085 // struct _protocol_t[protocol_count];
3086 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003087 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3088 llvm::ArrayType::get(
3089 ProtocolnfABITy, 0),
3090 NULL);
3091 CGM.getModule().addTypeName("struct._objc_protocol_list",
3092 ProtocolListnfABITy);
3093
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003094 // struct _objc_protocol_list*
3095 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003096
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003097 // FIXME! Is this doing the right thing?
3098 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3099 ProtocolListnfABIPtrTy);
3100
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003101 // struct _ivar_t {
3102 // unsigned long int *offset; // pointer to ivar offset location
3103 // char *name;
3104 // char *type;
3105 // uint32_t alignment;
3106 // uint32_t size;
3107 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003108 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3109 Int8PtrTy,
3110 Int8PtrTy,
3111 IntTy,
3112 IntTy,
3113 NULL);
3114 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3115
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003116 // struct _ivar_list_t {
3117 // uint32 entsize; // sizeof(struct _ivar_t)
3118 // uint32 count;
3119 // struct _iver_t list[count];
3120 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003121 IvarListnfABITy = llvm::StructType::get(IntTy,
3122 IntTy,
3123 llvm::ArrayType::get(
3124 IvarnfABITy, 0),
3125 NULL);
3126 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3127
3128 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003129
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003130 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003131 // uint32_t const flags;
3132 // uint32_t const instanceStart;
3133 // uint32_t const instanceSize;
3134 // uint32_t const reserved; // only when building for 64bit targets
3135 // const uint8_t * const ivarLayout;
3136 // const char *const name;
3137 // const struct _method_list_t * const baseMethods;
3138 // const struct _objc_protocol_list *const baseProtocols;
3139 // const struct _ivar_list_t *const ivars;
3140 // const uint8_t * const weakIvarLayout;
3141 // const struct _prop_list_t * const properties;
3142 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003143
3144 // FIXME. Add 'reserved' field in 64bit abi mode!
3145 ClassRonfABITy = llvm::StructType::get(IntTy,
3146 IntTy,
3147 IntTy,
3148 Int8PtrTy,
3149 Int8PtrTy,
3150 MethodListnfABIPtrTy,
3151 ProtocolListnfABIPtrTy,
3152 IvarListnfABIPtrTy,
3153 Int8PtrTy,
3154 PropertyListPtrTy,
3155 NULL);
3156 CGM.getModule().addTypeName("struct._class_ro_t",
3157 ClassRonfABITy);
3158
3159 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3160 std::vector<const llvm::Type*> Params;
3161 Params.push_back(ObjectPtrTy);
3162 Params.push_back(SelectorPtrTy);
3163 ImpnfABITy = llvm::PointerType::getUnqual(
3164 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3165
3166 // struct _class_t {
3167 // struct _class_t *isa;
3168 // struct _class_t * const superclass;
3169 // void *cache;
3170 // IMP *vtable;
3171 // struct class_ro_t *ro;
3172 // }
3173
3174 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3175 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3176 llvm::PointerType::getUnqual(ClassTyHolder),
3177 CachePtrTy,
3178 llvm::PointerType::getUnqual(ImpnfABITy),
3179 llvm::PointerType::getUnqual(
3180 ClassRonfABITy),
3181 NULL);
3182 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3183
3184 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3185 ClassnfABITy);
3186
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003187 // LLVM for struct _class_t *
3188 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3189
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003190 // struct _category_t {
3191 // const char * const name;
3192 // struct _class_t *const cls;
3193 // const struct _method_list_t * const instance_methods;
3194 // const struct _method_list_t * const class_methods;
3195 // const struct _protocol_list_t * const protocols;
3196 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003197 // }
3198 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003199 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003200 MethodListnfABIPtrTy,
3201 MethodListnfABIPtrTy,
3202 ProtocolListnfABIPtrTy,
3203 PropertyListPtrTy,
3204 NULL);
3205 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003206
3207 // New types for nonfragile abi messaging.
3208
3209 // MessageRefTy - LLVM for:
3210 // struct _message_ref_t {
3211 // IMP messenger;
3212 // SEL name;
3213 // };
3214 MessageRefTy = llvm::StructType::get(ImpnfABITy,
3215 SelectorPtrTy,
3216 NULL);
3217 CGM.getModule().addTypeName("struct._message_ref_t", MessageRefTy);
3218
3219 // MessageRefPtrTy - LLVM for struct _message_ref_t*
3220 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
3221
3222 // SuperMessageRefTy - LLVM for:
3223 // struct _super_message_ref_t {
3224 // SUPER_IMP messenger;
3225 // SEL name;
3226 // };
3227 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
3228 SelectorPtrTy,
3229 NULL);
3230 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
3231
3232 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
3233 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
3234
3235 // id objc_msgSend_fixup (id, struct message_ref_t*, ...)
3236 Params.clear();
3237 Params.push_back(ObjectPtrTy);
3238 Params.push_back(MessageRefPtrTy);
3239 MessageSendFixupFn =
3240 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3241 Params,
3242 true),
3243 "objc_msgSend_fixup");
3244
3245 // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...)
3246 MessageSendFpretFixupFn =
3247 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3248 Params,
3249 true),
3250 "objc_msgSend_fpret_fixup");
3251
3252 // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...)
3253 MessageSendStretFixupFn =
3254 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3255 Params,
3256 true),
3257 "objc_msgSend_stret_fixup");
3258
3259 // id objc_msgSendId_fixup (id, struct message_ref_t*, ...)
3260 MessageSendIdFixupFn =
3261 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3262 Params,
3263 true),
3264 "objc_msgSendId_fixup");
3265
3266
3267 // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...)
3268 MessageSendIdStretFixupFn =
3269 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3270 Params,
3271 true),
3272 "MessageSendIdStretFixupFn");
3273
3274 // id objc_msgSendSuper2_fixup (struct objc_super *,
3275 // struct _super_message_ref_t*, ...)
3276 Params.clear();
3277 Params.push_back(SuperPtrTy);
3278 Params.push_back(SuperMessageRefPtrTy);
3279 MessageSendSuper2FixupFn =
3280 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3281 Params,
3282 true),
3283 "objc_msgSendSuper2_fixup");
3284
3285
3286 // id objc_msgSendSuper2_stret_fixup (struct objc_super *,
3287 // struct _super_message_ref_t*, ...)
3288 MessageSendSuper2StretFixupFn =
3289 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3290 Params,
3291 true),
3292 "objc_msgSendSuper2_stret_fixup");
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003293
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003294}
3295
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003296llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3297 FinishNonFragileABIModule();
3298
3299 return NULL;
3300}
3301
3302void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3303 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003304
3305 // Build list of all implemented classe addresses in array
3306 // L_OBJC_LABEL_CLASS_$.
3307 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
3308 // list of 'nonlazy' implementations (defined as those with a +load{}
3309 // method!!).
3310 unsigned NumClasses = DefinedClasses.size();
3311 if (NumClasses) {
3312 std::vector<llvm::Constant*> Symbols(NumClasses);
3313 for (unsigned i=0; i<NumClasses; i++)
3314 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3315 ObjCTypes.Int8PtrTy);
3316 llvm::Constant* Init =
3317 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3318 NumClasses),
3319 Symbols);
3320
3321 llvm::GlobalVariable *GV =
3322 new llvm::GlobalVariable(Init->getType(), false,
3323 llvm::GlobalValue::InternalLinkage,
3324 Init,
3325 "\01L_OBJC_LABEL_CLASS_$",
3326 &CGM.getModule());
3327 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
3328 UsedGlobals.push_back(GV);
3329 }
3330
3331 // Build list of all implemented category addresses in array
3332 // L_OBJC_LABEL_CATEGORY_$.
3333 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
3334 // list of 'nonlazy' category implementations (defined as those with a +load{}
3335 // method!!).
3336 unsigned NumCategory = DefinedCategories.size();
3337 if (NumCategory) {
3338 std::vector<llvm::Constant*> Symbols(NumCategory);
3339 for (unsigned i=0; i<NumCategory; i++)
3340 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
3341 ObjCTypes.Int8PtrTy);
3342 llvm::Constant* Init =
3343 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3344 NumCategory),
3345 Symbols);
3346
3347 llvm::GlobalVariable *GV =
3348 new llvm::GlobalVariable(Init->getType(), false,
3349 llvm::GlobalValue::InternalLinkage,
3350 Init,
3351 "\01L_OBJC_LABEL_CATEGORY_$",
3352 &CGM.getModule());
3353 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
3354 UsedGlobals.push_back(GV);
3355 }
3356
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00003357 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
3358 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
3359 std::vector<llvm::Constant*> Values(2);
3360 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3361 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3362 llvm::Constant* Init = llvm::ConstantArray::get(
3363 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
3364 Values);
3365 llvm::GlobalVariable *IMGV =
3366 new llvm::GlobalVariable(Init->getType(), false,
3367 llvm::GlobalValue::InternalLinkage,
3368 Init,
3369 "\01L_OBJC_IMAGE_INFO",
3370 &CGM.getModule());
3371 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
3372 UsedGlobals.push_back(IMGV);
3373
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003374 std::vector<llvm::Constant*> Used;
3375 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3376 e = UsedGlobals.end(); i != e; ++i) {
3377 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3378 }
3379
3380 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3381 llvm::GlobalValue *GV =
3382 new llvm::GlobalVariable(AT, false,
3383 llvm::GlobalValue::AppendingLinkage,
3384 llvm::ConstantArray::get(AT, Used),
3385 "llvm.used",
3386 &CGM.getModule());
3387
3388 GV->setSection("llvm.metadata");
3389
3390}
3391
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003392// Metadata flags
3393enum MetaDataDlags {
3394 CLS = 0x0,
3395 CLS_META = 0x1,
3396 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003397 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003398 CLS_EXCEPTION = 0x20
3399};
3400/// BuildClassRoTInitializer - generate meta-data for:
3401/// struct _class_ro_t {
3402/// uint32_t const flags;
3403/// uint32_t const instanceStart;
3404/// uint32_t const instanceSize;
3405/// uint32_t const reserved; // only when building for 64bit targets
3406/// const uint8_t * const ivarLayout;
3407/// const char *const name;
3408/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00003409/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003410/// const struct _ivar_list_t *const ivars;
3411/// const uint8_t * const weakIvarLayout;
3412/// const struct _prop_list_t * const properties;
3413/// }
3414///
3415llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
3416 unsigned flags,
3417 unsigned InstanceStart,
3418 unsigned InstanceSize,
3419 const ObjCImplementationDecl *ID) {
3420 std::string ClassName = ID->getNameAsString();
3421 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
3422 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
3423 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
3424 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
3425 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianda320092009-01-29 19:24:30 +00003426 // FIXME. ivarLayout is currently null!
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003427 Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3428 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003429 // const struct _method_list_t * const baseMethods;
3430 std::vector<llvm::Constant*> Methods;
3431 std::string MethodListName("\01l_OBJC_$_");
3432 if (flags & CLS_META) {
3433 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
3434 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
3435 e = ID->classmeth_end(); i != e; ++i) {
3436 // Class methods should always be defined.
3437 Methods.push_back(GetMethodConstant(*i));
3438 }
3439 } else {
3440 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
3441 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
3442 e = ID->instmeth_end(); i != e; ++i) {
3443 // Instance methods should always be defined.
3444 Methods.push_back(GetMethodConstant(*i));
3445 }
Fariborz Jahanian939abce2009-01-28 22:46:49 +00003446 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
3447 e = ID->propimpl_end(); i != e; ++i) {
3448 ObjCPropertyImplDecl *PID = *i;
3449
3450 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
3451 ObjCPropertyDecl *PD = PID->getPropertyDecl();
3452
3453 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3454 if (llvm::Constant *C = GetMethodConstant(MD))
3455 Methods.push_back(C);
3456 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3457 if (llvm::Constant *C = GetMethodConstant(MD))
3458 Methods.push_back(C);
3459 }
3460 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003461 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003462 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003463 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003464
3465 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3466 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
3467 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
3468 + OID->getNameAsString(),
3469 OID->protocol_begin(),
3470 OID->protocol_end());
3471
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003472 if (flags & CLS_META)
3473 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3474 else
3475 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003476 // FIXME. weakIvarLayout is currently null.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003477 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003478 if (flags & CLS_META)
3479 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3480 else
3481 Values[ 9] =
3482 EmitPropertyList(
3483 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
3484 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003485 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
3486 Values);
3487 llvm::GlobalVariable *CLASS_RO_GV =
3488 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
3489 llvm::GlobalValue::InternalLinkage,
3490 Init,
3491 (flags & CLS_META) ?
3492 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
3493 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
3494 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003495 CLASS_RO_GV->setAlignment(
3496 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003497 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003498 UsedGlobals.push_back(CLASS_RO_GV);
3499 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003500
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003501}
3502
3503/// BuildClassMetaData - This routine defines that to-level meta-data
3504/// for the given ClassName for:
3505/// struct _class_t {
3506/// struct _class_t *isa;
3507/// struct _class_t * const superclass;
3508/// void *cache;
3509/// IMP *vtable;
3510/// struct class_ro_t *ro;
3511/// }
3512///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003513llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
3514 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003515 llvm::Constant *IsAGV,
3516 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003517 llvm::Constant *ClassRoGV,
3518 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003519 std::vector<llvm::Constant*> Values(5);
3520 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003521 Values[1] = SuperClassGV
3522 ? SuperClassGV
3523 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003524 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
3525 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
3526 Values[4] = ClassRoGV; // &CLASS_RO_GV
3527 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
3528 Values);
3529 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName);
3530 if (GV)
3531 GV->setInitializer(Init);
3532 else
3533 GV =
3534 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3535 llvm::GlobalValue::ExternalLinkage,
3536 Init,
3537 ClassName,
3538 &CGM.getModule());
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00003539 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003540 GV->setAlignment(
3541 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003542 if (HiddenVisibility)
3543 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003544 UsedGlobals.push_back(GV);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003545 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003546}
3547
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003548void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
3549 std::string ClassName = ID->getNameAsString();
3550 if (!ObjCEmptyCacheVar) {
3551 ObjCEmptyCacheVar = new llvm::GlobalVariable(
3552 ObjCTypes.CachePtrTy,
3553 false,
3554 llvm::GlobalValue::ExternalLinkage,
3555 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00003556 "\01__objc_empty_cache",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003557 &CGM.getModule());
3558 UsedGlobals.push_back(ObjCEmptyCacheVar);
3559
3560 ObjCEmptyVtableVar = new llvm::GlobalVariable(
3561 llvm::PointerType::getUnqual(
3562 ObjCTypes.ImpnfABITy),
3563 false,
3564 llvm::GlobalValue::ExternalLinkage,
3565 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00003566 "\01__objc_empty_vtable",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003567 &CGM.getModule());
3568 UsedGlobals.push_back(ObjCEmptyVtableVar);
3569 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003570 assert(ID->getClassInterface() &&
3571 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003572 uint32_t InstanceStart =
3573 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
3574 uint32_t InstanceSize = InstanceStart;
3575 uint32_t flags = CLS_META;
3576 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
3577 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003578
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003579 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003580
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003581 bool classIsHidden = IsClassHidden(ID->getClassInterface());
3582 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003583 flags |= OBJC2_CLS_HIDDEN;
3584 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003585 // class is root
3586 flags |= CLS_ROOT;
3587 std::string SuperClassName = ObjCClassName + ClassName;
3588 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3589 if (!SuperClassGV)
3590 SuperClassGV =
3591 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3592 llvm::GlobalValue::ExternalLinkage,
3593 0,
3594 SuperClassName,
3595 &CGM.getModule());
3596 UsedGlobals.push_back(SuperClassGV);
3597 std::string IsAClassName = ObjCMetaClassName + ClassName;
3598 IsAGV = CGM.getModule().getGlobalVariable(IsAClassName);
3599 if (!IsAGV)
3600 IsAGV =
3601 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3602 llvm::GlobalValue::ExternalLinkage,
3603 0,
3604 IsAClassName,
3605 &CGM.getModule());
3606 UsedGlobals.push_back(IsAGV);
3607 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003608 // Has a root. Current class is not a root.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003609 std::string RootClassName =
3610 ID->getClassInterface()->getSuperClass()->getNameAsString();
3611 std::string SuperClassName = ObjCMetaClassName + RootClassName;
3612 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3613 if (!SuperClassGV)
3614 SuperClassGV =
3615 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3616 llvm::GlobalValue::ExternalLinkage,
3617 0,
3618 SuperClassName,
3619 &CGM.getModule());
3620 UsedGlobals.push_back(SuperClassGV);
3621 IsAGV = SuperClassGV;
3622 }
3623 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
3624 InstanceStart,
3625 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003626 std::string TClassName = ObjCMetaClassName + ClassName;
3627 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003628 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
3629 classIsHidden);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003630
3631 // Metadata for the class
3632 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003633 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003634 flags |= OBJC2_CLS_HIDDEN;
3635 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003636 flags |= CLS_ROOT;
3637 SuperClassGV = 0;
3638 }
3639 else {
3640 // Has a root. Current class is not a root.
3641 std::string RootClassName =
3642 ID->getClassInterface()->getSuperClass()->getNameAsString();
3643 std::string SuperClassName = ObjCClassName + RootClassName;
3644 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3645 if (!SuperClassGV)
3646 SuperClassGV =
3647 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3648 llvm::GlobalValue::ExternalLinkage,
3649 0,
3650 SuperClassName,
3651 &CGM.getModule());
3652 UsedGlobals.push_back(SuperClassGV);
3653
3654 }
3655
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003656 InstanceStart = InstanceSize = 0;
3657 if (ObjCInterfaceDecl *OID =
3658 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
3659 // FIXME. Share this with the one in EmitIvarList.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003660 const llvm::Type *InterfaceTy =
3661 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
3662 const llvm::StructLayout *Layout =
3663 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003664
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003665 RecordDecl::field_iterator firstField, lastField;
3666 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003667
3668 for (RecordDecl::field_iterator e = RD->field_end(),
3669 ifield = firstField; ifield != e; ++ifield)
3670 lastField = ifield;
3671
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003672 if (lastField != RD->field_end()) {
3673 FieldDecl *Field = *lastField;
3674 const llvm::Type *FieldTy =
3675 CGM.getTypes().ConvertTypeForMem(Field->getType());
3676 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3677 InstanceSize = Layout->getElementOffset(
3678 CGM.getTypes().getLLVMFieldNo(Field)) +
3679 Size;
3680 if (firstField == RD->field_end())
3681 InstanceStart = InstanceSize;
3682 else
3683 InstanceStart = Layout->getElementOffset(CGM.getTypes().
3684 getLLVMFieldNo(*firstField));
3685 }
3686 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003687 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003688 InstanceStart,
3689 InstanceSize,
3690 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003691
3692 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003693 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003694 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
3695 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003696 DefinedClasses.push_back(ClassMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003697}
3698
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00003699/// GenerateProtocolRef - This routine is called to generate code for
3700/// a protocol reference expression; as in:
3701/// @code
3702/// @protocol(Proto1);
3703/// @endcode
3704/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
3705/// which will hold address of the protocol meta-data.
3706///
3707llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
3708 const ObjCProtocolDecl *PD) {
3709
3710 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
3711 ObjCTypes.ExternalProtocolPtrTy);
3712
3713 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
3714 ProtocolName += PD->getNameAsCString();
3715
3716 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
3717 if (PTGV)
3718 return Builder.CreateLoad(PTGV, false, "tmp");
3719 PTGV = new llvm::GlobalVariable(
3720 Init->getType(), false,
3721 llvm::GlobalValue::WeakLinkage,
3722 Init,
3723 ProtocolName,
3724 &CGM.getModule());
3725 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
3726 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3727 UsedGlobals.push_back(PTGV);
3728 return Builder.CreateLoad(PTGV, false, "tmp");
3729}
3730
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003731/// GenerateCategory - Build metadata for a category implementation.
3732/// struct _category_t {
3733/// const char * const name;
3734/// struct _class_t *const cls;
3735/// const struct _method_list_t * const instance_methods;
3736/// const struct _method_list_t * const class_methods;
3737/// const struct _protocol_list_t * const protocols;
3738/// const struct _prop_list_t * const properties;
3739/// }
3740///
3741void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
3742{
3743 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003744 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
3745 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003746 "_$_" + OCD->getNameAsString());
3747 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString());
3748
3749 std::vector<llvm::Constant*> Values(6);
3750 Values[0] = GetClassName(OCD->getIdentifier());
3751 // meta-class entry symbol
3752 llvm::GlobalVariable *ClassGV =
3753 CGM.getModule().getGlobalVariable(ExtClassName);
3754 if (!ClassGV)
3755 ClassGV =
3756 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3757 llvm::GlobalValue::ExternalLinkage,
3758 0,
3759 ExtClassName,
3760 &CGM.getModule());
3761 UsedGlobals.push_back(ClassGV);
3762 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003763 std::vector<llvm::Constant*> Methods;
3764 std::string MethodListName(Prefix);
3765 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
3766 "_$_" + OCD->getNameAsString();
3767
3768 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
3769 e = OCD->instmeth_end(); i != e; ++i) {
3770 // Instance methods should always be defined.
3771 Methods.push_back(GetMethodConstant(*i));
3772 }
3773
3774 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003775 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003776 Methods);
3777
3778 MethodListName = Prefix;
3779 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
3780 OCD->getNameAsString();
3781 Methods.clear();
3782 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
3783 e = OCD->classmeth_end(); i != e; ++i) {
3784 // Class methods should always be defined.
3785 Methods.push_back(GetMethodConstant(*i));
3786 }
3787
3788 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003789 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003790 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003791 const ObjCCategoryDecl *Category =
3792 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanianeb732132009-01-29 23:23:06 +00003793 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
3794 + Interface->getNameAsString() + "_$_"
3795 + Category->getNameAsString(),
3796 Category->protocol_begin(),
3797 Category->protocol_end());
3798
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003799 std::string ExtName(Interface->getNameAsString() + "_$_" +
3800 OCD->getNameAsString());
3801 Values[5] =
3802 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
3803 OCD, Category, ObjCTypes);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003804 llvm::Constant *Init =
3805 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
3806 Values);
3807 llvm::GlobalVariable *GCATV
3808 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
3809 false,
3810 llvm::GlobalValue::InternalLinkage,
3811 Init,
3812 ExtCatName,
3813 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003814 GCATV->setAlignment(
3815 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003816 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003817 UsedGlobals.push_back(GCATV);
3818 DefinedCategories.push_back(GCATV);
3819}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003820
3821/// GetMethodConstant - Return a struct objc_method constant for the
3822/// given method if it has been defined. The result is null if the
3823/// method has not been defined. The return value has type MethodPtrTy.
3824llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
3825 const ObjCMethodDecl *MD) {
3826 // FIXME: Use DenseMap::lookup
3827 llvm::Function *Fn = MethodDefinitions[MD];
3828 if (!Fn)
3829 return 0;
3830
3831 std::vector<llvm::Constant*> Method(3);
3832 Method[0] =
3833 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
3834 ObjCTypes.SelectorPtrTy);
3835 Method[1] = GetMethodVarType(MD);
3836 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
3837 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
3838}
3839
3840/// EmitMethodList - Build meta-data for method declarations
3841/// struct _method_list_t {
3842/// uint32_t entsize; // sizeof(struct _objc_method)
3843/// uint32_t method_count;
3844/// struct _objc_method method_list[method_count];
3845/// }
3846///
3847llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
3848 const std::string &Name,
3849 const char *Section,
3850 const ConstantVector &Methods) {
3851 // Return null for empty list.
3852 if (Methods.empty())
3853 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
3854
3855 std::vector<llvm::Constant*> Values(3);
3856 // sizeof(struct _objc_method)
3857 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
3858 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3859 // method_count
3860 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
3861 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
3862 Methods.size());
3863 Values[2] = llvm::ConstantArray::get(AT, Methods);
3864 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3865
3866 llvm::GlobalVariable *GV =
3867 new llvm::GlobalVariable(Init->getType(), false,
3868 llvm::GlobalValue::InternalLinkage,
3869 Init,
3870 Name,
3871 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003872 GV->setAlignment(
3873 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003874 GV->setSection(Section);
3875 UsedGlobals.push_back(GV);
3876 return llvm::ConstantExpr::getBitCast(GV,
3877 ObjCTypes.MethodListnfABIPtrTy);
3878}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003879
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003880llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
3881 const ObjCImplementationDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003882 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003883 unsigned long int Offset) {
3884
3885 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003886 + Ivar->getNameAsString());
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003887 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.LongTy, Offset);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003888
3889 llvm::GlobalVariable *IvarOffsetGV =
3890 CGM.getModule().getGlobalVariable(ExternalName);
3891 if (IvarOffsetGV) {
3892 // ivar offset symbol already built due to user code referencing it.
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003893 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003894 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003895 IvarOffsetGV->setInitializer(Init);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003896 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003897 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003898 return IvarOffsetGV;
3899 }
3900
3901 IvarOffsetGV =
3902 new llvm::GlobalVariable(Init->getType(),
3903 false,
3904 llvm::GlobalValue::ExternalLinkage,
3905 Init,
3906 ExternalName,
3907 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003908 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003909 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003910 // @private and @package have hidden visibility.
3911 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public ||
3912 Ivar->getAccessControl() == ObjCIvarDecl::Protected);
3913 if (!globalVisibility)
3914 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003915 else
3916 if (const ObjCInterfaceDecl *OID = ID->getClassInterface())
3917 if (IsClassHidden(OID))
3918 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3919
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003920 IvarOffsetGV->setSection("__DATA, __objc_const");
3921 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003922 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003923}
3924
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003925/// EmitIvarList - Emit the ivar list for the given
3926/// implementation. If ForClass is true the list of class ivars
3927/// (i.e. metaclass ivars) is emitted, otherwise the list of
3928/// interface ivars will be emitted. The return value has type
3929/// IvarListnfABIPtrTy.
3930/// struct _ivar_t {
3931/// unsigned long int *offset; // pointer to ivar offset location
3932/// char *name;
3933/// char *type;
3934/// uint32_t alignment;
3935/// uint32_t size;
3936/// }
3937/// struct _ivar_list_t {
3938/// uint32 entsize; // sizeof(struct _ivar_t)
3939/// uint32 count;
3940/// struct _iver_t list[count];
3941/// }
3942///
3943llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
3944 const ObjCImplementationDecl *ID) {
3945
3946 std::vector<llvm::Constant*> Ivars, Ivar(5);
3947
3948 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3949 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
3950
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003951 // FIXME. Consolidate this with similar code in GenerateClass.
3952 const llvm::Type *InterfaceTy =
3953 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(
3954 const_cast<ObjCInterfaceDecl*>(OID)));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003955 const llvm::StructLayout *Layout =
3956 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003957
3958 RecordDecl::field_iterator i,p;
3959 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003960 ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin();
3961
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003962 for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) {
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003963 FieldDecl *Field = *i;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003964 unsigned long offset = Layout->getElementOffset(CGM.getTypes().
3965 getLLVMFieldNo(Field));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003966 const ObjCIvarDecl *ivarDecl = *I++;
3967 Ivar[0] = EmitIvarOffsetVar(ID, ivarDecl, offset);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003968 if (Field->getIdentifier())
3969 Ivar[1] = GetMethodVarName(Field->getIdentifier());
3970 else
3971 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3972 std::string TypeStr;
3973 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3974 Ivar[2] = GetMethodVarType(TypeStr);
3975 const llvm::Type *FieldTy =
3976 CGM.getTypes().ConvertTypeForMem(Field->getType());
3977 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3978 unsigned Align = CGM.getContext().getPreferredTypeAlign(
3979 Field->getType().getTypePtr()) >> 3;
3980 Align = llvm::Log2_32(Align);
3981 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Fariborz Jahanian07236ba2009-01-27 22:27:56 +00003982 // NOTE. Size of a bitfield does not match gcc's, because of the way
3983 // bitfields are treated special in each. But I am told that 'size'
3984 // for bitfield ivars is ignored by the runtime so it does not matter.
3985 // (even if it matters, some day, there is enough info. to get the bitfield
3986 // right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003987 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3988 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
3989 }
3990 // Return null for empty list.
3991 if (Ivars.empty())
3992 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3993 std::vector<llvm::Constant*> Values(3);
3994 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
3995 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3996 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
3997 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
3998 Ivars.size());
3999 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4000 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4001 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4002 llvm::GlobalVariable *GV =
4003 new llvm::GlobalVariable(Init->getType(), false,
4004 llvm::GlobalValue::InternalLinkage,
4005 Init,
4006 Prefix + OID->getNameAsString(),
4007 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004008 GV->setAlignment(
4009 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004010 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004011
4012 UsedGlobals.push_back(GV);
4013 return llvm::ConstantExpr::getBitCast(GV,
4014 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004015}
4016
4017llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4018 const ObjCProtocolDecl *PD) {
4019 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4020
4021 if (!Entry) {
4022 // We use the initializer as a marker of whether this is a forward
4023 // reference or not. At module finalization we add the empty
4024 // contents for protocols which were referenced but never defined.
4025 Entry =
4026 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4027 llvm::GlobalValue::ExternalLinkage,
4028 0,
4029 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4030 &CGM.getModule());
4031 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4032 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004033 }
4034
4035 return Entry;
4036}
4037
4038/// GetOrEmitProtocol - Generate the protocol meta-data:
4039/// @code
4040/// struct _protocol_t {
4041/// id isa; // NULL
4042/// const char * const protocol_name;
4043/// const struct _protocol_list_t * protocol_list; // super protocols
4044/// const struct method_list_t * const instance_methods;
4045/// const struct method_list_t * const class_methods;
4046/// const struct method_list_t *optionalInstanceMethods;
4047/// const struct method_list_t *optionalClassMethods;
4048/// const struct _prop_list_t * properties;
4049/// const uint32_t size; // sizeof(struct _protocol_t)
4050/// const uint32_t flags; // = 0
4051/// }
4052/// @endcode
4053///
4054
4055llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4056 const ObjCProtocolDecl *PD) {
4057 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4058
4059 // Early exit if a defining object has already been generated.
4060 if (Entry && Entry->hasInitializer())
4061 return Entry;
4062
4063 const char *ProtocolName = PD->getNameAsCString();
4064
4065 // Construct method lists.
4066 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4067 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
4068 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
4069 e = PD->instmeth_end(); i != e; ++i) {
4070 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004071 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004072 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4073 OptInstanceMethods.push_back(C);
4074 } else {
4075 InstanceMethods.push_back(C);
4076 }
4077 }
4078
4079 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
4080 e = PD->classmeth_end(); i != e; ++i) {
4081 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004082 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004083 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4084 OptClassMethods.push_back(C);
4085 } else {
4086 ClassMethods.push_back(C);
4087 }
4088 }
4089
4090 std::vector<llvm::Constant*> Values(10);
4091 // isa is NULL
4092 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4093 Values[1] = GetClassName(PD->getIdentifier());
4094 Values[2] = EmitProtocolList(
4095 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4096 PD->protocol_begin(),
4097 PD->protocol_end());
4098
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004099 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004100 + PD->getNameAsString(),
4101 "__DATA, __objc_const",
4102 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004103 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004104 + PD->getNameAsString(),
4105 "__DATA, __objc_const",
4106 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004107 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004108 + PD->getNameAsString(),
4109 "__DATA, __objc_const",
4110 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004111 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004112 + PD->getNameAsString(),
4113 "__DATA, __objc_const",
4114 OptClassMethods);
4115 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4116 0, PD, ObjCTypes);
4117 uint32_t Size =
4118 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
4119 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4120 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4121 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4122 Values);
4123
4124 if (Entry) {
4125 // Already created, fix the linkage and update the initializer.
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004126 Entry->setLinkage(llvm::GlobalValue::WeakLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004127 Entry->setInitializer(Init);
4128 } else {
4129 Entry =
4130 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004131 llvm::GlobalValue::WeakLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004132 Init,
4133 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4134 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004135 Entry->setAlignment(
4136 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004137 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004138 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004139 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4140
4141 // Use this protocol meta-data to build protocol list table in section
4142 // __DATA, __objc_protolist
4143 llvm::Type *ptype = llvm::PointerType::getUnqual(ObjCTypes.ProtocolnfABITy);
4144 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
4145 ptype, false,
4146 llvm::GlobalValue::WeakLinkage,
4147 Entry,
4148 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4149 +ProtocolName,
4150 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004151 PTGV->setAlignment(
4152 CGM.getTargetData().getPrefTypeAlignment(ptype));
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004153 PTGV->setSection("__DATA, __objc_protolist");
4154 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4155 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004156 return Entry;
4157}
4158
4159/// EmitProtocolList - Generate protocol list meta-data:
4160/// @code
4161/// struct _protocol_list_t {
4162/// long protocol_count; // Note, this is 32/64 bit
4163/// struct _protocol_t[protocol_count];
4164/// }
4165/// @endcode
4166///
4167llvm::Constant *
4168CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4169 ObjCProtocolDecl::protocol_iterator begin,
4170 ObjCProtocolDecl::protocol_iterator end) {
4171 std::vector<llvm::Constant*> ProtocolRefs;
4172
4173 for (; begin != end; ++begin)
4174 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4175
4176 // Just return null for empty protocol lists
4177 if (ProtocolRefs.empty())
4178 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4179
4180 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4181 if (GV)
4182 return GV;
4183 // This list is null terminated.
4184 ProtocolRefs.push_back(llvm::Constant::getNullValue(
4185 ObjCTypes.ProtocolListnfABIPtrTy));
4186
4187 std::vector<llvm::Constant*> Values(2);
4188 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4189 Values[1] =
4190 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolListnfABIPtrTy,
4191 ProtocolRefs.size()),
4192 ProtocolRefs);
4193
4194 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4195 GV = new llvm::GlobalVariable(Init->getType(), false,
4196 llvm::GlobalValue::InternalLinkage,
4197 Init,
4198 Name,
4199 &CGM.getModule());
4200 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004201 GV->setAlignment(
4202 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004203 UsedGlobals.push_back(GV);
4204 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
4205}
4206
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004207/// GetMethodDescriptionConstant - This routine build following meta-data:
4208/// struct _objc_method {
4209/// SEL _cmd;
4210/// char *method_type;
4211/// char *_imp;
4212/// }
4213
4214llvm::Constant *
4215CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4216 std::vector<llvm::Constant*> Desc(3);
4217 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4218 ObjCTypes.SelectorPtrTy);
4219 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004220 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004221 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4222 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4223}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004224
4225/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4226/// This code gen. amounts to generating code for:
4227/// @code
4228/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4229/// @encode
4230///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004231LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004232 CodeGen::CodeGenFunction &CGF,
4233 QualType ObjectTy,
4234 llvm::Value *BaseValue,
4235 const ObjCIvarDecl *Ivar,
4236 const FieldDecl *Field,
4237 unsigned CVRQualifiers) {
4238 assert(ObjectTy->isObjCInterfaceType() &&
4239 "CGObjCNonFragileABIMac::EmitObjCValueForIvar");
4240 NamedDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
4241 // NOTE. This name must match one in EmitIvarOffsetVar.
4242 // FIXME. Consolidate into one naming routine.
4243 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
4244 + Ivar->getNameAsString());
4245
4246 llvm::GlobalVariable *IvarOffsetGV =
4247 CGM.getModule().getGlobalVariable(ExternalName);
4248 if (!IvarOffsetGV)
4249 IvarOffsetGV =
4250 new llvm::GlobalVariable(ObjCTypes.LongTy,
4251 false,
4252 llvm::GlobalValue::ExternalLinkage,
4253 0,
4254 ExternalName,
4255 &CGM.getModule());
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004256
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004257 // (char *) BaseValue
4258 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue,
4259 ObjCTypes.Int8PtrTy);
4260 llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV);
4261 // (char*)BaseValue + Offset_symbol
4262 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
4263 // (type *)((char*)BaseValue + Offset_symbol)
4264 const llvm::Type *IvarTy =
4265 CGM.getTypes().ConvertType(Ivar->getType());
4266 llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy);
4267 V = CGF.Builder.CreateBitCast(V, ptrIvarTy);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004268
4269 if (Ivar->isBitField())
4270 return CGF.EmitLValueForBitfield(V, const_cast<FieldDecl *>(Field),
4271 CVRQualifiers);
4272
4273 LValue LV = LValue::MakeAddr(V,
4274 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
4275 LValue::SetObjCIvar(LV, true);
4276 return LV;
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004277}
4278
Fariborz Jahanian46551122009-02-04 00:22:57 +00004279CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4280 CodeGen::CodeGenFunction &CGF,
4281 QualType ResultType,
4282 Selector Sel,
4283 llvm::Value *Arg0,
4284 QualType Arg0Ty,
4285 bool IsSuper,
4286 const CallArgList &CallArgs) {
4287 return RValue::get(0);
4288}
4289
4290/// Generate code for a message send expression in the nonfragile abi.
4291CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
4292 CodeGen::CodeGenFunction &CGF,
4293 QualType ResultType,
4294 Selector Sel,
4295 llvm::Value *Receiver,
4296 bool IsClassMessage,
4297 const CallArgList &CallArgs) {
4298 llvm::Value *Arg0 =
4299 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
4300 return EmitMessageSend(CGF, ResultType, Sel,
4301 Arg0, CGF.getContext().getObjCIdType(),
4302 false, CallArgs);
4303}
4304
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004305/* *** */
4306
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004307CodeGen::CGObjCRuntime *
4308CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00004309 return new CGObjCMac(CGM);
4310}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004311
4312CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004313CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004314 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004315}