blob: f8d456c466b8da80fd50fe2b43db5a854334ec18 [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 Jahanian83a8a752009-02-04 20:42:28 +0000218public:
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000219 llvm::Function *MessageSendFixupFn, *MessageSendFpretFixupFn,
220 *MessageSendStretFixupFn, *MessageSendIdFixupFn,
221 *MessageSendIdStretFixupFn, *MessageSendSuper2FixupFn,
222 *MessageSendSuper2StretFixupFn;
223
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000224 // MethodListnfABITy - LLVM for struct _method_list_t
225 const llvm::StructType *MethodListnfABITy;
226
227 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
228 const llvm::Type *MethodListnfABIPtrTy;
229
230 // ProtocolnfABITy = LLVM for struct _protocol_t
231 const llvm::StructType *ProtocolnfABITy;
232
233 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
234 const llvm::StructType *ProtocolListnfABITy;
235
236 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
237 const llvm::Type *ProtocolListnfABIPtrTy;
238
239 // ClassnfABITy - LLVM for struct _class_t
240 const llvm::StructType *ClassnfABITy;
241
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000242 // ClassnfABIPtrTy - LLVM for struct _class_t*
243 const llvm::Type *ClassnfABIPtrTy;
244
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000245 // IvarnfABITy - LLVM for struct _ivar_t
246 const llvm::StructType *IvarnfABITy;
247
248 // IvarListnfABITy - LLVM for struct _ivar_list_t
249 const llvm::StructType *IvarListnfABITy;
250
251 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
252 const llvm::Type *IvarListnfABIPtrTy;
253
254 // ClassRonfABITy - LLVM for struct _class_ro_t
255 const llvm::StructType *ClassRonfABITy;
256
257 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
258 const llvm::Type *ImpnfABITy;
259
260 // CategorynfABITy - LLVM for struct _category_t
261 const llvm::StructType *CategorynfABITy;
262
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000263 // New types for nonfragile abi messaging.
264
265 // MessageRefTy - LLVM for:
266 // struct _message_ref_t {
267 // IMP messenger;
268 // SEL name;
269 // };
270 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000271 // MessageRefCTy - clang type for struct _message_ref_t
272 QualType MessageRefCTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000273
274 // MessageRefPtrTy - LLVM for struct _message_ref_t*
275 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000276 // MessageRefCPtrTy - clang type for struct _message_ref_t*
277 QualType MessageRefCPtrTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000278
Fariborz Jahanianef163782009-02-05 01:13:09 +0000279 // MessengerTy - Type of the messenger (shown as IMP above)
280 const llvm::FunctionType *MessengerTy;
281
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000282 // SuperMessageRefTy - LLVM for:
283 // struct _super_message_ref_t {
284 // SUPER_IMP messenger;
285 // SEL name;
286 // };
287 const llvm::StructType *SuperMessageRefTy;
288
289 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
290 const llvm::Type *SuperMessageRefPtrTy;
291
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000292 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
293 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000294};
295
296class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
297protected:
298 CodeGen::CodeGenModule &CGM;
299 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000300 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000301
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000302 /// LazySymbols - Symbols to generate a lazy reference for. See
303 /// DefinedSymbols and FinishModule().
304 std::set<IdentifierInfo*> LazySymbols;
305
306 /// DefinedSymbols - External symbols which are defined by this
307 /// module. The symbols in this list and LazySymbols are used to add
308 /// special linker symbols which ensure that Objective-C modules are
309 /// linked properly.
310 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000311
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000312 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000313 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000314
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000315 /// MethodVarNames - uniqued method variable names.
316 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000317
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000318 /// MethodVarTypes - uniqued method type signatures. We have to use
319 /// a StringMap here because have no other unique reference.
320 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000321
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000322 /// MethodDefinitions - map of methods which have been defined in
323 /// this translation unit.
324 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000325
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000326 /// PropertyNames - uniqued method variable names.
327 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000328
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000329 /// ClassReferences - uniqued class references.
330 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000331
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000332 /// SelectorReferences - uniqued selector references.
333 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000334
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000335 /// Protocols - Protocols for which an objc_protocol structure has
336 /// been emitted. Forward declarations are handled by creating an
337 /// empty structure whose initializer is filled in when/if defined.
338 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000339
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000340 /// DefinedProtocols - Protocols which have actually been
341 /// defined. We should not need this, see FIXME in GenerateProtocol.
342 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000343
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000344 /// DefinedClasses - List of defined classes.
345 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000346
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000347 /// DefinedCategories - List of defined categories.
348 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000349
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000350 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000351 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000352 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000353
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000354 /// GetNameForMethod - Return a name for the given method.
355 /// \param[out] NameOut - The return value.
356 void GetNameForMethod(const ObjCMethodDecl *OMD,
357 const ObjCContainerDecl *CD,
358 std::string &NameOut);
359
360 /// GetMethodVarName - Return a unique constant for the given
361 /// selector's name. The return value has type char *.
362 llvm::Constant *GetMethodVarName(Selector Sel);
363 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
364 llvm::Constant *GetMethodVarName(const std::string &Name);
365
366 /// GetMethodVarType - Return a unique constant for the given
367 /// selector's name. The return value has type char *.
368
369 // FIXME: This is a horrible name.
370 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
371 llvm::Constant *GetMethodVarType(const std::string &Name);
372
373 /// GetPropertyName - Return a unique constant for the given
374 /// name. The return value has type char *.
375 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
376
377 // FIXME: This can be dropped once string functions are unified.
378 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
379 const Decl *Container);
380
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000381 /// GetClassName - Return a unique constant for the given selector's
382 /// name. The return value has type char *.
383 llvm::Constant *GetClassName(IdentifierInfo *Ident);
384
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000385 const RecordDecl *GetFirstIvarInRecord(const ObjCInterfaceDecl *OID,
386 RecordDecl::field_iterator &FIV,
387 RecordDecl::field_iterator &PIV);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000388 /// EmitPropertyList - Emit the given property list. The return
389 /// value has type PropertyListPtrTy.
390 llvm::Constant *EmitPropertyList(const std::string &Name,
391 const Decl *Container,
392 const ObjCContainerDecl *OCD,
393 const ObjCCommonTypesHelper &ObjCTypes);
394
Fariborz Jahanianda320092009-01-29 19:24:30 +0000395 /// GetProtocolRef - Return a reference to the internal protocol
396 /// description, creating an empty one if it has not been
397 /// defined. The return value has type ProtocolPtrTy.
398 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
399
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000400public:
401 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
402 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000403
404 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000405
406 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
407 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000408
409 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
410
411 /// GetOrEmitProtocol - Get the protocol object for the given
412 /// declaration, emitting it if necessary. The return value has type
413 /// ProtocolPtrTy.
414 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
415
416 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
417 /// object for the given declaration, emitting it if needed. These
418 /// forward references will be filled in with empty bodies if no
419 /// definition is seen. The return value has type ProtocolPtrTy.
420 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000421};
422
423class CGObjCMac : public CGObjCCommonMac {
424private:
425 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000426 /// EmitImageInfo - Emit the image info marker used to encode some module
427 /// level information.
428 void EmitImageInfo();
429
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000430 /// EmitModuleInfo - Another marker encoding module level
431 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000432 void EmitModuleInfo();
433
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000434 /// EmitModuleSymols - Emit module symbols, the list of defined
435 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000436 llvm::Constant *EmitModuleSymbols();
437
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000438 /// FinishModule - Write out global data structures at the end of
439 /// processing a translation unit.
440 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000441
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000442 /// EmitClassExtension - Generate the class extension structure used
443 /// to store the weak ivar layout and properties. The return value
444 /// has type ClassExtensionPtrTy.
445 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
446
447 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
448 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000449 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000450 const ObjCInterfaceDecl *ID);
451
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000452 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000453 QualType ResultType,
454 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000455 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000456 QualType Arg0Ty,
457 bool IsSuper,
458 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000459
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000460 /// EmitIvarList - Emit the ivar list for the given
461 /// implementation. If ForClass is true the list of class ivars
462 /// (i.e. metaclass ivars) is emitted, otherwise the list of
463 /// interface ivars will be emitted. The return value has type
464 /// IvarListPtrTy.
465 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000466 bool ForClass);
467
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000468 /// EmitMetaClass - Emit a forward reference to the class structure
469 /// for the metaclass of the given interface. The return value has
470 /// type ClassPtrTy.
471 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
472
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000473 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000474 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000475 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
476 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000477 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000478 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000479
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000480 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000481
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000482 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000483
484 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000485 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000486 llvm::Constant *EmitMethodList(const std::string &Name,
487 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000488 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000489
490 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000491 /// method declarations.
492 /// - TypeName: The name for the type containing the methods.
493 /// - IsProtocol: True iff these methods are for a protocol.
494 /// - ClassMethds: True iff these are class methods.
495 /// - Required: When true, only "required" methods are
496 /// listed. Similarly, when false only "optional" methods are
497 /// listed. For classes this should always be true.
498 /// - begin, end: The method list to output.
499 ///
500 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000501 llvm::Constant *EmitMethodDescList(const std::string &Name,
502 const char *Section,
503 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000504
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000505 /// GetOrEmitProtocol - Get the protocol object for the given
506 /// declaration, emitting it if necessary. The return value has type
507 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000508 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000509
510 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
511 /// object for the given declaration, emitting it if needed. These
512 /// forward references will be filled in with empty bodies if no
513 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000514 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000515
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000516 /// EmitProtocolExtension - Generate the protocol extension
517 /// structure used to store optional instance and class methods, and
518 /// protocol properties. The return value has type
519 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000520 llvm::Constant *
521 EmitProtocolExtension(const ObjCProtocolDecl *PD,
522 const ConstantVector &OptInstanceMethods,
523 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000524
525 /// EmitProtocolList - Generate the list of referenced
526 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +0000527 llvm::Constant *EmitProtocolList(const std::string &Name,
528 ObjCProtocolDecl::protocol_iterator begin,
529 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000530
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000531 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
532 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000533 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000534
Fariborz Jahanianda320092009-01-29 19:24:30 +0000535 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000536 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000537
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000538 virtual llvm::Function *ModuleInitFunction();
539
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000540 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000541 QualType ResultType,
542 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000543 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000544 bool IsClassMessage,
545 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000546
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000547 virtual CodeGen::RValue
548 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000549 QualType ResultType,
550 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000551 const ObjCInterfaceDecl *Class,
552 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000553 bool IsClassMessage,
554 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000555
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000556 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000557 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000558
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000559 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000560
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000561 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000562
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000563 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000564
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000565 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000566 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000567
Daniel Dunbar49f66022008-09-24 03:38:44 +0000568 virtual llvm::Function *GetPropertyGetFunction();
569 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000570 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000571
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000572 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
573 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000574 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
575 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000576 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000577 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000578 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
579 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000580 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
581 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000582 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
583 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000584 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
585 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +0000586
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000587 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
588 QualType ObjectTy,
589 llvm::Value *BaseValue,
590 const ObjCIvarDecl *Ivar,
591 const FieldDecl *Field,
592 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000593 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
594 ObjCInterfaceDecl *Interface,
595 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000596};
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000597
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000598class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000599private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000600 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000601 llvm::GlobalVariable* ObjCEmptyCacheVar;
602 llvm::GlobalVariable* ObjCEmptyVtableVar;
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000603 /// MetaClassReferences - uniqued meta class references.
604 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000605
606 /// FinishNonFragileABIModule - Write out global data structures at the end of
607 /// processing a translation unit.
608 void FinishNonFragileABIModule();
609
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000610 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
611 unsigned InstanceStart,
612 unsigned InstanceSize,
613 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +0000614 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
615 llvm::Constant *IsAGV,
616 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +0000617 llvm::Constant *ClassRoGV,
618 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000619
620 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
621
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +0000622 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
623
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000624 /// EmitMethodList - Emit the method list for the given
625 /// implementation. The return value has type MethodListnfABITy.
626 llvm::Constant *EmitMethodList(const std::string &Name,
627 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +0000628 const ConstantVector &Methods);
629 /// EmitIvarList - Emit the ivar list for the given
630 /// implementation. If ForClass is true the list of class ivars
631 /// (i.e. metaclass ivars) is emitted, otherwise the list of
632 /// interface ivars will be emitted. The return value has type
633 /// IvarListnfABIPtrTy.
634 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000635
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000636 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +0000637 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000638 unsigned long int offset);
639
Fariborz Jahanianda320092009-01-29 19:24:30 +0000640 /// GetOrEmitProtocol - Get the protocol object for the given
641 /// declaration, emitting it if necessary. The return value has type
642 /// ProtocolPtrTy.
643 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
644
645 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
646 /// object for the given declaration, emitting it if needed. These
647 /// forward references will be filled in with empty bodies if no
648 /// definition is seen. The return value has type ProtocolPtrTy.
649 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
650
651 /// EmitProtocolList - Generate the list of referenced
652 /// protocols. The return value has type ProtocolListPtrTy.
653 llvm::Constant *EmitProtocolList(const std::string &Name,
654 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000655 ObjCProtocolDecl::protocol_iterator end);
656
657 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
658 QualType ResultType,
659 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000660 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000661 QualType Arg0Ty,
662 bool IsSuper,
663 const CallArgList &CallArgs);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000664
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000665 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
666 /// for the given class.
667 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000668 const ObjCInterfaceDecl *ID,
669 bool IsSuper = false);
670
671 /// EmitMetaClassRef - Return a Value * of the address of _class_t
672 /// meta-data
673 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
674 const ObjCInterfaceDecl *ID);
675
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000676 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
677 /// the given ivar.
678 ///
679 llvm::GlobalVariable * ObjCIvarOffsetVariable(std::string &Name,
680 const NamedDecl *IDName,
681 const ObjCIvarDecl *Ivar);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000682
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +0000683 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
684 /// for the given selector.
685 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
686
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000687public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000688 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000689 // FIXME. All stubs for now!
690 virtual llvm::Function *ModuleInitFunction();
691
692 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
693 QualType ResultType,
694 Selector Sel,
695 llvm::Value *Receiver,
696 bool IsClassMessage,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000697 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000698
699 virtual CodeGen::RValue
700 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
701 QualType ResultType,
702 Selector Sel,
703 const ObjCInterfaceDecl *Class,
704 llvm::Value *Receiver,
705 bool IsClassMessage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000706 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000707
708 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000709 const ObjCInterfaceDecl *ID);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000710
711 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +0000712 { return EmitSelector(Builder, Sel); }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000713
Fariborz Jahanianeb062d92009-01-26 18:32:24 +0000714 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000715
716 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000717 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +0000718 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000719
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000720 virtual llvm::Function *GetPropertyGetFunction(){
721 return ObjCTypes.GetPropertyFn;
722 }
723 virtual llvm::Function *GetPropertySetFunction(){
724 return ObjCTypes.SetPropertyFn;
725 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000726 virtual llvm::Function *EnumerationMutationFunction()
Fariborz Jahanian3afdfd72009-02-10 22:46:12 +0000727 { return ObjCTypes.EnumerationMutationFn; }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000728
729 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
730 const Stmt &S)
731 { return; }
732 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
733 const ObjCAtThrowStmt &S)
734 { return; }
735 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
736 llvm::Value *AddrWeakObj)
737 { return 0; }
738 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
739 llvm::Value *src, llvm::Value *dst)
740 { return; }
741 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
742 llvm::Value *src, llvm::Value *dest)
743 { return; }
744 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
745 llvm::Value *src, llvm::Value *dest)
746 { return; }
747 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
748 llvm::Value *src, llvm::Value *dest)
749 { return; }
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000750 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
751 QualType ObjectTy,
752 llvm::Value *BaseValue,
753 const ObjCIvarDecl *Ivar,
754 const FieldDecl *Field,
755 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000756 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
757 ObjCInterfaceDecl *Interface,
758 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000759};
760
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000761} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000762
763/* *** Helper Functions *** */
764
765/// getConstantGEP() - Help routine to construct simple GEPs.
766static llvm::Constant *getConstantGEP(llvm::Constant *C,
767 unsigned idx0,
768 unsigned idx1) {
769 llvm::Value *Idxs[] = {
770 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
771 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
772 };
773 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
774}
775
776/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000777
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000778CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
779 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000780{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000781 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000782 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000783}
784
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000785/// GetClass - Return a reference to the class for the given interface
786/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000787llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000788 const ObjCInterfaceDecl *ID) {
789 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000790}
791
792/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000793llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000794 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000795}
796
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000797/// Generate a constant CFString object.
798/*
799 struct __builtin_CFString {
800 const int *isa; // point to __CFConstantStringClassReference
801 int flags;
802 const char *str;
803 long length;
804 };
805*/
806
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000807llvm::Constant *CGObjCCommonMac::GenerateConstantString(
808 const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000809 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000810}
811
812/// Generates a message send where the super is the receiver. This is
813/// a message send to self with special delivery semantics indicating
814/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000815CodeGen::RValue
816CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000817 QualType ResultType,
818 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000819 const ObjCInterfaceDecl *Class,
820 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000821 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000822 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000823 // Create and init a super structure; this is a (receiver, class)
824 // pair we will pass to objc_msgSendSuper.
825 llvm::Value *ObjCSuper =
826 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
827 llvm::Value *ReceiverAsObject =
828 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
829 CGF.Builder.CreateStore(ReceiverAsObject,
830 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000831
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000832 // If this is a class message the metaclass is passed as the target.
833 llvm::Value *Target;
834 if (IsClassMessage) {
835 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
836 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
837 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
838 Target = Super;
839 } else {
840 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
841 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000842 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
843 // and ObjCTypes types.
844 const llvm::Type *ClassTy =
845 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000846 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000847 CGF.Builder.CreateStore(Target,
848 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
849
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000850 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000851 ObjCSuper, ObjCTypes.SuperPtrCTy,
852 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000853}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000854
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000855/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000856CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000857 QualType ResultType,
858 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000859 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000860 bool IsClassMessage,
861 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000862 llvm::Value *Arg0 =
863 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000864 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000865 Arg0, CGF.getContext().getObjCIdType(),
866 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000867}
868
869CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000870 QualType ResultType,
871 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000872 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000873 QualType Arg0Ty,
874 bool IsSuper,
875 const CallArgList &CallArgs) {
876 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000877 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
878 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
879 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000880 CGF.getContext().getObjCSelType()));
881 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000882
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000883 CodeGenTypes &Types = CGM.getTypes();
884 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
885 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbar5669e572008-10-17 03:24:53 +0000886
887 llvm::Constant *Fn;
Daniel Dunbar88b53962009-02-02 22:03:45 +0000888 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000889 Fn = ObjCTypes.getSendStretFn(IsSuper);
890 } else if (ResultType->isFloatingType()) {
891 // FIXME: Sadly, this is wrong. This actually depends on the
892 // architecture. This happens to be right for x86-32 though.
893 Fn = ObjCTypes.getSendFpretFn(IsSuper);
894 } else {
895 Fn = ObjCTypes.getSendFn(IsSuper);
896 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000897 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +0000898 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000899}
900
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000901llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000902 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +0000903 // FIXME: I don't understand why gcc generates this, or where it is
904 // resolved. Investigate. Its also wasteful to look this up over and
905 // over.
906 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
907
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000908 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
909 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000910}
911
Fariborz Jahanianda320092009-01-29 19:24:30 +0000912void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000913 // FIXME: We shouldn't need this, the protocol decl should contain
914 // enough information to tell us whether this was a declaration or a
915 // definition.
916 DefinedProtocols.insert(PD->getIdentifier());
917
918 // If we have generated a forward reference to this protocol, emit
919 // it now. Otherwise do nothing, the protocol objects are lazily
920 // emitted.
921 if (Protocols.count(PD->getIdentifier()))
922 GetOrEmitProtocol(PD);
923}
924
Fariborz Jahanianda320092009-01-29 19:24:30 +0000925llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000926 if (DefinedProtocols.count(PD->getIdentifier()))
927 return GetOrEmitProtocol(PD);
928 return GetOrEmitProtocolRef(PD);
929}
930
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000931/*
932 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
933 struct _objc_protocol {
934 struct _objc_protocol_extension *isa;
935 char *protocol_name;
936 struct _objc_protocol_list *protocol_list;
937 struct _objc__method_prototype_list *instance_methods;
938 struct _objc__method_prototype_list *class_methods
939 };
940
941 See EmitProtocolExtension().
942*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000943llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
944 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
945
946 // Early exit if a defining object has already been generated.
947 if (Entry && Entry->hasInitializer())
948 return Entry;
949
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000950 // FIXME: I don't understand why gcc generates this, or where it is
951 // resolved. Investigate. Its also wasteful to look this up over and
952 // over.
953 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
954
Chris Lattner8ec03f52008-11-24 03:54:41 +0000955 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000956
957 // Construct method lists.
958 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
959 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
960 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
961 e = PD->instmeth_end(); i != e; ++i) {
962 ObjCMethodDecl *MD = *i;
963 llvm::Constant *C = GetMethodDescriptionConstant(MD);
964 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
965 OptInstanceMethods.push_back(C);
966 } else {
967 InstanceMethods.push_back(C);
968 }
969 }
970
971 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
972 e = PD->classmeth_end(); i != e; ++i) {
973 ObjCMethodDecl *MD = *i;
974 llvm::Constant *C = GetMethodDescriptionConstant(MD);
975 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
976 OptClassMethods.push_back(C);
977 } else {
978 ClassMethods.push_back(C);
979 }
980 }
981
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000982 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000983 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000984 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000985 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000986 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +0000987 PD->protocol_begin(),
988 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000989 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000990 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
991 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000992 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
993 InstanceMethods);
994 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000995 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
996 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000997 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
998 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000999 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
1000 Values);
1001
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001002 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001003 // Already created, fix the linkage and update the initializer.
1004 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001005 Entry->setInitializer(Init);
1006 } else {
1007 Entry =
1008 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
1009 llvm::GlobalValue::InternalLinkage,
1010 Init,
1011 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
1012 &CGM.getModule());
1013 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1014 UsedGlobals.push_back(Entry);
1015 // FIXME: Is this necessary? Why only for protocol?
1016 Entry->setAlignment(4);
1017 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001018
1019 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001020}
1021
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001022llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001023 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1024
1025 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001026 // We use the initializer as a marker of whether this is a forward
1027 // reference or not. At module finalization we add the empty
1028 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001029 Entry =
1030 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001031 llvm::GlobalValue::ExternalLinkage,
1032 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001033 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001034 &CGM.getModule());
1035 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1036 UsedGlobals.push_back(Entry);
1037 // FIXME: Is this necessary? Why only for protocol?
1038 Entry->setAlignment(4);
1039 }
1040
1041 return Entry;
1042}
1043
1044/*
1045 struct _objc_protocol_extension {
1046 uint32_t size;
1047 struct objc_method_description_list *optional_instance_methods;
1048 struct objc_method_description_list *optional_class_methods;
1049 struct objc_property_list *instance_properties;
1050 };
1051*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001052llvm::Constant *
1053CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1054 const ConstantVector &OptInstanceMethods,
1055 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001056 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001057 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001058 std::vector<llvm::Constant*> Values(4);
1059 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001060 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001061 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1062 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001063 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1064 OptInstanceMethods);
1065 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001066 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1067 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001068 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1069 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001070 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1071 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001072 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001073
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001074 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001075 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1076 Values[3]->isNullValue())
1077 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1078
1079 llvm::Constant *Init =
1080 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
1081 llvm::GlobalVariable *GV =
1082 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
1083 llvm::GlobalValue::InternalLinkage,
1084 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001085 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001086 &CGM.getModule());
1087 // No special section, but goes in llvm.used
1088 UsedGlobals.push_back(GV);
1089
1090 return GV;
1091}
1092
1093/*
1094 struct objc_protocol_list {
1095 struct objc_protocol_list *next;
1096 long count;
1097 Protocol *list[];
1098 };
1099*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001100llvm::Constant *
1101CGObjCMac::EmitProtocolList(const std::string &Name,
1102 ObjCProtocolDecl::protocol_iterator begin,
1103 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001104 std::vector<llvm::Constant*> ProtocolRefs;
1105
Daniel Dunbardbc933702008-08-21 21:57:41 +00001106 for (; begin != end; ++begin)
1107 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001108
1109 // Just return null for empty protocol lists
1110 if (ProtocolRefs.empty())
1111 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1112
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001113 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001114 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1115
1116 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001117 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001118 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1119 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1120 Values[2] =
1121 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1122 ProtocolRefs.size()),
1123 ProtocolRefs);
1124
1125 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1126 llvm::GlobalVariable *GV =
1127 new llvm::GlobalVariable(Init->getType(), false,
1128 llvm::GlobalValue::InternalLinkage,
1129 Init,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001130 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001131 &CGM.getModule());
1132 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
1133 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1134}
1135
1136/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001137 struct _objc_property {
1138 const char * const name;
1139 const char * const attributes;
1140 };
1141
1142 struct _objc_property_list {
1143 uint32_t entsize; // sizeof (struct _objc_property)
1144 uint32_t prop_count;
1145 struct _objc_property[prop_count];
1146 };
1147*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001148llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1149 const Decl *Container,
1150 const ObjCContainerDecl *OCD,
1151 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001152 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +00001153 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1154 E = OCD->prop_end(); I != E; ++I) {
1155 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001156 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001157 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001158 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1159 Prop));
1160 }
1161
1162 // Return null for empty list.
1163 if (Properties.empty())
1164 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1165
1166 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001167 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001168 std::vector<llvm::Constant*> Values(3);
1169 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1170 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1171 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1172 Properties.size());
1173 Values[2] = llvm::ConstantArray::get(AT, Properties);
1174 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1175
1176 llvm::GlobalVariable *GV =
1177 new llvm::GlobalVariable(Init->getType(), false,
1178 llvm::GlobalValue::InternalLinkage,
1179 Init,
1180 Name,
1181 &CGM.getModule());
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001182 if (ObjCABI == 2)
1183 GV->setSection("__DATA, __objc_const");
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001184 // No special section on property lists?
1185 UsedGlobals.push_back(GV);
1186 return llvm::ConstantExpr::getBitCast(GV,
1187 ObjCTypes.PropertyListPtrTy);
1188
1189}
1190
1191/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001192 struct objc_method_description_list {
1193 int count;
1194 struct objc_method_description list[];
1195 };
1196*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001197llvm::Constant *
1198CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1199 std::vector<llvm::Constant*> Desc(2);
1200 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1201 ObjCTypes.SelectorPtrTy);
1202 Desc[1] = GetMethodVarType(MD);
1203 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1204 Desc);
1205}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001206
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001207llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1208 const char *Section,
1209 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001210 // Return null for empty list.
1211 if (Methods.empty())
1212 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1213
1214 std::vector<llvm::Constant*> Values(2);
1215 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1216 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1217 Methods.size());
1218 Values[1] = llvm::ConstantArray::get(AT, Methods);
1219 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1220
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001221 llvm::GlobalVariable *GV =
1222 new llvm::GlobalVariable(Init->getType(), false,
1223 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001224 Init, Name, &CGM.getModule());
1225 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001226 UsedGlobals.push_back(GV);
1227 return llvm::ConstantExpr::getBitCast(GV,
1228 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001229}
1230
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001231/*
1232 struct _objc_category {
1233 char *category_name;
1234 char *class_name;
1235 struct _objc_method_list *instance_methods;
1236 struct _objc_method_list *class_methods;
1237 struct _objc_protocol_list *protocols;
1238 uint32_t size; // <rdar://4585769>
1239 struct _objc_property_list *instance_properties;
1240 };
1241 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001242void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001243 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001244
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001245 // FIXME: This is poor design, the OCD should have a pointer to the
1246 // category decl. Additionally, note that Category can be null for
1247 // the @implementation w/o an @interface case. Sema should just
1248 // create one for us as it does for @implementation so everyone else
1249 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001250 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001251 const ObjCCategoryDecl *Category =
1252 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001253 std::string ExtName(Interface->getNameAsString() + "_" +
1254 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001255
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001256 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1257 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1258 e = OCD->instmeth_end(); i != e; ++i) {
1259 // Instance methods should always be defined.
1260 InstanceMethods.push_back(GetMethodConstant(*i));
1261 }
1262 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1263 e = OCD->classmeth_end(); i != e; ++i) {
1264 // Class methods should always be defined.
1265 ClassMethods.push_back(GetMethodConstant(*i));
1266 }
1267
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001268 std::vector<llvm::Constant*> Values(7);
1269 Values[0] = GetClassName(OCD->getIdentifier());
1270 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001271 Values[2] =
1272 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1273 ExtName,
1274 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001275 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001276 Values[3] =
1277 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1278 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001279 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001280 if (Category) {
1281 Values[4] =
1282 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1283 Category->protocol_begin(),
1284 Category->protocol_end());
1285 } else {
1286 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1287 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001288 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001289
1290 // If there is no category @interface then there can be no properties.
1291 if (Category) {
1292 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001293 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001294 } else {
1295 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1296 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001297
1298 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1299 Values);
1300
1301 llvm::GlobalVariable *GV =
1302 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1303 llvm::GlobalValue::InternalLinkage,
1304 Init,
1305 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1306 &CGM.getModule());
1307 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1308 UsedGlobals.push_back(GV);
1309 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001310}
1311
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001312// FIXME: Get from somewhere?
1313enum ClassFlags {
1314 eClassFlags_Factory = 0x00001,
1315 eClassFlags_Meta = 0x00002,
1316 // <rdr://5142207>
1317 eClassFlags_HasCXXStructors = 0x02000,
1318 eClassFlags_Hidden = 0x20000,
1319 eClassFlags_ABI2_Hidden = 0x00010,
1320 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1321};
1322
1323// <rdr://5142207&4705298&4843145>
1324static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1325 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1326 // FIXME: Support -fvisibility
1327 switch (attr->getVisibility()) {
1328 default:
1329 assert(0 && "Unknown visibility");
1330 return false;
1331 case VisibilityAttr::DefaultVisibility:
1332 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1333 return false;
1334 case VisibilityAttr::HiddenVisibility:
1335 return true;
1336 }
1337 } else {
1338 return false; // FIXME: Support -fvisibility
1339 }
1340}
1341
1342/*
1343 struct _objc_class {
1344 Class isa;
1345 Class super_class;
1346 const char *name;
1347 long version;
1348 long info;
1349 long instance_size;
1350 struct _objc_ivar_list *ivars;
1351 struct _objc_method_list *methods;
1352 struct _objc_cache *cache;
1353 struct _objc_protocol_list *protocols;
1354 // Objective-C 1.0 extensions (<rdr://4585769>)
1355 const char *ivar_layout;
1356 struct _objc_class_ext *ext;
1357 };
1358
1359 See EmitClassExtension();
1360 */
1361void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001362 DefinedSymbols.insert(ID->getIdentifier());
1363
Chris Lattner8ec03f52008-11-24 03:54:41 +00001364 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001365 // FIXME: Gross
1366 ObjCInterfaceDecl *Interface =
1367 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001368 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001369 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001370 Interface->protocol_begin(),
1371 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001372 const llvm::Type *InterfaceTy =
1373 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
1374 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001375 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001376
1377 // FIXME: Set CXX-structors flag.
1378 if (IsClassHidden(ID->getClassInterface()))
1379 Flags |= eClassFlags_Hidden;
1380
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001381 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1382 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1383 e = ID->instmeth_end(); i != e; ++i) {
1384 // Instance methods should always be defined.
1385 InstanceMethods.push_back(GetMethodConstant(*i));
1386 }
1387 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1388 e = ID->classmeth_end(); i != e; ++i) {
1389 // Class methods should always be defined.
1390 ClassMethods.push_back(GetMethodConstant(*i));
1391 }
1392
1393 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1394 e = ID->propimpl_end(); i != e; ++i) {
1395 ObjCPropertyImplDecl *PID = *i;
1396
1397 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1398 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1399
1400 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1401 if (llvm::Constant *C = GetMethodConstant(MD))
1402 InstanceMethods.push_back(C);
1403 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1404 if (llvm::Constant *C = GetMethodConstant(MD))
1405 InstanceMethods.push_back(C);
1406 }
1407 }
1408
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001409 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001410 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001411 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001412 // Record a reference to the super class.
1413 LazySymbols.insert(Super->getIdentifier());
1414
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001415 Values[ 1] =
1416 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1417 ObjCTypes.ClassPtrTy);
1418 } else {
1419 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1420 }
1421 Values[ 2] = GetClassName(ID->getIdentifier());
1422 // Version is always 0.
1423 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1424 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1425 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001426 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001427 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001428 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001429 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001430 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001431 // cache is always NULL.
1432 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1433 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001434 // FIXME: Set ivar_layout
1435 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001436 Values[11] = EmitClassExtension(ID);
1437 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1438 Values);
1439
1440 llvm::GlobalVariable *GV =
1441 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1442 llvm::GlobalValue::InternalLinkage,
1443 Init,
1444 std::string("\01L_OBJC_CLASS_")+ClassName,
1445 &CGM.getModule());
1446 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1447 UsedGlobals.push_back(GV);
1448 // FIXME: Why?
1449 GV->setAlignment(32);
1450 DefinedClasses.push_back(GV);
1451}
1452
1453llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1454 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001455 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001456 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001457 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001458 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001459
1460 if (IsClassHidden(ID->getClassInterface()))
1461 Flags |= eClassFlags_Hidden;
1462
1463 std::vector<llvm::Constant*> Values(12);
1464 // The isa for the metaclass is the root of the hierarchy.
1465 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1466 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1467 Root = Super;
1468 Values[ 0] =
1469 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1470 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001471 // The super class for the metaclass is emitted as the name of the
1472 // super class. The runtime fixes this up to point to the
1473 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001474 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1475 Values[ 1] =
1476 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1477 ObjCTypes.ClassPtrTy);
1478 } else {
1479 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1480 }
1481 Values[ 2] = GetClassName(ID->getIdentifier());
1482 // Version is always 0.
1483 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1484 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1485 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001486 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001487 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001488 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001489 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001490 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001491 // cache is always NULL.
1492 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1493 Values[ 9] = Protocols;
1494 // ivar_layout for metaclass is always NULL.
1495 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1496 // The class extension is always unused for metaclasses.
1497 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1498 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1499 Values);
1500
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001501 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001502 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001503
1504 // Check for a forward reference.
1505 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1506 if (GV) {
1507 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1508 "Forward metaclass reference has incorrect type.");
1509 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1510 GV->setInitializer(Init);
1511 } else {
1512 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1513 llvm::GlobalValue::InternalLinkage,
1514 Init, Name,
1515 &CGM.getModule());
1516 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001517 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1518 UsedGlobals.push_back(GV);
1519 // FIXME: Why?
1520 GV->setAlignment(32);
1521
1522 return GV;
1523}
1524
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001525llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001526 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001527
1528 // FIXME: Should we look these up somewhere other than the
1529 // module. Its a bit silly since we only generate these while
1530 // processing an implementation, so exactly one pointer would work
1531 // if know when we entered/exitted an implementation block.
1532
1533 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001534 // Previously, metaclass with internal linkage may have been defined.
1535 // pass 'true' as 2nd argument so it is returned.
1536 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001537 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1538 "Forward metaclass reference has incorrect type.");
1539 return GV;
1540 } else {
1541 // Generate as an external reference to keep a consistent
1542 // module. This will be patched up when we emit the metaclass.
1543 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1544 llvm::GlobalValue::ExternalLinkage,
1545 0,
1546 Name,
1547 &CGM.getModule());
1548 }
1549}
1550
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001551/*
1552 struct objc_class_ext {
1553 uint32_t size;
1554 const char *weak_ivar_layout;
1555 struct _objc_property_list *properties;
1556 };
1557*/
1558llvm::Constant *
1559CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1560 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001561 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001562
1563 std::vector<llvm::Constant*> Values(3);
1564 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001565 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001566 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001567 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001568 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001569
1570 // Return null if no extension bits are used.
1571 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1572 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1573
1574 llvm::Constant *Init =
1575 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1576 llvm::GlobalVariable *GV =
1577 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1578 llvm::GlobalValue::InternalLinkage,
1579 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001580 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001581 &CGM.getModule());
1582 // No special section, but goes in llvm.used
1583 UsedGlobals.push_back(GV);
1584
1585 return GV;
1586}
1587
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001588/// countInheritedIvars - count number of ivars in class and its super class(s)
1589///
1590static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1591 int count = 0;
1592 if (!OI)
1593 return 0;
1594 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1595 if (SuperClass)
1596 count += countInheritedIvars(SuperClass);
1597 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1598 E = OI->ivar_end(); I != E; ++I)
1599 ++count;
1600 return count;
1601}
1602
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001603/*
1604 struct objc_ivar {
1605 char *ivar_name;
1606 char *ivar_type;
1607 int ivar_offset;
1608 };
1609
1610 struct objc_ivar_list {
1611 int ivar_count;
1612 struct objc_ivar list[count];
1613 };
1614 */
1615llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001616 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001617 std::vector<llvm::Constant*> Ivars, Ivar(3);
1618
1619 // When emitting the root class GCC emits ivar entries for the
1620 // actual class structure. It is not clear if we need to follow this
1621 // behavior; for now lets try and get away with not doing it. If so,
1622 // the cleanest solution would be to make up an ObjCInterfaceDecl
1623 // for the class.
1624 if (ForClass)
1625 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001626
1627 ObjCInterfaceDecl *OID =
1628 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
1629 const llvm::Type *InterfaceTy =
1630 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001631 const llvm::StructLayout *Layout =
1632 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001633
1634 RecordDecl::field_iterator ifield, pfield;
1635 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001636 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1637 FieldDecl *Field = *ifield;
1638 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1639 getLLVMFieldNo(Field));
1640 if (Field->getIdentifier())
1641 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1642 else
1643 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001644 std::string TypeStr;
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001645 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001646 Ivar[1] = GetMethodVarType(TypeStr);
1647 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001648 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001649 }
1650
1651 // Return null for empty list.
1652 if (Ivars.empty())
1653 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1654
1655 std::vector<llvm::Constant*> Values(2);
1656 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1657 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1658 Ivars.size());
1659 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1660 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1661
1662 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1663 "\01L_OBJC_INSTANCE_VARIABLES_");
1664 llvm::GlobalVariable *GV =
1665 new llvm::GlobalVariable(Init->getType(), false,
1666 llvm::GlobalValue::InternalLinkage,
1667 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001668 Prefix + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001669 &CGM.getModule());
1670 if (ForClass) {
1671 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1672 // FIXME: Why is this only here?
1673 GV->setAlignment(32);
1674 } else {
1675 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1676 }
1677 UsedGlobals.push_back(GV);
1678 return llvm::ConstantExpr::getBitCast(GV,
1679 ObjCTypes.IvarListPtrTy);
1680}
1681
1682/*
1683 struct objc_method {
1684 SEL method_name;
1685 char *method_types;
1686 void *method;
1687 };
1688
1689 struct objc_method_list {
1690 struct objc_method_list *obsolete;
1691 int count;
1692 struct objc_method methods_list[count];
1693 };
1694*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001695
1696/// GetMethodConstant - Return a struct objc_method constant for the
1697/// given method if it has been defined. The result is null if the
1698/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001699llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001700 // FIXME: Use DenseMap::lookup
1701 llvm::Function *Fn = MethodDefinitions[MD];
1702 if (!Fn)
1703 return 0;
1704
1705 std::vector<llvm::Constant*> Method(3);
1706 Method[0] =
1707 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1708 ObjCTypes.SelectorPtrTy);
1709 Method[1] = GetMethodVarType(MD);
1710 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1711 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1712}
1713
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001714llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1715 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001716 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001717 // Return null for empty list.
1718 if (Methods.empty())
1719 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1720
1721 std::vector<llvm::Constant*> Values(3);
1722 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1723 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1724 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1725 Methods.size());
1726 Values[2] = llvm::ConstantArray::get(AT, Methods);
1727 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1728
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001729 llvm::GlobalVariable *GV =
1730 new llvm::GlobalVariable(Init->getType(), false,
1731 llvm::GlobalValue::InternalLinkage,
1732 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001733 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001734 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001735 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001736 UsedGlobals.push_back(GV);
1737 return llvm::ConstantExpr::getBitCast(GV,
1738 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001739}
1740
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001741llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001742 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001743 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001744 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001745
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001746 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001747 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001748 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001749 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001750 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001751 llvm::GlobalValue::InternalLinkage,
1752 Name,
1753 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001754 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001755
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001756 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001757}
1758
1759llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001760 // Abuse this interface function as a place to finalize.
1761 FinishModule();
1762
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001763 return NULL;
1764}
1765
Daniel Dunbar49f66022008-09-24 03:38:44 +00001766llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1767 return ObjCTypes.GetPropertyFn;
1768}
1769
1770llvm::Function *CGObjCMac::GetPropertySetFunction() {
1771 return ObjCTypes.SetPropertyFn;
1772}
1773
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001774llvm::Function *CGObjCMac::EnumerationMutationFunction()
1775{
1776 return ObjCTypes.EnumerationMutationFn;
1777}
1778
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001779/*
1780
1781Objective-C setjmp-longjmp (sjlj) Exception Handling
1782--
1783
1784The basic framework for a @try-catch-finally is as follows:
1785{
1786 objc_exception_data d;
1787 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00001788 bool _call_try_exit = true;
1789
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001790 objc_exception_try_enter(&d);
1791 if (!setjmp(d.jmp_buf)) {
1792 ... try body ...
1793 } else {
1794 // exception path
1795 id _caught = objc_exception_extract(&d);
1796
1797 // enter new try scope for handlers
1798 if (!setjmp(d.jmp_buf)) {
1799 ... match exception and execute catch blocks ...
1800
1801 // fell off end, rethrow.
1802 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001803 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001804 } else {
1805 // exception in catch block
1806 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00001807 _call_try_exit = false;
1808 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001809 }
1810 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001811 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001812
1813finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00001814 if (_call_try_exit)
1815 objc_exception_try_exit(&d);
1816
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001817 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001818 ... dispatch to finally destination ...
1819
1820finally_rethrow:
1821 objc_exception_throw(_rethrow);
1822
1823finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001824}
1825
1826This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001827uses _rethrow to determine if objc_exception_try_exit should be called
1828and if the object should be rethrown. This breaks in the face of
1829throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001830
1831We specialize this framework for a few particular circumstances:
1832
1833 - If there are no catch blocks, then we avoid emitting the second
1834 exception handling context.
1835
1836 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1837 e)) we avoid emitting the code to rethrow an uncaught exception.
1838
1839 - FIXME: If there is no @finally block we can do a few more
1840 simplifications.
1841
1842Rethrows and Jumps-Through-Finally
1843--
1844
1845Support for implicit rethrows and jumping through the finally block is
1846handled by storing the current exception-handling context in
1847ObjCEHStack.
1848
Daniel Dunbar898d5082008-09-30 01:06:03 +00001849In order to implement proper @finally semantics, we support one basic
1850mechanism for jumping through the finally block to an arbitrary
1851destination. Constructs which generate exits from a @try or @catch
1852block use this mechanism to implement the proper semantics by chaining
1853jumps, as necessary.
1854
1855This mechanism works like the one used for indirect goto: we
1856arbitrarily assign an ID to each destination and store the ID for the
1857destination in a variable prior to entering the finally block. At the
1858end of the finally block we simply create a switch to the proper
1859destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001860
1861Code gen for @synchronized(expr) stmt;
1862Effectively generating code for:
1863objc_sync_enter(expr);
1864@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001865*/
1866
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001867void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1868 const Stmt &S) {
1869 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001870 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001871 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00001872 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00001873 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1874 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1875 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001876
1877 // Push an EH context entry, used for handling rethrows and jumps
1878 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00001879 CGF.PushCleanupBlock(FinallyBlock);
1880
Anders Carlsson273558f2009-02-07 21:37:21 +00001881 CGF.ObjCEHValueStack.push_back(0);
1882
Daniel Dunbar898d5082008-09-30 01:06:03 +00001883 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00001884 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1885 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001886 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1887 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00001888 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
1889 "_call_try_exit");
1890 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr);
1891
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001892 if (!isTry) {
1893 // For @synchronized, call objc_sync_enter(sync.expr)
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001894 llvm::Value *Arg = CGF.EmitScalarExpr(
1895 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1896 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1897 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001898 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001899
1900 // Enter a new try block and call setjmp.
1901 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1902 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1903 "jmpbufarray");
1904 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1905 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1906 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001907
Daniel Dunbar55e87422008-11-11 02:29:29 +00001908 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1909 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001910 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001911 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001912
1913 // Emit the @try block.
1914 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001915 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1916 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00001917 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001918
1919 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001920 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001921
1922 // Retrieve the exception object. We may emit multiple blocks but
1923 // nothing can cross this so the value is already in SSA form.
1924 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1925 ExceptionData,
1926 "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00001927 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001928 if (!isTry)
1929 {
1930 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00001931 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00001932 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001933 }
1934 else if (const ObjCAtCatchStmt* CatchStmt =
1935 cast<ObjCAtTryStmt>(S).getCatchStmts())
1936 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00001937 // Enter a new exception try block (in case a @catch block throws
1938 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00001939 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001940
Anders Carlsson80f25672008-09-09 17:59:25 +00001941 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1942 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001943 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00001944
Daniel Dunbar55e87422008-11-11 02:29:29 +00001945 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1946 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001947 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001948
1949 CGF.EmitBlock(CatchBlock);
1950
Daniel Dunbar55e40722008-09-27 07:03:52 +00001951 // Handle catch list. As a special case we check if everything is
1952 // matched and avoid generating code for falling off the end if
1953 // so.
1954 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00001955 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00001956 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00001957
Anders Carlssondde0a942008-09-11 09:15:33 +00001958 const DeclStmt *CatchParam =
1959 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001960 const VarDecl *VD = 0;
1961 const PointerType *PT = 0;
1962
Anders Carlsson80f25672008-09-09 17:59:25 +00001963 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00001964 if (!CatchParam) {
1965 AllMatched = true;
1966 } else {
Ted Kremenekde3b8fb2008-10-06 20:58:56 +00001967 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001968 PT = VD->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001969
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001970 // catch(id e) always matches.
1971 // FIXME: For the time being we also match id<X>; this should
1972 // be rejected by Sema instead.
1973 if ((PT && CGF.getContext().isObjCIdType(PT->getPointeeType())) ||
1974 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00001975 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00001976 }
1977
Daniel Dunbar55e40722008-09-27 07:03:52 +00001978 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00001979 if (CatchParam) {
1980 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001981 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar129271a2008-09-27 07:36:24 +00001982 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001983 }
Anders Carlsson1452f552008-09-11 08:21:54 +00001984
Anders Carlssondde0a942008-09-11 09:15:33 +00001985 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00001986 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001987 break;
1988 }
1989
Daniel Dunbar129271a2008-09-27 07:36:24 +00001990 assert(PT && "Unexpected non-pointer type in @catch");
1991 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00001992 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001993 assert(ObjCType && "Catch parameter must have Objective-C type!");
1994
1995 // Check if the @catch block matches the exception object.
1996 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
1997
Anders Carlsson80f25672008-09-09 17:59:25 +00001998 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
1999 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00002000
Daniel Dunbar55e87422008-11-11 02:29:29 +00002001 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00002002
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002003 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002004 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002005
2006 // Emit the @catch block.
2007 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002008 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002009 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002010
2011 llvm::Value *Tmp =
2012 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
2013 "tmp");
2014 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00002015
2016 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002017 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002018
2019 CGF.EmitBlock(NextCatchBlock);
2020 }
2021
Daniel Dunbar55e40722008-09-27 07:03:52 +00002022 if (!AllMatched) {
2023 // None of the handlers caught the exception, so store it to be
2024 // rethrown at the end of the @finally block.
2025 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002026 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002027 }
2028
2029 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002030 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002031 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
2032 ExceptionData),
2033 RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002034 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002035 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002036 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002037 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002038 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002039 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002040 }
2041
Daniel Dunbar898d5082008-09-30 01:06:03 +00002042 // Pop the exception-handling stack entry. It is important to do
2043 // this now, because the code in the @finally block is not in this
2044 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002045 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2046
Anders Carlsson273558f2009-02-07 21:37:21 +00002047 CGF.ObjCEHValueStack.pop_back();
2048
Anders Carlsson80f25672008-09-09 17:59:25 +00002049 // Emit the @finally block.
2050 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002051 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2052
2053 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2054
2055 CGF.EmitBlock(FinallyExit);
Anders Carlsson80f25672008-09-09 17:59:25 +00002056 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002057
2058 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002059 if (isTry) {
2060 if (const ObjCAtFinallyStmt* FinallyStmt =
2061 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2062 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2063 }
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002064 else {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002065 // For @synchronized objc_sync_exit(expr); As finally's sole statement.
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002066 // For @synchronized, call objc_sync_enter(sync.expr)
2067 llvm::Value *Arg = CGF.EmitScalarExpr(
2068 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2069 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
2070 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg);
2071 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002072
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002073 // Emit the switch block
2074 if (Info.SwitchBlock)
2075 CGF.EmitBlock(Info.SwitchBlock);
2076 if (Info.EndBlock)
2077 CGF.EmitBlock(Info.EndBlock);
2078
Daniel Dunbar898d5082008-09-30 01:06:03 +00002079 CGF.EmitBlock(FinallyRethrow);
2080 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
2081 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002082 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002083
2084 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002085}
2086
2087void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002088 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002089 llvm::Value *ExceptionAsObject;
2090
2091 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2092 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2093 ExceptionAsObject =
2094 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2095 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002096 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002097 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002098 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002099 }
2100
2101 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002102 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002103
2104 // Clear the insertion point to indicate we are in unreachable code.
2105 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002106}
2107
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002108/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002109/// object: objc_read_weak (id *src)
2110///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002111llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002112 llvm::Value *AddrWeakObj)
2113{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002114 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002115 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002116 AddrWeakObj, "weakread");
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002117 return read_weak;
2118}
2119
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002120/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2121/// objc_assign_weak (id src, id *dst)
2122///
2123void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2124 llvm::Value *src, llvm::Value *dst)
2125{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002126 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2127 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002128 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2129 src, dst, "weakassign");
2130 return;
2131}
2132
Fariborz Jahanian58626502008-11-19 00:59:10 +00002133/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2134/// objc_assign_global (id src, id *dst)
2135///
2136void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2137 llvm::Value *src, llvm::Value *dst)
2138{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002139 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2140 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002141 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2142 src, dst, "globalassign");
2143 return;
2144}
2145
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002146/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2147/// objc_assign_ivar (id src, id *dst)
2148///
2149void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2150 llvm::Value *src, llvm::Value *dst)
2151{
2152 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2153 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2154 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2155 src, dst, "assignivar");
2156 return;
2157}
2158
Fariborz Jahanian58626502008-11-19 00:59:10 +00002159/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2160/// objc_assign_strongCast (id src, id *dst)
2161///
2162void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2163 llvm::Value *src, llvm::Value *dst)
2164{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002165 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2166 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002167 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2168 src, dst, "weakassign");
2169 return;
2170}
2171
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002172/// EmitObjCValueForIvar - Code Gen for ivar reference.
2173///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002174LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2175 QualType ObjectTy,
2176 llvm::Value *BaseValue,
2177 const ObjCIvarDecl *Ivar,
2178 const FieldDecl *Field,
2179 unsigned CVRQualifiers) {
2180 if (Ivar->isBitField())
2181 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
2182 CVRQualifiers);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002183 // TODO: Add a special case for isa (index 0)
2184 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
2185 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002186 LValue LV = LValue::MakeAddr(V,
2187 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
2188 LValue::SetObjCIvar(LV, true);
2189 return LV;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002190}
2191
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002192llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
2193 ObjCInterfaceDecl *Interface,
2194 const ObjCIvarDecl *Ivar) {
2195 const llvm::Type *InterfaceLTy =
2196 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
2197 const llvm::StructLayout *Layout =
2198 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceLTy));
2199 FieldDecl *Field = Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar);
2200 uint64_t Offset =
2201 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
2202
2203 return llvm::ConstantInt::get(
2204 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2205 Offset);
2206}
2207
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002208/* *** Private Interface *** */
2209
2210/// EmitImageInfo - Emit the image info marker used to encode some module
2211/// level information.
2212///
2213/// See: <rdr://4810609&4810587&4810587>
2214/// struct IMAGE_INFO {
2215/// unsigned version;
2216/// unsigned flags;
2217/// };
2218enum ImageInfoFlags {
2219 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2220 eImageInfo_GarbageCollected = (1 << 1),
2221 eImageInfo_GCOnly = (1 << 2)
2222};
2223
2224void CGObjCMac::EmitImageInfo() {
2225 unsigned version = 0; // Version is unused?
2226 unsigned flags = 0;
2227
2228 // FIXME: Fix and continue?
2229 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2230 flags |= eImageInfo_GarbageCollected;
2231 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2232 flags |= eImageInfo_GCOnly;
2233
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002234 // Emitted as int[2];
2235 llvm::Constant *values[2] = {
2236 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2237 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2238 };
2239 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002240 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002241 new llvm::GlobalVariable(AT, true,
2242 llvm::GlobalValue::InternalLinkage,
2243 llvm::ConstantArray::get(AT, values, 2),
2244 "\01L_OBJC_IMAGE_INFO",
2245 &CGM.getModule());
2246
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002247 if (ObjCABI == 1) {
2248 GV->setSection("__OBJC, __image_info,regular");
2249 } else {
2250 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2251 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002252
2253 UsedGlobals.push_back(GV);
2254}
2255
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002256
2257// struct objc_module {
2258// unsigned long version;
2259// unsigned long size;
2260// const char *name;
2261// Symtab symtab;
2262// };
2263
2264// FIXME: Get from somewhere
2265static const int ModuleVersion = 7;
2266
2267void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002268 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002269
2270 std::vector<llvm::Constant*> Values(4);
2271 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2272 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002273 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002274 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002275 Values[3] = EmitModuleSymbols();
2276
2277 llvm::GlobalVariable *GV =
2278 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2279 llvm::GlobalValue::InternalLinkage,
2280 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2281 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002282 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002283 &CGM.getModule());
2284 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2285 UsedGlobals.push_back(GV);
2286}
2287
2288llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002289 unsigned NumClasses = DefinedClasses.size();
2290 unsigned NumCategories = DefinedCategories.size();
2291
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002292 // Return null if no symbols were defined.
2293 if (!NumClasses && !NumCategories)
2294 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2295
2296 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002297 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2298 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2299 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2300 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2301
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002302 // The runtime expects exactly the list of defined classes followed
2303 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002304 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002305 for (unsigned i=0; i<NumClasses; i++)
2306 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2307 ObjCTypes.Int8PtrTy);
2308 for (unsigned i=0; i<NumCategories; i++)
2309 Symbols[NumClasses + i] =
2310 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2311 ObjCTypes.Int8PtrTy);
2312
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002313 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002314 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002315 NumClasses + NumCategories),
2316 Symbols);
2317
2318 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2319
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002320 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002321 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002322 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002323 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002324 "\01L_OBJC_SYMBOLS",
2325 &CGM.getModule());
2326 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2327 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002328 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2329}
2330
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002331llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002332 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002333 LazySymbols.insert(ID->getIdentifier());
2334
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002335 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2336
2337 if (!Entry) {
2338 llvm::Constant *Casted =
2339 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2340 ObjCTypes.ClassPtrTy);
2341 Entry =
2342 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2343 llvm::GlobalValue::InternalLinkage,
2344 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2345 &CGM.getModule());
2346 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2347 UsedGlobals.push_back(Entry);
2348 }
2349
2350 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002351}
2352
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002353llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002354 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2355
2356 if (!Entry) {
2357 llvm::Constant *Casted =
2358 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2359 ObjCTypes.SelectorPtrTy);
2360 Entry =
2361 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2362 llvm::GlobalValue::InternalLinkage,
2363 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2364 &CGM.getModule());
2365 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2366 UsedGlobals.push_back(Entry);
2367 }
2368
2369 return Builder.CreateLoad(Entry, false, "tmp");
2370}
2371
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002372llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002373 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002374
2375 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002376 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002377 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002378 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002379 llvm::GlobalValue::InternalLinkage,
2380 C, "\01L_OBJC_CLASS_NAME_",
2381 &CGM.getModule());
2382 Entry->setSection("__TEXT,__cstring,cstring_literals");
2383 UsedGlobals.push_back(Entry);
2384 }
2385
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002386 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002387}
2388
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002389llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002390 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2391
2392 if (!Entry) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00002393 // FIXME: Avoid std::string copying.
2394 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002395 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002396 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002397 llvm::GlobalValue::InternalLinkage,
2398 C, "\01L_OBJC_METH_VAR_NAME_",
2399 &CGM.getModule());
2400 Entry->setSection("__TEXT,__cstring,cstring_literals");
2401 UsedGlobals.push_back(Entry);
2402 }
2403
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002404 return getConstantGEP(Entry, 0, 0);
2405}
2406
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002407// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002408llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002409 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2410}
2411
2412// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002413llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002414 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2415}
2416
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002417llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002418 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002419
2420 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002421 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002422 Entry =
2423 new llvm::GlobalVariable(C->getType(), false,
2424 llvm::GlobalValue::InternalLinkage,
2425 C, "\01L_OBJC_METH_VAR_TYPE_",
2426 &CGM.getModule());
2427 Entry->setSection("__TEXT,__cstring,cstring_literals");
2428 UsedGlobals.push_back(Entry);
2429 }
2430
2431 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002432}
2433
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002434// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002435llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002436 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002437 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2438 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002439 return GetMethodVarType(TypeStr);
2440}
2441
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002442// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002443llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002444 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2445
2446 if (!Entry) {
2447 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2448 Entry =
2449 new llvm::GlobalVariable(C->getType(), false,
2450 llvm::GlobalValue::InternalLinkage,
2451 C, "\01L_OBJC_PROP_NAME_ATTR_",
2452 &CGM.getModule());
2453 Entry->setSection("__TEXT,__cstring,cstring_literals");
2454 UsedGlobals.push_back(Entry);
2455 }
2456
2457 return getConstantGEP(Entry, 0, 0);
2458}
2459
2460// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002461// FIXME: This Decl should be more precise.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002462llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002463 const Decl *Container) {
2464 std::string TypeStr;
2465 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002466 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2467}
2468
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002469void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2470 const ObjCContainerDecl *CD,
2471 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002472 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002473 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002474 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002475 assert (CD && "Missing container decl in GetNameForMethod");
2476 NameOut += CD->getNameAsString();
Fariborz Jahanian52847332009-01-26 23:49:05 +00002477 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
2478 // Right now! there is not enough info. to do this.
Chris Lattner077bf5e2008-11-24 03:33:13 +00002479 NameOut += ' ';
2480 NameOut += D->getSelector().getAsString();
2481 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002482}
2483
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002484/// GetFirstIvarInRecord - This routine returns the record for the
2485/// implementation of the fiven class OID. It also returns field
2486/// corresponding to the first ivar in the class in FIV. It also
2487/// returns the one before the first ivar.
2488///
2489const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord(
2490 const ObjCInterfaceDecl *OID,
2491 RecordDecl::field_iterator &FIV,
2492 RecordDecl::field_iterator &PIV) {
2493 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
2494 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
2495 RecordDecl::field_iterator ifield = RD->field_begin();
2496 RecordDecl::field_iterator pfield = RD->field_end();
2497 while (countSuperClassIvars-- > 0) {
2498 pfield = ifield;
2499 ++ifield;
2500 }
2501 FIV = ifield;
2502 PIV = pfield;
2503 return RD;
2504}
2505
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002506void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002507 EmitModuleInfo();
2508
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002509 // Emit the dummy bodies for any protocols which were referenced but
2510 // never defined.
2511 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2512 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2513 if (i->second->hasInitializer())
2514 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002515
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002516 std::vector<llvm::Constant*> Values(5);
2517 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2518 Values[1] = GetClassName(i->first);
2519 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2520 Values[3] = Values[4] =
2521 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2522 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2523 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2524 Values));
2525 }
2526
2527 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002528 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002529 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002530 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002531 }
2532
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002533 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002534 llvm::GlobalValue *GV =
2535 new llvm::GlobalVariable(AT, false,
2536 llvm::GlobalValue::AppendingLinkage,
2537 llvm::ConstantArray::get(AT, Used),
2538 "llvm.used",
2539 &CGM.getModule());
2540
2541 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002542
2543 // Add assembler directives to add lazy undefined symbol references
2544 // for classes which are referenced but not defined. This is
2545 // important for correct linker interaction.
2546
2547 // FIXME: Uh, this isn't particularly portable.
2548 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00002549
2550 if (!CGM.getModule().getModuleInlineAsm().empty())
2551 s << "\n";
2552
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002553 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2554 e = LazySymbols.end(); i != e; ++i) {
2555 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2556 }
2557 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2558 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002559 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002560 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2561 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00002562
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002563 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002564}
2565
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002566CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002567 : CGObjCCommonMac(cgm),
2568 ObjCTypes(cgm)
2569{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002570 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002571 ObjCABI = 2;
2572}
2573
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002574/* *** */
2575
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002576ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2577: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002578{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002579 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2580 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002581
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002582 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002583 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002584 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002585 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2586
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002587 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002588 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002589 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002590
2591 // FIXME: It would be nice to unify this with the opaque type, so
2592 // that the IR comes out a bit cleaner.
2593 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2594 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002595
2596 // I'm not sure I like this. The implicit coordination is a bit
2597 // gross. We should solve this in a reasonable fashion because this
2598 // is a pretty common task (match some runtime data structure with
2599 // an LLVM data structure).
2600
2601 // FIXME: This is leaked.
2602 // FIXME: Merge with rewriter code?
2603
2604 // struct _objc_super {
2605 // id self;
2606 // Class cls;
2607 // }
2608 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2609 SourceLocation(),
2610 &Ctx.Idents.get("_objc_super"));
2611 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2612 Ctx.getObjCIdType(), 0, false));
2613 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2614 Ctx.getObjCClassType(), 0, false));
2615 RD->completeDefinition(Ctx);
2616
2617 SuperCTy = Ctx.getTagDeclType(RD);
2618 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2619
2620 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002621 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2622
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002623 // struct _prop_t {
2624 // char *name;
2625 // char *attributes;
2626 // }
2627 PropertyTy = llvm::StructType::get(Int8PtrTy,
2628 Int8PtrTy,
2629 NULL);
2630 CGM.getModule().addTypeName("struct._prop_t",
2631 PropertyTy);
2632
2633 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002634 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002635 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002636 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002637 // }
2638 PropertyListTy = llvm::StructType::get(IntTy,
2639 IntTy,
2640 llvm::ArrayType::get(PropertyTy, 0),
2641 NULL);
2642 CGM.getModule().addTypeName("struct._prop_list_t",
2643 PropertyListTy);
2644 // struct _prop_list_t *
2645 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2646
2647 // struct _objc_method {
2648 // SEL _cmd;
2649 // char *method_type;
2650 // char *_imp;
2651 // }
2652 MethodTy = llvm::StructType::get(SelectorPtrTy,
2653 Int8PtrTy,
2654 Int8PtrTy,
2655 NULL);
2656 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002657
2658 // struct _objc_cache *
2659 CacheTy = llvm::OpaqueType::get();
2660 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2661 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002662
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002663 // Property manipulation functions.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002664
2665 QualType IdType = Ctx.getObjCIdType();
2666 QualType SelType = Ctx.getObjCSelType();
2667 llvm::SmallVector<QualType,16> Params;
2668 const llvm::FunctionType *FTy;
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002669
2670 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002671 Params.push_back(IdType);
2672 Params.push_back(SelType);
2673 Params.push_back(Ctx.LongTy);
2674 Params.push_back(Ctx.BoolTy);
2675 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params),
2676 false);
2677 GetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002678
2679 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2680 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002681 Params.push_back(IdType);
2682 Params.push_back(SelType);
2683 Params.push_back(Ctx.LongTy);
2684 Params.push_back(IdType);
2685 Params.push_back(Ctx.BoolTy);
2686 Params.push_back(Ctx.BoolTy);
2687 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2688 SetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
2689
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002690 // Enumeration mutation.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002691
2692 // void objc_enumerationMutation (id)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002693 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002694 Params.push_back(IdType);
2695 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2696 EnumerationMutationFn = CGM.CreateRuntimeFunction(FTy,
2697 "objc_enumerationMutation");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002698
2699 // gc's API
2700 // id objc_read_weak (id *)
2701 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002702 Params.push_back(Ctx.getPointerType(IdType));
2703 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
2704 GcReadWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
2705
2706 // id objc_assign_weak (id, id *)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002707 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002708 Params.push_back(IdType);
2709 Params.push_back(Ctx.getPointerType(IdType));
2710
2711 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
2712 GcAssignWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
2713 GcAssignGlobalFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
2714 GcAssignIvarFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
2715 GcAssignStrongCastFn =
2716 CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002717}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002718
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002719ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2720 : ObjCCommonTypesHelper(cgm)
2721{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002722 // struct _objc_method_description {
2723 // SEL name;
2724 // char *types;
2725 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002726 MethodDescriptionTy =
2727 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002728 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002729 NULL);
2730 CGM.getModule().addTypeName("struct._objc_method_description",
2731 MethodDescriptionTy);
2732
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002733 // struct _objc_method_description_list {
2734 // int count;
2735 // struct _objc_method_description[1];
2736 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002737 MethodDescriptionListTy =
2738 llvm::StructType::get(IntTy,
2739 llvm::ArrayType::get(MethodDescriptionTy, 0),
2740 NULL);
2741 CGM.getModule().addTypeName("struct._objc_method_description_list",
2742 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002743
2744 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002745 MethodDescriptionListPtrTy =
2746 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2747
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002748 // Protocol description structures
2749
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002750 // struct _objc_protocol_extension {
2751 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2752 // struct _objc_method_description_list *optional_instance_methods;
2753 // struct _objc_method_description_list *optional_class_methods;
2754 // struct _objc_property_list *instance_properties;
2755 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002756 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002757 llvm::StructType::get(IntTy,
2758 MethodDescriptionListPtrTy,
2759 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002760 PropertyListPtrTy,
2761 NULL);
2762 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2763 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002764
2765 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002766 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2767
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002768 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002769
2770 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2771 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2772
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002773 const llvm::Type *T =
2774 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2775 LongTy,
2776 llvm::ArrayType::get(ProtocolTyHolder, 0),
2777 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002778 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2779
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002780 // struct _objc_protocol {
2781 // struct _objc_protocol_extension *isa;
2782 // char *protocol_name;
2783 // struct _objc_protocol **_objc_protocol_list;
2784 // struct _objc_method_description_list *instance_methods;
2785 // struct _objc_method_description_list *class_methods;
2786 // }
2787 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002788 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002789 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2790 MethodDescriptionListPtrTy,
2791 MethodDescriptionListPtrTy,
2792 NULL);
2793 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2794
2795 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2796 CGM.getModule().addTypeName("struct._objc_protocol_list",
2797 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002798 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002799 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2800
2801 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002802 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002803 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002804
2805 // Class description structures
2806
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002807 // struct _objc_ivar {
2808 // char *ivar_name;
2809 // char *ivar_type;
2810 // int ivar_offset;
2811 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002812 IvarTy = llvm::StructType::get(Int8PtrTy,
2813 Int8PtrTy,
2814 IntTy,
2815 NULL);
2816 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2817
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002818 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002819 IvarListTy = llvm::OpaqueType::get();
2820 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2821 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2822
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002823 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002824 MethodListTy = llvm::OpaqueType::get();
2825 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2826 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2827
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002828 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002829 ClassExtensionTy =
2830 llvm::StructType::get(IntTy,
2831 Int8PtrTy,
2832 PropertyListPtrTy,
2833 NULL);
2834 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2835 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2836
2837 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2838
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002839 // struct _objc_class {
2840 // Class isa;
2841 // Class super_class;
2842 // char *name;
2843 // long version;
2844 // long info;
2845 // long instance_size;
2846 // struct _objc_ivar_list *ivars;
2847 // struct _objc_method_list *methods;
2848 // struct _objc_cache *cache;
2849 // struct _objc_protocol_list *protocols;
2850 // char *ivar_layout;
2851 // struct _objc_class_ext *ext;
2852 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002853 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2854 llvm::PointerType::getUnqual(ClassTyHolder),
2855 Int8PtrTy,
2856 LongTy,
2857 LongTy,
2858 LongTy,
2859 IvarListPtrTy,
2860 MethodListPtrTy,
2861 CachePtrTy,
2862 ProtocolListPtrTy,
2863 Int8PtrTy,
2864 ClassExtensionPtrTy,
2865 NULL);
2866 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2867
2868 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2869 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2870 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2871
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002872 // struct _objc_category {
2873 // char *category_name;
2874 // char *class_name;
2875 // struct _objc_method_list *instance_method;
2876 // struct _objc_method_list *class_method;
2877 // uint32_t size; // sizeof(struct _objc_category)
2878 // struct _objc_property_list *instance_properties;// category's @property
2879 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002880 CategoryTy = llvm::StructType::get(Int8PtrTy,
2881 Int8PtrTy,
2882 MethodListPtrTy,
2883 MethodListPtrTy,
2884 ProtocolListPtrTy,
2885 IntTy,
2886 PropertyListPtrTy,
2887 NULL);
2888 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2889
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002890 // Global metadata structures
2891
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002892 // struct _objc_symtab {
2893 // long sel_ref_cnt;
2894 // SEL *refs;
2895 // short cls_def_cnt;
2896 // short cat_def_cnt;
2897 // char *defs[cls_def_cnt + cat_def_cnt];
2898 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002899 SymtabTy = llvm::StructType::get(LongTy,
2900 SelectorPtrTy,
2901 ShortTy,
2902 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002903 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002904 NULL);
2905 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2906 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2907
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002908 // struct _objc_module {
2909 // long version;
2910 // long size; // sizeof(struct _objc_module)
2911 // char *name;
2912 // struct _objc_symtab* symtab;
2913 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002914 ModuleTy =
2915 llvm::StructType::get(LongTy,
2916 LongTy,
2917 Int8PtrTy,
2918 SymtabPtrTy,
2919 NULL);
2920 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002921
Daniel Dunbar49f66022008-09-24 03:38:44 +00002922 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002923
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002924 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002925 std::vector<const llvm::Type*> Params;
2926 Params.push_back(ObjectPtrTy);
2927 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002928 MessageSendFn =
2929 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2930 Params,
2931 true),
2932 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002933
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002934 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002935 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002936 Params.push_back(ObjectPtrTy);
2937 Params.push_back(SelectorPtrTy);
2938 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002939 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2940 Params,
2941 true),
2942 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002943
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002944 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00002945 Params.clear();
2946 Params.push_back(ObjectPtrTy);
2947 Params.push_back(SelectorPtrTy);
2948 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002949 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00002950 MessageSendFpretFn =
2951 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
2952 Params,
2953 true),
2954 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002955
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002956 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002957 Params.clear();
2958 Params.push_back(SuperPtrTy);
2959 Params.push_back(SelectorPtrTy);
2960 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002961 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2962 Params,
2963 true),
2964 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002965
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002966 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
2967 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002968 Params.clear();
2969 Params.push_back(Int8PtrTy);
2970 Params.push_back(SuperPtrTy);
2971 Params.push_back(SelectorPtrTy);
2972 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002973 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2974 Params,
2975 true),
2976 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002977
2978 // There is no objc_msgSendSuper_fpret? How can that work?
2979 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002980
Anders Carlsson124526b2008-09-09 10:10:21 +00002981 // FIXME: This is the size of the setjmp buffer and should be
2982 // target specific. 18 is what's used on 32-bit X86.
2983 uint64_t SetJmpBufferSize = 18;
2984
2985 // Exceptions
2986 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00002987 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00002988
2989 ExceptionDataTy =
2990 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
2991 SetJmpBufferSize),
2992 StackPtrTy, NULL);
2993 CGM.getModule().addTypeName("struct._objc_exception_data",
2994 ExceptionDataTy);
2995
2996 Params.clear();
2997 Params.push_back(ObjectPtrTy);
2998 ExceptionThrowFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002999 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3000 Params,
3001 false),
3002 "objc_exception_throw");
Anders Carlsson124526b2008-09-09 10:10:21 +00003003
3004 Params.clear();
3005 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
3006 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003007 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3008 Params,
3009 false),
3010 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00003011 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003012 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3013 Params,
3014 false),
3015 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00003016 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003017 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3018 Params,
3019 false),
3020 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00003021
3022 Params.clear();
3023 Params.push_back(ClassPtrTy);
3024 Params.push_back(ObjectPtrTy);
3025 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003026 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3027 Params,
3028 false),
3029 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00003030
3031 // synchronized APIs
3032 // void objc_sync_enter (id)
3033 Params.clear();
3034 Params.push_back(ObjectPtrTy);
3035 SyncEnterFn =
3036 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3037 Params,
3038 false),
3039 "objc_sync_enter");
3040 // void objc_sync_exit (id)
3041 SyncExitFn =
3042 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3043 Params,
3044 false),
3045 "objc_sync_exit");
3046
Anders Carlsson124526b2008-09-09 10:10:21 +00003047
3048 Params.clear();
3049 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
3050 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003051 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3052 Params,
3053 false),
3054 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003055
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003056}
3057
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003058ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003059: ObjCCommonTypesHelper(cgm)
3060{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003061 // struct _method_list_t {
3062 // uint32_t entsize; // sizeof(struct _objc_method)
3063 // uint32_t method_count;
3064 // struct _objc_method method_list[method_count];
3065 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003066 MethodListnfABITy = llvm::StructType::get(IntTy,
3067 IntTy,
3068 llvm::ArrayType::get(MethodTy, 0),
3069 NULL);
3070 CGM.getModule().addTypeName("struct.__method_list_t",
3071 MethodListnfABITy);
3072 // struct method_list_t *
3073 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003074
3075 // struct _protocol_t {
3076 // id isa; // NULL
3077 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003078 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003079 // const struct method_list_t * const instance_methods;
3080 // const struct method_list_t * const class_methods;
3081 // const struct method_list_t *optionalInstanceMethods;
3082 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003083 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003084 // const uint32_t size; // sizeof(struct _protocol_t)
3085 // const uint32_t flags; // = 0
3086 // }
3087
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003088 // Holder for struct _protocol_list_t *
3089 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3090
3091 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3092 Int8PtrTy,
3093 llvm::PointerType::getUnqual(
3094 ProtocolListTyHolder),
3095 MethodListnfABIPtrTy,
3096 MethodListnfABIPtrTy,
3097 MethodListnfABIPtrTy,
3098 MethodListnfABIPtrTy,
3099 PropertyListPtrTy,
3100 IntTy,
3101 IntTy,
3102 NULL);
3103 CGM.getModule().addTypeName("struct._protocol_t",
3104 ProtocolnfABITy);
3105
Fariborz Jahanianda320092009-01-29 19:24:30 +00003106 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003107 // long protocol_count; // Note, this is 32/64 bit
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003108 // struct _protocol_t[protocol_count];
3109 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003110 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3111 llvm::ArrayType::get(
3112 ProtocolnfABITy, 0),
3113 NULL);
3114 CGM.getModule().addTypeName("struct._objc_protocol_list",
3115 ProtocolListnfABITy);
3116
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003117 // struct _objc_protocol_list*
3118 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003119
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003120 // FIXME! Is this doing the right thing?
3121 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3122 ProtocolListnfABIPtrTy);
3123
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003124 // struct _ivar_t {
3125 // unsigned long int *offset; // pointer to ivar offset location
3126 // char *name;
3127 // char *type;
3128 // uint32_t alignment;
3129 // uint32_t size;
3130 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003131 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3132 Int8PtrTy,
3133 Int8PtrTy,
3134 IntTy,
3135 IntTy,
3136 NULL);
3137 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3138
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003139 // struct _ivar_list_t {
3140 // uint32 entsize; // sizeof(struct _ivar_t)
3141 // uint32 count;
3142 // struct _iver_t list[count];
3143 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003144 IvarListnfABITy = llvm::StructType::get(IntTy,
3145 IntTy,
3146 llvm::ArrayType::get(
3147 IvarnfABITy, 0),
3148 NULL);
3149 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3150
3151 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003152
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003153 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003154 // uint32_t const flags;
3155 // uint32_t const instanceStart;
3156 // uint32_t const instanceSize;
3157 // uint32_t const reserved; // only when building for 64bit targets
3158 // const uint8_t * const ivarLayout;
3159 // const char *const name;
3160 // const struct _method_list_t * const baseMethods;
3161 // const struct _objc_protocol_list *const baseProtocols;
3162 // const struct _ivar_list_t *const ivars;
3163 // const uint8_t * const weakIvarLayout;
3164 // const struct _prop_list_t * const properties;
3165 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003166
3167 // FIXME. Add 'reserved' field in 64bit abi mode!
3168 ClassRonfABITy = llvm::StructType::get(IntTy,
3169 IntTy,
3170 IntTy,
3171 Int8PtrTy,
3172 Int8PtrTy,
3173 MethodListnfABIPtrTy,
3174 ProtocolListnfABIPtrTy,
3175 IvarListnfABIPtrTy,
3176 Int8PtrTy,
3177 PropertyListPtrTy,
3178 NULL);
3179 CGM.getModule().addTypeName("struct._class_ro_t",
3180 ClassRonfABITy);
3181
3182 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3183 std::vector<const llvm::Type*> Params;
3184 Params.push_back(ObjectPtrTy);
3185 Params.push_back(SelectorPtrTy);
3186 ImpnfABITy = llvm::PointerType::getUnqual(
3187 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3188
3189 // struct _class_t {
3190 // struct _class_t *isa;
3191 // struct _class_t * const superclass;
3192 // void *cache;
3193 // IMP *vtable;
3194 // struct class_ro_t *ro;
3195 // }
3196
3197 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3198 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3199 llvm::PointerType::getUnqual(ClassTyHolder),
3200 CachePtrTy,
3201 llvm::PointerType::getUnqual(ImpnfABITy),
3202 llvm::PointerType::getUnqual(
3203 ClassRonfABITy),
3204 NULL);
3205 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3206
3207 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3208 ClassnfABITy);
3209
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003210 // LLVM for struct _class_t *
3211 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3212
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003213 // struct _category_t {
3214 // const char * const name;
3215 // struct _class_t *const cls;
3216 // const struct _method_list_t * const instance_methods;
3217 // const struct _method_list_t * const class_methods;
3218 // const struct _protocol_list_t * const protocols;
3219 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003220 // }
3221 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003222 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003223 MethodListnfABIPtrTy,
3224 MethodListnfABIPtrTy,
3225 ProtocolListnfABIPtrTy,
3226 PropertyListPtrTy,
3227 NULL);
3228 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003229
3230 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003231 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3232 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003233
3234 // MessageRefTy - LLVM for:
3235 // struct _message_ref_t {
3236 // IMP messenger;
3237 // SEL name;
3238 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003239
3240 // First the clang type for struct _message_ref_t
3241 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3242 SourceLocation(),
3243 &Ctx.Idents.get("_message_ref_t"));
3244 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3245 Ctx.VoidPtrTy, 0, false));
3246 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3247 Ctx.getObjCSelType(), 0, false));
3248 RD->completeDefinition(Ctx);
3249
3250 MessageRefCTy = Ctx.getTagDeclType(RD);
3251 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
3252 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003253
3254 // MessageRefPtrTy - LLVM for struct _message_ref_t*
3255 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
3256
3257 // SuperMessageRefTy - LLVM for:
3258 // struct _super_message_ref_t {
3259 // SUPER_IMP messenger;
3260 // SEL name;
3261 // };
3262 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
3263 SelectorPtrTy,
3264 NULL);
3265 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
3266
3267 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
3268 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
3269
3270 // id objc_msgSend_fixup (id, struct message_ref_t*, ...)
3271 Params.clear();
3272 Params.push_back(ObjectPtrTy);
3273 Params.push_back(MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00003274 MessengerTy = llvm::FunctionType::get(ObjectPtrTy,
3275 Params,
3276 true);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003277 MessageSendFixupFn =
Fariborz Jahanianef163782009-02-05 01:13:09 +00003278 CGM.CreateRuntimeFunction(MessengerTy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003279 "objc_msgSend_fixup");
3280
3281 // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...)
3282 MessageSendFpretFixupFn =
3283 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3284 Params,
3285 true),
3286 "objc_msgSend_fpret_fixup");
3287
3288 // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...)
3289 MessageSendStretFixupFn =
3290 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3291 Params,
3292 true),
3293 "objc_msgSend_stret_fixup");
3294
3295 // id objc_msgSendId_fixup (id, struct message_ref_t*, ...)
3296 MessageSendIdFixupFn =
3297 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3298 Params,
3299 true),
3300 "objc_msgSendId_fixup");
3301
3302
3303 // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...)
3304 MessageSendIdStretFixupFn =
3305 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3306 Params,
3307 true),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003308 "objc_msgSendId_stret_fixup");
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003309
3310 // id objc_msgSendSuper2_fixup (struct objc_super *,
3311 // struct _super_message_ref_t*, ...)
3312 Params.clear();
3313 Params.push_back(SuperPtrTy);
3314 Params.push_back(SuperMessageRefPtrTy);
3315 MessageSendSuper2FixupFn =
3316 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3317 Params,
3318 true),
3319 "objc_msgSendSuper2_fixup");
3320
3321
3322 // id objc_msgSendSuper2_stret_fixup (struct objc_super *,
3323 // struct _super_message_ref_t*, ...)
3324 MessageSendSuper2StretFixupFn =
3325 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3326 Params,
3327 true),
3328 "objc_msgSendSuper2_stret_fixup");
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003329
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003330}
3331
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003332llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3333 FinishNonFragileABIModule();
3334
3335 return NULL;
3336}
3337
3338void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3339 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003340
3341 // Build list of all implemented classe addresses in array
3342 // L_OBJC_LABEL_CLASS_$.
3343 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
3344 // list of 'nonlazy' implementations (defined as those with a +load{}
3345 // method!!).
3346 unsigned NumClasses = DefinedClasses.size();
3347 if (NumClasses) {
3348 std::vector<llvm::Constant*> Symbols(NumClasses);
3349 for (unsigned i=0; i<NumClasses; i++)
3350 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3351 ObjCTypes.Int8PtrTy);
3352 llvm::Constant* Init =
3353 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3354 NumClasses),
3355 Symbols);
3356
3357 llvm::GlobalVariable *GV =
3358 new llvm::GlobalVariable(Init->getType(), false,
3359 llvm::GlobalValue::InternalLinkage,
3360 Init,
3361 "\01L_OBJC_LABEL_CLASS_$",
3362 &CGM.getModule());
3363 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
3364 UsedGlobals.push_back(GV);
3365 }
3366
3367 // Build list of all implemented category addresses in array
3368 // L_OBJC_LABEL_CATEGORY_$.
3369 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
3370 // list of 'nonlazy' category implementations (defined as those with a +load{}
3371 // method!!).
3372 unsigned NumCategory = DefinedCategories.size();
3373 if (NumCategory) {
3374 std::vector<llvm::Constant*> Symbols(NumCategory);
3375 for (unsigned i=0; i<NumCategory; i++)
3376 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
3377 ObjCTypes.Int8PtrTy);
3378 llvm::Constant* Init =
3379 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3380 NumCategory),
3381 Symbols);
3382
3383 llvm::GlobalVariable *GV =
3384 new llvm::GlobalVariable(Init->getType(), false,
3385 llvm::GlobalValue::InternalLinkage,
3386 Init,
3387 "\01L_OBJC_LABEL_CATEGORY_$",
3388 &CGM.getModule());
3389 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
3390 UsedGlobals.push_back(GV);
3391 }
3392
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00003393 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
3394 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
3395 std::vector<llvm::Constant*> Values(2);
3396 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3397 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3398 llvm::Constant* Init = llvm::ConstantArray::get(
3399 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
3400 Values);
3401 llvm::GlobalVariable *IMGV =
3402 new llvm::GlobalVariable(Init->getType(), false,
3403 llvm::GlobalValue::InternalLinkage,
3404 Init,
3405 "\01L_OBJC_IMAGE_INFO",
3406 &CGM.getModule());
3407 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
3408 UsedGlobals.push_back(IMGV);
3409
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003410 std::vector<llvm::Constant*> Used;
3411 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3412 e = UsedGlobals.end(); i != e; ++i) {
3413 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3414 }
3415
3416 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3417 llvm::GlobalValue *GV =
3418 new llvm::GlobalVariable(AT, false,
3419 llvm::GlobalValue::AppendingLinkage,
3420 llvm::ConstantArray::get(AT, Used),
3421 "llvm.used",
3422 &CGM.getModule());
3423
3424 GV->setSection("llvm.metadata");
3425
3426}
3427
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003428// Metadata flags
3429enum MetaDataDlags {
3430 CLS = 0x0,
3431 CLS_META = 0x1,
3432 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003433 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003434 CLS_EXCEPTION = 0x20
3435};
3436/// BuildClassRoTInitializer - generate meta-data for:
3437/// struct _class_ro_t {
3438/// uint32_t const flags;
3439/// uint32_t const instanceStart;
3440/// uint32_t const instanceSize;
3441/// uint32_t const reserved; // only when building for 64bit targets
3442/// const uint8_t * const ivarLayout;
3443/// const char *const name;
3444/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00003445/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003446/// const struct _ivar_list_t *const ivars;
3447/// const uint8_t * const weakIvarLayout;
3448/// const struct _prop_list_t * const properties;
3449/// }
3450///
3451llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
3452 unsigned flags,
3453 unsigned InstanceStart,
3454 unsigned InstanceSize,
3455 const ObjCImplementationDecl *ID) {
3456 std::string ClassName = ID->getNameAsString();
3457 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
3458 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
3459 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
3460 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
3461 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianda320092009-01-29 19:24:30 +00003462 // FIXME. ivarLayout is currently null!
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003463 Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3464 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003465 // const struct _method_list_t * const baseMethods;
3466 std::vector<llvm::Constant*> Methods;
3467 std::string MethodListName("\01l_OBJC_$_");
3468 if (flags & CLS_META) {
3469 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
3470 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
3471 e = ID->classmeth_end(); i != e; ++i) {
3472 // Class methods should always be defined.
3473 Methods.push_back(GetMethodConstant(*i));
3474 }
3475 } else {
3476 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
3477 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
3478 e = ID->instmeth_end(); i != e; ++i) {
3479 // Instance methods should always be defined.
3480 Methods.push_back(GetMethodConstant(*i));
3481 }
Fariborz Jahanian939abce2009-01-28 22:46:49 +00003482 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
3483 e = ID->propimpl_end(); i != e; ++i) {
3484 ObjCPropertyImplDecl *PID = *i;
3485
3486 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
3487 ObjCPropertyDecl *PD = PID->getPropertyDecl();
3488
3489 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3490 if (llvm::Constant *C = GetMethodConstant(MD))
3491 Methods.push_back(C);
3492 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3493 if (llvm::Constant *C = GetMethodConstant(MD))
3494 Methods.push_back(C);
3495 }
3496 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003497 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003498 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003499 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003500
3501 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3502 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
3503 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
3504 + OID->getNameAsString(),
3505 OID->protocol_begin(),
3506 OID->protocol_end());
3507
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003508 if (flags & CLS_META)
3509 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3510 else
3511 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003512 // FIXME. weakIvarLayout is currently null.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003513 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003514 if (flags & CLS_META)
3515 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3516 else
3517 Values[ 9] =
3518 EmitPropertyList(
3519 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
3520 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003521 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
3522 Values);
3523 llvm::GlobalVariable *CLASS_RO_GV =
3524 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
3525 llvm::GlobalValue::InternalLinkage,
3526 Init,
3527 (flags & CLS_META) ?
3528 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
3529 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
3530 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003531 CLASS_RO_GV->setAlignment(
3532 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003533 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003534 UsedGlobals.push_back(CLASS_RO_GV);
3535 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003536
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003537}
3538
3539/// BuildClassMetaData - This routine defines that to-level meta-data
3540/// for the given ClassName for:
3541/// struct _class_t {
3542/// struct _class_t *isa;
3543/// struct _class_t * const superclass;
3544/// void *cache;
3545/// IMP *vtable;
3546/// struct class_ro_t *ro;
3547/// }
3548///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003549llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
3550 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003551 llvm::Constant *IsAGV,
3552 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003553 llvm::Constant *ClassRoGV,
3554 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003555 std::vector<llvm::Constant*> Values(5);
3556 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003557 Values[1] = SuperClassGV
3558 ? SuperClassGV
3559 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003560 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
3561 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
3562 Values[4] = ClassRoGV; // &CLASS_RO_GV
3563 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
3564 Values);
3565 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName);
3566 if (GV)
3567 GV->setInitializer(Init);
3568 else
3569 GV =
3570 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3571 llvm::GlobalValue::ExternalLinkage,
3572 Init,
3573 ClassName,
3574 &CGM.getModule());
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00003575 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003576 GV->setAlignment(
3577 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003578 if (HiddenVisibility)
3579 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003580 UsedGlobals.push_back(GV);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003581 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003582}
3583
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003584void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
3585 std::string ClassName = ID->getNameAsString();
3586 if (!ObjCEmptyCacheVar) {
3587 ObjCEmptyCacheVar = new llvm::GlobalVariable(
3588 ObjCTypes.CachePtrTy,
3589 false,
3590 llvm::GlobalValue::ExternalLinkage,
3591 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00003592 "\01__objc_empty_cache",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003593 &CGM.getModule());
3594 UsedGlobals.push_back(ObjCEmptyCacheVar);
3595
3596 ObjCEmptyVtableVar = new llvm::GlobalVariable(
3597 llvm::PointerType::getUnqual(
3598 ObjCTypes.ImpnfABITy),
3599 false,
3600 llvm::GlobalValue::ExternalLinkage,
3601 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00003602 "\01__objc_empty_vtable",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003603 &CGM.getModule());
3604 UsedGlobals.push_back(ObjCEmptyVtableVar);
3605 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003606 assert(ID->getClassInterface() &&
3607 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003608 uint32_t InstanceStart =
3609 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
3610 uint32_t InstanceSize = InstanceStart;
3611 uint32_t flags = CLS_META;
3612 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
3613 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003614
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003615 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003616
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003617 bool classIsHidden = IsClassHidden(ID->getClassInterface());
3618 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003619 flags |= OBJC2_CLS_HIDDEN;
3620 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003621 // class is root
3622 flags |= CLS_ROOT;
3623 std::string SuperClassName = ObjCClassName + ClassName;
3624 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003625 if (!SuperClassGV) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003626 SuperClassGV =
3627 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3628 llvm::GlobalValue::ExternalLinkage,
3629 0,
3630 SuperClassName,
3631 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003632 UsedGlobals.push_back(SuperClassGV);
3633 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003634 std::string IsAClassName = ObjCMetaClassName + ClassName;
3635 IsAGV = CGM.getModule().getGlobalVariable(IsAClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003636 if (!IsAGV) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003637 IsAGV =
3638 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3639 llvm::GlobalValue::ExternalLinkage,
3640 0,
3641 IsAClassName,
3642 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003643 UsedGlobals.push_back(IsAGV);
3644 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003645 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003646 // Has a root. Current class is not a root.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003647 std::string RootClassName =
3648 ID->getClassInterface()->getSuperClass()->getNameAsString();
3649 std::string SuperClassName = ObjCMetaClassName + RootClassName;
3650 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003651 if (!SuperClassGV) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003652 SuperClassGV =
3653 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3654 llvm::GlobalValue::ExternalLinkage,
3655 0,
3656 SuperClassName,
3657 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003658 UsedGlobals.push_back(SuperClassGV);
3659 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003660 IsAGV = SuperClassGV;
3661 }
3662 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
3663 InstanceStart,
3664 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003665 std::string TClassName = ObjCMetaClassName + ClassName;
3666 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003667 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
3668 classIsHidden);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003669
3670 // Metadata for the class
3671 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003672 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003673 flags |= OBJC2_CLS_HIDDEN;
3674 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003675 flags |= CLS_ROOT;
3676 SuperClassGV = 0;
3677 }
3678 else {
3679 // Has a root. Current class is not a root.
3680 std::string RootClassName =
3681 ID->getClassInterface()->getSuperClass()->getNameAsString();
3682 std::string SuperClassName = ObjCClassName + RootClassName;
3683 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003684 if (!SuperClassGV) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003685 SuperClassGV =
3686 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3687 llvm::GlobalValue::ExternalLinkage,
3688 0,
3689 SuperClassName,
3690 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003691 UsedGlobals.push_back(SuperClassGV);
3692 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003693 }
3694
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003695 InstanceStart = InstanceSize = 0;
3696 if (ObjCInterfaceDecl *OID =
3697 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
3698 // FIXME. Share this with the one in EmitIvarList.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003699 const llvm::Type *InterfaceTy =
3700 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
3701 const llvm::StructLayout *Layout =
3702 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003703
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003704 RecordDecl::field_iterator firstField, lastField;
3705 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003706
3707 for (RecordDecl::field_iterator e = RD->field_end(),
3708 ifield = firstField; ifield != e; ++ifield)
3709 lastField = ifield;
3710
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003711 if (lastField != RD->field_end()) {
3712 FieldDecl *Field = *lastField;
3713 const llvm::Type *FieldTy =
3714 CGM.getTypes().ConvertTypeForMem(Field->getType());
3715 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3716 InstanceSize = Layout->getElementOffset(
3717 CGM.getTypes().getLLVMFieldNo(Field)) +
3718 Size;
3719 if (firstField == RD->field_end())
3720 InstanceStart = InstanceSize;
3721 else
3722 InstanceStart = Layout->getElementOffset(CGM.getTypes().
3723 getLLVMFieldNo(*firstField));
3724 }
3725 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003726 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003727 InstanceStart,
3728 InstanceSize,
3729 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003730
3731 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003732 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003733 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
3734 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003735 DefinedClasses.push_back(ClassMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003736}
3737
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00003738/// GenerateProtocolRef - This routine is called to generate code for
3739/// a protocol reference expression; as in:
3740/// @code
3741/// @protocol(Proto1);
3742/// @endcode
3743/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
3744/// which will hold address of the protocol meta-data.
3745///
3746llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
3747 const ObjCProtocolDecl *PD) {
3748
3749 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
3750 ObjCTypes.ExternalProtocolPtrTy);
3751
3752 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
3753 ProtocolName += PD->getNameAsCString();
3754
3755 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
3756 if (PTGV)
3757 return Builder.CreateLoad(PTGV, false, "tmp");
3758 PTGV = new llvm::GlobalVariable(
3759 Init->getType(), false,
3760 llvm::GlobalValue::WeakLinkage,
3761 Init,
3762 ProtocolName,
3763 &CGM.getModule());
3764 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
3765 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3766 UsedGlobals.push_back(PTGV);
3767 return Builder.CreateLoad(PTGV, false, "tmp");
3768}
3769
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003770/// GenerateCategory - Build metadata for a category implementation.
3771/// struct _category_t {
3772/// const char * const name;
3773/// struct _class_t *const cls;
3774/// const struct _method_list_t * const instance_methods;
3775/// const struct _method_list_t * const class_methods;
3776/// const struct _protocol_list_t * const protocols;
3777/// const struct _prop_list_t * const properties;
3778/// }
3779///
3780void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
3781{
3782 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003783 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
3784 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003785 "_$_" + OCD->getNameAsString());
3786 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString());
3787
3788 std::vector<llvm::Constant*> Values(6);
3789 Values[0] = GetClassName(OCD->getIdentifier());
3790 // meta-class entry symbol
3791 llvm::GlobalVariable *ClassGV =
3792 CGM.getModule().getGlobalVariable(ExtClassName);
3793 if (!ClassGV)
3794 ClassGV =
3795 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3796 llvm::GlobalValue::ExternalLinkage,
3797 0,
3798 ExtClassName,
3799 &CGM.getModule());
3800 UsedGlobals.push_back(ClassGV);
3801 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003802 std::vector<llvm::Constant*> Methods;
3803 std::string MethodListName(Prefix);
3804 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
3805 "_$_" + OCD->getNameAsString();
3806
3807 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
3808 e = OCD->instmeth_end(); i != e; ++i) {
3809 // Instance methods should always be defined.
3810 Methods.push_back(GetMethodConstant(*i));
3811 }
3812
3813 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003814 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003815 Methods);
3816
3817 MethodListName = Prefix;
3818 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
3819 OCD->getNameAsString();
3820 Methods.clear();
3821 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
3822 e = OCD->classmeth_end(); i != e; ++i) {
3823 // Class methods should always be defined.
3824 Methods.push_back(GetMethodConstant(*i));
3825 }
3826
3827 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003828 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003829 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003830 const ObjCCategoryDecl *Category =
3831 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanianeb732132009-01-29 23:23:06 +00003832 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
3833 + Interface->getNameAsString() + "_$_"
3834 + Category->getNameAsString(),
3835 Category->protocol_begin(),
3836 Category->protocol_end());
3837
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003838 std::string ExtName(Interface->getNameAsString() + "_$_" +
3839 OCD->getNameAsString());
3840 Values[5] =
3841 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
3842 OCD, Category, ObjCTypes);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003843 llvm::Constant *Init =
3844 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
3845 Values);
3846 llvm::GlobalVariable *GCATV
3847 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
3848 false,
3849 llvm::GlobalValue::InternalLinkage,
3850 Init,
3851 ExtCatName,
3852 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003853 GCATV->setAlignment(
3854 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003855 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003856 UsedGlobals.push_back(GCATV);
3857 DefinedCategories.push_back(GCATV);
3858}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003859
3860/// GetMethodConstant - Return a struct objc_method constant for the
3861/// given method if it has been defined. The result is null if the
3862/// method has not been defined. The return value has type MethodPtrTy.
3863llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
3864 const ObjCMethodDecl *MD) {
3865 // FIXME: Use DenseMap::lookup
3866 llvm::Function *Fn = MethodDefinitions[MD];
3867 if (!Fn)
3868 return 0;
3869
3870 std::vector<llvm::Constant*> Method(3);
3871 Method[0] =
3872 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
3873 ObjCTypes.SelectorPtrTy);
3874 Method[1] = GetMethodVarType(MD);
3875 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
3876 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
3877}
3878
3879/// EmitMethodList - Build meta-data for method declarations
3880/// struct _method_list_t {
3881/// uint32_t entsize; // sizeof(struct _objc_method)
3882/// uint32_t method_count;
3883/// struct _objc_method method_list[method_count];
3884/// }
3885///
3886llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
3887 const std::string &Name,
3888 const char *Section,
3889 const ConstantVector &Methods) {
3890 // Return null for empty list.
3891 if (Methods.empty())
3892 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
3893
3894 std::vector<llvm::Constant*> Values(3);
3895 // sizeof(struct _objc_method)
3896 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
3897 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3898 // method_count
3899 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
3900 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
3901 Methods.size());
3902 Values[2] = llvm::ConstantArray::get(AT, Methods);
3903 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3904
3905 llvm::GlobalVariable *GV =
3906 new llvm::GlobalVariable(Init->getType(), false,
3907 llvm::GlobalValue::InternalLinkage,
3908 Init,
3909 Name,
3910 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003911 GV->setAlignment(
3912 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003913 GV->setSection(Section);
3914 UsedGlobals.push_back(GV);
3915 return llvm::ConstantExpr::getBitCast(GV,
3916 ObjCTypes.MethodListnfABIPtrTy);
3917}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003918
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003919/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
3920/// the given ivar.
3921///
3922llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
3923 std::string &Name,
3924 const NamedDecl *IDName,
3925 const ObjCIvarDecl *Ivar) {
3926 Name += "\01_OBJC_IVAR_$_" + IDName->getNameAsString() + '.'
3927 + Ivar->getNameAsString();
3928 llvm::GlobalVariable *IvarOffsetGV =
3929 CGM.getModule().getGlobalVariable(Name);
3930 if (!IvarOffsetGV)
3931 IvarOffsetGV =
3932 new llvm::GlobalVariable(ObjCTypes.LongTy,
3933 false,
3934 llvm::GlobalValue::ExternalLinkage,
3935 0,
3936 Name,
3937 &CGM.getModule());
3938 return IvarOffsetGV;
3939}
3940
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003941llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003942 const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003943 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003944 unsigned long int Offset) {
3945
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003946 assert(ID && "EmitIvarOffsetVar - null interface decl.");
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003947 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003948 + Ivar->getNameAsString());
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003949 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.LongTy, Offset);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003950
3951 llvm::GlobalVariable *IvarOffsetGV =
3952 CGM.getModule().getGlobalVariable(ExternalName);
3953 if (IvarOffsetGV) {
3954 // ivar offset symbol already built due to user code referencing it.
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003955 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003956 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003957 IvarOffsetGV->setInitializer(Init);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003958 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003959 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003960 return IvarOffsetGV;
3961 }
3962
3963 IvarOffsetGV =
3964 new llvm::GlobalVariable(Init->getType(),
3965 false,
3966 llvm::GlobalValue::ExternalLinkage,
3967 Init,
3968 ExternalName,
3969 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003970 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003971 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003972 // @private and @package have hidden visibility.
3973 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public ||
3974 Ivar->getAccessControl() == ObjCIvarDecl::Protected);
3975 if (!globalVisibility)
3976 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003977 else
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003978 if (IsClassHidden(ID))
3979 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003980
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003981 IvarOffsetGV->setSection("__DATA, __objc_const");
3982 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003983 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003984}
3985
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003986/// EmitIvarList - Emit the ivar list for the given
3987/// implementation. If ForClass is true the list of class ivars
3988/// (i.e. metaclass ivars) is emitted, otherwise the list of
3989/// interface ivars will be emitted. The return value has type
3990/// IvarListnfABIPtrTy.
3991/// struct _ivar_t {
3992/// unsigned long int *offset; // pointer to ivar offset location
3993/// char *name;
3994/// char *type;
3995/// uint32_t alignment;
3996/// uint32_t size;
3997/// }
3998/// struct _ivar_list_t {
3999/// uint32 entsize; // sizeof(struct _ivar_t)
4000/// uint32 count;
4001/// struct _iver_t list[count];
4002/// }
4003///
4004llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4005 const ObjCImplementationDecl *ID) {
4006
4007 std::vector<llvm::Constant*> Ivars, Ivar(5);
4008
4009 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4010 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4011
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004012 // FIXME. Consolidate this with similar code in GenerateClass.
4013 const llvm::Type *InterfaceTy =
4014 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(
4015 const_cast<ObjCInterfaceDecl*>(OID)));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004016 const llvm::StructLayout *Layout =
4017 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004018
4019 RecordDecl::field_iterator i,p;
4020 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004021 ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin();
4022
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004023 for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) {
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004024 FieldDecl *Field = *i;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004025 unsigned long offset = Layout->getElementOffset(CGM.getTypes().
4026 getLLVMFieldNo(Field));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004027 const ObjCIvarDecl *ivarDecl = *I++;
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004028 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), ivarDecl, offset);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004029 if (Field->getIdentifier())
4030 Ivar[1] = GetMethodVarName(Field->getIdentifier());
4031 else
4032 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4033 std::string TypeStr;
4034 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4035 Ivar[2] = GetMethodVarType(TypeStr);
4036 const llvm::Type *FieldTy =
4037 CGM.getTypes().ConvertTypeForMem(Field->getType());
4038 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
4039 unsigned Align = CGM.getContext().getPreferredTypeAlign(
4040 Field->getType().getTypePtr()) >> 3;
4041 Align = llvm::Log2_32(Align);
4042 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Fariborz Jahanian07236ba2009-01-27 22:27:56 +00004043 // NOTE. Size of a bitfield does not match gcc's, because of the way
4044 // bitfields are treated special in each. But I am told that 'size'
4045 // for bitfield ivars is ignored by the runtime so it does not matter.
4046 // (even if it matters, some day, there is enough info. to get the bitfield
4047 // right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004048 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4049 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
4050 }
4051 // Return null for empty list.
4052 if (Ivars.empty())
4053 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4054 std::vector<llvm::Constant*> Values(3);
4055 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
4056 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4057 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4058 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4059 Ivars.size());
4060 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4061 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4062 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4063 llvm::GlobalVariable *GV =
4064 new llvm::GlobalVariable(Init->getType(), false,
4065 llvm::GlobalValue::InternalLinkage,
4066 Init,
4067 Prefix + OID->getNameAsString(),
4068 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004069 GV->setAlignment(
4070 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004071 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004072
4073 UsedGlobals.push_back(GV);
4074 return llvm::ConstantExpr::getBitCast(GV,
4075 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004076}
4077
4078llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4079 const ObjCProtocolDecl *PD) {
4080 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4081
4082 if (!Entry) {
4083 // We use the initializer as a marker of whether this is a forward
4084 // reference or not. At module finalization we add the empty
4085 // contents for protocols which were referenced but never defined.
4086 Entry =
4087 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4088 llvm::GlobalValue::ExternalLinkage,
4089 0,
4090 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4091 &CGM.getModule());
4092 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4093 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004094 }
4095
4096 return Entry;
4097}
4098
4099/// GetOrEmitProtocol - Generate the protocol meta-data:
4100/// @code
4101/// struct _protocol_t {
4102/// id isa; // NULL
4103/// const char * const protocol_name;
4104/// const struct _protocol_list_t * protocol_list; // super protocols
4105/// const struct method_list_t * const instance_methods;
4106/// const struct method_list_t * const class_methods;
4107/// const struct method_list_t *optionalInstanceMethods;
4108/// const struct method_list_t *optionalClassMethods;
4109/// const struct _prop_list_t * properties;
4110/// const uint32_t size; // sizeof(struct _protocol_t)
4111/// const uint32_t flags; // = 0
4112/// }
4113/// @endcode
4114///
4115
4116llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4117 const ObjCProtocolDecl *PD) {
4118 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4119
4120 // Early exit if a defining object has already been generated.
4121 if (Entry && Entry->hasInitializer())
4122 return Entry;
4123
4124 const char *ProtocolName = PD->getNameAsCString();
4125
4126 // Construct method lists.
4127 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4128 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
4129 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
4130 e = PD->instmeth_end(); i != e; ++i) {
4131 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004132 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004133 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4134 OptInstanceMethods.push_back(C);
4135 } else {
4136 InstanceMethods.push_back(C);
4137 }
4138 }
4139
4140 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
4141 e = PD->classmeth_end(); i != e; ++i) {
4142 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004143 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004144 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4145 OptClassMethods.push_back(C);
4146 } else {
4147 ClassMethods.push_back(C);
4148 }
4149 }
4150
4151 std::vector<llvm::Constant*> Values(10);
4152 // isa is NULL
4153 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4154 Values[1] = GetClassName(PD->getIdentifier());
4155 Values[2] = EmitProtocolList(
4156 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4157 PD->protocol_begin(),
4158 PD->protocol_end());
4159
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004160 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004161 + PD->getNameAsString(),
4162 "__DATA, __objc_const",
4163 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004164 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004165 + PD->getNameAsString(),
4166 "__DATA, __objc_const",
4167 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004168 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004169 + PD->getNameAsString(),
4170 "__DATA, __objc_const",
4171 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004172 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004173 + PD->getNameAsString(),
4174 "__DATA, __objc_const",
4175 OptClassMethods);
4176 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4177 0, PD, ObjCTypes);
4178 uint32_t Size =
4179 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
4180 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4181 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4182 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4183 Values);
4184
4185 if (Entry) {
4186 // Already created, fix the linkage and update the initializer.
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004187 Entry->setLinkage(llvm::GlobalValue::WeakLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004188 Entry->setInitializer(Init);
4189 } else {
4190 Entry =
4191 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004192 llvm::GlobalValue::WeakLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004193 Init,
4194 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4195 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004196 Entry->setAlignment(
4197 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004198 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004199 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004200 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4201
4202 // Use this protocol meta-data to build protocol list table in section
4203 // __DATA, __objc_protolist
4204 llvm::Type *ptype = llvm::PointerType::getUnqual(ObjCTypes.ProtocolnfABITy);
4205 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
4206 ptype, false,
4207 llvm::GlobalValue::WeakLinkage,
4208 Entry,
4209 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4210 +ProtocolName,
4211 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004212 PTGV->setAlignment(
4213 CGM.getTargetData().getPrefTypeAlignment(ptype));
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004214 PTGV->setSection("__DATA, __objc_protolist");
4215 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4216 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004217 return Entry;
4218}
4219
4220/// EmitProtocolList - Generate protocol list meta-data:
4221/// @code
4222/// struct _protocol_list_t {
4223/// long protocol_count; // Note, this is 32/64 bit
4224/// struct _protocol_t[protocol_count];
4225/// }
4226/// @endcode
4227///
4228llvm::Constant *
4229CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4230 ObjCProtocolDecl::protocol_iterator begin,
4231 ObjCProtocolDecl::protocol_iterator end) {
4232 std::vector<llvm::Constant*> ProtocolRefs;
4233
4234 for (; begin != end; ++begin)
4235 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4236
4237 // Just return null for empty protocol lists
4238 if (ProtocolRefs.empty())
4239 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4240
4241 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4242 if (GV)
4243 return GV;
4244 // This list is null terminated.
4245 ProtocolRefs.push_back(llvm::Constant::getNullValue(
4246 ObjCTypes.ProtocolListnfABIPtrTy));
4247
4248 std::vector<llvm::Constant*> Values(2);
4249 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4250 Values[1] =
4251 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolListnfABIPtrTy,
4252 ProtocolRefs.size()),
4253 ProtocolRefs);
4254
4255 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4256 GV = new llvm::GlobalVariable(Init->getType(), false,
4257 llvm::GlobalValue::InternalLinkage,
4258 Init,
4259 Name,
4260 &CGM.getModule());
4261 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004262 GV->setAlignment(
4263 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004264 UsedGlobals.push_back(GV);
4265 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
4266}
4267
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004268/// GetMethodDescriptionConstant - This routine build following meta-data:
4269/// struct _objc_method {
4270/// SEL _cmd;
4271/// char *method_type;
4272/// char *_imp;
4273/// }
4274
4275llvm::Constant *
4276CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4277 std::vector<llvm::Constant*> Desc(3);
4278 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4279 ObjCTypes.SelectorPtrTy);
4280 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004281 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004282 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4283 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4284}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004285
4286/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4287/// This code gen. amounts to generating code for:
4288/// @code
4289/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4290/// @encode
4291///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004292LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004293 CodeGen::CodeGenFunction &CGF,
4294 QualType ObjectTy,
4295 llvm::Value *BaseValue,
4296 const ObjCIvarDecl *Ivar,
4297 const FieldDecl *Field,
4298 unsigned CVRQualifiers) {
4299 assert(ObjectTy->isObjCInterfaceType() &&
4300 "CGObjCNonFragileABIMac::EmitObjCValueForIvar");
4301 NamedDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004302 std::string ExternalName;
4303 llvm::GlobalVariable *IvarOffsetGV =
4304 ObjCIvarOffsetVariable(ExternalName, ID, Ivar);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004305
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004306 // (char *) BaseValue
4307 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue,
4308 ObjCTypes.Int8PtrTy);
4309 llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV);
4310 // (char*)BaseValue + Offset_symbol
4311 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
4312 // (type *)((char*)BaseValue + Offset_symbol)
4313 const llvm::Type *IvarTy =
4314 CGM.getTypes().ConvertType(Ivar->getType());
4315 llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy);
4316 V = CGF.Builder.CreateBitCast(V, ptrIvarTy);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004317
4318 if (Ivar->isBitField())
4319 return CGF.EmitLValueForBitfield(V, const_cast<FieldDecl *>(Field),
4320 CVRQualifiers);
4321
4322 LValue LV = LValue::MakeAddr(V,
4323 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
4324 LValue::SetObjCIvar(LV, true);
4325 return LV;
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004326}
4327
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004328llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4329 CodeGen::CodeGenFunction &CGF,
4330 ObjCInterfaceDecl *Interface,
4331 const ObjCIvarDecl *Ivar) {
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004332 std::string ExternalName;
4333 llvm::GlobalVariable *IvarOffsetGV =
4334 ObjCIvarOffsetVariable(ExternalName, Interface, Ivar);
4335
4336 return CGF.Builder.CreateLoad(IvarOffsetGV, false, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004337}
4338
Fariborz Jahanian46551122009-02-04 00:22:57 +00004339CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4340 CodeGen::CodeGenFunction &CGF,
4341 QualType ResultType,
4342 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004343 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004344 QualType Arg0Ty,
4345 bool IsSuper,
4346 const CallArgList &CallArgs) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004347 // FIXME. Even though IsSuper is passes. This function doese not
4348 // handle calls to 'super' receivers.
4349 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004350 llvm::Value *Arg0 = Receiver;
4351 if (!IsSuper)
4352 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004353
4354 // Find the message function name.
Fariborz Jahanianef163782009-02-05 01:13:09 +00004355 // FIXME. This is too much work to get the ABI-specific result type
4356 // needed to find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004357 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4358 llvm::SmallVector<QualType, 16>());
4359 llvm::Constant *Fn;
4360 std::string Name("\01l_");
4361 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004362#if 0
4363 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004364 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4365 Fn = ObjCTypes.MessageSendIdStretFixupFn;
4366 // FIXME. Is there a better way of getting these names.
4367 // They are available in RuntimeFunctions vector pair.
4368 Name += "objc_msgSendId_stret_fixup";
4369 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004370 else
4371#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004372 if (IsSuper) {
4373 Fn = ObjCTypes.MessageSendSuper2StretFixupFn;
4374 Name += "objc_msgSendSuper2_stret_fixup";
4375 }
4376 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004377 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004378 Fn = ObjCTypes.MessageSendStretFixupFn;
4379 Name += "objc_msgSend_stret_fixup";
4380 }
4381 }
Fariborz Jahanian1a6b3682009-02-05 19:35:43 +00004382 else if (ResultType->isFloatingType() &&
4383 // Selection of frret API only happens in 32bit nonfragile ABI.
4384 CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004385 Fn = ObjCTypes.MessageSendFpretFixupFn;
4386 Name += "objc_msgSend_fpret_fixup";
4387 }
4388 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004389#if 0
4390// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004391 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4392 Fn = ObjCTypes.MessageSendIdFixupFn;
4393 Name += "objc_msgSendId_fixup";
4394 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004395 else
4396#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004397 if (IsSuper) {
4398 Fn = ObjCTypes.MessageSendSuper2FixupFn;
4399 Name += "objc_msgSendSuper2_fixup";
4400 }
4401 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004402 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004403 Fn = ObjCTypes.MessageSendFixupFn;
4404 Name += "objc_msgSend_fixup";
4405 }
4406 }
4407 Name += '_';
4408 std::string SelName(Sel.getAsString());
4409 // Replace all ':' in selector name with '_' ouch!
4410 for(unsigned i = 0; i < SelName.size(); i++)
4411 if (SelName[i] == ':')
4412 SelName[i] = '_';
4413 Name += SelName;
4414 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
4415 if (!GV) {
4416 // Build messafe ref table entry.
4417 std::vector<llvm::Constant*> Values(2);
4418 Values[0] = Fn;
4419 Values[1] = GetMethodVarName(Sel);
4420 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4421 GV = new llvm::GlobalVariable(Init->getType(), false,
4422 llvm::GlobalValue::WeakLinkage,
4423 Init,
4424 Name,
4425 &CGM.getModule());
4426 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4427 GV->setAlignment(
4428 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.MessageRefTy));
4429 GV->setSection("__DATA, __objc_msgrefs, coalesced");
4430 UsedGlobals.push_back(GV);
4431 }
4432 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00004433
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004434 CallArgList ActualArgs;
4435 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
4436 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
4437 ObjCTypes.MessageRefCPtrTy));
4438 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00004439 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
4440 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
4441 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanianef163782009-02-05 01:13:09 +00004442 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, false);
4443 Callee = CGF.Builder.CreateBitCast(Callee,
4444 llvm::PointerType::getUnqual(FTy));
4445 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00004446}
4447
4448/// Generate code for a message send expression in the nonfragile abi.
4449CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
4450 CodeGen::CodeGenFunction &CGF,
4451 QualType ResultType,
4452 Selector Sel,
4453 llvm::Value *Receiver,
4454 bool IsClassMessage,
4455 const CallArgList &CallArgs) {
Fariborz Jahanian46551122009-02-04 00:22:57 +00004456 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004457 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanian46551122009-02-04 00:22:57 +00004458 false, CallArgs);
4459}
4460
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004461llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004462 const ObjCInterfaceDecl *ID,
4463 bool IsSuper) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004464
4465 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
4466
4467 if (!Entry) {
4468 std::string ClassName("\01_OBJC_CLASS_$_" + ID->getNameAsString());
4469 llvm::GlobalVariable *ClassGV =
4470 CGM.getModule().getGlobalVariable(ClassName);
4471 if (!ClassGV) {
4472 ClassGV =
4473 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
4474 llvm::GlobalValue::ExternalLinkage,
4475 0,
4476 ClassName,
4477 &CGM.getModule());
4478 UsedGlobals.push_back(ClassGV);
4479 }
4480 Entry =
4481 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
4482 llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004483 ClassGV,
4484 IsSuper ? "\01L_OBJC_CLASSLIST_SUP_REFS_$_"
4485 : "\01L_OBJC_CLASSLIST_REFERENCES_$_",
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004486 &CGM.getModule());
4487 Entry->setAlignment(
4488 CGM.getTargetData().getPrefTypeAlignment(
4489 ObjCTypes.ClassnfABIPtrTy));
4490
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004491 if (IsSuper)
4492 Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip");
4493 else
4494 Entry->setSection("__OBJC,__objc_classrefs,regular,no_dead_strip");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004495 UsedGlobals.push_back(Entry);
4496 }
4497
4498 return Builder.CreateLoad(Entry, false, "tmp");
4499}
4500
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004501/// EmitMetaClassRef - Return a Value * of the address of _class_t
4502/// meta-data
4503///
4504llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
4505 const ObjCInterfaceDecl *ID) {
4506 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
4507 if (Entry)
4508 return Builder.CreateLoad(Entry, false, "tmp");
4509
4510 std::string MetaClassName("\01_OBJC_METACLASS_$_" + ID->getNameAsString());
4511 llvm::GlobalVariable *MetaClassGV =
4512 CGM.getModule().getGlobalVariable(MetaClassName);
4513 if (!MetaClassGV) {
4514 MetaClassGV =
4515 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
4516 llvm::GlobalValue::ExternalLinkage,
4517 0,
4518 MetaClassName,
4519 &CGM.getModule());
4520 UsedGlobals.push_back(MetaClassGV);
4521 }
4522
4523 Entry =
4524 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
4525 llvm::GlobalValue::InternalLinkage,
4526 MetaClassGV,
4527 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
4528 &CGM.getModule());
4529 Entry->setAlignment(
4530 CGM.getTargetData().getPrefTypeAlignment(
4531 ObjCTypes.ClassnfABIPtrTy));
4532
4533 Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip");
4534 UsedGlobals.push_back(Entry);
4535
4536 return Builder.CreateLoad(Entry, false, "tmp");
4537}
4538
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004539/// GetClass - Return a reference to the class for the given interface
4540/// decl.
4541llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
4542 const ObjCInterfaceDecl *ID) {
4543 return EmitClassRef(Builder, ID);
4544}
4545
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004546/// Generates a message send where the super is the receiver. This is
4547/// a message send to self with special delivery semantics indicating
4548/// which class's method should be called.
4549CodeGen::RValue
4550CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
4551 QualType ResultType,
4552 Selector Sel,
4553 const ObjCInterfaceDecl *Class,
4554 llvm::Value *Receiver,
4555 bool IsClassMessage,
4556 const CodeGen::CallArgList &CallArgs) {
4557 // ...
4558 // Create and init a super structure; this is a (receiver, class)
4559 // pair we will pass to objc_msgSendSuper.
4560 llvm::Value *ObjCSuper =
4561 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
4562
4563 llvm::Value *ReceiverAsObject =
4564 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
4565 CGF.Builder.CreateStore(ReceiverAsObject,
4566 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
4567
4568 // If this is a class message the metaclass is passed as the target.
4569 llvm::Value *Target =
4570 IsClassMessage ? EmitMetaClassRef(CGF.Builder, Class)
4571 : EmitClassRef(CGF.Builder, Class, true);
4572
4573 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
4574 // and ObjCTypes types.
4575 const llvm::Type *ClassTy =
4576 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
4577 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
4578 CGF.Builder.CreateStore(Target,
4579 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
4580
4581 return EmitMessageSend(CGF, ResultType, Sel,
4582 ObjCSuper, ObjCTypes.SuperPtrCTy,
4583 true, CallArgs);
4584}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00004585
4586llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
4587 Selector Sel) {
4588 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
4589
4590 if (!Entry) {
4591 llvm::Constant *Casted =
4592 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
4593 ObjCTypes.SelectorPtrTy);
4594 Entry =
4595 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
4596 llvm::GlobalValue::InternalLinkage,
4597 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
4598 &CGM.getModule());
4599 Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip");
4600 UsedGlobals.push_back(Entry);
4601 }
4602
4603 return Builder.CreateLoad(Entry, false, "tmp");
4604}
4605
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004606/* *** */
4607
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004608CodeGen::CGObjCRuntime *
4609CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00004610 return new CGObjCMac(CGM);
4611}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004612
4613CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004614CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004615 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004616}